From 0d6a432967dc730404b97de62d32836ab7e08457 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 1 Dec 2026 19:37:29 +0100 Subject: [PATCH] test(server): type the hosted assistant regression on ES2022 Use the lookup overload for all addresses and drain the stream with the existing TypeScript target. Server types rejected the default lookup overload and Array.fromAsync. --- .../ai-chat-client.service.test.ts | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) 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();