Repository navigation
Expand file tree
/
Copy pathframework2.test.ts
More file actions
159 lines (148 loc) · 4.58 KB
/
Copy pathframework2.test.ts
File metadata and controls
159 lines (148 loc) · 4.58 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { afterEach, beforeAll, expect, expectTypeOf, test, vi } from "vitest";
import * as z3 from "zod/v3";
import * as z4 from "zod/v4";
import {
CodeFunction,
ProjectNameIdMap,
projects,
serializeEvalParametersToStaticParametersSchema,
} from "./framework2";
import { _internalGetGlobalState } from "./logger";
import { configureNode } from "./node/config";
import { zodToJsonSchema } from "./zod/utils";
beforeAll(() => {
configureNode();
});
afterEach(() => {
vi.restoreAllMocks();
});
test("decision scorers preserve questions and classifier settings", () => {
const project = projects.create({ name: "test-decision-scorers" });
const addPrompt = vi.spyOn(project, "addPrompt");
const questions = [
{ type: "predicate" as const, instructions: "Is {{output}} correct?" },
];
project.scorers.create({
name: "correctness",
model: "decision-model",
input: "{{input}}",
questions,
params: { temperature: 0 },
useCot: false,
choiceScores: { true: 1, false: 0 },
templateFormat: "none",
});
expect(addPrompt.mock.calls[0][0].prompt).toEqual({
prompt: { type: "decisions", input: "{{input}}", questions },
options: { model: "decision-model", params: { temperature: 0 } },
parser: {
type: "llm_classifier",
use_cot: false,
choice_scores: { true: 1, false: 0 },
},
template_format: "none",
});
});
function mockProjectRegister() {
const postJson = vi.fn(async () => ({
project: {
id: "00000000-0000-0000-0000-000000000001",
org_id: "00000000-0000-0000-0000-000000000002",
name: "project",
},
}));
vi.spyOn(_internalGetGlobalState(), "appConn").mockReturnValue({
post_json: postJson,
} as never);
return postJson;
}
test("project resolution creates the project in its project group", async () => {
const postJson = mockProjectRegister();
const project = projects.create({
name: "project",
projectGroupName: "my-group",
});
expect(await new ProjectNameIdMap().resolve(project)).toBe(
"00000000-0000-0000-0000-000000000001",
);
expect(postJson).toHaveBeenCalledWith("api/project/register", {
project_name: "project",
project_group_name: "my-group",
});
});
test("project resolution omits project_group_name when unspecified", async () => {
const postJson = mockProjectRegister();
await new ProjectNameIdMap().resolve(projects.create({ name: "project" }));
expect(postJson).toHaveBeenCalledWith("api/project/register", {
project_name: "project",
});
});
test.each([
{
version: "v3",
parameters: z3.object({ text: z3.string() }),
returns: z3.number(),
},
{
version: "v4",
parameters: z4.object({ text: z4.string() }),
returns: z4.number(),
},
{
version: "mixed",
parameters: z3.object({ text: z3.string() }),
returns: z4.number(),
},
])("code functions accept $version schemas", ({ parameters, returns }) => {
const project = projects.create({ name: "test-zod-functions" });
const fn = new CodeFunction(project, {
name: "length",
slug: "length",
type: "tool",
parameters,
returns,
handler: ({ text }) => {
expectTypeOf(text).toEqualTypeOf<string>();
return text.length;
},
});
expectTypeOf(fn.parameters!.parse({ text: "hello" })).toEqualTypeOf<{
text: string;
}>();
expectTypeOf(fn.returns!.parse(5)).toEqualTypeOf<number>();
expect(fn.handler({ text: "hello" })).toBe(5);
expect(zodToJsonSchema(fn.parameters!)).toMatchObject({
type: "object",
properties: { text: { type: "string" } },
required: ["text"],
});
expect(zodToJsonSchema(fn.returns!)).toMatchObject({ type: "number" });
const tool = project.tools.create({
name: "length-tool",
parameters,
returns,
handler: ({ text }) => {
expectTypeOf(text).toEqualTypeOf<string>();
return text.length;
},
});
expectTypeOf(tool.handler({ text: "hello" })).toEqualTypeOf<number>();
expect(tool.handler({ text: "hello" })).toBe(5);
});
test("parameter builders serialize mixed Zod v3 and v4 schemas", () => {
const project = projects.create({ name: "test-zod-parameters" });
const schema = project.parameters.create({
name: "mixed",
schema: {
prefix: z3.string().default("start:"),
suffix: z4.string().default(":end"),
},
});
expectTypeOf(schema.suffix).toEqualTypeOf<z4.ZodDefault<z4.ZodString>>();
expect(serializeEvalParametersToStaticParametersSchema(schema)).toMatchObject(
{
prefix: { type: "data", schema: { type: "string" }, default: "start:" },
suffix: { type: "data", schema: { type: "string" }, default: ":end" },
},
);
});