import type { AgentProvider, AgentQuery, ProviderEvent, QueryInput } from './types.js'; /** * Mock provider for testing. Returns canned responses. * Supports push() — queued messages produce additional results. */ export class MockProvider implements AgentProvider { private responseFactory: (prompt: string) => string; constructor(responseFactory?: (prompt: string) => string) { this.responseFactory = responseFactory ?? ((prompt) => `Mock response to: ${prompt.slice(0, 100)}`); } query(input: QueryInput): AgentQuery { const pending: string[] = []; let waiting: (() => void) | null = null; let ended = false; let aborted = false; const responseFactory = this.responseFactory; const events: AsyncIterable = { async *[Symbol.asyncIterator]() { yield { type: 'activity' }; yield { type: 'init', sessionId: `mock-session-${Date.now()}` }; // Process initial prompt yield { type: 'activity' }; yield { type: 'result', text: responseFactory(input.prompt) }; // Process any pushed follow-ups while (!ended && !aborted) { if (pending.length > 0) { const msg = pending.shift()!; yield { type: 'result', text: responseFactory(msg) }; continue; } // Wait for push() or end() await new Promise((resolve) => { waiting = resolve; }); waiting = null; } // Drain remaining while (pending.length > 0) { const msg = pending.shift()!; yield { type: 'result', text: responseFactory(msg) }; } }, }; return { push(message: string) { pending.push(message); waiting?.(); }, end() { ended = true; waiting?.(); }, events, abort() { aborted = true; waiting?.(); }, }; } }