import { expect, test } from 'bun:test'; import { historyMessages, historyPath, historyScope, historySelection } from '../src/lib/eddyHistory'; import type { ConversationEnvelope } from '@bbbug/shared/eddy_conversations'; const row = (sequence: number, position: number, text: string, part = 0, parts = 1): ConversationEnvelope => ({ sequence, machine: 'a'.repeat(16), taskId: 'task-one', taskTitle: 'Task', sourceId: 'b'.repeat(64), provider: 'codex', receivedAt: 1, event: {id: String(sequence), streamId: 'c'.repeat(64), position, text, part, parts, block: 0, kind: 'message', role: 'user', redacted: false}, }); test('fragmented Unicode messages reconstruct by file position across pages, preserving repeated text', () => { const rows = [row(1, 100, '后半🧪', 1, 2), row(2, 200, 'again'), row(3, 100, '前半', 0, 2), row(4, 300, 'again')]; const messages = historyMessages(rows); expect(messages.map(m => m.text)).toEqual(['前半后半🧪', 'again', 'again']); expect(messages.every(m => m.complete)).toBe(true); }); test('missing fragments remain explicit; later pages complete the message and keep masking metadata', () => { const first = row(1, 0, '尾巴', 1, 2); first.event.redacted = true; const partial = historyMessages([first])[0]; expect(partial.complete).toBe(false); expect(partial.text).toContain('第 1 段尚未加载'); const complete = historyMessages([first, row(2, 0, '开头', 0, 2)])[0]; expect(complete.text).toBe('开头尾巴'); expect(complete.event.redacted).toBe(true); }); test('equal offsets from different files and devices never collapse into one message', () => { const a = row(1, 0, 'first'), b = row(2, 0, 'second'), c = row(3, 0, 'third'); b.event.streamId = 'd'.repeat(64); c.machine = 'f'.repeat(16); expect(historyMessages([a, b, c]).map(m => m.text)).toEqual(['first', 'second', 'third']); }); test('links round trip exact task, machine and source, ignoring invalid external selectors', () => { const selection = {machine: 'a'.repeat(16), taskId: 'task-one', sourceId: 'b'.repeat(64)}; expect(historySelection(historyPath(selection).split('?')[1])).toEqual(selection); expect(historyPath(null)).toBe('/eddy/tasks'); expect(historySelection('?task=../escape&machine=' + selection.machine)).toBeNull(); expect(historySelection('?task=task-one&machine=host')).toBeNull(); }); test('the administrator all-users view keeps scope and account in the URL', () => { const machine = 'f'.repeat(16), source = 'a'.repeat(64); expect(historyScope('?scope=all&owner=Someone@Example.com')).toEqual({ all: true, owner: 'someone@example.com' }); expect(historyScope('?owner=someone@example.com')).toEqual({ all: false }); // A transcript link in the all-users view needs the account it belongs to. expect(historySelection(`?scope=all&machine=${machine}&task=t1`)).toBeNull(); const selected = historySelection(`?scope=all&owner=someone@example.com&machine=${machine}&task=t1&source=${source}`)!; expect(selected).toEqual({ machine, taskId: 't1', sourceId: source, owner: 'someone@example.com' }); expect(historyPath(selected, historyScope('?scope=all'))).toBe(`/eddy/tasks?scope=all&owner=someone%40example.com&machine=${machine}&task=t1&source=${source}`); expect(historyPath(null, { all: true, owner: 'someone@example.com' })).toBe('/eddy/tasks?scope=all&owner=someone%40example.com'); expect(historyPath(null, { all: true })).toBe('/eddy/tasks?scope=all'); // The personal view is unchanged. expect(historyPath({ machine, taskId: 't1' })).toBe(`/eddy/tasks?machine=${machine}&task=t1`); expect(historyPath(null)).toBe('/eddy/tasks'); });