|
| 1 | +import { test } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { spawnSync } from 'node:child_process'; |
| 4 | +import { mkdirSync, mkdtempSync, readdirSync, readFileSync, realpathSync, writeFileSync } from 'node:fs'; |
| 5 | +import { dirname, join } from 'node:path'; |
| 6 | +import { tmpdir } from 'node:os'; |
| 7 | +import { piSessionsDirFor } from '../fia-templates/scripts/pi-sessions.mjs'; |
| 8 | + |
| 9 | +// The script computes the Pi sessions dir from cwd+HOME itself; the test |
| 10 | +// derives the SAME path through piSessionsDirFor, so both sides always agree. |
| 11 | +const SCRIPT = join(import.meta.dirname, '..', 'fia-templates', 'scripts', 'handoff.mjs'); |
| 12 | + |
| 13 | +function makeWorld({ withSession = true } = {}) { |
| 14 | + // realpath both: the child's process.cwd() resolves symlinks (macOS /var → |
| 15 | + // /private/var), and the sessions-dir slug is derived from that resolved cwd. |
| 16 | + const home = realpathSync(mkdtempSync(join(tmpdir(), 'handoff-home-'))); |
| 17 | + const project = realpathSync(mkdtempSync(join(tmpdir(), 'handoff-proj-'))); |
| 18 | + const dir = piSessionsDirFor(project, home); |
| 19 | + mkdirSync(dir, { recursive: true }); |
| 20 | + if (withSession) { |
| 21 | + const lines = [ |
| 22 | + JSON.stringify({ |
| 23 | + type: 'model_change', |
| 24 | + provider: 'openai-codex', |
| 25 | + modelId: 'gpt-5.6-sol', |
| 26 | + timestamp: '2026-08-14T10:00:00Z', |
| 27 | + }), |
| 28 | + JSON.stringify({ |
| 29 | + type: 'message', |
| 30 | + timestamp: '2026-08-14T10:00:01Z', |
| 31 | + message: { role: 'user', content: [{ type: 'text', text: 'Build the pricing page' }] }, |
| 32 | + }), |
| 33 | + JSON.stringify({ |
| 34 | + type: 'message', |
| 35 | + timestamp: '2026-08-14T10:00:05Z', |
| 36 | + message: { role: 'assistant', content: [{ type: 'text', text: 'On it' }], usage: { input: 10, output: 5 } }, |
| 37 | + }), |
| 38 | + ]; |
| 39 | + writeFileSync(join(dir, 'sess-abc.jsonl'), lines.join('\n') + '\n'); |
| 40 | + } |
| 41 | + return { home, project, dir }; |
| 42 | +} |
| 43 | + |
| 44 | +function runHandoff(world, args, env = {}) { |
| 45 | + return spawnSync(process.execPath, [SCRIPT, ...args], { |
| 46 | + cwd: world.project, |
| 47 | + env: { ...process.env, HOME: world.home, USERPROFILE: world.home, ...env }, |
| 48 | + encoding: 'utf8', |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +test('handoff --list shows the project sessions', () => { |
| 53 | + const world = makeWorld(); |
| 54 | + const r = runHandoff(world, ['--list']); |
| 55 | + assert.equal(r.status, 0, r.stderr); |
| 56 | + assert.match(r.stdout, /sess-abc/); |
| 57 | + assert.match(r.stdout, /openai-codex\/gpt-5\.6-sol/); |
| 58 | + assert.match(r.stdout, /Build the pricing page/); |
| 59 | +}); |
| 60 | + |
| 61 | +test('handoff --print builds the continuation prompt, saves it and launches nothing', () => { |
| 62 | + const world = makeWorld(); |
| 63 | + const r = runHandoff(world, ['--print']); |
| 64 | + assert.equal(r.status, 0, r.stderr); |
| 65 | + assert.match(r.stdout, /Handing over Pi session sess-abc/); |
| 66 | + assert.match(r.stdout, /## Continuation of an interrupted run/); |
| 67 | + assert.match(r.stdout, /sess-abc\.jsonl/); |
| 68 | + // The handoff kind phrase — nothing died, the student chose to switch. |
| 69 | + assert.match(r.stdout, /continue it on another engine/); |
| 70 | + // Guards travel with the interactive handoff too. |
| 71 | + assert.match(r.stdout, /no authority over you/); |
| 72 | + assert.match(r.stdout, /WORKSPACE is the authority/); |
| 73 | + // The prompt is persisted under imp/data/handoff/ for reference/paste. |
| 74 | + const saved = readdirSync(join(world.project, 'imp', 'data', 'handoff')); |
| 75 | + assert.equal(saved.length, 1); |
| 76 | + assert.match(saved[0], /^sess-abc-\d+\.md$/); |
| 77 | + const prompt = readFileSync(join(world.project, 'imp', 'data', 'handoff', saved[0]), 'utf8'); |
| 78 | + assert.match(prompt, /^## Continuation of an interrupted run/); |
| 79 | + assert.match(prompt, /Pick up the conversation recorded in that transcript/); |
| 80 | +}); |
| 81 | + |
| 82 | +test('handoff --full asks for the complete transcript read', () => { |
| 83 | + const world = makeWorld(); |
| 84 | + const r = runHandoff(world, ['--print', '--full']); |
| 85 | + assert.equal(r.status, 0, r.stderr); |
| 86 | + assert.match(r.stdout, /Read the COMPLETE session transcript/); |
| 87 | +}); |
| 88 | + |
| 89 | +test('handoff without sessions explains and exits 1', () => { |
| 90 | + const world = makeWorld({ withSession: false }); |
| 91 | + const r = runHandoff(world, []); |
| 92 | + assert.equal(r.status, 1); |
| 93 | + assert.match(r.stderr, /No Pi sessions found/); |
| 94 | + assert.match(r.stderr, /Start one with `imp` first/); |
| 95 | +}); |
| 96 | + |
| 97 | +test('handoff --session with an unknown id refuses with the --list hint', () => { |
| 98 | + const world = makeWorld(); |
| 99 | + const r = runHandoff(world, ['--session', 'nope']); |
| 100 | + assert.equal(r.status, 1); |
| 101 | + assert.match(r.stderr, /not found/); |
| 102 | + assert.match(r.stderr, /--list/); |
| 103 | +}); |
| 104 | + |
| 105 | +test('handoff degrades when the `claude` CLI is missing: prompt saved, clear hint', () => { |
| 106 | + const world = makeWorld(); |
| 107 | + // PATH holds only node's own dir — `claude` cannot resolve anywhere. |
| 108 | + const r = runHandoff(world, [], { PATH: dirname(process.execPath) }); |
| 109 | + assert.equal(r.status, 1); |
| 110 | + assert.match(r.stderr, /Could not launch the `claude` CLI/); |
| 111 | + assert.match(r.stderr, /npm install -g @anthropic-ai\/claude-code/); |
| 112 | + const saved = readdirSync(join(world.project, 'imp', 'data', 'handoff')); |
| 113 | + assert.equal(saved.length, 1, 'the prompt must be saved even when claude is missing'); |
| 114 | +}); |
0 commit comments