Skip to content

Commit e782303

Browse files
Cover async run-store cleanup on stream subscribe failures
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent fe5cd3d commit e782303

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,55 @@ describe("TriggerChatTransport", function () {
12491249
expect(runStore.get("chat-stream-subscribe-onerror-failure")).toBeUndefined();
12501250
});
12511251

1252+
it("cleans up async run-store state when stream subscription fails", async function () {
1253+
const runStore = new AsyncTrackedRunStore();
1254+
1255+
const server = await startServer(function (req, res) {
1256+
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
1257+
res.writeHead(200, {
1258+
"content-type": "application/json",
1259+
"x-trigger-jwt": "pk_stream_subscribe_async_failure",
1260+
});
1261+
res.end(JSON.stringify({ id: "run_stream_subscribe_async_failure" }));
1262+
return;
1263+
}
1264+
1265+
res.writeHead(404);
1266+
res.end();
1267+
});
1268+
1269+
const transport = new TriggerChatTransport({
1270+
task: "chat-task",
1271+
stream: "chat-stream",
1272+
accessToken: "pk_trigger",
1273+
baseURL: server.url,
1274+
runStore,
1275+
});
1276+
1277+
(transport as any).fetchRunStream = async function fetchRunStream() {
1278+
throw new Error("stream subscribe async failure");
1279+
};
1280+
1281+
await expect(
1282+
transport.sendMessages({
1283+
trigger: "submit-message",
1284+
chatId: "chat-stream-subscribe-async-failure",
1285+
messageId: undefined,
1286+
messages: [],
1287+
abortSignal: undefined,
1288+
})
1289+
).rejects.toThrowError("stream subscribe async failure");
1290+
1291+
expect(runStore.setCalls).toEqual([
1292+
"chat-stream-subscribe-async-failure",
1293+
"chat-stream-subscribe-async-failure",
1294+
]);
1295+
expect(runStore.deleteCalls).toEqual(["chat-stream-subscribe-async-failure"]);
1296+
await expect(
1297+
runStore.get("chat-stream-subscribe-async-failure")
1298+
).resolves.toBeUndefined();
1299+
});
1300+
12521301
it("supports creating transport with factory function", async function () {
12531302
let observedRunId: string | undefined;
12541303
let callbackCompleted = false;

0 commit comments

Comments
 (0)