-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathai.test.ts
More file actions
79 lines (71 loc) · 2.04 KB
/
ai.test.ts
File metadata and controls
79 lines (71 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { ai } from "./ai.js";
import type { TaskWithSchema } from "@trigger.dev/core/v3";
describe("ai helper", function () {
it("creates a tool from a schema task and executes through triggerAndWait", async function () {
let receivedInput: unknown = undefined;
const fakeTask = {
id: "fake-task",
description: "A fake task",
schema: z.object({
name: z.string(),
}),
triggerAndWait: function (payload: { name: string }) {
receivedInput = payload;
const resultPromise = Promise.resolve({
ok: true,
id: "run_123",
taskIdentifier: "fake-task",
output: {
greeting: `Hello ${payload.name}`,
},
});
return Object.assign(resultPromise, {
unwrap: async function () {
return {
greeting: `Hello ${payload.name}`,
};
},
});
},
} as unknown as TaskWithSchema<
"fake-task",
z.ZodObject<{ name: z.ZodString }>,
{ greeting: string }
>;
const tool = ai.tool(fakeTask);
const result = await tool.execute?.(
{
name: "Ada",
},
undefined as never
);
expect(receivedInput).toEqual({
name: "Ada",
});
expect(result).toEqual({
greeting: "Hello Ada",
});
});
it("throws when creating a tool from a task without schema", function () {
const fakeTask = {
id: "no-schema",
description: "No schema task",
schema: undefined,
triggerAndWait: async function () {
return {
unwrap: async function () {
return {};
},
};
},
} as unknown as TaskWithSchema<"no-schema", undefined, unknown>;
expect(function () {
ai.tool(fakeTask);
}).toThrowError("task has no schema");
});
it("returns undefined for current tool options outside task execution context", function () {
expect(ai.currentToolOptions()).toBeUndefined();
});
});