|
| 1 | +import { afterEach, describe, expect, expectTypeOf, it } from "vitest"; |
| 2 | +import { z } from "zod"; |
| 3 | +import type { CreateSessionRequestBody, CreatedSessionResponseBody } from "@trigger.dev/core/v3"; |
| 4 | + |
| 5 | +import { chat } from "./ai.js"; |
| 6 | +import { |
| 7 | + __setSessionStartImplForTests, |
| 8 | + __setSessionOpenImplForTests, |
| 9 | + SessionHandle, |
| 10 | +} from "./sessions.js"; |
| 11 | +import { apiClientManager } from "@trigger.dev/core/v3"; |
| 12 | + |
| 13 | +// `auth.createPublicToken` is called by the action when no start token is |
| 14 | +// supplied. Provide a minimal API client config so the mint path doesn't |
| 15 | +// throw before we get to assert the captured request body. |
| 16 | +apiClientManager.setGlobalAPIClientConfiguration({ |
| 17 | + baseURL: "https://example.invalid", |
| 18 | + accessToken: "tr_test_secret", |
| 19 | +}); |
| 20 | + |
| 21 | +// Capture the request body the action would send to `sessions.start()`. |
| 22 | +let lastStartBody: CreateSessionRequestBody | undefined; |
| 23 | + |
| 24 | +function installStartFixture() { |
| 25 | + __setSessionStartImplForTests(async (body): Promise<CreatedSessionResponseBody> => { |
| 26 | + lastStartBody = body; |
| 27 | + return { |
| 28 | + id: "session_fixture", |
| 29 | + externalId: body.externalId ?? null, |
| 30 | + type: body.type, |
| 31 | + taskIdentifier: body.taskIdentifier, |
| 32 | + triggerConfig: body.triggerConfig, |
| 33 | + currentRunId: "run_fixture", |
| 34 | + tags: body.triggerConfig.tags ?? [], |
| 35 | + metadata: body.metadata ?? null, |
| 36 | + closedAt: null, |
| 37 | + closedReason: null, |
| 38 | + expiresAt: null, |
| 39 | + createdAt: new Date(), |
| 40 | + updatedAt: new Date(), |
| 41 | + runId: "run_fixture", |
| 42 | + publicAccessToken: "tr_pat_fixture", |
| 43 | + isCached: false, |
| 44 | + }; |
| 45 | + }); |
| 46 | + __setSessionOpenImplForTests(() => new SessionHandle("session_fixture")); |
| 47 | +} |
| 48 | + |
| 49 | +afterEach(() => { |
| 50 | + __setSessionStartImplForTests(undefined); |
| 51 | + __setSessionOpenImplForTests(undefined); |
| 52 | + lastStartBody = undefined; |
| 53 | +}); |
| 54 | + |
| 55 | +// Build a fake chat agent task shape that the generic can narrow against. |
| 56 | +// We only need the static type — the runtime never invokes this task because |
| 57 | +// `__setSessionStartImplForTests` intercepts the network call. |
| 58 | +const fakeChat = chat |
| 59 | + .withClientData({ |
| 60 | + schema: z.object({ |
| 61 | + userId: z.string(), |
| 62 | + plan: z.enum(["free", "pro"]), |
| 63 | + }), |
| 64 | + }) |
| 65 | + .agent({ |
| 66 | + id: "fake-chat", |
| 67 | + run: async () => undefined as any, |
| 68 | + }); |
| 69 | + |
| 70 | +describe("chat.createStartSessionAction — runtime", () => { |
| 71 | + it("folds typed clientData into basePayload.metadata so onChatStart sees it on the first turn", async () => { |
| 72 | + installStartFixture(); |
| 73 | + |
| 74 | + const start = chat.createStartSessionAction<typeof fakeChat>("fake-chat"); |
| 75 | + |
| 76 | + const result = await start({ |
| 77 | + chatId: "chat-1", |
| 78 | + clientData: { userId: "u-1", plan: "pro" }, |
| 79 | + }); |
| 80 | + |
| 81 | + expect(result.publicAccessToken).toBe("tr_pat_fixture"); |
| 82 | + expect(lastStartBody?.triggerConfig.basePayload).toMatchObject({ |
| 83 | + messages: [], |
| 84 | + trigger: "preload", |
| 85 | + metadata: { userId: "u-1", plan: "pro" }, |
| 86 | + chatId: "chat-1", |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it("leaves basePayload.metadata unset when clientData is not provided", async () => { |
| 91 | + installStartFixture(); |
| 92 | + |
| 93 | + const start = chat.createStartSessionAction("fake-chat"); |
| 94 | + await start({ chatId: "chat-2" }); |
| 95 | + |
| 96 | + expect(lastStartBody?.triggerConfig.basePayload).not.toHaveProperty("metadata"); |
| 97 | + }); |
| 98 | + |
| 99 | + it("keeps session-level metadata distinct from per-turn clientData", async () => { |
| 100 | + installStartFixture(); |
| 101 | + |
| 102 | + const start = chat.createStartSessionAction<typeof fakeChat>("fake-chat"); |
| 103 | + await start({ |
| 104 | + chatId: "chat-3", |
| 105 | + clientData: { userId: "u-3", plan: "free" }, |
| 106 | + metadata: { source: "marketing-site" }, |
| 107 | + }); |
| 108 | + |
| 109 | + // Per-turn shape (visible to onPreload / onChatStart): |
| 110 | + expect(lastStartBody?.triggerConfig.basePayload).toMatchObject({ |
| 111 | + metadata: { userId: "u-3", plan: "free" }, |
| 112 | + }); |
| 113 | + // Session-row metadata (opaque, never typed via clientDataSchema): |
| 114 | + expect(lastStartBody?.metadata).toEqual({ source: "marketing-site" }); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe("chat.createStartSessionAction — types", () => { |
| 119 | + it("narrows clientData against the chat agent's clientDataSchema", () => { |
| 120 | + const start = chat.createStartSessionAction<typeof fakeChat>("fake-chat"); |
| 121 | + |
| 122 | + // The clientData field is typed off the agent's schema. |
| 123 | + expectTypeOf<Parameters<typeof start>[0]["clientData"]>().toEqualTypeOf< |
| 124 | + { userId: string; plan: "free" | "pro" } | undefined |
| 125 | + >(); |
| 126 | + // The agent's typed clientData is strictly narrower than `unknown`. |
| 127 | + expectTypeOf<Parameters<typeof start>[0]["clientData"]>().not.toEqualTypeOf<unknown>(); |
| 128 | + }); |
| 129 | + |
| 130 | + it("defaults clientData to unknown when called without a generic", () => { |
| 131 | + const start = chat.createStartSessionAction("fake-chat"); |
| 132 | + expectTypeOf(start).parameter(0).toHaveProperty("clientData"); |
| 133 | + // Untyped variant — clientData is `unknown`. |
| 134 | + expectTypeOf<Parameters<typeof start>[0]["clientData"]>().toEqualTypeOf<unknown>(); |
| 135 | + }); |
| 136 | +}); |
0 commit comments