Skip to content

Commit fc3d9b8

Browse files
Cover completion path behavior when cleanup set fails
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent d4266a6 commit fc3d9b8

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,74 @@ describe("TriggerChatTransport", function () {
25362536
});
25372537
});
25382538

2539+
it("keeps completed streams successful when cleanup set fails", async function () {
2540+
const errors: TriggerChatTransportError[] = [];
2541+
const runStore = new FailingCleanupSetRunStore(4);
2542+
2543+
const server = await startServer(function (req, res) {
2544+
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
2545+
res.writeHead(200, {
2546+
"content-type": "application/json",
2547+
"x-trigger-jwt": "pk_run_cleanup_set_failure",
2548+
});
2549+
res.end(JSON.stringify({ id: "run_cleanup_set_failure" }));
2550+
return;
2551+
}
2552+
2553+
if (
2554+
req.method === "GET" &&
2555+
req.url === "/realtime/v1/streams/run_cleanup_set_failure/chat-stream"
2556+
) {
2557+
res.writeHead(200, {
2558+
"content-type": "text/event-stream",
2559+
});
2560+
writeSSE(
2561+
res,
2562+
"1-0",
2563+
JSON.stringify({ type: "text-start", id: "cleanup_set_failure_1" })
2564+
);
2565+
writeSSE(
2566+
res,
2567+
"2-0",
2568+
JSON.stringify({ type: "text-end", id: "cleanup_set_failure_1" })
2569+
);
2570+
res.end();
2571+
return;
2572+
}
2573+
2574+
res.writeHead(404);
2575+
res.end();
2576+
});
2577+
2578+
const transport = new TriggerChatTransport({
2579+
task: "chat-task",
2580+
stream: "chat-stream",
2581+
accessToken: "pk_trigger",
2582+
baseURL: server.url,
2583+
runStore,
2584+
onError: function onError(error) {
2585+
errors.push(error);
2586+
},
2587+
});
2588+
2589+
const stream = await transport.sendMessages({
2590+
trigger: "submit-message",
2591+
chatId: "chat-cleanup-set-failure",
2592+
messageId: undefined,
2593+
messages: [],
2594+
abortSignal: undefined,
2595+
});
2596+
2597+
const chunks = await readChunks(stream);
2598+
expect(chunks).toHaveLength(2);
2599+
expect(errors).toHaveLength(0);
2600+
2601+
await waitForCondition(function () {
2602+
const state = runStore.get("chat-cleanup-set-failure");
2603+
return Boolean(state && state.isActive === true && state.lastEventId === "2-0");
2604+
});
2605+
});
2606+
25392607
it("returns null from reconnect after stream completion cleanup", async function () {
25402608
const server = await startServer(function (req, res) {
25412609
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {

0 commit comments

Comments
 (0)