Skip to content

Commit 889194a

Browse files
Cover typed stream definition ids in transport tests
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 7d17be7 commit 889194a

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
createTriggerChatTransport,
77
TriggerChatTransport,
88
} from "./chatTransport.js";
9+
import type { TriggerChatStream } from "./types.js";
910
import type { UIMessage, UIMessageChunk } from "ai";
1011
import type {
1112
TriggerChatRunState,
@@ -149,6 +150,72 @@ describe("TriggerChatTransport", function () {
149150
expect(observedStreamPath).toBe("/realtime/v1/streams/run_encoded_stream/chat%2Fspecial%20stream");
150151
});
151152

153+
it("uses defined stream object id when provided", async function () {
154+
let observedStreamPath: string | undefined;
155+
156+
const streamDefinition = {
157+
id: "typed-stream-id",
158+
pipe: async function pipe() {
159+
throw new Error("not used in this test");
160+
},
161+
} as unknown as TriggerChatStream<UIMessage>;
162+
163+
const server = await startServer(function (req, res) {
164+
if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") {
165+
res.writeHead(200, {
166+
"content-type": "application/json",
167+
"x-trigger-jwt": "pk_run_stream_object",
168+
});
169+
res.end(JSON.stringify({ id: "run_stream_object" }));
170+
return;
171+
}
172+
173+
if (req.method === "GET") {
174+
observedStreamPath = req.url ?? "";
175+
}
176+
177+
if (req.method === "GET" && req.url === "/realtime/v1/streams/run_stream_object/typed-stream-id") {
178+
res.writeHead(200, {
179+
"content-type": "text/event-stream",
180+
});
181+
writeSSE(
182+
res,
183+
"1-0",
184+
JSON.stringify({ type: "text-start", id: "typed_1" })
185+
);
186+
writeSSE(
187+
res,
188+
"2-0",
189+
JSON.stringify({ type: "text-end", id: "typed_1" })
190+
);
191+
res.end();
192+
return;
193+
}
194+
195+
res.writeHead(404);
196+
res.end();
197+
});
198+
199+
const transport = new TriggerChatTransport({
200+
task: "chat-task",
201+
accessToken: "pk_trigger",
202+
baseURL: server.url,
203+
stream: streamDefinition,
204+
});
205+
206+
const stream = await transport.sendMessages({
207+
trigger: "submit-message",
208+
chatId: "chat-typed-stream",
209+
messageId: undefined,
210+
messages: [],
211+
abortSignal: undefined,
212+
});
213+
214+
const chunks = await readChunks(stream);
215+
expect(chunks).toHaveLength(2);
216+
expect(observedStreamPath).toBe("/realtime/v1/streams/run_stream_object/typed-stream-id");
217+
});
218+
152219
it("triggers task and streams chunks with rich default payload", async function () {
153220
let receivedTriggerBody: Record<string, unknown> | undefined;
154221

0 commit comments

Comments
 (0)