Skip to content

Commit 618389c

Browse files
committed
test: add unit tests for anomalyco#11413, anomalyco#11130, anomalyco#11436 fixes
- test(transform): add tests for Gemini schema sanitization removing ref and $schema fields - test(config): add tests for duplicate key detection in config files - test(file): add tests for FileTime.assert 100ms tolerance
1 parent c2bd5c9 commit 618389c

3 files changed

Lines changed: 189 additions & 0 deletions

File tree

‎packages/opencode/test/config/config.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,61 @@ test("throws error for invalid JSON", async () => {
225225
})
226226
})
227227

228+
test("throws error for duplicate keys in config (#11130)", async () => {
229+
await using tmp = await tmpdir({
230+
init: async (dir) => {
231+
await Bun.write(
232+
path.join(dir, "opencode.json"),
233+
`{
234+
"plugin": "opencode-gemini-auth@latest",
235+
"plugin": "@franlol/[email protected]"
236+
}`,
237+
)
238+
},
239+
})
240+
await Instance.provide({
241+
directory: tmp.path,
242+
fn: async () => {
243+
try {
244+
await Config.get()
245+
expect(true).toBe(false)
246+
} catch (e: any) {
247+
expect(e.data?.message || e.message).toMatch(/Duplicate key/)
248+
}
249+
},
250+
})
251+
})
252+
253+
test("throws error for nested duplicate keys (#11130)", async () => {
254+
await using tmp = await tmpdir({
255+
init: async (dir) => {
256+
await Bun.write(
257+
path.join(dir, "opencode.json"),
258+
`{
259+
"$schema": "https://opencode.ai/config.json",
260+
"agent": {
261+
"test": {
262+
"model": "model1",
263+
"model": "model2"
264+
}
265+
}
266+
}`,
267+
)
268+
},
269+
})
270+
await Instance.provide({
271+
directory: tmp.path,
272+
fn: async () => {
273+
try {
274+
await Config.get()
275+
expect(true).toBe(false)
276+
} catch (e: any) {
277+
expect(e.data?.message || e.message).toMatch(/Duplicate key.*model/)
278+
}
279+
},
280+
})
281+
})
282+
228283
test("handles agent configuration", async () => {
229284
await using tmp = await tmpdir({
230285
init: async (dir) => {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { describe, expect, test, beforeEach } from "bun:test"
2+
import { FileTime } from "../../src/file/time"
3+
import { Instance } from "../../src/project/instance"
4+
import { tmpdir } from "../fixture/fixture"
5+
import path from "path"
6+
7+
describe("FileTime.assert - tolerance (#11436)", () => {
8+
test("allows file with mtime within 100ms tolerance", async () => {
9+
await using tmp = await tmpdir()
10+
await Instance.provide({
11+
directory: tmp.path,
12+
fn: async () => {
13+
const file = path.join(tmp.path, "test.txt")
14+
await Bun.write(file, "content")
15+
16+
FileTime.read("test-session", file)
17+
18+
// Should not throw - mtime is within tolerance
19+
await expect(FileTime.assert("test-session", file)).resolves.toBeUndefined()
20+
},
21+
})
22+
})
23+
24+
test("throws when file is modified after read", async () => {
25+
await using tmp = await tmpdir()
26+
await Instance.provide({
27+
directory: tmp.path,
28+
fn: async () => {
29+
const file = path.join(tmp.path, "test.txt")
30+
await Bun.write(file, "content")
31+
32+
FileTime.read("test-session", file)
33+
34+
// Wait and modify file
35+
await new Promise((r) => setTimeout(r, 150))
36+
await Bun.write(file, "modified content")
37+
38+
await expect(FileTime.assert("test-session", file)).rejects.toThrow(/has been modified/)
39+
},
40+
})
41+
})
42+
43+
test("throws when file was never read", async () => {
44+
await using tmp = await tmpdir()
45+
await Instance.provide({
46+
directory: tmp.path,
47+
fn: async () => {
48+
const file = path.join(tmp.path, "test.txt")
49+
await Bun.write(file, "content")
50+
51+
await expect(FileTime.assert("test-session", file)).rejects.toThrow(/must read file/)
52+
},
53+
})
54+
})
55+
})

‎packages/opencode/test/provider/transform.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,85 @@ describe("ProviderTransform.schema - gemini array items", () => {
363363
})
364364
})
365365

366+
describe("ProviderTransform.schema - gemini removes non-standard fields (#11413)", () => {
367+
const geminiModel = {
368+
providerID: "google",
369+
api: { id: "gemini-3-pro" },
370+
} as any
371+
372+
test("removes ref field from schema", () => {
373+
const schema = {
374+
ref: "QuestionInfo",
375+
type: "object",
376+
properties: {
377+
question: { type: "string" },
378+
},
379+
} as any
380+
381+
const result = ProviderTransform.schema(geminiModel, schema) as any
382+
383+
expect(result.ref).toBeUndefined()
384+
expect(result.type).toBe("object")
385+
expect(result.properties.question.type).toBe("string")
386+
})
387+
388+
test("removes $schema field from schema", () => {
389+
const schema = {
390+
$schema: "https://json-schema.org/draft/2020-12/schema",
391+
type: "object",
392+
properties: {
393+
name: { type: "string" },
394+
},
395+
} as any
396+
397+
const result = ProviderTransform.schema(geminiModel, schema) as any
398+
399+
expect(result.$schema).toBeUndefined()
400+
expect(result.type).toBe("object")
401+
})
402+
403+
test("removes ref from nested objects", () => {
404+
const schema = {
405+
ref: "QuestionInfo",
406+
type: "object",
407+
properties: {
408+
options: {
409+
type: "array",
410+
items: {
411+
ref: "QuestionOption",
412+
type: "object",
413+
properties: {
414+
label: { type: "string" },
415+
},
416+
},
417+
},
418+
},
419+
} as any
420+
421+
const result = ProviderTransform.schema(geminiModel, schema) as any
422+
423+
expect(result.ref).toBeUndefined()
424+
expect(result.properties.options.items.ref).toBeUndefined()
425+
expect(result.properties.options.items.type).toBe("object")
426+
})
427+
428+
test("does not remove ref for non-gemini providers", () => {
429+
const openaiModel = {
430+
providerID: "openai",
431+
api: { id: "gpt-4" },
432+
} as any
433+
434+
const schema = {
435+
ref: "QuestionInfo",
436+
type: "object",
437+
} as any
438+
439+
const result = ProviderTransform.schema(openaiModel, schema) as any
440+
441+
expect(result.ref).toBe("QuestionInfo")
442+
})
443+
})
444+
366445
describe("ProviderTransform.message - DeepSeek reasoning content", () => {
367446
test("DeepSeek with tool calls includes reasoning_content in providerOptions", () => {
368447
const msgs = [

0 commit comments

Comments
 (0)