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.
This commit is contained in:
Maurice
2026-12-01 19:37:29 +01:00
parent 89035059ec
commit 0d6a432967
@@ -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<LookupAddress[]> } = 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();