// daemon e2e tests cannot see browser-local unread or flash state 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/attention.ts', import.meta.url), 'utf8', ); const { Attention } = await import( `data:text/javascript;base64,${Buffer.from( stripTypeScriptTypes(source, { mode: 'transform' }), ).toString('base64')}` ); const session = ( id, phase = 'idle', answer = null, blocking_reason = null, ) => ({ id, status: { phase, blocking_reason }, activity: { latest_answer: answer && { message_id: answer } }, }); test('read one chat without clearing another; a later turn can mark it again', () => { const attention = new Attention(); for (const id of ['one', 'two']) assert.equal( attention.changed(session(id, 'running'), session(id), true, true), 'finished', ); attention.read('one'); assert.deepEqual([...attention.unread], ['two']); assert.equal( attention.changed(session('one', 'running'), session('one'), true, true), 'finished', ); assert.equal( attention.changed(session('two', 'running'), session('two'), true, true), null, ); assert.equal(attention.unread.size, 2); }); test('initial loads, attended chats, and unwatched sessions do not become unread', () => { const attention = new Attention(); assert.equal( attention.changed(undefined, session('one', 'idle', 'old'), true, true), null, ); assert.equal( attention.changed(session('one', 'running'), session('one'), true, false), null, ); assert.equal( attention.changed(session('one', 'running'), session('one'), false, true), null, ); assert.equal(attention.unread.size, 0); }); test('a complete turn missed between list reads still marks its new answer', () => { const attention = new Attention(); assert.equal( attention.changed( session('one', 'idle', 'old'), session('one', 'idle', 'new'), true, true, ), 'finished', ); attention.read('one'); assert.equal( attention.changed( session('one', 'idle', 'new'), session('one', 'idle', 'new'), true, true, ), null, ); attention.changed( session('two'), session('two', 'idle', null, { kind: 'approval' }), true, true, ); attention.prune(new Set(['two'])); assert.deepEqual([...attention.unread], ['two']); }); test('reading mail does not let its replay mark a chat unread again', () => { const attention = new Attention(); const mail = { mail_id: 'm1', sender_session_id: 'child', receiver_session_id: 'parent', kind: 'result', }; assert.equal(attention.mail(mail, true, true), 'mail'); attention.read('parent'); assert.equal(attention.mail(mail, true, true), null); assert.equal(attention.unread.size, 0); }); test('a completion in the viewed board stays unread without flashing, even after leaving', () => { const attention = new Attention(); assert.equal( attention.changed( session('one', 'running'), session('one'), true, true, true, ), 'finished', ); assert.deepEqual([...attention.unread], ['one']); assert.equal(attention.flashing.size, 0); attention.changed( session('one', 'running'), session('one'), true, true, false, ); assert.equal(attention.flashing.size, 0); attention.read('one'); attention.changed( session('one', 'running'), session('one'), true, true, false, ); assert.deepEqual([...attention.flashing], ['one']); attention.read('one'); assert.equal(attention.unread.size, 0); assert.equal(attention.flashing.size, 0); }); test('background-board flashes are pruned with unread state; mail on the viewed board stays quiet', () => { const attention = new Attention(); attention.changed( session('one', 'running'), session('one'), true, true, false, ); attention.mail( { mail_id: 'm1', sender_session_id: 'child', receiver_session_id: 'two', kind: 'result', }, true, true, true, ); assert.deepEqual([...attention.flashing], ['one']); assert.deepEqual([...attention.unread], ['one', 'two']); attention.prune(new Set(['two'])); assert.equal(attention.flashing.size, 0); assert.deepEqual([...attention.unread], ['two']); });