Skip to content

Commit d706534

Browse files
Test reconnect null behavior after completed streams
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent b511566 commit d706534

File tree

1 file changed

+65
-2
lines changed

1 file changed

+65
-2
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,66 @@ describe("TriggerChatTransport", function () {
399399
expect(trackedRunStore.get("chat-cleanup")).toBeUndefined();
400400
});
401401

402+
it("returns null from reconnect after stream completion cleanup", async function () {
403+
const server = await startServer(function (req, res) {
404+
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
405+
res.writeHead(200, {
406+
"content-type": "application/json",
407+
"x-trigger-jwt": "pk_run_done",
408+
});
409+
res.end(JSON.stringify({ id: "run_done" }));
410+
return;
411+
}
412+
413+
if (req.method === "GET" && req.url === "/realtime/v1/streams/run_done/chat-stream") {
414+
res.writeHead(200, {
415+
"content-type": "text/event-stream",
416+
});
417+
writeSSE(
418+
res,
419+
"1-0",
420+
JSON.stringify({ type: "text-start", id: "done_1" })
421+
);
422+
writeSSE(
423+
res,
424+
"2-0",
425+
JSON.stringify({ type: "text-end", id: "done_1" })
426+
);
427+
res.end();
428+
return;
429+
}
430+
431+
res.writeHead(404);
432+
res.end();
433+
});
434+
435+
const transport = new TriggerChatTransport({
436+
task: "chat-task",
437+
stream: "chat-stream",
438+
accessToken: "pk_trigger",
439+
baseURL: server.url,
440+
});
441+
442+
const stream = await transport.sendMessages({
443+
trigger: "submit-message",
444+
chatId: "chat-done",
445+
messageId: undefined,
446+
messages: [],
447+
abortSignal: undefined,
448+
});
449+
450+
const chunks = await readChunks(stream);
451+
expect(chunks).toHaveLength(2);
452+
453+
await waitForCondition(async function () {
454+
const reconnect = await transport.reconnectToStream({
455+
chatId: "chat-done",
456+
});
457+
458+
return reconnect === null;
459+
});
460+
});
461+
402462
it("reconnects active streams using tracked lastEventId", async function () {
403463
let reconnectLastEventId: string | undefined;
404464
let firstStreamResponse: ServerResponse<IncomingMessage> | undefined;
@@ -574,11 +634,14 @@ async function readChunks(stream: ReadableStream<UIMessageChunk>) {
574634
return parts;
575635
}
576636

577-
async function waitForCondition(condition: () => boolean, timeoutInMs = 5000) {
637+
async function waitForCondition(
638+
condition: () => boolean | Promise<boolean>,
639+
timeoutInMs = 5000
640+
) {
578641
const start = Date.now();
579642

580643
while (Date.now() - start < timeoutInMs) {
581-
if (condition()) {
644+
if (await condition()) {
582645
return;
583646
}
584647

0 commit comments

Comments
 (0)