Skip to content

Commit c132ca4

Browse files
Add baseURL whitespace trim path-prefix transport test
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent b38d231 commit c132ca4

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,71 @@ describe("TriggerChatTransport", function () {
544544
expect(observedStreamPath).toBe("/realtime/v1/streams/run_trimmed_baseurl/chat-stream");
545545
});
546546

547+
it("preserves baseURL path prefixes after trimming surrounding whitespace", async function () {
548+
let observedTriggerPath: string | undefined;
549+
let observedStreamPath: string | undefined;
550+
551+
const server = await startServer(function (req, res) {
552+
if (req.method === "POST") {
553+
observedTriggerPath = req.url ?? "";
554+
}
555+
556+
if (req.method === "GET") {
557+
observedStreamPath = req.url ?? "";
558+
}
559+
560+
if (req.method === "POST" && req.url === "/trimmed-prefix/api/v1/tasks/chat-task/trigger") {
561+
res.writeHead(200, {
562+
"content-type": "application/json",
563+
"x-trigger-jwt": "pk_run_trimmed_prefix",
564+
});
565+
res.end(JSON.stringify({ id: "run_trimmed_prefix" }));
566+
return;
567+
}
568+
569+
if (req.method === "GET" && req.url === "/trimmed-prefix/realtime/v1/streams/run_trimmed_prefix/chat-stream") {
570+
res.writeHead(200, {
571+
"content-type": "text/event-stream",
572+
});
573+
writeSSE(
574+
res,
575+
"1-0",
576+
JSON.stringify({ type: "text-start", id: "trimmed_prefix_1" })
577+
);
578+
writeSSE(
579+
res,
580+
"2-0",
581+
JSON.stringify({ type: "text-end", id: "trimmed_prefix_1" })
582+
);
583+
res.end();
584+
return;
585+
}
586+
587+
res.writeHead(404);
588+
res.end();
589+
});
590+
591+
const transport = new TriggerChatTransport({
592+
task: "chat-task",
593+
accessToken: "pk_trigger",
594+
baseURL: ` ${server.url}/trimmed-prefix/// `,
595+
stream: "chat-stream",
596+
});
597+
598+
const stream = await transport.sendMessages({
599+
trigger: "submit-message",
600+
chatId: "chat-trimmed-prefix-baseurl",
601+
messageId: undefined,
602+
messages: [],
603+
abortSignal: undefined,
604+
});
605+
606+
const chunks = await readChunks(stream);
607+
expect(chunks).toHaveLength(2);
608+
expect(observedTriggerPath).toBe("/trimmed-prefix/api/v1/tasks/chat-task/trigger");
609+
expect(observedStreamPath).toBe("/trimmed-prefix/realtime/v1/streams/run_trimmed_prefix/chat-stream");
610+
});
611+
547612
it("combines path prefixes with run and stream URL encoding", async function () {
548613
let observedTriggerPath: string | undefined;
549614
let observedStreamPath: string | undefined;

0 commit comments

Comments
 (0)