Skip to content

Commit c94dc9f

Browse files
committed
tweaking tests
1 parent 45d2c25 commit c94dc9f

File tree

1 file changed

+81
-104
lines changed

1 file changed

+81
-104
lines changed
Lines changed: 81 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { assertEquals } from "@std/assert";
2+
import { describe, it } from "@std/testing/bdd";
23
import env from "@/env.ts";
3-
4-
// Import the functions for testing
54
import { cleanJsonResponse, configureJsonMode } from "./generateText.ts";
65

76
if (env.ENV !== "test") {
87
throw new Error("ENV must be 'test'");
98
}
109

11-
Deno.test("JSON mode configuration", async (t) => {
12-
// Test for Groq models
13-
await t.step("Configures JSON mode correctly for Groq models", () => {
10+
describe("configureJsonMode", () => {
11+
it("configures JSON mode correctly for Groq models", () => {
1412
const streamParams: Record<string, any> = {};
1513
const messages = [{
1614
role: "user" as const,
@@ -31,8 +29,7 @@ Deno.test("JSON mode configuration", async (t) => {
3129
);
3230
});
3331

34-
// Test for OpenAI models
35-
await t.step("Configures JSON mode correctly for OpenAI models", () => {
32+
it("configures JSON mode correctly for OpenAI models", () => {
3633
const streamParams: Record<string, any> = {};
3734
const messages = [{
3835
role: "user" as const,
@@ -48,8 +45,7 @@ Deno.test("JSON mode configuration", async (t) => {
4845
});
4946
});
5047

51-
// Test for Anthropic models
52-
await t.step("Configures JSON mode correctly for Anthropic models", () => {
48+
it("configures JSON mode correctly for Anthropic models", () => {
5349
const streamParams: Record<string, any> = {};
5450
const messages = [{
5551
role: "user" as const,
@@ -71,40 +67,35 @@ Deno.test("JSON mode configuration", async (t) => {
7167
assertEquals(streamParams.prefill?.text, "{\n");
7268
});
7369

74-
// Test with existing system prompt
75-
await t.step(
76-
"Preserves existing system prompt while adding JSON instructions",
77-
() => {
78-
const streamParams: Record<string, any> = {
79-
system: "You are an expert assistant.",
80-
};
81-
const messages = [{
82-
role: "user" as const,
83-
content: "Generate a JSON response",
84-
}];
85-
86-
configureJsonMode(
87-
streamParams,
88-
"anthropic:claude-3-7-sonnet",
89-
messages,
90-
false,
91-
);
92-
93-
assertEquals(
94-
streamParams.system.includes(
95-
"You are a JSON generation assistant. You are an expert assistant.",
96-
),
97-
true,
98-
);
99-
assertEquals(
100-
streamParams.system.includes("response must be ONLY valid JSON"),
101-
true,
102-
);
103-
},
104-
);
105-
106-
// Test for other providers
107-
await t.step("Configures JSON mode correctly for other providers", () => {
70+
it("preserves existing system prompt while adding JSON instructions", () => {
71+
const streamParams: Record<string, any> = {
72+
system: "You are an expert assistant.",
73+
};
74+
const messages = [{
75+
role: "user" as const,
76+
content: "Generate a JSON response",
77+
}];
78+
79+
configureJsonMode(
80+
streamParams,
81+
"anthropic:claude-3-7-sonnet",
82+
messages,
83+
false,
84+
);
85+
86+
assertEquals(
87+
streamParams.system.includes(
88+
"You are a JSON generation assistant. You are an expert assistant.",
89+
),
90+
true,
91+
);
92+
assertEquals(
93+
streamParams.system.includes("response must be ONLY valid JSON"),
94+
true,
95+
);
96+
});
97+
98+
it("configures JSON mode correctly for other providers", () => {
10899
const streamParams: Record<string, any> = {};
109100
const messages = [{
110101
role: "user" as const,
@@ -120,82 +111,68 @@ Deno.test("JSON mode configuration", async (t) => {
120111
);
121112
});
122113

123-
// Test for other providers with existing system prompt
124-
await t.step(
125-
"Adds JSON instructions to existing system prompt for other providers",
126-
() => {
127-
const streamParams: Record<string, any> = {
128-
system: "You are an expert assistant.",
129-
};
130-
const messages = [{
131-
role: "user" as const,
132-
content: "Generate a JSON response",
133-
}];
134-
135-
configureJsonMode(streamParams, "other:model", messages, false);
136-
137-
assertEquals(
138-
streamParams.system,
139-
"You are an expert assistant.\nEnsure the response is valid JSON. DO NOT include any other text or formatting.",
140-
);
141-
},
142-
);
143-
144-
// Test that JSON instructions are always added, even if the prompt already mentions JSON
145-
await t.step(
146-
"Always adds JSON instructions even when system prompt already mentions JSON",
147-
() => {
148-
const streamParams: Record<string, any> = {
149-
system: "You are an expert assistant who responds in JSON format.",
150-
};
151-
const messages = [{
152-
role: "user" as const,
153-
content: "Generate a JSON response",
154-
}];
155-
156-
configureJsonMode(streamParams, "other:model", messages, false);
157-
158-
// Should always add our JSON instructions
159-
assertEquals(
160-
streamParams.system,
161-
"You are an expert assistant who responds in JSON format.\nEnsure the response is valid JSON. DO NOT include any other text or formatting.",
162-
);
163-
},
164-
);
114+
it("adds JSON instructions to existing system prompt for other providers", () => {
115+
const streamParams: Record<string, any> = {
116+
system: "You are an expert assistant.",
117+
};
118+
const messages = [{
119+
role: "user" as const,
120+
content: "Generate a JSON response",
121+
}];
122+
123+
configureJsonMode(streamParams, "other:model", messages, false);
124+
125+
assertEquals(
126+
streamParams.system,
127+
"You are an expert assistant.\nEnsure the response is valid JSON. DO NOT include any other text or formatting.",
128+
);
129+
});
130+
131+
it("always adds JSON instructions even when system prompt already mentions JSON", () => {
132+
const streamParams: Record<string, any> = {
133+
system: "You are an expert assistant who responds in JSON format.",
134+
};
135+
const messages = [{
136+
role: "user" as const,
137+
content: "Generate a JSON response",
138+
}];
139+
140+
configureJsonMode(streamParams, "other:model", messages, false);
141+
142+
// Should always add our JSON instructions
143+
assertEquals(
144+
streamParams.system,
145+
"You are an expert assistant who responds in JSON format.\nEnsure the response is valid JSON. DO NOT include any other text or formatting.",
146+
);
147+
});
165148
});
166149

167-
Deno.test("cleanJsonResponse function", async (t) => {
168-
await t.step("Extracts JSON from markdown code blocks", () => {
150+
describe("cleanJsonResponse", () => {
151+
it("extracts JSON from markdown code blocks", () => {
169152
const input = '```json\n{"name": "Test", "value": 123}\n```';
170153
const expected = '{"name": "Test", "value": 123}';
171154
assertEquals(cleanJsonResponse(input), expected);
172155
});
173156

174-
await t.step(
175-
"Extracts JSON from code blocks without language specifier",
176-
() => {
177-
const input = '```\n{"name": "Test", "value": 123}\n```';
178-
const expected = '{"name": "Test", "value": 123}';
179-
assertEquals(cleanJsonResponse(input), expected);
180-
},
181-
);
157+
it("extracts JSON from code blocks without language specifier", () => {
158+
const input = '```\n{"name": "Test", "value": 123}\n```';
159+
const expected = '{"name": "Test", "value": 123}';
160+
assertEquals(cleanJsonResponse(input), expected);
161+
});
182162

183-
await t.step("Handles multiline JSON in code blocks", () => {
163+
it("handles multiline JSON in code blocks", () => {
184164
const input = '```json\n{\n "name": "Test",\n "value": 123\n}\n```';
185165
const expected = '{\n "name": "Test",\n "value": 123\n}';
186166
assertEquals(cleanJsonResponse(input), expected);
187167
});
188168

189-
await t.step("Returns original text if no code blocks found", () => {
169+
it("returns original text if no code blocks found", () => {
190170
const input = '{"name": "Test", "value": 123}';
191171
assertEquals(cleanJsonResponse(input), input);
192172
});
193173

194-
await t.step(
195-
"Returns original text if code block format is incorrect",
196-
() => {
197-
const input = '```json {"name": "Test", "value": 123}```';
198-
assertEquals(cleanJsonResponse(input), input);
199-
},
200-
);
174+
it("returns original text if code block format is incorrect", () => {
175+
const input = '```json {"name": "Test", "value": 123}```';
176+
assertEquals(cleanJsonResponse(input), input);
177+
});
201178
});

0 commit comments

Comments
 (0)