// The agents menu and tree window order an agent's siblings in the browser alone: // a daemon run cannot see which row comes first, or that rows hold still under a // pointer. Exercise the real ordering on families shaped like the daemon's. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; const source = stripTypeScriptTypes( await readFile(new URL('../src/lib/agentTree.ts', import.meta.url), 'utf8'), ); const { descendants, layoutTree, ranksOf } = await import( `data:text/javascript;base64,${Buffer.from(source).toString('base64')}` ); const at = (minute) => `2026-10-14T12:${String(minute).padStart(2, '0')}:00Z`; const agent = ( id, parent, { created = 0, activity = created, closed = false, working = false } = {}, ) => ({ id, parent_id: parent, created_at: at(created), activity_at: at(activity), closed, working, }); const working = (session) => session.working; const order = (family, held) => { const ranks = ranksOf(family, working, held); return descendants(family, 'root', (session) => ranks.get(session.id)).map( (session) => session.id, ); }; const family = [ agent('root', null), agent('old-idle', 'root', { created: 1, activity: 5 }), agent('recent-idle', 'root', { created: 2, activity: 40 }), agent('closed', 'root', { created: 3, activity: 50, closed: true }), agent('working', 'root', { created: 4, activity: 10, working: true }), agent('twin-b', 'root', { created: 6, activity: 20 }), agent('twin-a', 'root', { created: 6, activity: 20 }), ]; test('working agents lead, then the latest turn, closed last, ties by age and id', () => { assert.deepEqual(order(family), [ 'working', 'recent-idle', 'twin-a', 'twin-b', 'old-idle', 'closed', ]); }); test('an idle agent with a working agent below it comes up with it', () => { const nested = [ ...family, agent('deep', 'old-idle', { created: 7, activity: 30, working: true }), ]; assert.deepEqual(order(nested).slice(0, 2), ['old-idle', 'deep']); }); test('held ranks keep the order an agent starting would change', () => { const held = ranksOf(family, working); const started = family.map((session) => session.id === 'old-idle' ? { ...session, working: true, activity_at: at(55) } : session, ); assert.equal(order(started)[0], 'old-idle'); assert.deepEqual(order(started, held), order(family)); const joined = [ ...family, agent('new', 'root', { created: 8, activity: 58, working: true }), ]; assert.equal(order(joined, held)[0], 'new'); }); test('the tree stacks siblings in the same order', () => { const open = family.filter((session) => !session.closed); const ranks = ranksOf(open, working); const tree = layoutTree(open, 'root', undefined, undefined, (session) => ranks.get(session.id), ); const stacked = tree.nodes .filter((node) => node.parentId === 'root') .sort((a, b) => a.y - b.y) .map((node) => node.id); assert.deepEqual(stacked, [ 'working', 'recent-idle', 'twin-a', 'twin-b', 'old-idle', ]); });