-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
355 lines (311 loc) · 11.9 KB
/
Copy pathtest.js
File metadata and controls
355 lines (311 loc) · 11.9 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env node
// Tests for OpenCode OpenAI Proxy
// Run: node test.js
const http = require("http");
const assert = require("assert");
const fs = require("fs");
const path = require("path");
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(` ✅ ${name}`);
passed++;
} catch (e) {
console.log(` ❌ ${name}`);
console.log(` ${e.message}`);
failed++;
}
}
function asyncTest(name, fn) {
return fn().then(() => {
console.log(` ✅ ${name}`);
passed++;
}).catch(e => {
console.log(` ❌ ${name}`);
console.log(` ${e.message}`);
failed++;
});
}
// ── Unit tests for buildMap (mirrors index.js logic) ──
console.log("\n📦 Model mapping tests:");
function readConfig(file) {
return JSON.parse(fs.readFileSync(path.join(__dirname, file), "utf8"));
}
test("tier configs declare model inventory without inline aliases", () => {
for (const file of ["models-go.json", "models-zen.json"]) {
const cfg = readConfig(file);
assert.ok(Array.isArray(cfg.models), `${file} should have models array`);
assert.ok(cfg.models.length > 0, `${file} should list at least one model`);
assert.strictEqual(cfg.routing, undefined, `${file} should not have routing aliases`);
}
});
test("OpenAI route configs cover tier models without duplicate targets", () => {
for (const [modelsFile, routesFile] of [
["models-go.json", "routes-openai-go.json"],
["models-zen.json", "routes-openai-zen.json"],
]) {
const modelsCfg = readConfig(modelsFile);
const routes = readConfig(routesFile);
const models = new Set(modelsCfg.models);
const routeTargets = Object.values(routes);
assert.ok(routeTargets.length > 0, `${routesFile} should have routes`);
assert.deepStrictEqual(new Set(routeTargets), models, `${routesFile} should cover every tier model`);
assert.strictEqual(routeTargets.length, models.size, `${routesFile} should not repeat targets`);
for (const [source, target] of Object.entries(routes)) {
assert.ok(source.startsWith("gpt-") || source.startsWith("o"), `${source} should be a real OpenAI-style model id`);
assert.ok(models.has(target), `${routesFile} should route ${source} to a declared tier model`);
}
}
});
function buildMap(models = [], routes = {}) {
const map = {};
for (const model of models) {
map[model.toLowerCase()] = model;
}
for (const model of models) {
const base = model.toLowerCase();
for (const suffix of ["[1m]", "[8k]", "[200k]", "[1]"]) {
map[base + suffix] = model;
}
}
for (const [source, target] of Object.entries(routes)) {
const base = source.toLowerCase();
map[base] = target;
for (const suffix of ["[1m]", "[8k]", "[200k]", "[1]"]) {
map[base + suffix] = target;
}
}
return map;
}
const testModels = ["deepseek-v4-pro", "qwen3.6-plus"];
const testRoutes = { "gpt-5.2": "deepseek-v4-pro", "gpt-4.1": "qwen3.6-plus" };
const testMap = buildMap(testModels, testRoutes);
test("direct model names map from model inventory", () => {
assert.strictEqual(testMap["deepseek-v4-pro"], "deepseek-v4-pro");
assert.strictEqual(testMap["qwen3.6-plus"], "qwen3.6-plus");
});
test("OpenAI route names map from route config", () => {
assert.strictEqual(testMap["gpt-5.2"], "deepseek-v4-pro");
assert.strictEqual(testMap["gpt-4.1"], "qwen3.6-plus");
});
test("context suffix maps correctly", () => {
assert.strictEqual(testMap["deepseek-v4-pro[1m]"], "deepseek-v4-pro");
assert.strictEqual(testMap["qwen3.6-plus[8k]"], "qwen3.6-plus");
assert.strictEqual(testMap["deepseek-v4-pro[200k]"], "deepseek-v4-pro");
assert.strictEqual(testMap["qwen3.6-plus[1]"], "qwen3.6-plus");
assert.strictEqual(testMap["gpt-5.2[200k]"], "deepseek-v4-pro");
});
test("case-insensitive", () => {
assert.strictEqual(testMap["GPT-4O"], undefined); // map keys are lowercased at build time
assert.strictEqual(testMap["Gpt-4o"], undefined);
});
// cleanModel function (mirrors index.js)
function cleanModel(name, map) {
name = name.replace(/\[.*?\]$/, "");
const m = map[name.toLowerCase()];
return m || name;
}
test("cleanModel strips suffix and maps", () => {
assert.strictEqual(cleanModel("deepseek-v4-pro[200k]", testMap), "deepseek-v4-pro");
assert.strictEqual(cleanModel("QWEN3.6-PLUS[8k]", testMap), "qwen3.6-plus");
});
test("cleanModel returns original if not mapped", () => {
assert.strictEqual(cleanModel("unknown-model", testMap), "unknown-model");
assert.strictEqual(cleanModel("deepseek-v4-pro", testMap), "deepseek-v4-pro");
});
test("cleanModel is case-insensitive", () => {
assert.strictEqual(cleanModel("DeepSeek-V4-Pro", testMap), "deepseek-v4-pro");
assert.strictEqual(cleanModel("Qwen3.6-Plus", testMap), "qwen3.6-plus");
});
test("cleanModel strips context suffix before lookup", () => {
assert.strictEqual(cleanModel("deepseek-v4-pro[1m]", testMap), "deepseek-v4-pro");
assert.strictEqual(cleanModel("qwen3.6-plus[200k]", testMap), "qwen3.6-plus");
});
// ── HTTP integration tests ──
console.log("\n🌐 HTTP endpoint tests:");
const PORT = 11499;
async function startProxy({ tier = "go", port = PORT } = {}) {
return new Promise((resolve) => {
const child = require("child_process").spawn("node", ["index.js", String(port)], {
cwd: __dirname,
env: { ...process.env, OPENCODE_API_KEY: "test-key", OPENCODE_TIER: tier },
});
child.stderr.on("data", () => {});
child.stdout.on("data", () => {});
setTimeout(() => resolve(child), 500);
});
}
function httpGet(path, port = PORT) {
return new Promise((resolve, reject) => {
http.get(`http://127.0.0.1:${port}${path}`, res => {
let data = "";
res.on("data", c => data += c);
res.on("end", () => resolve({ status: res.statusCode, body: data }));
}).on("error", reject);
});
}
function httpPost(path, body, port = PORT) {
return new Promise((resolve, reject) => {
const postData = JSON.stringify(body);
const req = http.request(`http://127.0.0.1:${port}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json", "Content-Length": Buffer.byteLength(postData) },
}, res => {
let data = "";
res.on("data", c => data += c);
res.on("end", () => resolve({ status: res.statusCode, body: data }));
});
req.on("error", reject);
req.write(postData);
req.end();
});
}
async function runHttpTests() {
let child;
try {
child = await startProxy();
} catch (e) {
console.log(" ⚠️ Skipping HTTP tests — could not start proxy");
return;
}
await asyncTest("GET /health returns 200", async () => {
const res = await httpGet("/health");
assert.strictEqual(res.status, 200);
const json = JSON.parse(res.body);
assert.strictEqual(json.status, "ok");
assert.strictEqual(json.tier, "go");
});
await asyncTest("GET /v1/models returns model list", async () => {
const res = await httpGet("/v1/models");
assert.strictEqual(res.status, 200);
const json = JSON.parse(res.body);
assert.strictEqual(json.object, "list");
assert.ok(json.data.length > 0);
assert.ok(json.data.some(m => m.id === "glm-5"));
assert.ok(json.data.some(m => m.id === "deepseek-v4-pro"));
assert.ok(json.data.some(m => m.id === "gpt-5.2"));
const ids = json.data.map(m => m.id);
assert.ok(!ids.includes("deepseek"), "Should not expose invented aliases");
assert.ok(!ids.includes("qwen"), "Should not expose invented aliases");
});
await asyncTest("GET /v1/models uses Zen config when OPENCODE_TIER=zen", async () => {
const zenPort = PORT + 1;
const zenChild = await startProxy({ tier: "zen", port: zenPort });
try {
const res = await httpGet("/v1/models", zenPort);
assert.strictEqual(res.status, 200);
const json = JSON.parse(res.body);
const ids = json.data.map(m => m.id);
assert.ok(ids.includes("gpt-5.5"));
assert.ok(ids.includes("gpt-4o"));
assert.ok(ids.includes("claude-opus-4-7"));
assert.ok(ids.includes("gemini-3.1-pro"));
} finally {
zenChild.kill();
}
});
await asyncTest("POST /v1/chat/completions accepts valid request", async () => {
const res = await httpPost("/v1/chat/completions", {
model: "glm-5",
messages: [{ role: "user", content: "hello" }],
});
// Will fail upstream (no real API key), but proxy should process it
assert.ok([401, 502, 200].includes(res.status), `Unexpected status: ${res.status}`);
});
await asyncTest("POST /v1/chat/completions with direct model name", async () => {
const res = await httpPost("/v1/chat/completions", {
model: "deepseek-v4-pro",
messages: [{ role: "user", content: "hello" }],
});
assert.ok(res.status !== 404, "Endpoint should exist");
});
await asyncTest("POST /v1/chat/completions with OpenAI route model name", async () => {
const res = await httpPost("/v1/chat/completions", {
model: "gpt-5.2",
messages: [{ role: "user", content: "hello" }],
});
assert.ok(res.status !== 404, "Endpoint should exist");
});
await asyncTest("POST /v1/chat/completions accepts missing model", async () => {
const res = await httpPost("/v1/chat/completions", {
messages: [{ role: "user", content: "hello" }],
});
assert.ok(res.status !== 404, "Endpoint should exist");
});
await asyncTest("POST to wrong path returns 404", async () => {
const res = await httpPost("/v1/messages", { messages: [] });
assert.strictEqual(res.status, 404);
});
await asyncTest("GET to chat endpoint returns 404", async () => {
const res = await httpGet("/v1/chat/completions");
assert.strictEqual(res.status, 404);
});
await asyncTest("HEAD / returns 200", async () => {
return new Promise((resolve, reject) => {
const req = http.request(`http://127.0.0.1:${PORT}/`, { method: "HEAD" }, res => {
assert.strictEqual(res.statusCode, 200);
resolve();
});
req.on("error", reject);
req.end();
});
});
await asyncTest("HEAD /v1 returns 200", async () => {
return new Promise((resolve, reject) => {
const req = http.request(`http://127.0.0.1:${PORT}/v1`, { method: "HEAD" }, res => {
assert.strictEqual(res.statusCode, 200);
resolve();
});
req.on("error", reject);
req.end();
});
});
await asyncTest("POST /v1/chat/completions with streaming flag", async () => {
const res = await httpPost("/v1/chat/completions", {
model: "glm-5",
messages: [{ role: "user", content: "hello" }],
stream: true,
});
// Upstream will fail without real key, but endpoint should accept
assert.ok(res.status !== 404, "Streaming endpoint should exist");
});
await asyncTest("POST /v1/chat/completions with system message", async () => {
const res = await httpPost("/v1/chat/completions", {
model: "glm-5",
messages: [
{ role: "system", content: "You are a test assistant." },
{ role: "user", content: "hello" },
],
});
assert.ok([401, 502, 200].includes(res.status), `Unexpected status: ${res.status}`);
});
await asyncTest("POST /v1/chat/completions with invalid JSON returns 400", async () => {
return new Promise((resolve, reject) => {
const req = http.request(`http://127.0.0.1:${PORT}/v1/chat/completions`, {
method: "POST",
headers: { "Content-Type": "application/json" },
}, res => {
let data = "";
res.on("data", c => data += c);
res.on("end", () => {
assert.strictEqual(res.statusCode, 400);
resolve();
});
});
req.on("error", reject);
req.write("not json{{{");
req.end();
});
});
// Cleanup
child.kill();
}
runHttpTests().then(() => {
console.log(`\n${"─".repeat(40)}`);
console.log(`✅ Passed: ${passed}`);
console.log(`❌ Failed: ${failed}`);
console.log(`Total: ${passed + failed}`);
process.exit(failed > 0 ? 1 : 0);
});