Skip to content

Commit a1d32c4

Browse files
Assert stream subscribe cleanup transitions run state
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 9790990 commit a1d32c4

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

packages/ai/src/chatTransport.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,7 +1076,7 @@ describe("TriggerChatTransport", function () {
10761076

10771077
it("reports stream subscription failures through onError", async function () {
10781078
const errors: TriggerChatTransportError[] = [];
1079-
const runStore = new InMemoryTriggerChatRunStore();
1079+
const runStore = new TrackedRunStore();
10801080

10811081
const server = await startServer(function (req, res) {
10821082
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
@@ -1124,12 +1124,24 @@ describe("TriggerChatTransport", function () {
11241124
runId: "run_stream_subscribe_error",
11251125
});
11261126
expect(errors[0]?.error.message).toBe("stream subscribe failed root");
1127+
expect(runStore.setSnapshots).toHaveLength(2);
1128+
expect(runStore.setSnapshots[0]).toMatchObject({
1129+
chatId: "chat-stream-subscribe-error",
1130+
runId: "run_stream_subscribe_error",
1131+
isActive: true,
1132+
});
1133+
expect(runStore.setSnapshots[1]).toMatchObject({
1134+
chatId: "chat-stream-subscribe-error",
1135+
runId: "run_stream_subscribe_error",
1136+
isActive: false,
1137+
});
1138+
expect(runStore.deleteCalls).toEqual(["chat-stream-subscribe-error"]);
11271139
expect(runStore.get("chat-stream-subscribe-error")).toBeUndefined();
11281140
});
11291141

11301142
it("normalizes non-Error stream subscription failures before reporting onError", async function () {
11311143
const errors: TriggerChatTransportError[] = [];
1132-
const runStore = new InMemoryTriggerChatRunStore();
1144+
const runStore = new TrackedRunStore();
11331145

11341146
const server = await startServer(function (req, res) {
11351147
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
@@ -1177,6 +1189,18 @@ describe("TriggerChatTransport", function () {
11771189
runId: "run_stream_subscribe_string_error",
11781190
});
11791191
expect(errors[0]?.error.message).toBe("stream subscribe string failure");
1192+
expect(runStore.setSnapshots).toHaveLength(2);
1193+
expect(runStore.setSnapshots[0]).toMatchObject({
1194+
chatId: "chat-stream-subscribe-string-error",
1195+
runId: "run_stream_subscribe_string_error",
1196+
isActive: true,
1197+
});
1198+
expect(runStore.setSnapshots[1]).toMatchObject({
1199+
chatId: "chat-stream-subscribe-string-error",
1200+
runId: "run_stream_subscribe_string_error",
1201+
isActive: false,
1202+
});
1203+
expect(runStore.deleteCalls).toEqual(["chat-stream-subscribe-string-error"]);
11801204
expect(runStore.get("chat-stream-subscribe-string-error")).toBeUndefined();
11811205
});
11821206

@@ -2165,8 +2189,16 @@ async function waitForCondition(
21652189
}
21662190

21672191
class TrackedRunStore extends InMemoryTriggerChatRunStore {
2192+
public readonly setSnapshots: TriggerChatRunState[] = [];
21682193
public readonly deleteCalls: string[] = [];
21692194

2195+
public set(state: TriggerChatRunState): void {
2196+
this.setSnapshots.push({
2197+
...state,
2198+
});
2199+
super.set(state);
2200+
}
2201+
21702202
public delete(chatId: string): void {
21712203
this.deleteCalls.push(chatId);
21722204
super.delete(chatId);

0 commit comments

Comments
 (0)