import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; const source = await readFile( new URL('../src/lib/chat/mailRole.ts', import.meta.url), 'utf8', ); const { agentBubbleRole } = await import( `data:text/javascript;base64,${Buffer.from( stripTypeScriptTypes(source, { mode: 'transform' }), ).toString('base64')}` ); function session(id, parent_id = null) { return { id, parent_id }; } test('distinguishes human, external triggers, subagents, and other agents', () => { const root = session('root'); const child = session('child', 'root'); const grandchild = session('grandchild', 'child'); const sibling = session('sibling', 'root'); const outsider = session('outsider'); const known = new Map( [root, child, grandchild, sibling, outsider].map((s) => [s.id, s]), ); assert.equal( agentBubbleRole({ mail: null, turnType: 'user' }, 'root', known), null, ); assert.equal( agentBubbleRole({ mail: null, turnType: 'scheduled' }, 'root', known), null, ); assert.equal( agentBubbleRole( { mail: { senderId: null, external: true, answer: false }, turnType: 'webhook', }, 'root', known, ), null, ); assert.equal( agentBubbleRole( { mail: { senderId: null, external: true, answer: false }, turnType: 'event', }, 'root', known, ), null, ); assert.equal( agentBubbleRole( { mail: { senderId: 'deleted-child', external: false, answer: true }, turnType: 'agent', }, 'root', known, ), 'subagent', ); assert.equal( agentBubbleRole( { mail: { senderId: null, external: false, answer: true }, turnType: 'agent', }, 'root', known, ), 'subagent', ); assert.equal( agentBubbleRole( { mail: { senderId: 'child', external: false, answer: false }, turnType: 'agent', }, 'root', known, ), 'subagent', ); assert.equal( agentBubbleRole( { mail: { senderId: 'grandchild', external: false, answer: false }, turnType: 'agent', }, 'root', known, ), 'subagent', ); assert.equal( agentBubbleRole( { mail: { senderId: 'sibling', external: false, answer: false }, turnType: 'agent', }, 'child', known, ), 'agent', ); assert.equal( agentBubbleRole( { mail: { senderId: 'root', external: false, answer: false }, turnType: 'agent', }, 'child', known, ), 'agent', ); assert.equal( agentBubbleRole( { mail: { senderId: 'outsider', external: false, answer: false }, turnType: 'agent', }, 'root', known, ), 'agent', ); assert.equal( agentBubbleRole( { mail: { senderId: 'remote-agent', external: false, answer: false }, turnType: 'agent', }, 'root', known, ), 'agent', ); assert.equal( agentBubbleRole({ mail: null, turnType: 'agent' }, 'root', known), 'agent', ); }); test('handles cycles in parent_id links without looping', () => { const loopA = session('loop-a', 'loop-b'); const loopB = session('loop-b', 'loop-a'); const known = new Map([ [loopA.id, loopA], [loopB.id, loopB], ]); assert.equal( agentBubbleRole( { mail: { senderId: 'loop-a', external: false, answer: false }, turnType: 'agent', }, 'root', known, ), 'agent', ); });