Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

29.8 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import {
  collectGeneratedFiles,
  extractCommandOutputPathCandidates,
  extractDocumentArtifactMetadata,
  isExplicitFailedToolResult,
} from '../lib/generatedFiles';

const WORKDIR = '/work';

function toolUse(toolName: string, toolInput: unknown) {
  return { role: 'tool_use', toolName, toolInput };
}

describe('isExplicitFailedToolResult', () => {
  it('fails closed on structured error envelopes', () => {
    expect(isExplicitFailedToolResult(JSON.stringify({ ok: false }))).toBe(true);
    expect(isExplicitFailedToolResult(JSON.stringify({ success: false }))).toBe(true);
    expect(isExplicitFailedToolResult(JSON.stringify({ status: 'error' }))).toBe(true);
    expect(isExplicitFailedToolResult('<tool_use_error>denied</tool_use_error>')).toBe(true);
  });

  it('does not treat ordinary success text as a failure', () => {
    expect(isExplicitFailedToolResult('Wrote a.md')).toBe(false);
    expect(isExplicitFailedToolResult(JSON.stringify({ ok: true }))).toBe(false);
    expect(isExplicitFailedToolResult(undefined)).toBe(false);
  });
});

describe('collectGeneratedFiles', () => {
  it('turns a top-level cindy docs tool result into artifact metadata', () => {
    const files = collectGeneratedFiles(
      [
        {
          role: 'tool_use',
          toolName: 'mcp__cindy_docs__make_pptx',
          toolUseId: 'ppt-1',
          toolInput: {
            outPath: 'documents/roadmap.pptx',
            title: '产品路线图',
            theme: 'navy',
            slides: [{ title: '封面', layout: 'cover' }],
          },
        },
        {
          role: 'tool_result',
          toolUseId: 'ppt-1',
          content: JSON.stringify({
            ok: true,
            artifact: {
              format: 'pptx',
              title: '产品路线图',
              theme: 'navy',
              summary: { kind: 'slides', value: 5 },
            },
          }),
        },
      ],
      WORKDIR,
    );
    expect(files).toHaveLength(1);
    expect(files[0].artifactConfirmed).toBe(true);
    expect(files[0].artifact).toEqual({
      format: 'pptx',
      title: '产品路线图',
      theme: 'navy',
      summary: { kind: 'slides', value: 5 },
      preview: { kind: 'slide', title: '封面' },
    });
  });

  it('builds the spreadsheet thumbnail only from real tool input cells', () => {
    expect(
      extractDocumentArtifactMetadata('make_xlsx', {
        outPath: 'documents/progress.xlsx',
        sheets: [
          {
            name: '交付进度表',
            header: ['项目', '状态', '完成率'],
            rows: [
              ['文档工坊', '进行中', 0.82],
              ['作品卡', '已完成', { formula: '1', result: '100%' }],
            ],
          },
        ],
      }),
    ).toMatchObject({
      format: 'xlsx',
      title: '交付进度表',
      preview: {
        kind: 'sheet',
        hasHeader: true,
        rows: [
          ['项目', '状态', '完成率'],
          ['文档工坊', '进行中', '0.82'],
          ['作品卡', '已完成', '100%'],
        ],
      },
    });
  });

  it('recognizes document metadata without a result for replayed tool calls', () => {
    expect(
      extractDocumentArtifactMetadata('mcp:cindy_docs:make_docx', {
        outPath: 'documents/report.docx',
        title: '季度报告',
        subtitle: '内部版',
        theme: 'light',
      }),
    ).toMatchObject({
      format: 'docx',
      title: '季度报告',
      subtitle: '内部版',
      theme: 'light',
    });
  });

  it('uses generated byte size as a readable PDF/Word artifact summary', () => {
    expect(
      extractDocumentArtifactMetadata(
        'render_pdf',
        { outPath: 'documents/report.pdf', html: '<h1>报告</h1>' },
        JSON.stringify({ ok: true, bytes: 1536 }),
      ),
    ).toMatchObject({ format: 'pdf', summary: { kind: 'bytes', value: 1536 } });
    expect(
      extractDocumentArtifactMetadata(
        'make_docx',
        { outPath: 'documents/report.docx', markdown: '# 报告' },
        JSON.stringify({
          ok: true,
          artifact: { format: 'docx', summary: { kind: 'bytes', value: 2048 } },
        }),
      ),
    ).toMatchObject({ format: 'docx', summary: { kind: 'bytes', value: 2048 } });
  });

  it('does not attach artwork metadata to an explicit failed or malformed tool result', () => {
    const input = {
      outPath: 'documents/report.docx',
      title: '季度报告',
      theme: 'light',
    };
    expect(
      extractDocumentArtifactMetadata(
        'make_docx',
        input,
        JSON.stringify({ ok: false, errorCode: 'FILE_EXISTS' }),
      ),
    ).toBeUndefined();
    expect(extractDocumentArtifactMetadata('make_docx', input, 'not-json')).toBeUndefined();
    // 历史消息没有保存 tool_result 时仍保留兼容重建。
    expect(extractDocumentArtifactMetadata('make_docx', input)).toBeDefined();
  });

  it('does not list a file whose tool result is an explicit failure', () => {
    const files = collectGeneratedFiles(
      [
        {
          role: 'tool_use',
          toolName: 'Write',
          toolUseId: 'w-fail',
          toolInput: { file_path: 'a.md', content: 'x' },
        },
        {
          role: 'tool_result',
          toolUseId: 'w-fail',
          content: JSON.stringify({ ok: false, errorCode: 'FILE_EXISTS' }),
        },
      ],
      WORKDIR,
    );
    expect(files).toEqual([]);
  });

  it('does not drop a created path when the later delete is wrapped as a tool_use_error', () => {
    const files = collectGeneratedFiles(
      [
        {
          role: 'tool_use',
          toolName: 'Write',
          toolUseId: 'w1',
          toolInput: { file_path: 'new.txt', content: 'hi' },
        },
        { role: 'tool_result', toolUseId: 'w1', content: 'ok' },
        {
          role: 'tool_use',
          toolName: 'file_change',
          toolUseId: 'd1',
          toolInput: {
            changes: [{ path: 'new.txt', kind: { type: 'delete' }, diff: '-hi' }],
          },
        },
        {
          role: 'tool_result',
          toolUseId: 'd1',
          content: '<tool_use_error>delete new.txt</tool_use_error>',
        },
      ],
      WORKDIR,
    );
    expect(files.map((file) => file.name)).toEqual(['new.txt']);
  });

  it('drops a created path that the same turn later deletes', () => {
    const files = collectGeneratedFiles(
      [
        toolUse('Write', { file_path: 'new.txt', content: 'hi' }),
        toolUse('file_change', {
          changes: [{ path: 'new.txt', kind: { type: 'delete' }, diff: '-hi' }],
        }),
      ],
      WORKDIR,
    );
    expect(files).toEqual([]);
  });

  it('keeps a path that is deleted and then recreated later in the same turn', () => {
    const files = collectGeneratedFiles(
      [
        toolUse('Write', { file_path: 'new.txt', content: 'old' }),
        toolUse('file_change', {
          changes: [{ path: 'new.txt', kind: { type: 'delete' }, diff: '-old' }],
        }),
        toolUse('Write', { file_path: 'new.txt', content: 'new' }),
      ],
      WORKDIR,
    );
    expect(files.map((file) => file.name)).toEqual(['new.txt']);
  });

  it('keeps a confirmed document preview while a second write is still in flight', () => {
    const files = collectGeneratedFiles(
      [
        {
          role: 'tool_use',
          toolName: 'make_xlsx',
          toolUseId: 'x1',
          toolInput: {
            outPath: 'documents/progress.xlsx',
            sheets: [{ name: '旧表', header: ['A'] }],
          },
        },
        {
          role: 'tool_result',
          toolUseId: 'x1',
          content: JSON.stringify({ ok: true }),
        },
        {
          role: 'tool_use',
          toolName: 'make_xlsx',
          toolUseId: 'x2',
          toolInput: {
            outPath: 'documents/progress.xlsx',
            sheets: [{ name: '新表', header: ['B'] }],
          },
        },
      ],
      WORKDIR,
    );
    expect(files).toHaveLength(1);
    expect(files[0].artifactConfirmed).toBe(true);
    expect(files[0].ready).not.toBe(false);
    expect(files[0].artifact?.preview).toMatchObject({
      kind: 'sheet',
      hasHeader: true,
      rows: [['A']],
    });
  });

  it('keeps a created path when the later delete result is an explicit failure', () => {
    const files = collectGeneratedFiles(
      [
        {
          role: 'tool_use',
          toolName: 'Write',
          toolUseId: 'w1',
          toolInput: { file_path: 'new.txt', content: 'hi' },
        },
        { role: 'tool_result', toolUseId: 'w1', content: 'ok' },
        {
          role: 'tool_use',
          toolName: 'file_change',
          toolUseId: 'd1',
          toolInput: {
            changes: [{ path: 'new.txt', kind: { type: 'delete' }, diff: '-hi' }],
          },
        },
        {
          role: 'tool_result',
          toolUseId: 'd1',
          content: JSON.stringify({ ok: false, status: 'failed' }),
        },
      ],
      WORKDIR,
    );
    expect(files.map((file) => file.name)).toEqual(['new.txt']);
  });

  it('marks in-flight tool_use files as not ready until the result arrives', () => {
    const inflight = collectGeneratedFiles(
      [
        {
          role: 'tool_use',
          toolName: 'Write',
          toolUseId: 'w1',
          toolInput: { file_path: 'a.md', content: 'x' },
        },
      ],
      WORKDIR,
    );
    expect(inflight).toHaveLength(1);
    expect(inflight[0].ready).toBe(false);

    const settled = collectGeneratedFiles(
      [
        {
          role: 'tool_use',
          toolName: 'Write',
          toolUseId: 'w1',
          toolInput: { file_path: 'a.md', content: 'x' },
        },
        { role: 'tool_result', toolUseId: 'w1', content: 'ok' },
      ],
      WORKDIR,
    );
    expect(settled).toHaveLength(1);
    expect(settled[0].ready).toBeUndefined();
  });

  it('collects Write (claude) and write (pi) created files', () => {
    const files = collectGeneratedFiles(
      [
        toolUse('Write', { file_path: 'report.md', content: 'x' }),
        toolUse('write', { path: 'data/out.csv', content: 'y' }),
      ],
      WORKDIR,
    );
    expect(files.map((f) => f.name)).toEqual(['report.md', 'out.csv']);
    // 相对路径按 workingDir 解析成绝对路径。
    expect(files[0].path.replace(/\\/g, '/')).toContain('/work/report.md');
  });

  it('collects codex file_change add entries but not updates', () => {
    // codex 协议形态:change 需带 kind.type 与 diff 字符串,缺任一整次降级 generic。
    const files = collectGeneratedFiles(
      [
        toolUse('file_change', {
          changes: [
            { path: 'new.txt', kind: { type: 'add' }, diff: '+hi' },
            { path: 'existing.txt', kind: { type: 'update' }, diff: '-a\n+b' },
          ],
        }),
      ],
      WORKDIR,
    );
    expect(files.map((f) => f.name)).toEqual(['new.txt']);
  });

  it('excludes edits, reads and searches', () => {
    const files = collectGeneratedFiles(
      [
        toolUse('Edit', { file_path: 'a.ts', old_string: 'a', new_string: 'b' }),
        toolUse('Read', { file_path: 'b.ts' }),
        toolUse('Grep', { pattern: 'foo' }),
      ],
      WORKDIR,
    );
    expect(files).toEqual([]);
  });

  it('dedupes the same created path across tool calls, keeping first order', () => {
    const files = collectGeneratedFiles(
      [
        toolUse('Write', { file_path: 'dup.md', content: '1' }),
        toolUse('Write', { file_path: 'other.md', content: '2' }),
        toolUse('Write', { file_path: 'dup.md', content: '3' }),
      ],
      WORKDIR,
    );
    expect(files.map((f) => f.name)).toEqual(['dup.md', 'other.md']);
  });

  it('folds case only for Windows-shaped paths, keeps POSIX case-sensitive', () => {
    // Windows 绝对路径:大小写不敏感,C:/A.md 与 c:/a.md 视为同一文件。
    const win = collectGeneratedFiles(
      [
        toolUse('Write', { file_path: 'C:/work/A.md', content: '1' }),
        toolUse('Write', { file_path: 'c:/work/a.md', content: '2' }),
      ],
      'C:/work',
    );
    expect(win).toHaveLength(1);

    // POSIX 绝对路径:大小写敏感,/w/A.txt 与 /w/a.txt 是两个不同文件,不合并。
    const posix = collectGeneratedFiles(
      [
        toolUse('write', { path: '/w/A.txt', content: '1' }),
        toolUse('write', { path: '/w/a.txt', content: '2' }),
      ],
      '/w',
    );
    expect(posix.map((f) => f.name).sort()).toEqual(['A.txt', 'a.txt']);
  });

  it('ignores non-tool_use messages', () => {
    const files = collectGeneratedFiles(
      [
        { role: 'assistant', toolName: 'Write', toolInput: { file_path: 'no.md' } },
        toolUse('Write', { file_path: 'yes.md', content: 'x' }),
      ],
      WORKDIR,
    );
    expect(files.map((f) => f.name)).toEqual(['yes.md']);
  });

  it('collects script-generated paths from Bash commands as command-source candidates', () => {
    // Issue 场景:xlsx 由 python openpyxl 脚本生成,没有文件工具记录。
    const cmd =
      'python -c "\nimport openpyxl\nwb = openpyxl.Workbook()\n' +
      "wb.save(r'C:\\Users\\Admin\\Documents\\测试表格.xlsx')\nprint('done')\n\"\n";
    const files = collectGeneratedFiles([toolUse('Bash', { command: cmd })], 'C:/work');
    expect(files).toHaveLength(1);
    expect(files[0].name).toBe('测试表格.xlsx');
    expect(files[0].source).toBe('command');
  });

  it('canonicalizes Windows-shaped paths to backslashes and dedupes across slash forms', () => {
    // 正斜杠 Windows 路径(命令文本常见)必须归一成反斜杠本机形态:Explorer
    // /select 与 shell.openPath 对正斜杠会失败;且与 Write 记录的反斜杠形态
    // 是同一文件,不归一会重复出 chip。
    const files = collectGeneratedFiles(
      [
        toolUse('Write', { file_path: 'C:\\work\\report.docx', content: 'x' }),
        toolUse('Bash', { command: "open 'C:/work/report.docx'" }),
        toolUse('Bash', { command: "save 'C:/work/输出/表格.xlsx'" }),
      ],
      'C:\\work',
    );
    expect(files.map((f) => f.path)).toEqual([
      'C:\\work\\report.docx',
      'C:\\work\\输出\\表格.xlsx',
    ]);
    expect(files[0].source).toBe('tool');
  });

  it('collapses doubled backslashes so escaped wrapper commands do not duplicate chips', () => {
    // 包装脚本里的写出路径可能带转义残留(`C:\\Users\\...`);同轮另一条命令用
    // 正斜杠形态。fs 层它们是同一文件(Windows 归并重复分隔符),不折叠会出两个同名 chip。
    const files = collectGeneratedFiles(
      [
        toolUse('Bash', {
          command: 'node -e "save(\'C:\\\\Users\\\\U\\\\pr-watch\\\\registry.json\')"',
        }),
        toolUse('Bash', {
          command: 'node -e "save(\'C:/Users/U/pr-watch/registry.json\')"',
        }),
      ],
      'C:\\Users\\U\\pr-watch',
    );
    const registry = files.filter((f) => f.name === 'registry.json');
    expect(registry).toHaveLength(1);
    // 画布路径本身也折叠成单反斜杠本机形态,不带转义残留。
    expect(registry[0].path).toBe('C:\\Users\\U\\pr-watch\\registry.json');
  });

  it('keeps the UNC leading double backslash while collapsing inner separator runs', () => {
    const files = collectGeneratedFiles(
      [
        toolUse('Bash', { command: "copy out '\\\\server\\share\\\\dir\\report.csv'" }),
        toolUse('Bash', { command: "open '\\\\server\\share\\dir\\report.csv'" }),
      ],
      'C:\\work',
    );
    const reports = files.filter((f) => f.name === 'report.csv');
    expect(reports).toHaveLength(1);
    expect(reports[0].path.startsWith('\\\\server\\')).toBe(true);
  });

  it('excludes command mentions of files edited by file tools this turn', () => {
    // 编码会话形态:Edit 改了源码文件,随后命令引用它(跑测试)。它是编辑不是
    // 新建,不能因 mtime 落在本轮窗口就被当成产物。
    const files = collectGeneratedFiles(
      [
        toolUse('Bash', { command: 'pnpm vitest run C:/work/src/openWithApps.test.ts' }),
        toolUse('Edit', {
          file_path: 'C:/work/src/openWithApps.test.ts',
          old_string: 'a',
          new_string: 'b',
        }),
        toolUse('Bash', { command: "python gen.py > 'C:/work/out/report.csv'" }),
      ],
      'C:/work',
    );
    // Edit 出现在命令之后也要生效(两遍扫描),真产物 report.csv 不受影响。
    expect(files.map((f) => f.name)).toEqual(['report.csv']);
  });

  it('marks file-tool creates as tool source and wins over command mentions of the same path', () => {
    const files = collectGeneratedFiles(
      [
        toolUse('Bash', { command: 'ls C:/work/report.md' }),
        toolUse('Write', { file_path: 'C:/work/report.md', content: 'x' }),
      ],
      'C:/work',
    );
    expect(files).toHaveLength(1);
    expect(files[0].source).toBe('tool');
  });
});

