import { describe, it, expect } from 'vitest';
import { normalizeCommentToIndexText } from './normalize.js';

describe('normalizeCommentToIndexText', () => {
  it('keeps user request body and appends attachment filenames only', () => {
    const out = normalizeCommentToIndexText('request', '請求書を処理して', JSON.stringify(['a.pdf', 'b.xlsx']));
    expect(out).toContain('請求書を処理して');
    expect(out).toContain('a.pdf');
    expect(out).toContain('b.xlsx');
  });

  it('keeps agent result and ask verbatim', () => {
    expect(normalizeCommentToIndexText('result', '完了しました', null)).toBe('完了しました');
    expect(normalizeCommentToIndexText('ask', '確認したい点が…', null)).toBe('確認したい点が…');
  });

  it('indexes thinking text from progress body', () => {
    const body = JSON.stringify({ type: 'thinking', text: '認証エラーを疑う', movement: 'investigate' });
    expect(normalizeCommentToIndexText('progress', body, null)).toBe('認証エラーを疑う');
  });

  it('indexes ONLY the tool name for tool_call progress — no args values, no result', () => {
    const body = JSON.stringify({
      type: 'tool_call', name: 'BrowseWeb',
      args: { url: 'https://secret.example/?token=abc123' },
      result: 'PASSWORD=hunter2 leaked here', isError: false,
    });
    const out = normalizeCommentToIndexText('progress', body, null)!;
    expect(out).toContain('BrowseWeb');
    expect(out).not.toContain('abc123');
    expect(out).not.toContain('hunter2');
    expect(out).not.toContain('secret.example');
  });

  it('returns null for unknown kind and unknown progress type', () => {
    expect(normalizeCommentToIndexText('mystery_kind', 'x', null)).toBeNull();
    expect(normalizeCommentToIndexText('progress', JSON.stringify({ type: 'future_type', blob: 'x' }), null)).toBeNull();
  });

  it('returns null for empty/JSON-only noise', () => {
    expect(normalizeCommentToIndexText('result', '   ', null)).toBeNull();
  });
});
