import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import type { DialogKind } from '@arboretum/shared'; 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); }); }); // Campagne de fiabilité P4-C : chaque type de dialogue ciblé par la supervision mobile doit être // classifié de façon fiable (cf. « reste à faire P4 » du verdict S3 : couvrir le refus Esc et le plan). describe('campagne de fiabilité P4-C — tous les types de dialogue', () => { const realCaptures: Array<{ fixture: string; kind: DialogKind }> = [ { fixture: 'trust.raw.log', kind: 'trust' }, { fixture: 'perm-write2.raw.log', kind: 'permission' }, { fixture: 'perm-bash2.raw.log', kind: 'permission' }, { fixture: 'ask2.raw.log', kind: 'question' }, ]; for (const c of realCaptures) { it(`${c.fixture} → ${c.kind} détecté (capture réelle)`, async () => { const seen = await replay(c.fixture); expect(seen.some((s) => s.dialog.kind === c.kind)).toBe(true); }); } it('refus par Esc (deny-esc2) : un dialogue est bien affiché avant l’annulation', async () => { // un dialogue détecté = l'utilisateur peut répondre « deny » (Esc) depuis le mobile sans terminal. const seen = await replay('deny-esc2.raw.log'); expect(seen.length).toBeGreaterThan(0); }); it('plan (synthétique — pas de capture réelle) : « Would you like to proceed? »', () => { const d = classifyDialog([ 'Here is my implementation plan:', ' - step one', '❯ 1. Yes, proceed', '2. No, keep planning', 'Would you like to proceed?', ]); expect(d?.kind).toBe('plan'); expect(d?.options.find((o) => o.n === 1)).toMatchObject({ selected: true, label: 'Yes, proceed' }); }); });