|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const assert = require("node:assert/strict"); |
| 4 | +const http = require("node:http"); |
| 5 | +const { it } = require("node:test"); |
| 6 | +const { testKeyedProvider } = require("../src/lib/keyed-provider-test"); |
| 7 | + |
| 8 | +it("discovers models when no exact custom model ID is supplied", async () => { |
| 9 | + let listedProvider; |
| 10 | + const result = await testKeyedProvider( |
| 11 | + { baseUrl: "https://example.test/v1/", apiKey: "test-key" }, |
| 12 | + { |
| 13 | + adapter: { |
| 14 | + listModels: async (provider) => { |
| 15 | + listedProvider = provider; |
| 16 | + return [{ id: "model-a", name: "Model A" }]; |
| 17 | + }, |
| 18 | + }, |
| 19 | + } |
| 20 | + ); |
| 21 | + |
| 22 | + assert.equal(result.ok, true); |
| 23 | + assert.equal(result.validation, "models"); |
| 24 | + assert.equal(listedProvider.baseUrl, "https://example.test/v1"); |
| 25 | + assert.deepEqual(result.models, [{ id: "model-a", name: "Model A" }]); |
| 26 | +}); |
| 27 | + |
| 28 | +it("validates a known model through chat completions without calling /models", async () => { |
| 29 | + const requests = []; |
| 30 | + const server = http.createServer((request, response) => { |
| 31 | + let body = ""; |
| 32 | + request.on("data", (chunk) => { |
| 33 | + body += chunk; |
| 34 | + }); |
| 35 | + request.on("end", () => { |
| 36 | + requests.push({ |
| 37 | + path: request.url, |
| 38 | + authorization: request.headers.authorization, |
| 39 | + body: body ? JSON.parse(body) : null, |
| 40 | + }); |
| 41 | + if (request.url === "/v1/models") { |
| 42 | + response.writeHead(404).end("not supported"); |
| 43 | + return; |
| 44 | + } |
| 45 | + response.writeHead(200, { "Content-Type": "application/json" }); |
| 46 | + response.end(JSON.stringify({ choices: [{ message: { content: "ok" } }] })); |
| 47 | + }); |
| 48 | + }); |
| 49 | + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); |
| 50 | + |
| 51 | + try { |
| 52 | + const result = await testKeyedProvider({ |
| 53 | + baseUrl: `http://127.0.0.1:${server.address().port}/v1`, |
| 54 | + apiKey: "test-key", |
| 55 | + modelId: "cline-known-model", |
| 56 | + }); |
| 57 | + |
| 58 | + assert.equal(result.ok, true); |
| 59 | + assert.equal(result.validation, "chat-completions"); |
| 60 | + assert.deepEqual(requests.map((request) => request.path), ["/v1/chat/completions"]); |
| 61 | + assert.equal(requests[0].authorization, "Bearer test-key"); |
| 62 | + assert.equal(requests[0].body.model, "cline-known-model"); |
| 63 | + assert.deepEqual(result.models, [{ id: "cline-known-model", name: "cline-known-model" }]); |
| 64 | + } finally { |
| 65 | + await new Promise((resolve) => server.close(resolve)); |
| 66 | + } |
| 67 | +}); |
| 68 | + |
| 69 | +it("does not admit an exact model when its chat-completion test fails", async () => { |
| 70 | + const result = await testKeyedProvider( |
| 71 | + { |
| 72 | + baseUrl: "https://example.test/v1", |
| 73 | + apiKey: "test-key", |
| 74 | + modelId: "missing-model", |
| 75 | + }, |
| 76 | + { |
| 77 | + adapter: { |
| 78 | + chat: async () => new Response("unknown model", { status: 404 }), |
| 79 | + }, |
| 80 | + } |
| 81 | + ); |
| 82 | + |
| 83 | + assert.equal(result.ok, false); |
| 84 | + assert.match(result.error, /Model test failed \(404\): unknown model/); |
| 85 | +}); |
0 commit comments