Skip to content

Commit d1e9220

Browse files
Cover unicode-wrapped baseURL path-prefix runtime normalization
Co-authored-by: Eric Allam <eric@trigger.dev>
1 parent 17a1975 commit d1e9220

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

packages/ai/src/chatTransport.test.ts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,71 @@ describe("TriggerChatTransport", function () {
677677
expect(observedStreamPath).toBe("/trimmed-prefix/realtime/v1/streams/run_trimmed_prefix/chat-stream");
678678
});
679679

680+
it("preserves baseURL path prefixes after trimming unicode wrapper whitespace", async function () {
681+
let observedTriggerPath: string | undefined;
682+
let observedStreamPath: string | undefined;
683+
684+
const server = await startServer(function (req, res) {
685+
if (req.method === "POST") {
686+
observedTriggerPath = req.url ?? "";
687+
}
688+
689+
if (req.method === "GET") {
690+
observedStreamPath = req.url ?? "";
691+
}
692+
693+
if (req.method === "POST" && req.url === "/unicode-prefix/api/v1/tasks/chat-task/trigger") {
694+
res.writeHead(200, {
695+
"content-type": "application/json",
696+
"x-trigger-jwt": "pk_run_unicode_prefix",
697+
});
698+
res.end(JSON.stringify({ id: "run_unicode_prefix" }));
699+
return;
700+
}
701+
702+
if (req.method === "GET" && req.url === "/unicode-prefix/realtime/v1/streams/run_unicode_prefix/chat-stream") {
703+
res.writeHead(200, {
704+
"content-type": "text/event-stream",
705+
});
706+
writeSSE(
707+
res,
708+
"1-0",
709+
JSON.stringify({ type: "text-start", id: "unicode_prefix_1" })
710+
);
711+
writeSSE(
712+
res,
713+
"2-0",
714+
JSON.stringify({ type: "text-end", id: "unicode_prefix_1" })
715+
);
716+
res.end();
717+
return;
718+
}
719+
720+
res.writeHead(404);
721+
res.end();
722+
});
723+
724+
const transport = new TriggerChatTransport({
725+
task: "chat-task",
726+
accessToken: "pk_trigger",
727+
baseURL: `\u3000${server.url}/unicode-prefix///\u3000`,
728+
stream: "chat-stream",
729+
});
730+
731+
const stream = await transport.sendMessages({
732+
trigger: "submit-message",
733+
chatId: "chat-unicode-prefix-baseurl",
734+
messageId: undefined,
735+
messages: [],
736+
abortSignal: undefined,
737+
});
738+
739+
const chunks = await readChunks(stream);
740+
expect(chunks).toHaveLength(2);
741+
expect(observedTriggerPath).toBe("/unicode-prefix/api/v1/tasks/chat-task/trigger");
742+
expect(observedStreamPath).toBe("/unicode-prefix/realtime/v1/streams/run_unicode_prefix/chat-stream");
743+
});
744+
680745
it("throws when baseURL is empty after trimming", function () {
681746
expect(function () {
682747
new TriggerChatTransport({
@@ -3702,6 +3767,74 @@ describe("TriggerChatTransport", function () {
37023767
});
37033768
});
37043769

3770+
it("supports creating transport with factory function and unicode-wrapped baseURL path prefixes", async function () {
3771+
let observedTriggerPath: string | undefined;
3772+
let observedStreamPath: string | undefined;
3773+
3774+
const server = await startServer(function (req, res) {
3775+
if (req.method === "POST") {
3776+
observedTriggerPath = req.url ?? "";
3777+
}
3778+
3779+
if (req.method === "GET") {
3780+
observedStreamPath = req.url ?? "";
3781+
}
3782+
3783+
if (req.method === "POST" && req.url === "/factory-unicode-prefix/api/v1/tasks/chat-task/trigger") {
3784+
res.writeHead(200, {
3785+
"content-type": "application/json",
3786+
"x-trigger-jwt": "pk_run_factory_unicode_prefix",
3787+
});
3788+
res.end(JSON.stringify({ id: "run_factory_unicode_prefix" }));
3789+
return;
3790+
}
3791+
3792+
if (
3793+
req.method === "GET" &&
3794+
req.url === "/factory-unicode-prefix/realtime/v1/streams/run_factory_unicode_prefix/chat-stream"
3795+
) {
3796+
res.writeHead(200, {
3797+
"content-type": "text/event-stream",
3798+
});
3799+
writeSSE(
3800+
res,
3801+
"1-0",
3802+
JSON.stringify({ type: "text-start", id: "factory_unicode_prefix_1" })
3803+
);
3804+
writeSSE(
3805+
res,
3806+
"2-0",
3807+
JSON.stringify({ type: "text-end", id: "factory_unicode_prefix_1" })
3808+
);
3809+
res.end();
3810+
return;
3811+
}
3812+
3813+
res.writeHead(404);
3814+
res.end();
3815+
});
3816+
3817+
const transport = createTriggerChatTransport({
3818+
task: "chat-task",
3819+
stream: "chat-stream",
3820+
accessToken: "pk_trigger",
3821+
baseURL: `\u3000${server.url}/factory-unicode-prefix///\u3000`,
3822+
});
3823+
3824+
const stream = await transport.sendMessages({
3825+
trigger: "submit-message",
3826+
chatId: "chat-factory-unicode-prefix",
3827+
messageId: undefined,
3828+
messages: [],
3829+
abortSignal: undefined,
3830+
});
3831+
3832+
const chunks = await readChunks(stream);
3833+
expect(chunks).toHaveLength(2);
3834+
expect(observedTriggerPath).toBe("/factory-unicode-prefix/api/v1/tasks/chat-task/trigger");
3835+
expect(observedStreamPath).toBe("/factory-unicode-prefix/realtime/v1/streams/run_factory_unicode_prefix/chat-stream");
3836+
});
3837+
37053838
it("throws from factory when baseURL is empty after trimming", function () {
37063839
expect(function () {
37073840
createTriggerChatTransport({

0 commit comments

Comments
 (0)