Files
arboretum/packages/server/test/dialog-detection.test.ts
Johan LEROY 0ffc57ddd1 fix(ci): embarquer les fixtures de dialogue dans le paquet de test
Les tests de dialog-detection lisaient leurs captures dans
spikes/s3-tui/captures/, dossier exclu du suivi git (.gitignore :
spikes/**/captures/ + la règle globale *.log). Verts en local mais
absents au checkout CI → ENOENT sur 9 tests.

Les 5 fixtures réellement utilisées sont déplacées dans
packages/server/test/fixtures/dialogs/ et renommées *.raw (sans .log)
pour échapper aux deux règles d'ignore. Le test pointe désormais ce
dossier. Suite hermétique : 175/175 verts, fixtures versionnées.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 12:11:17 +02:00

116 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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)), 'fixtures', 'dialogs');
/** Rejoue un flux PTY brut par segments (comme l'adapter en prod) et collecte écran+dialogue détecté. */
async function replay(fixture: string): Promise<Array<{ dialog: ClassifiedDialog; text: string }>> {
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');
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')).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')).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');
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', kind: 'trust' },
{ fixture: 'perm-write2.raw', kind: 'permission' },
{ fixture: 'perm-bash2.raw', kind: 'permission' },
{ fixture: 'ask2.raw', 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 lannulation', async () => {
// un dialogue détecté = l'utilisateur peut répondre « deny » (Esc) depuis le mobile sans terminal.
const seen = await replay('deny-esc2.raw');
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' });
});
});