import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { ScreenReader } from '../src/core/screen-reader.js'; import { classifyDialog, parseOptions, type ClassifiedDialog } from '../src/core/dialog-classifier.js'; const capturesDir = join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..', 'spikes', 's3-tui', 'captures'); /** Rejoue un flux PTY brut par segments (comme l'adapter en prod) et collecte écran+dialogue détecté. */ async function replay(fixture: string): Promise> { const data = readFileSync(join(capturesDir, fixture)); const reader = new ScreenReader(120, 40); const seen: Array<{ dialog: ClassifiedDialog; text: string }> = []; const STEP = 512; for (let i = 0; i < data.length; i += STEP) { await reader.feed(data.subarray(i, i + STEP)); // xterm gère les séquences coupées en frontière const lines = reader.snapshotLines(); const d = classifyDialog(lines); if (d) seen.push({ dialog: d, text: lines.join('\n') }); } reader.dispose(); return seen; } describe('ScreenReader (@xterm/headless) — anti strip-ANSI naïf', () => { it('préserve les espaces du dialogue (« Do you want to create », pas « Doyouwant »)', async () => { const seen = await replay('perm-write2.raw.log'); expect(seen.length).toBeGreaterThan(0); expect(seen.some((s) => /Do you want to create/.test(s.text))).toBe(true); }); }); describe('classifyDialog — unitaire (lignes synthétiques)', () => { it('trust', () => { const d = classifyDialog(['Do you trust the files in this folder?', '❯ 1. Yes, I trust this folder', '2. No, exit']); expect(d?.kind).toBe('trust'); expect(d?.options).toHaveLength(2); expect(d?.options[0]).toMatchObject({ n: 1, selected: true }); }); it('permission', () => { const d = classifyDialog(['Do you want to create s3-edit.txt?', '❯ 1. Yes', '3. No', 'Esc to cancel · Tab to amend']); expect(d?.kind).toBe('permission'); }); it('question (prime sur permission malgré « Esc to cancel »)', () => { const d = classifyDialog(['☐ Couleur', '❯ 1. Rouge', '2. Bleu', 'Enter to select · ↑/↓ to navigate · Esc to cancel']); expect(d?.kind).toBe('question'); }); it('aucun dialogue → null', () => { expect(classifyDialog(['just some output', 'no options here'])).toBeNull(); }); it('parseOptions tolère « ❯ 2) Label » et trim', () => { expect(parseOptions([' ❯ 2) Yes, allow all '])).toEqual([{ n: 2, label: 'Yes, allow all', selected: true }]); }); }); describe('détection sur fixtures réelles S3 (replay headless)', () => { it('perm-write2 → permission avec options Yes/…/No, option 1 sélectionnée', async () => { const perms = (await replay('perm-write2.raw.log')).filter((s) => s.dialog.kind === 'permission'); expect(perms.length).toBeGreaterThan(0); const withOpts = perms.find((s) => s.dialog.options.length >= 2); expect(withOpts).toBeDefined(); const labels = withOpts!.dialog.options.map((o) => o.label).join(' | '); expect(labels).toMatch(/Yes/); expect(labels).toMatch(/No/); expect(withOpts!.dialog.options.find((o) => o.n === 1)?.selected).toBe(true); }); it('ask2 → question avec une option « Rouge »', async () => { const qs = (await replay('ask2.raw.log')).filter((s) => s.dialog.kind === 'question'); expect(qs.length).toBeGreaterThan(0); expect(qs.some((s) => s.dialog.options.some((o) => /Rouge/.test(o.label)))).toBe(true); }); it('trust → dialogue trust détecté', async () => { const seen = await replay('trust.raw.log'); expect(seen.some((s) => s.dialog.kind === 'trust')).toBe(true); }); });