Skip to content

Commit 50b9407

Browse files
Cover consumeTracking path when cleanup set and delete both fail
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 526b66f commit 50b9407

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,70 @@ describe("TriggerChatTransport", function () {
22322232
expect(errors[0]?.error.message).toBe("tracking failed root cause");
22332233
});
22342234

2235+
it(
2236+
"attempts consumeTracking cleanup set and delete when both cleanup steps throw",
2237+
async function () {
2238+
const errors: TriggerChatTransportError[] = [];
2239+
const runStore = new FailingCleanupSetAndDeleteRunStore();
2240+
2241+
const server = await startServer(function (req, res) {
2242+
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
2243+
res.writeHead(200, {
2244+
"content-type": "application/json",
2245+
"x-trigger-jwt": "pk_run_tracking_cleanup_both_failure",
2246+
});
2247+
res.end(JSON.stringify({ id: "run_tracking_cleanup_both_failure" }));
2248+
return;
2249+
}
2250+
2251+
res.writeHead(404);
2252+
res.end();
2253+
});
2254+
2255+
const transport = new TriggerChatTransport({
2256+
task: "chat-task",
2257+
stream: "chat-stream",
2258+
accessToken: "pk_trigger",
2259+
baseURL: server.url,
2260+
runStore,
2261+
onError: function onError(error) {
2262+
errors.push(error);
2263+
},
2264+
});
2265+
2266+
(transport as any).fetchRunStream = async function fetchRunStream() {
2267+
return new ReadableStream({
2268+
start(controller) {
2269+
controller.error(new Error("tracking failed root cause"));
2270+
},
2271+
});
2272+
};
2273+
2274+
const stream = await transport.sendMessages({
2275+
trigger: "submit-message",
2276+
chatId: "chat-tracking-cleanup-both-failure",
2277+
messageId: undefined,
2278+
messages: [],
2279+
abortSignal: undefined,
2280+
});
2281+
2282+
await expect(readChunks(stream)).rejects.toThrowError("tracking failed root cause");
2283+
2284+
await waitForCondition(function () {
2285+
return errors.length === 1;
2286+
});
2287+
2288+
expect(errors[0]).toMatchObject({
2289+
phase: "consumeTrackingStream",
2290+
chatId: "chat-tracking-cleanup-both-failure",
2291+
runId: "run_tracking_cleanup_both_failure",
2292+
});
2293+
expect(errors[0]?.error.message).toBe("tracking failed root cause");
2294+
expect(runStore.setCalls).toContain("chat-tracking-cleanup-both-failure");
2295+
expect(runStore.deleteCalls).toContain("chat-tracking-cleanup-both-failure");
2296+
}
2297+
);
2298+
22352299
it("reports reconnect failures through onError", async function () {
22362300
const errors: TriggerChatTransportError[] = [];
22372301
const runStore = new InMemoryTriggerChatRunStore();

0 commit comments

Comments
 (0)