Repository navigation
Expand file tree
/
Copy pathopencodereview.test.mjs
More file actions
103 lines (93 loc) · 3.65 KB
/
Copy pathopencodereview.test.mjs
File metadata and controls
103 lines (93 loc) · 3.65 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
import { expect, test } from "bun:test"
import { agent, connect } from "./opencodereview.agent.js"
const gateway = { url: "http://127.0.0.1:3425", v1: "http://127.0.0.1:3425/v1", key: "magpie-opencodereview" }
const model = { id: "zai/glm-5.3", name: "GLM-5.3" }
// Apply key-paths the way magpie edits a JSON config: dotted paths address
// nested keys, missing intermediates are created, other keys are preserved.
function apply(config, entries) {
const out = structuredClone(config)
for (const [path, value] of Object.entries(entries)) {
const parts = path.split(".")
let node = out
for (const part of parts.slice(0, -1)) {
if (typeof node[part] !== "object" || node[part] === null) node[part] = {}
node = node[part]
}
node[parts[parts.length - 1]] = value
}
return out
}
test("says where ocr keeps its model", () => {
expect(agent).toMatchObject({
id: "opencodereview",
bin: "ocr",
dir: "~/.opencodereview",
config: "~/.opencodereview/config.json",
format: "json",
model: "model",
})
// ocr sends magpie's model id as is
expect(agent.prefix).toBeUndefined()
})
test("shows ocr's own icon, as the package gives it", async () => {
const pkg = await Bun.file(new URL("./package.json", import.meta.url)).json()
expect(agent.icon).toBe("https://raw.githubusercontent.com/alibaba/open-code-review/main/imgs/logo.svg")
expect(pkg.magpie.icon).toBe(agent.icon)
})
test("matches ocr's User-Agent, which magpie reads as lowercase prefixes", () => {
const real = "open-code-review/v1.13.2 | claude"
expect(agent.ua.length).toBeGreaterThan(0)
for (const entry of agent.ua) {
expect(entry).toBe(entry.toLowerCase())
expect(real.startsWith(entry)).toBe(true)
}
expect(agent.ua).toContain("open-code-review")
})
test("tells the user when a new model takes effect, in English and Chinese", () => {
expect(agent.notice).toContain("the next ocr review / ocr scan uses the new model")
expect(agent.notice).toContain("下一次 ocr review / ocr scan 即使用新模型")
})
test("points ocr at magpie's anthropic endpoint with its own key", () => {
const out = connect({ gateway, model })
expect(out).toEqual({
provider: "magpie",
"custom_providers.magpie": {
url: "http://127.0.0.1:3425",
protocol: "anthropic",
api_key: "magpie-opencodereview",
},
})
// no /v1 suffix: ocr's anthropic protocol appends /v1/messages itself
expect(out["custom_providers.magpie"].url).not.toContain("/v1")
// one unit, as pig writes its provider: no empty magpie provider is left
// behind when disconnect takes the key out
expect(Object.keys(out)).toEqual(["provider", "custom_providers.magpie"])
})
test("yields a config ocr accepts, leaving other providers alone", () => {
const existing = {
provider: "local",
model: "zai/glm-5.3",
custom_providers: {
local: { url: "http://127.0.0.1:3425", protocol: "anthropic", api_key: "x" },
},
llm: {},
}
// magpie writes the model key itself, connect's keys alongside it
const out = apply(apply(existing, { model: model.id }), connect({ gateway, model }))
expect(out).toEqual({
provider: "magpie",
model: "zai/glm-5.3",
custom_providers: {
local: { url: "http://127.0.0.1:3425", protocol: "anthropic", api_key: "x" },
magpie: { url: gateway.url, protocol: "anthropic", api_key: gateway.key },
},
llm: {},
})
})
test("creates custom_providers on a fresh config", () => {
const out = apply({}, connect({ gateway, model }))
expect(out.custom_providers.magpie.url).toBe(gateway.url)
})
test("does not depend on receiving a model", () => {
expect(connect({ gateway }).provider).toBe("magpie")
})