import { describe, expect, it } from 'vitest';
import {
  buildTitleFallback,
  buildTitlePrompt,
  isUrlOnlyTitleInput,
  stripUrlsForTitle,
} from './title-generation.js';

describe('title generation helpers', () => {
  it('detects URL-only input', () => {
    expect(isUrlOnlyTitleInput('https://example.com/articles/123')).toBe(true);
    expect(isUrlOnlyTitleInput('  https://example.com/a\nhttps://example.com/b  ')).toBe(true);
    expect(isUrlOnlyTitleInput('このURLを調べて https://example.com/articles/123')).toBe(false);
  });

  it('does not pass URL-only input to the LLM prompt', () => {
    expect(buildTitlePrompt('https://example.com/articles/123')).toBeNull();
    expect(buildTitleFallback('https://example.com/articles/123')).toBe('URLの確認');
  });

  it('masks URLs before building the prompt and fallback', () => {
    const input = 'このページを要約して https://example.com/secret/path?token=abc';

    expect(stripUrlsForTitle(input)).toBe('このページを要約して [URL]');
    expect(buildTitleFallback(input)).toBe('このページを要約して [URL]');
    expect(buildTitlePrompt(input)).toContain('このページを要約して [URL]');
    expect(buildTitlePrompt(input)).not.toContain('example.com');
  });
});