describe('extractCommandOutputPathCandidates', () => {
  it('extracts paths from explicit save, redirection and copy destinations', () => {
    expect(
      extractCommandOutputPathCandidates("wb.save(r'C:\\Users\\A\\My Docs\\报表 v2.xlsx')"),
    ).toEqual(['C:\\Users\\A\\My Docs\\报表 v2.xlsx']);
    expect(extractCommandOutputPathCandidates('node gen.js > C:/out/result.json')).toContain(
      'C:/out/result.json',
    );
    expect(extractCommandOutputPathCandidates('cp a "/home/u/输出/report.pdf"')).toContain(
      '/home/u/输出/report.pdf',
    );
    expect(
      extractCommandOutputPathCandidates(
        'Get-Content "inputs/source.md" && copy "inputs/source.md" \'artifacts/report.md\'',
      ),
    ).toEqual(['artifacts/report.md']);
    expect(
      extractCommandOutputPathCandidates(
        "python -c \"from pathlib import Path; Path('out/report.md').write_text('ok')\"",
      ),
    ).toEqual(['out/report.md']);
    expect(extractCommandOutputPathCandidates("Path('out/data.bin').write_bytes(payload)")).toEqual(
      ['out/data.bin'],
    );
    expect(
      extractCommandOutputPathCandidates("df.to_csv(path_or_buf='out/report.csv', index=False)"),
    ).toEqual(['out/report.csv']);
    expect(
      extractCommandOutputPathCandidates("df.to_csv(index=False, path_or_buf='out/report.csv')"),
    ).toEqual(['out/report.csv']);
    expect(
      extractCommandOutputPathCandidates("torch.save(model.state_dict(), 'out/model.pt')"),
    ).toEqual(['out/model.pt']);
    expect(extractCommandOutputPathCandidates("joblib.dump(model, 'out/model.pkl')")).toEqual([
      'out/model.pkl',
    ]);
    expect(
      extractCommandOutputPathCandidates('workbook.to_excel(excel_writer="out/report.xlsx")'),
    ).toEqual(['out/report.xlsx']);
    expect(extractCommandOutputPathCandidates("plt.savefig('out/chart.png')")).toEqual([
      'out/chart.png',
    ]);
    expect(
      extractCommandOutputPathCandidates("plt.savefig(fname='out/chart.pdf', bbox_inches='tight')"),
    ).toEqual(['out/chart.pdf']);
    expect(extractCommandOutputPathCandidates("printf 'report' | tee 'out/report.md'")).toEqual([
      'out/report.md',
    ]);
    expect(extractCommandOutputPathCandidates('tee /work/out/report.txt')).toEqual([
      '/work/out/report.txt',
    ]);
    expect(
      extractCommandOutputPathCandidates(
        "wget --output-document='out/report.pdf' https://example.com/report.pdf",
      ),
    ).toEqual(['out/report.pdf']);
    expect(extractCommandOutputPathCandidates('cp source.txt output/')).toEqual([
      'output/source.txt',
    ]);
    expect(extractCommandOutputPathCandidates('copy source.txt output\\')).toEqual([
      'output\\source.txt',
    ]);
    expect(extractCommandOutputPathCandidates('cp inputs/a.txt inputs/b.txt output/')).toEqual([
      'output/a.txt',
      'output/b.txt',
    ]);
    expect(extractCommandOutputPathCandidates('cp -t output source.txt')).toEqual([
      'output/source.txt',
    ]);
    expect(extractCommandOutputPathCandidates('mv --target-directory output source.txt')).toEqual([
      'output/source.txt',
    ]);
    expect(extractCommandOutputPathCandidates("cp 'source file.txt' output/")).toEqual([
      'output/source file.txt',
    ]);
    expect(extractCommandOutputPathCandidates('cp source\\ file.txt output/')).toEqual([
      'output/source file.txt',
    ]);
    expect(extractCommandOutputPathCandidates('cp -t /dest /src/a.txt')).toEqual(['/dest/a.txt']);
    expect(extractCommandOutputPathCandidates('cp --target-directory=/dest /src/a.txt')).toEqual([
      '/dest/a.txt',
    ]);
    expect(
      extractCommandOutputPathCandidates(
        'Get-ChildItem inputs/source.txt | Copy-Item -Destination output/',
      ),
    ).toEqual(['output/source.txt']);
    expect(
      extractCommandOutputPathCandidates('Get-ChildItem inputs/ | Copy-Item -Destination output/'),
    ).toEqual(['output/']);
    expect(
      extractCommandOutputPathCandidates('Copy-Item -Destination output/ -Path inputs/source.txt'),
    ).toEqual(['output/source.txt']);
    expect(
      extractCommandOutputPathCandidates("Copy-Item 'inputs/source.txt' 'artifacts/report.txt'"),
    ).toEqual(['artifacts/report.txt']);
    expect(extractCommandOutputPathCandidates('Copy-Item inputs/source.txt output/')).toEqual([
      'output/source.txt',
    ]);
    expect(extractCommandOutputPathCandidates('Copy-Item inputs/source.txt report.txt')).toEqual([
      'report.txt',
    ]);
    expect(extractCommandOutputPathCandidates('cp source.txt report.txt')).toEqual(['report.txt']);
    expect(extractCommandOutputPathCandidates('copy source.txt report.txt')).toEqual([
      'report.txt',
    ]);
    expect(
      extractCommandOutputPathCandidates("mv 'tmp/report.pdf' 'artifacts/report.pdf'"),
    ).toEqual(['artifacts/report.pdf']);
    expect(extractCommandOutputPathCandidates("mv 'tmp/report.pdf' 'artifacts/'")).toEqual([
      'artifacts/report.pdf',
    ]);
    expect(extractCommandOutputPathCandidates('mv source.txt report.txt')).toEqual(['report.txt']);
    expect(extractCommandOutputPathCandidates('move source.txt report.txt')).toEqual([
      'report.txt',
    ]);
    expect(
      extractCommandOutputPathCandidates('Move-Item -Destination output/ -Path inputs/source.txt'),
    ).toEqual(['output/source.txt']);
  });

  it('skips temp dirs, extension-less tokens, plain filenames and URLs', () => {
    expect(extractCommandOutputPathCandidates("save('/tmp/x.xlsx')")).toEqual([]);
    expect(
      extractCommandOutputPathCandidates("save('C:\\Users\\A\\AppData\\Local\\Temp\\x.xlsx')"),
    ).toEqual([]);
    expect(extractCommandOutputPathCandidates('cat /dev/null && echo done')).toEqual([]);
    // 纯文件名不收(随机带点 token 误报率太高),相对路径需含分隔符。
    expect(extractCommandOutputPathCandidates("save('输出.xlsx')")).toEqual([]);
    expect(extractCommandOutputPathCandidates("save('out/输出.xlsx')")).toEqual(['out/输出.xlsx']);
    expect(extractCommandOutputPathCandidates('curl https://x.com/a.js')).toEqual([]);
    expect(extractCommandOutputPathCandidates('')).toEqual([]);
  });

  it('dedupes the same output path', () => {
    expect(
      extractCommandOutputPathCandidates(
        'python gen.py > C:/out/a.csv && node gen.js --output "C:/out/a.csv"',
      ),
    ).toEqual(['C:/out/a.csv']);
  });

  it('does not treat paths in PowerShell and shell read commands as outputs', () => {
    const powershellRead =
      'powershell.exe -Command \'$p="docs/design-rules/DESIGN.md"; ' +
      "$l=[System.IO.File]::ReadAllLines($p); $l[0..20]'";
    expect(extractCommandOutputPathCandidates(powershellRead)).toEqual([]);
    expect(
      extractCommandOutputPathCandidates(
        "Get-Content 'docs/dev-rules/repo-map.md'; rg foo 'apps/desktop/src/index.tsx'",
      ),
    ).toEqual([]);
  });

  it('keeps only the explicit output when a command also names input files', () => {
    expect(
      extractCommandOutputPathCandidates(
        "python convert.py 'inputs/source.md' --output 'artifacts/report.pdf'",
      ),
    ).toEqual(['artifacts/report.pdf']);
    expect(
      extractCommandOutputPathCandidates(
        "open('inputs/source.txt', 'r'); open('artifacts/result.txt', 'wb')",
      ),
    ).toEqual(['artifacts/result.txt']);
    expect(extractCommandOutputPathCandidates("open('artifacts/result.txt', mode='w')")).toEqual([
      'artifacts/result.txt',
    ]);
    expect(
      extractCommandOutputPathCandidates(
        "open('artifacts/result.bin', encoding='utf8', mode='wb')",
      ),
    ).toEqual(['artifacts/result.bin']);
    expect(extractCommandOutputPathCandidates("open('inputs/source.txt', mode='r')")).toEqual([]);
    expect(
      extractCommandOutputPathCandidates(
        "Set-Content -Path 'artifacts/result.txt' -Value (Get-Content 'inputs/source.txt')",
      ),
    ).toEqual(['artifacts/result.txt']);
    expect(
      extractCommandOutputPathCandidates(
        "Set-Content -Value (Get-Content 'inputs/source.txt') -Path 'artifacts/result.txt'",
      ),
    ).toEqual(['artifacts/result.txt']);
    expect(
      extractCommandOutputPathCandidates(
        "Set-Content -Value (Get-Content -Path 'inputs/source.txt') -Path 'artifacts/result.txt'",
      ),
    ).toEqual(['artifacts/result.txt']);
    expect(
      extractCommandOutputPathCandidates(
        `Set-Content -Value '${'x'.repeat(300)}' -Path 'artifacts/result.txt'`,
      ),
    ).toEqual(['artifacts/result.txt']);
    expect(
      extractCommandOutputPathCandidates(
        "Set-Content -Value (Get-Content -Path 'inputs/source.txt')",
      ),
    ).toEqual([]);
    expect(
      extractCommandOutputPathCandidates(
        "Set-Content $output ([System.IO.File]::ReadAllText('inputs/source.txt'))",
      ),
    ).toEqual([]);
    expect(
      extractCommandOutputPathCandidates(
        "node convert.js 'inputs/source.md' --output='artifacts/result.html'",
      ),
    ).toEqual(['artifacts/result.html']);
  });

  // 真机场景:PDF 是命令转换出来的,既没有文件工具记录,输出位置也不一定在命令
  // 文本里字面出现。下面每条转换器语义都配一条「只读输入」的反例。
  describe('converter and headless-browser write-out semantics', () => {
    it('takes the Chromium --print-to-pdf / --screenshot switch value', () => {
      expect(
        extractCommandOutputPathCandidates(
          'chrome --headless --print-to-pdf=/work/out/report.pdf file:///work/in.html',
        ),
      ).toEqual(['/work/out/report.pdf']);
      expect(
        extractCommandOutputPathCandidates(
          'chromium --headless --print-to-pdf=out/report.pdf file:///work/in.html',
        ),
      ).toEqual(['out/report.pdf']);
      expect(
        extractCommandOutputPathCandidates(
          '"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --headless ' +
            '--screenshot="out/shot 1.png" --window-size=1200,800 file:///work/in.html',
        ),
      ).toEqual(['out/shot 1.png']);
    });

    it('does not invent a Chromium artifact from reads or the unsupported spaced form', () => {
      expect(
        extractCommandOutputPathCandidates('chrome --headless --dump-dom file:///work/in.html'),
      ).toEqual([]);
      // Chrome 开关只吃 `--switch=value`;空格形态里的路径是位置参数,不会被写出。
      expect(
        extractCommandOutputPathCandidates('chrome --headless --print-to-pdf out/report.pdf'),
      ).toEqual([]);
    });

    it('derives the LibreOffice --convert-to artifact from the input name', () => {
      expect(
        extractCommandOutputPathCandidates(
          'soffice --headless --convert-to pdf --outdir artifacts docs/plan.docx',
        ),
      ).toEqual(['artifacts/plan.pdf']);
      expect(
        extractCommandOutputPathCandidates('libreoffice --convert-to pdf docs/plan.docx'),
      ).toEqual(['plan.pdf']);
      expect(
        extractCommandOutputPathCandidates(
          '/usr/bin/soffice --headless --convert-to=pdf --outdir=/work/out /work/in/plan.pptx',
        ),
      ).toEqual(['/work/out/plan.pdf']);
      // 带过滤器参数的目标格式:冒号前那一段才是扩展名。多输入各出一件。
      expect(
        extractCommandOutputPathCandidates(
          'soffice --headless --convert-to csv:"Text - txt - csv (StarCalc)" --outdir out/ a.xlsx b.xlsx',
        ),
      ).toEqual(['out/a.csv', 'out/b.csv']);
    });

    it('stays silent when LibreOffice only opens or lists a file', () => {
      expect(extractCommandOutputPathCandidates('soffice --headless docs/plan.docx')).toEqual([]);
      expect(extractCommandOutputPathCandidates('libreoffice --version')).toEqual([]);
    });

    it('takes the trailing positional as the wkhtmltopdf / weasyprint artifact', () => {
      expect(extractCommandOutputPathCandidates('wkhtmltopdf in.html out/report.pdf')).toEqual([
        'out/report.pdf',
      ]);
      expect(
        extractCommandOutputPathCandidates(
          'wkhtmltopdf --margin-top 10mm --page-size A4 docs/in.html artifacts/report.pdf',
        ),
      ).toEqual(['artifacts/report.pdf']);
      expect(
        extractCommandOutputPathCandidates('weasyprint docs/in.html artifacts/report.pdf'),
      ).toEqual(['artifacts/report.pdf']);
    });

    it('does not treat a lone wkhtmltopdf / weasyprint input as an artifact', () => {
      expect(extractCommandOutputPathCandidates('wkhtmltopdf --version')).toEqual([]);
      expect(extractCommandOutputPathCandidates('weasyprint docs/in.html')).toEqual([]);
    });

    it('keeps pandoc covered by the -o output option, including the unquoted relative form', () => {
      expect(
        extractCommandOutputPathCandidates('pandoc docs/plan.md -o artifacts/plan.pdf'),
      ).toEqual(['artifacts/plan.pdf']);
      expect(
        extractCommandOutputPathCandidates(
          'pandoc docs/plan.md --output=artifacts/plan.docx --standalone',
        ),
      ).toEqual(['artifacts/plan.docx']);
      // 同一 gap 的另一半:未加引号的重定向目标。
      expect(extractCommandOutputPathCandidates('node gen.js > out/result.json')).toEqual([
        'out/result.json',
      ]);
      // 反例:pandoc 只读输入,没有 -o 就没有候选。
      expect(extractCommandOutputPathCandidates('pandoc docs/plan.md --to=html')).toEqual([]);
    });
  });
});