import { describe, expect, it, vi } from 'vitest' import { HttpError } from './errors.js' import type { SubagentTranscriptSub } from './subagent-transcript-hub.js' import { createSubagentTranscriptHub, type SubagentTranscriptSource } from './subagent-transcript-source.js' function source(): SubagentTranscriptSource & { calls: Array<{ sessionId: string; agentId: string }> sub: SubagentTranscriptSub close: () => void } { const calls: Array<{ sessionId: string; agentId: string }> = [] const sub: SubagentTranscriptSub = { iterable: (async function* () { yield { sessionId: 's', agentId: 'a', reset: true, frames: [] } })(), unsubscribe: () => {}, } return { calls, sub, subscribeSubagentTranscript(sessionId, agentId) { calls.push({ sessionId, agentId }) return sub }, close: vi.fn(), } } describe('createSubagentTranscriptHub', () => { it('resolves the session, then delegates to the source', () => { const src = source() const resolve = vi.fn(() => ({ name: 'native' })) const hub = createSubagentTranscriptHub({ resolve, source: src }) expect(hub.subscribeSubagentTranscript('s1', 'agent-1')).toBe(src.sub) expect(resolve).toHaveBeenCalledWith('s1') expect(src.calls).toEqual([{ sessionId: 's1', agentId: 'agent-1' }]) }) it('propagates a resolve failure instead of falling back to the source', () => { const src = source() const failure = new HttpError(501, 'provider native does not support subagent transcripts') const hub = createSubagentTranscriptHub({ resolve: () => { throw failure }, source: src, }) expect(() => hub.subscribeSubagentTranscript('s1', 'agent-1')).toThrow(failure) expect(src.calls).toEqual([]) }) it('rejects an unknown session with the resolver 404', () => { const src = source() const hub = createSubagentTranscriptHub({ resolve: () => { throw new HttpError(404, 'session ghost not found') }, source: src, }) expect(() => hub.subscribeSubagentTranscript('ghost', 'agent-1')).toThrowError( expect.objectContaining({ status: 404 }), ) expect(src.calls).toEqual([]) }) it('closes the source', () => { const src = source() const hub = createSubagentTranscriptHub({ resolve: () => ({}), source: src }) hub.close() expect(src.close).toHaveBeenCalledTimes(1) }) })