// A daemon run cannot inspect client animation selection or browser cache identity. // Exercise the real picker/cache: mode changes must invalidate a stretch without // rerolling on every render or changing connection, compaction and stopping cues. import assert from 'node:assert/strict'; import { mkdtemp, readFile, rm } from 'node:fs/promises'; import { execFileSync } from 'node:child_process'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; import vm from 'node:vm'; const source = stripTypeScriptTypes(await readFile(new URL('../src/lib/moods.ts', import.meta.url), 'utf8')); const moods = await import(`data:text/javascript;base64,${Buffer.from(source).toString('base64')}`); const live = (await readFile(new URL('../src/lib/live.svelte.ts', import.meta.url), 'utf8')).replace(/^import .*;\n/gm, '').replaceAll('export ', ''); function fixture() { let rolls = 0; const context = vm.createContext({ ...moods, $state: (value) => value, Math: Object.assign(Object.create(Math), { random: () => { rolls++; return 0.25; } }) }); vm.runInContext(stripTypeScriptTypes(`${live}\nthis.readFrames = liveFrames;`), context); return { frames: context.readFrames, rolls: () => rolls }; } // Compare executable selectors, including asset order, without parsing Go source // or duplicating either client's hash algorithm. test('web frames match real terminal selections in both modes', async () => { const directory = await mkdtemp(join(tmpdir(), 'albedo-mood-parity-')); try { const output = join(directory, 'selections.json'); execFileSync('go', ['test', './internal/tui', '-run', '^TestMoodSelectorParityBridge$', '-count=1'], { cwd: fileURLToPath(new URL('../../cli/', import.meta.url)), env: { ...process.env, ALBEDO_TEST_MOOD_OUTPUT: output }, stdio: 'pipe', }); const selections = JSON.parse(await readFile(output, 'utf8')); assert.equal(selections.length, 7 * 2 * 67); for (const { mood, seed, fast, frames } of selections) { const web = moods.frames(mood, seed, fast); assert.deepEqual(frames, Array.from({ length: 12 }, (_, tick) => web[tick % web.length]), `${mood} seed=${seed} fast=${fast}`); } } finally { await rm(directory, { recursive: true, force: true }); } }); test('one animation holds per session and mood until fast mode changes', () => { const f = fixture(); const normal = f.frames('one', 'thinking', false); assert.equal(f.frames('one', 'thinking', false), normal); assert.equal(f.rolls(), 1); const fast = f.frames('one', 'thinking', true); assert.notDeepEqual(fast, normal); assert.equal(f.frames('one', 'thinking', true), fast); assert.equal(f.rolls(), 2); f.frames('two', 'thinking', false); assert.equal(f.frames('one', 'thinking', true), fast, 'another session must not replace this stretch'); const work = f.frames('one', 'working', true); assert.equal(f.frames('one', 'working', true), work); assert.equal(f.rolls(), 4); assert.deepEqual(f.frames('one', 'working', false), moods.frames('working', 2 ** 46, false)); assert.equal(f.rolls(), 5); }); test('fast mode changes active faces but preserves special waiting and stopping cues', () => { for (const mood of ['preparing', 'thinking', 'responding', 'working']) { for (let seed = 0; seed < 32; seed++) { assert.notDeepEqual(moods.frames(mood, seed, true), moods.frames(mood, seed, false)); assert.deepEqual(moods.frames(mood, seed), moods.frames(mood, seed, false)); } } for (const mood of ['connecting', 'compacting', 'stopping']) { for (let seed = 0; seed < 32; seed++) assert.deepEqual(moods.frames(mood, seed, true), moods.frames(mood, seed, false)); } });