Files
arboretum/packages/server/test/dialog-detection.test.ts
Johan LEROY 1b4c19e00a P4-C: PWA installable, service worker push & campagne de fiabilité dialogues
- PWA: public/manifest.webmanifest + icon.svg + meta index.html (theme-color, apple-touch-icon)
- public/sw.js: service worker push-only (push → showNotification tag=sessionId ;
  notificationclick → focus/ouvre /sessions/<id>) — pas de précache (hors périmètre)
- lib/push.ts + stores/push.ts: enable/disable, abonnement VAPID, enregistrement SW au boot
- DashboardView: toggle « Notifications » (gardé par pushSupported) + i18n en/fr
- campagne de fiabilité (verdict S3): classification de tous les types de dialogue sur captures
  réelles (trust/permission×2/question) + refus Esc (deny-esc2) + plan (synthétique)
- acceptance-p4.mjs: VAPID/auth/Origin, subscribe idempotent/unsubscribe, answer (rejets)

Choix: SW écrit à la main plutôt que vite-plugin-pwa (qui tirait ~295 paquets Workbox +
2 vulns esbuild high pour un SW push-only). Zéro dépendance front nouvelle. 175 tests verts,
acceptance p1..p4 ALL GREEN.

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

116 lines
5.4 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)), '..', '..', '..', '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<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.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 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.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' });
});
});