import { beforeEach, describe, expect, it, vi } from 'vitest'; const hostApi = vi.hoisted(() => ({ fetch: vi.fn(), ensureToken: vi.fn(), createEvents: vi.fn(), })); vi.mock('@/lib/host-api', () => ({ hostApiFetch: hostApi.fetch, ensureHostApiToken: hostApi.ensureToken, createHostEventSource: hostApi.createEvents, })); describe('coding Conversations Host facade', () => { beforeEach(() => { vi.clearAllMocks(); }); it('loads the target snapshot through the encoded Host route', async () => { const snapshot = { conversation: { id: 'conversation/a' } }; hostApi.fetch.mockResolvedValueOnce({ snapshot }); const { getCodingConversationSnapshot } = await import('@/lib/coding-conversations'); await expect(getCodingConversationSnapshot('conversation/a')).resolves.toBe(snapshot); expect(hostApi.fetch).toHaveBeenCalledWith( '/api/coding/conversations/conversation%2Fa/snapshot', ); }); it('submits only the product prompt DTO and returns its acceptance', async () => { const acceptance = { accepted: true, conversationId: 'conversation/a', clientRequestId: 'request-1', runId: 'run-1', mode: 'prompt', }; hostApi.fetch.mockResolvedValueOnce({ acceptance }); const { submitCodingConversationPrompt } = await import('@/lib/coding-conversations'); await expect(submitCodingConversationPrompt({ conversationId: 'conversation/a', clientRequestId: 'request-1', mode: 'prompt', text: 'Build it', attachments: [{ attachmentId: 'attachment-1' }], })).resolves.toBe(acceptance); expect(hostApi.fetch).toHaveBeenCalledWith( '/api/coding/conversations/conversation%2Fa/prompt', { method: 'POST', body: JSON.stringify({ clientRequestId: 'request-1', mode: 'prompt', text: 'Build it', attachments: [{ attachmentId: 'attachment-1' }], }), }, ); }); it('hydrates the Host token before constructing the EventSource URL', async () => { const order: string[] = []; const source = { close: vi.fn() } as unknown as EventSource; hostApi.ensureToken.mockImplementationOnce(async () => { order.push('token'); return 'host-token'; }); hostApi.createEvents.mockImplementationOnce((path: string) => { order.push(`events:${path}`); return source; }); const { openCodingConversationEvents } = await import('@/lib/coding-conversations'); await expect(openCodingConversationEvents('conversation/a')).resolves.toBe(source); expect(order).toEqual([ 'token', 'events:/api/coding/events?conversationId=conversation%2Fa', ]); }); });