|
| 1 | +import { expectTypeOf, it } from "vitest"; |
| 2 | +import type { InferUIMessageChunk, UIMessage } from "ai"; |
| 3 | +import { |
| 4 | + TriggerChatTransport, |
| 5 | + TriggerChatTransportOptions, |
| 6 | + type TriggerChatTransportPayload, |
| 7 | + type TriggerChatTransportRequest, |
| 8 | + type TriggerChatRunState, |
| 9 | +} from "./index.js"; |
| 10 | +import type { RealtimeDefinedStream } from "@trigger.dev/core/v3"; |
| 11 | + |
| 12 | +it("infers rich default payload contract", function () { |
| 13 | + const transport = new TriggerChatTransport({ |
| 14 | + task: "ai-chat", |
| 15 | + accessToken: "pk_test", |
| 16 | + stream: "chat-stream", |
| 17 | + }); |
| 18 | + |
| 19 | + expectTypeOf(transport).toEqualTypeOf< |
| 20 | + TriggerChatTransport<UIMessage, TriggerChatTransportPayload<UIMessage>> |
| 21 | + >(); |
| 22 | +}); |
| 23 | + |
| 24 | +it("requires payload mapper for custom payload types", function () { |
| 25 | + // @ts-expect-error Custom payload generic requires payloadMapper |
| 26 | + const invalidOptions: TriggerChatTransportOptions<UIMessage, { prompt: string }> = { |
| 27 | + task: "ai-chat", |
| 28 | + accessToken: "pk_test", |
| 29 | + stream: "chat-stream", |
| 30 | + }; |
| 31 | + |
| 32 | + expectTypeOf(invalidOptions).toBeObject(); |
| 33 | +}); |
| 34 | + |
| 35 | +it("types mapper input with rich request context", function () { |
| 36 | + const options: TriggerChatTransportOptions< |
| 37 | + UIMessage, |
| 38 | + { prompt: string; chatId: string; source: string | undefined } |
| 39 | + > = { |
| 40 | + task: "ai-chat", |
| 41 | + accessToken: "pk_test", |
| 42 | + stream: "chat-stream", |
| 43 | + payloadMapper: function payloadMapper(request: TriggerChatTransportRequest<UIMessage>) { |
| 44 | + const firstMessage = request.messages[0]; |
| 45 | + const firstPart = firstMessage?.parts[0]; |
| 46 | + const prompt = |
| 47 | + firstPart && firstPart.type === "text" |
| 48 | + ? firstPart.text |
| 49 | + : ""; |
| 50 | + |
| 51 | + return { |
| 52 | + prompt, |
| 53 | + chatId: request.chatId, |
| 54 | + source: request.request.headers?.["x-source"], |
| 55 | + }; |
| 56 | + }, |
| 57 | + onTriggeredRun: function onTriggeredRun(state: TriggerChatRunState) { |
| 58 | + expectTypeOf(state.chatId).toEqualTypeOf<string>(); |
| 59 | + expectTypeOf(state.publicAccessToken).toEqualTypeOf<string>(); |
| 60 | + }, |
| 61 | + }; |
| 62 | + |
| 63 | + expectTypeOf(options.payloadMapper).toBeFunction(); |
| 64 | +}); |
| 65 | + |
| 66 | +it("accepts typed stream definition objects", function () { |
| 67 | + const typedStream = { |
| 68 | + id: "chat-stream", |
| 69 | + pipe: async function pipe() { |
| 70 | + throw new Error("not used in type test"); |
| 71 | + }, |
| 72 | + } as unknown as RealtimeDefinedStream<InferUIMessageChunk<UIMessage>>; |
| 73 | + |
| 74 | + const transport = new TriggerChatTransport({ |
| 75 | + task: "ai-chat", |
| 76 | + accessToken: "pk_test", |
| 77 | + stream: typedStream, |
| 78 | + }); |
| 79 | + |
| 80 | + expectTypeOf(transport).toBeObject(); |
| 81 | +}); |
0 commit comments