Skip to content

Commit 8dffb95

Browse files
Cover reconnect after completion with dual cleanup-step failures
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 8e8f3e0 commit 8dffb95

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,6 +3102,89 @@ describe("TriggerChatTransport", function () {
31023102
expect(runStore.deleteCalls).toContain("chat-cleanup-set-delete-failure");
31033103
});
31043104

3105+
it("returns null on reconnect after completion when cleanup set and delete both fail", async function () {
3106+
const errors: TriggerChatTransportError[] = [];
3107+
const runStore = new FailingCleanupSetAndDeleteRunStore(4);
3108+
3109+
const server = await startServer(function (req, res) {
3110+
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
3111+
res.writeHead(200, {
3112+
"content-type": "application/json",
3113+
"x-trigger-jwt": "pk_run_done_cleanup_both_failure",
3114+
});
3115+
res.end(JSON.stringify({ id: "run_done_cleanup_both_failure" }));
3116+
return;
3117+
}
3118+
3119+
if (
3120+
req.method === "GET" &&
3121+
req.url === "/realtime/v1/streams/run_done_cleanup_both_failure/chat-stream"
3122+
) {
3123+
res.writeHead(200, {
3124+
"content-type": "text/event-stream",
3125+
});
3126+
writeSSE(
3127+
res,
3128+
"1-0",
3129+
JSON.stringify({ type: "text-start", id: "done_cleanup_both_failure_1" })
3130+
);
3131+
writeSSE(
3132+
res,
3133+
"2-0",
3134+
JSON.stringify({ type: "text-end", id: "done_cleanup_both_failure_1" })
3135+
);
3136+
res.end();
3137+
return;
3138+
}
3139+
3140+
res.writeHead(404);
3141+
res.end();
3142+
});
3143+
3144+
const transport = new TriggerChatTransport({
3145+
task: "chat-task",
3146+
stream: "chat-stream",
3147+
accessToken: "pk_trigger",
3148+
baseURL: server.url,
3149+
runStore,
3150+
onError: function onError(error) {
3151+
errors.push(error);
3152+
},
3153+
});
3154+
3155+
const stream = await transport.sendMessages({
3156+
trigger: "submit-message",
3157+
chatId: "chat-done-cleanup-both-failure",
3158+
messageId: undefined,
3159+
messages: [],
3160+
abortSignal: undefined,
3161+
});
3162+
3163+
const chunks = await readChunks(stream);
3164+
expect(chunks).toHaveLength(2);
3165+
3166+
await waitForCondition(function () {
3167+
return runStore.deleteCalls.includes("chat-done-cleanup-both-failure");
3168+
});
3169+
3170+
(transport as any).fetchRunStream = async function fetchRunStream() {
3171+
throw new Error("reconnect root cause");
3172+
};
3173+
3174+
const reconnect = await transport.reconnectToStream({
3175+
chatId: "chat-done-cleanup-both-failure",
3176+
});
3177+
3178+
expect(reconnect).toBeNull();
3179+
expect(errors).toHaveLength(1);
3180+
expect(errors[0]).toMatchObject({
3181+
phase: "reconnect",
3182+
chatId: "chat-done-cleanup-both-failure",
3183+
runId: "run_done_cleanup_both_failure",
3184+
});
3185+
expect(errors[0]?.error.message).toBe("reconnect root cause");
3186+
});
3187+
31053188
it("returns null from reconnect after stream completion cleanup", async function () {
31063189
const server = await startServer(function (req, res) {
31073190
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {

0 commit comments

Comments
 (0)