From 5714e3863ac10068a250645072ccbdda189f7445 Mon Sep 17 00:00:00 2001 From: Patrick Wozniak Date: Tue, 26 May 2026 21:51:54 +0200 Subject: [PATCH] Test dynamic Command Code model discovery --- package.json | 3 ++- tests/test-models.ts | 36 +++++++++++++++++++++++++ tests/test-pi-local.mjs | 60 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 tests/test-models.ts diff --git a/package.json b/package.json index 2757d5b..971b815 100644 --- a/package.json +++ b/package.json @@ -25,11 +25,12 @@ "LICENSE" ], "scripts": { - "test": "npm run typecheck && tsx tests/test-pure-functions.ts && tsx tests/test-oauth.ts && tsx tests/test-abort.ts && tsx tests/test-stream.ts && node tests/test-pi-local.mjs", + "test": "npm run typecheck && tsx tests/test-pure-functions.ts && tsx tests/test-models.ts && tsx tests/test-oauth.ts && tsx tests/test-abort.ts && tsx tests/test-stream.ts && node tests/test-pi-local.mjs", "typecheck": "tsc --noEmit", "format:check": "prettier --check '**/*.{ts,mjs,json,md}'", "format": "prettier --write '**/*.{ts,mjs,json,md}'", "test:unit": "tsx tests/test-pure-functions.ts", + "test:models": "tsx tests/test-models.ts", "test:oauth": "tsx tests/test-oauth.ts", "test:abort": "tsx tests/test-abort.ts", "test:stream": "tsx tests/test-stream.ts", diff --git a/tests/test-models.ts b/tests/test-models.ts new file mode 100644 index 0000000..15ee3fd --- /dev/null +++ b/tests/test-models.ts @@ -0,0 +1,36 @@ +import assert from "node:assert/strict" +import { describe, it } from "node:test" + +import { commandCodeModelsFromApiResponse } from "../src/models.ts" + +describe("commandCodeModelsFromApiResponse()", () => { + it("converts the Provider API model list to pi models", () => { + const models = commandCodeModelsFromApiResponse({ + object: "list", + data: [ + { + id: "Qwen/Qwen3.7-Max", + object: "model", + created: 1779824324, + owned_by: "command-code", + name: "Qwen 3.7 Max", + context_length: 1_000_000, + }, + ], + }) + + assert.deepEqual(models, [ + { + id: "Qwen/Qwen3.7-Max", + name: "Qwen 3.7 Max (CC)", + reasoning: true, + contextWindow: 1_000_000, + maxTokens: 65_536, + }, + ]) + }) + + it("rejects unexpected API shapes", () => { + assert.throws(() => commandCodeModelsFromApiResponse({ object: "list", data: [{}] })) + }) +}) diff --git a/tests/test-pi-local.mjs b/tests/test-pi-local.mjs index 9f97fd7..8b37560 100644 --- a/tests/test-pi-local.mjs +++ b/tests/test-pi-local.mjs @@ -56,10 +56,40 @@ if (piCheck.error) { } let requestCount = 0 +let modelListRequestCount = 0 let lastRequestBody let lastRequestHeaders = {} const server = createServer((req, res) => { + if (req.method === "GET" && req.url === "/provider/v1/models") { + modelListRequestCount += 1 + res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" }) + res.end( + JSON.stringify({ + object: "list", + data: [ + { + id: TEST_MODEL, + object: "model", + created: 1779824324, + owned_by: "command-code", + name: "DeepSeek V4 Flash", + context_length: 1_000_000, + }, + { + id: "Qwen/Qwen3.7-Max", + object: "model", + created: 1779824324, + owned_by: "command-code", + name: "Qwen 3.7 Max", + context_length: 1_000_000, + }, + ], + }), + ) + return + } + if (req.method !== "POST" || req.url !== "/alpha/generate") { res.writeHead(404) res.end("Not found") @@ -114,6 +144,7 @@ let tempHome const env = { ...process.env, COMMANDCODE_API_BASE: apiBase, + COMMANDCODE_MODELS_URL: `${apiBase}/provider/v1/models`, } if (hasLivePiAuth()) { @@ -161,7 +192,17 @@ function runPi(args, timeoutMs = 30_000) { async function runRpcQuery(timeoutMs = 30_000) { const child = spawn( PI_BIN, - ["--mode", "rpc", "-e", EXT_PATH, "--provider", "commandcode", "--model", TEST_MODEL], + [ + "--no-extensions", + "--mode", + "rpc", + "-e", + EXT_PATH, + "--provider", + "commandcode", + "--model", + TEST_MODEL, + ], { cwd: PROJECT_DIR, env, @@ -250,15 +291,28 @@ async function runRpcQuery(timeoutMs = 30_000) { try { console.log("[pi-local] list models through real extension") - const list = await runPi(["-e", EXT_PATH, "--list-models"], 20_000) + modelListRequestCount = 0 + const list = await runPi(["--no-extensions", "-e", EXT_PATH, "--list-models"], 20_000) assert.equal(list.code, 0, list.stderr) assert.match(list.stdout, /commandcode/) assert.match(list.stdout, /deepseek\/deepseek-v4-flash/) + assert.match(list.stdout, /Qwen\/Qwen3\.7-Max/) + assert.equal(modelListRequestCount, 1) console.log("[pi-local] print mode through real extension and mock API") requestCount = 0 const print = await runPi( - ["-e", EXT_PATH, "-p", "say mock token", "--provider", "commandcode", "--model", TEST_MODEL], + [ + "--no-extensions", + "-e", + EXT_PATH, + "-p", + "say mock token", + "--provider", + "commandcode", + "--model", + TEST_MODEL, + ], 30_000, ) assert.equal(print.code, 0, print.stderr)