diff --git a/server/tests/unit/nest/ai-connections/ai-chat-client.service.test.ts b/server/tests/unit/nest/ai-connections/ai-chat-client.service.test.ts index 9774e78f6..a91c2198a 100644 --- a/server/tests/unit/nest/ai-connections/ai-chat-client.service.test.ts +++ b/server/tests/unit/nest/ai-connections/ai-chat-client.service.test.ts @@ -3,6 +3,7 @@ * OpenAI reasoning model behind an API key takes its function tools only on the * Responses API, so it goes there like a ChatGPT plan does (#2792). */ +import type { AiChatEvent } from '../../../../src/nest/ai-connections/ai-chat-client.interface'; import { AiChatClientService, openAiWantsResponses } from '../../../../src/nest/ai-connections/ai-chat-client.service'; import { AiConnectionsModule } from '../../../../src/nest/ai-connections/ai-connections.module'; import type { ResolvedAiConnection } from '../../../../src/nest/ai-connections/ai-connections.types'; @@ -16,6 +17,7 @@ import { OidcService } from '../../../../src/nest/oidc/oidc.service'; import { createSnapshotTestDb } from '../../../helpers/db-mock'; import { createTestModule, type TestModule } from '../../../helpers/test-module'; +import type { LookupAddress, LookupAllOptions } from 'node:dns'; import dns from 'node:dns/promises'; import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; @@ -79,7 +81,8 @@ describe('AiChatClientService', () => { }); it('uses the operator gateway through its private proxy and refuses a redirect', async () => { - const lookup = vi.spyOn(dns, 'lookup').mockResolvedValue([{ address: '10.210.0.2', family: 4 }]); + const resolver: { lookup(hostname: string, options: LookupAllOptions): Promise } = dns; + const lookup = vi.spyOn(resolver, 'lookup').mockResolvedValue([{ address: '10.210.0.2', family: 4 }]); const fetch = vi.spyOn(globalThis, 'fetch'); try { const base = 'https://gate.example/v1'; @@ -88,16 +91,21 @@ describe('AiChatClientService', () => { managed: true, baseUrl: base, }); - const ask = () => - client.stream( + const ask = async () => { + let lastEvent: AiChatEvent | undefined; + const stream = client.stream( { system: 'Test instructions', messages: [{ role: 'user', text: 'Hello' }], tools: [] }, new AbortController().signal, ); + for await (const event of stream) { + lastEvent = event; + } + return lastEvent; + }; fetch.mockResolvedValueOnce( new Response('data: [DONE]\n\n', { headers: { 'content-type': 'text/event-stream' } }), ); - const events = await Array.fromAsync(ask()); - expect(events.at(-1)).toMatchObject({ type: 'finish' }); + await expect(ask()).resolves.toMatchObject({ type: 'finish' }); expect(fetch).toHaveBeenLastCalledWith( `${base}/chat/completions`, expect.objectContaining({ redirect: 'manual' }), @@ -106,7 +114,7 @@ describe('AiChatClientService', () => { fetch.mockResolvedValueOnce( new Response(null, { status: 307, headers: { location: 'https://other.example/v1' } }), ); - await expect(Array.fromAsync(ask())).rejects.toMatchObject({ code: 'provider_unavailable' }); + await expect(ask()).rejects.toMatchObject({ code: 'provider_unavailable' }); expect(fetch).toHaveBeenCalledTimes(2); } finally { fetch.mockRestore();