import { describe, it, expect } from 'vitest' import { classifyTaskNotificationInjection, classifyUserFrameOrigin, imagePayloadOfContent, imagePayloadOfMessage, isHumanUserMessage, monitorEventSearchText, monitorEventTitle, parseMonitorEventBatch, parseTaskNotificationInjection, parseUnroutableCompletion, } from './user-frames' const msg = (text: string) => ({ type: 'user', message: { content: [{ type: 'text', text }] } }) as Parameters< typeof parseTaskNotificationInjection >[0] describe('parseTaskNotificationInjection', () => { it('parses the full XML shape', () => { const p = parseTaskNotificationInjection(msg( 't1tu1' + 'completedsR')) expect(p).toEqual({ taskId: 't1', toolUseId: 'tu1', status: 'completed', summary: 's', result: 'R' }) }) it('falls back to tool-use-id when task-id is absent', () => { const p = parseTaskNotificationInjection(msg( 'tu1' + 'completedR')) expect(p?.taskId).toBe('tu1') expect(p?.toolUseId).toBe('tu1') }) it('returns null when tool-use-id is absent', () => { expect(parseTaskNotificationInjection(msg( 't1' + 'completedR'))).toBeNull() }) it('normalises a missing status to failed, never completed', () => { const p = parseTaskNotificationInjection(msg( 'tu1R')) expect(p?.status).toBe('failed') const p2 = parseTaskNotificationInjection(msg( 'tu1weird')) expect(p2?.status).toBe('failed') }) it('unescapes XML entities in the result body', () => { const p = parseTaskNotificationInjection(msg( 'tu1' + 'completeda <b> & c')) expect(p?.result).toBe('a & c') }) it('returns null for a non-notification frame', () => { expect(parseTaskNotificationInjection(msg('plain user text'))).toBeNull() }) }) describe('classifyTaskNotificationInjection', () => { it('marks a status-only recovery notice as neither foldable nor payload-bearing', () => { // The harness's resume-time notice for a background agent abandoned when // the previous process exited — one per restart. Nothing to render. expect(classifyTaskNotificationInjection(msg( 'a1/tmp/a1.output' + 'stoppedBackground agent "x" didn\'t finish before the previous session ended' + 'No completion record was found for it in the previous session.'))).toEqual({ foldable: false, hasPayload: false, }) }) it('marks an inline body as payload (not foldable without an id)', () => { expect(classifyTaskNotificationInjection(msg( 'a1completed' + 'the report'))).toEqual({ foldable: false, hasPayload: true, }) }) it('marks a monitor line as payload (not foldable)', () => { expect(classifyTaskNotificationInjection(msg( 'm1Monitor event: "build"' + 'exe produced'))).toEqual({ foldable: false, hasPayload: true, }) }) it('marks a tool-use-id injection as foldable', () => { expect(classifyTaskNotificationInjection(msg( 't1tu1' + 'completedR'))).toEqual({ foldable: true, hasPayload: true, }) }) it('does not count whitespace-only payload elements', () => { expect(classifyTaskNotificationInjection(msg( 'a1completed' + '\n '))?.hasPayload).toBe(false) }) it('counts a later repeated payload element when the first is empty', () => { expect(classifyTaskNotificationInjection(msg( 'm1' + 'exe produced'))?.hasPayload).toBe(true) }) it('returns null for a non-notification frame', () => { expect(classifyTaskNotificationInjection(msg('plain user text'))).toBeNull() }) }) describe('parseUnroutableCompletion', () => { it('parses a result-bearing injection with no tool-use-id', () => { expect(parseUnroutableCompletion(msg( 'a1completed' + 'Agent "x" finishedthe report'))).toEqual({ taskId: 'a1', status: 'completed', summary: 'Agent "x" finished', result: 'the report', }) }) it('leaves summary undefined when the injection carries none', () => { const p = parseUnroutableCompletion(msg( 'a1completed' + 'the report')) expect(p?.summary).toBeUndefined() expect(p?.result).toBe('the report') }) it('returns null for a foldable injection (the tool-use-id parser owns it)', () => { expect(parseUnroutableCompletion(msg( 't1tu1' + 'completedR'))).toBeNull() }) it('returns null when task-id is absent', () => { expect(parseUnroutableCompletion(msg( 'completedR'))).toBeNull() }) it('returns null for a monitor event-only notification', () => { expect(parseUnroutableCompletion(msg( 'm1Monitor event: "build"' + 'exe produced'))).toBeNull() }) it('returns null for a status-only recovery notice', () => { expect(parseUnroutableCompletion(msg( 'a1stopped' + 'Background agent "x" didn\'t finish before the previous session ended' + ''))).toBeNull() }) it('returns null for a whitespace-only result body', () => { expect(parseUnroutableCompletion(msg( 'a1completed' + '\n '))).toBeNull() }) it('normalises an unrecognised status to failed', () => { expect(parseUnroutableCompletion(msg( 'a1weird' + 'R'))?.status).toBe('failed') }) it('takes the first non-empty repeated result element', () => { expect(parseUnroutableCompletion(msg( 'a1completed' + 'second'))?.result).toBe('second') }) it('returns null for a non-notification frame', () => { expect(parseUnroutableCompletion(msg('plain user text'))).toBeNull() }) }) describe('parseMonitorEventBatch', () => { it('parses a CLI monitor event batch (summary framing kept verbatim)', () => { expect(parseMonitorEventBatch(msg( 'm1Monitor event: "build"' + 'exe produced'))).toEqual({ taskId: 'm1', summary: 'Monitor event: "build"', events: ['exe produced'], }) }) it('parses the native shape (empty result element, bare description)', () => { expect(parseMonitorEventBatch(msg( 'tu1completed' + 'watch the feedtick 1'))).toEqual({ taskId: 'tu1', summary: 'watch the feed', events: ['tick 1'], }) }) it('collects EVERY non-empty event body in document order, unescaped', () => { const p = parseMonitorEventBatch(msg( 'm1line a ' + 'b <c> & d')) expect(p?.events).toEqual(['line a', 'b & d']) }) it('keeps multi-line event bodies intact (a batched delivery)', () => { const p = parseMonitorEventBatch(msg( 'm1l1\nl2\nl3')) expect(p?.events).toEqual(['l1\nl2\nl3']) }) it('returns null for a foldable injection (TasksPanel owns it)', () => { expect(parseMonitorEventBatch(msg( 't1tu1' + 'completedx'))).toBeNull() }) it('returns null for a result-bearing completion (unroutable parser owns it)', () => { expect(parseMonitorEventBatch(msg( 'a1completed' + 'reportx'))).toBeNull() }) it('returns null for a status-only notice and for whitespace-only events', () => { expect(parseMonitorEventBatch(msg( 'a1stopped'))).toBeNull() expect(parseMonitorEventBatch(msg( 'm1\n '))).toBeNull() }) it('returns null for a non-notification frame', () => { expect(parseMonitorEventBatch(msg('plain user text'))).toBeNull() }) it('leaves taskId/summary undefined when absent', () => { const p = parseMonitorEventBatch(msg( 'x')) expect(p).toEqual({ events: ['x'] }) }) }) describe('monitorEventTitle / monitorEventSearchText', () => { it('strips the CLI framing prefix for display', () => { expect(monitorEventTitle({ summary: 'Monitor event: "build watch"', events: ['x'] })).toBe('build watch') }) it('keeps a bare (native) description verbatim', () => { expect(monitorEventTitle({ summary: 'watch the feed', events: ['x'] })).toBe('watch the feed') }) it('returns empty for a missing summary', () => { expect(monitorEventTitle({ events: ['x'] })).toBe('') }) it('joins title then events with newlines — the card visible strings', () => { expect(monitorEventSearchText({ summary: 'Monitor event: "build"', events: ['line a', 'line b'], })).toBe('build\nline a\nline b') }) it('skips an empty title in the join (matches the index filter)', () => { expect(monitorEventSearchText({ events: ['only event'] })).toBe('only event') }) }) describe('imagePayloadOfContent', () => { it('counts image blocks and their base64 payload length', () => { expect(imagePayloadOfContent([ { type: 'text', text: 'look' }, { type: 'image', source: { type: 'base64', data: 'AAAA' } }, { type: 'image', source: { type: 'base64', data: 'BBBBBB' } }, ])).toEqual({ images: 2, bytes: 10 }) }) it('returns zeros for non-array or image-less content', () => { expect(imagePayloadOfContent('text')).toEqual({ images: 0, bytes: 0 }) expect(imagePayloadOfContent([{ type: 'text', text: 'x' }])).toEqual({ images: 0, bytes: 0 }) expect(imagePayloadOfContent([{ type: 'image', source: { type: 'url', url: 'x' } }])).toEqual({ images: 0, bytes: 0 }) }) }) describe('imagePayloadOfMessage', () => { it('reads the SDK message wrapper', () => { expect(imagePayloadOfMessage({ message: { content: [{ type: 'image', source: { type: 'base64', data: '12345678' } }] }, })).toEqual({ images: 1, bytes: 8 }) }) it('tolerates a missing or string content', () => { expect(imagePayloadOfMessage(null)).toEqual({ images: 0, bytes: 0 }) expect(imagePayloadOfMessage({ message: { content: 'text' } })).toEqual({ images: 0, bytes: 0 }) }) }) // Origin classification for null-parent user frames at ingestion. Shapes are // the ones verified live against SDK 0.3.311 / CLI 2.1.311 (see the agent note // 2026-11-19-user-frame-origin-identity): synthetic frames carry isSynthetic / // isReplay but NOT the disk-only isCompactSummary / isMeta markers. type ClassifierFrame = Parameters[0] const userText = (uuid: string, text = 'hello'): ClassifierFrame => ({ type: 'user', uuid, parent_tool_use_id: null, message: { content: [{ type: 'text', text }] } }) as ClassifierFrame const toolResultFrame = (toolUseId: string): ClassifierFrame => ({ type: 'user', uuid: 'tr-1', parent_tool_use_id: null, message: { content: [{ type: 'tool_result', tool_use_id: toolUseId, content: 'ok' }] } }) as ClassifierFrame const taskNotificationFrame = (): ClassifierFrame => userText('notif-1', 'tu1completedR') const isSent = (u: string) => u === 'sent-1' describe('classifyUserFrameOrigin', () => { it('a ledger uuid with no markers is a host echo', () => { expect(classifyUserFrameOrigin(userText('sent-1'), isSent)).toBe('host-echo') }) it('isSynthetic marks a CLI synthetic frame regardless of uuid', () => { expect(classifyUserFrameOrigin({ ...userText('foreign'), isSynthetic: true }, isSent)).toBe('cli-synthetic') // marker outranks ledger membership expect(classifyUserFrameOrigin({ ...userText('sent-1'), isSynthetic: true }, isSent)).toBe('cli-synthetic') }) it('isReplay marks a CLI synthetic frame (the /compact stdout shape)', () => { expect(classifyUserFrameOrigin({ ...userText('foreign'), isReplay: true }, isSent)).toBe('cli-synthetic') }) it('isCompactSummary marks a CLI synthetic frame', () => { expect(classifyUserFrameOrigin({ ...userText('foreign'), isCompactSummary: true }, isSent)).toBe('cli-synthetic') }) it('a foreign uuid with no markers is cli-other (fail-visible, never an echo)', () => { expect(classifyUserFrameOrigin(userText('foreign'), isSent)).toBe('cli-other') }) it('tool_result carriers classify ahead of the ledger', () => { expect(classifyUserFrameOrigin(toolResultFrame('t1'), isSent)).toBe('tool-result') }) it('task-notification injections classify ahead of the ledger', () => { expect(classifyUserFrameOrigin(taskNotificationFrame(), isSent)).toBe('task-notification') }) }) describe('isHumanUserMessage (shared)', () => { it('excludes a synthetic skill-injection frame', () => { expect(isHumanUserMessage({ ...userText('x', 'Base directory for this skill: /tmp/s'), isSynthetic: true })).toBe(false) }) it('accepts a plain human prompt', () => { expect(isHumanUserMessage(userText('x'))).toBe(true) }) })