|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { ai } from "./ai.js"; |
| 4 | +import type { TaskWithSchema } from "@trigger.dev/core/v3"; |
| 5 | + |
| 6 | +describe("@trigger.dev/sdk/ai compatibility", function () { |
| 7 | + it("creates a tool from a schema task and executes triggerAndWait", async function () { |
| 8 | + let receivedInput: unknown = undefined; |
| 9 | + |
| 10 | + const fakeTask = { |
| 11 | + id: "fake-task", |
| 12 | + description: "A fake task", |
| 13 | + schema: z.object({ |
| 14 | + name: z.string(), |
| 15 | + }), |
| 16 | + triggerAndWait: function (payload: { name: string }) { |
| 17 | + receivedInput = payload; |
| 18 | + const resultPromise = Promise.resolve({ |
| 19 | + ok: true, |
| 20 | + id: "run_123", |
| 21 | + taskIdentifier: "fake-task", |
| 22 | + output: { |
| 23 | + greeting: `Hello ${payload.name}`, |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + return Object.assign(resultPromise, { |
| 28 | + unwrap: async function () { |
| 29 | + return { |
| 30 | + greeting: `Hello ${payload.name}`, |
| 31 | + }; |
| 32 | + }, |
| 33 | + }); |
| 34 | + }, |
| 35 | + } as unknown as TaskWithSchema< |
| 36 | + "fake-task", |
| 37 | + z.ZodObject<{ name: z.ZodString }>, |
| 38 | + { greeting: string } |
| 39 | + >; |
| 40 | + |
| 41 | + const tool = ai.tool(fakeTask); |
| 42 | + const result = await tool.execute?.( |
| 43 | + { |
| 44 | + name: "Ada", |
| 45 | + }, |
| 46 | + undefined as never |
| 47 | + ); |
| 48 | + |
| 49 | + expect(receivedInput).toEqual({ |
| 50 | + name: "Ada", |
| 51 | + }); |
| 52 | + expect(result).toEqual({ |
| 53 | + greeting: "Hello Ada", |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("throws when converting tasks without schema", function () { |
| 58 | + const fakeTask = { |
| 59 | + id: "no-schema", |
| 60 | + description: "No schema task", |
| 61 | + schema: undefined, |
| 62 | + triggerAndWait: async function () { |
| 63 | + return { |
| 64 | + unwrap: async function () { |
| 65 | + return {}; |
| 66 | + }, |
| 67 | + }; |
| 68 | + }, |
| 69 | + } as unknown as TaskWithSchema<"no-schema", undefined, unknown>; |
| 70 | + |
| 71 | + expect(function () { |
| 72 | + ai.tool(fakeTask); |
| 73 | + }).toThrowError("task has no schema"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("preserves currentToolOptions behavior outside task execution", function () { |
| 77 | + expect(function () { |
| 78 | + ai.currentToolOptions(); |
| 79 | + }).toThrowError("Method not implemented."); |
| 80 | + }); |
| 81 | +}); |
0 commit comments