Skip to content

Commit 4963095

Browse files
authored
test: advance Mistral reconnect backoff virtually (#132422)
1 parent dc9405e commit 4963095

1 file changed

Lines changed: 62 additions & 36 deletions

File tree

‎extensions/mistral/realtime-transcription-provider.test.ts‎

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ async function createRealtimeServer(
1313
onRequest: (url: URL) => void,
1414
transcriptionEvents: readonly Record<string, unknown>[] = [],
1515
eventsByConnection?: readonly (readonly Record<string, unknown>[])[],
16+
onConnection?: (socket: WebSocket) => void,
1617
) {
1718
const server = createServer();
1819
const wss = new WebSocketServer({ noServer: true, maxPayload: 1024 * 1024 });
@@ -23,6 +24,7 @@ async function createRealtimeServer(
2324
wss.handleUpgrade(request, socket, head, (ws) => {
2425
const connectionIndex = connectionCount++;
2526
clients.add(ws);
27+
onConnection?.(ws);
2628
ws.on("close", () => {
2729
clients.delete(ws);
2830
});
@@ -37,9 +39,6 @@ async function createRealtimeServer(
3739
for (const event of eventsByConnection?.[connectionIndex] ?? transcriptionEvents) {
3840
ws.send(JSON.stringify(event));
3941
}
40-
if (eventsByConnection && connectionIndex === 0) {
41-
setTimeout(() => ws.terminate(), 10);
42-
}
4342
}
4443
});
4544
ws.send(JSON.stringify({ type: "session.created" }));
@@ -70,6 +69,7 @@ describe("buildMistralRealtimeTranscriptionProvider", () => {
7069
afterEach(async () => {
7170
await cleanup?.();
7271
cleanup = undefined;
72+
vi.useRealTimers();
7373
vi.unstubAllEnvs();
7474
});
7575

@@ -349,6 +349,7 @@ describe("buildMistralRealtimeTranscriptionProvider", () => {
349349
name: "preserves the replacement session's terminal-only speech",
350350
firstEvents: [{ type: "transcription.segment", text: "earlier turn" }],
351351
secondEvents: [{ type: "transcription.done", text: "replacement final" }],
352+
firstPartials: [],
352353
partials: [],
353354
},
354355
{
@@ -361,43 +362,68 @@ describe("buildMistralRealtimeTranscriptionProvider", () => {
361362
{ type: "transcription.text.delta", text: "new fragment" },
362363
{ type: "transcription.done", text: "replacement final" },
363364
],
365+
firstPartials: ["old fragment "],
364366
partials: ["old fragment ", "new fragment"],
365367
},
366-
])("$name after a real provider reconnect", async ({ firstEvents, secondEvents, partials }) => {
367-
const requests: URL[] = [];
368-
const baseUrl = await createRealtimeServer(
369-
(url) => requests.push(url),
370-
[],
371-
[firstEvents, secondEvents],
372-
);
373-
const onPartial = vi.fn();
374-
const onTranscript = vi.fn();
375-
const onError = vi.fn();
376-
const session = buildMistralRealtimeTranscriptionProvider().createSession({
377-
providerConfig: { apiKey: "fixture-value", baseUrl },
378-
onPartial,
379-
onTranscript,
380-
onError,
381-
});
368+
])(
369+
"$name after a real provider reconnect",
370+
async ({ firstEvents, secondEvents, firstPartials, partials }) => {
371+
const requests: URL[] = [];
372+
let firstSocket: WebSocket | undefined;
373+
const baseUrl = await createRealtimeServer(
374+
(url) => requests.push(url),
375+
[],
376+
[firstEvents, secondEvents],
377+
(socket) => {
378+
firstSocket ??= socket;
379+
},
380+
);
381+
const onPartial = vi.fn();
382+
const onTranscript = vi.fn();
383+
const onError = vi.fn();
384+
const session = buildMistralRealtimeTranscriptionProvider().createSession({
385+
providerConfig: { apiKey: "fixture-value", baseUrl },
386+
onPartial,
387+
onTranscript,
388+
onError,
389+
});
382390

383-
await session.connect();
384-
// Wait on the delivered transcripts, not just the socket close: the replacement
385-
// connection can close before its `transcription.done` reaches onTranscript.
386-
await vi.waitFor(
387-
() => {
388-
expect(requests).toHaveLength(2);
389-
expect(session.isConnected()).toBe(false);
390-
expect(onTranscript.mock.calls.map(([text]) => text)).toEqual([
391-
"earlier turn",
392-
"replacement final",
393-
]);
394-
},
395-
{ timeout: 3_000 },
396-
);
391+
try {
392+
await session.connect();
393+
await vi.waitFor(() => {
394+
expect(onTranscript.mock.calls.map(([text]) => text)).toEqual(["earlier turn"]);
395+
expect(onPartial.mock.calls.map(([text]) => text)).toEqual(firstPartials);
396+
});
397+
if (!firstSocket) {
398+
throw new Error("Mistral fixture did not establish its first WebSocket");
399+
}
400+
vi.useFakeTimers();
401+
firstSocket.terminate();
402+
// The real close must arm the retry before its backoff clock advances.
403+
await vi.waitFor(() => expect(session.isConnected()).toBe(false));
404+
await vi.advanceTimersByTimeAsync(1_000);
405+
// Wait on the delivered transcripts, not just the socket close: the replacement
406+
// connection can close before its `transcription.done` reaches onTranscript.
407+
await vi.waitFor(
408+
() => {
409+
expect(requests).toHaveLength(2);
410+
expect(session.isConnected()).toBe(false);
411+
expect(onTranscript.mock.calls.map(([text]) => text)).toEqual([
412+
"earlier turn",
413+
"replacement final",
414+
]);
415+
},
416+
{ timeout: 3_000 },
417+
);
397418

398-
expect(onPartial.mock.calls.map(([text]) => text)).toEqual(partials);
399-
expect(onError).not.toHaveBeenCalled();
400-
});
419+
expect(onPartial.mock.calls.map(([text]) => text)).toEqual(partials);
420+
expect(onError).not.toHaveBeenCalled();
421+
} finally {
422+
session.close();
423+
vi.useRealTimers();
424+
}
425+
},
426+
);
401427

402428
it("tracks the in-progress transcript limit as aggregate UTF-8 bytes", async () => {
403429
const exactUtf8Limit = "🙂".repeat((256 * 1024) / 4);

0 commit comments

Comments
 (0)