Files
OmniRoute/tests/unit/opencode-premium-keyless-gate-8681.test.ts
Diego Rodrigues de Sa e Souza ebddc51575 fix(providers): gate premium opencode-zen/opencode-go models behind an API key (#8681)
* fix(providers): gate premium opencode-zen/opencode-go models behind an API key (#8681)

Root cause: the free/noauth opencode provider (and opencode-zen/opencode-go)
expose the full upstream model list including PREMIUM models (gpt-5, claude-*,
gemini-*, kimi-k2.6, etc.). With a keyless connection, the executor sends no
Authorization header and upstream returns 401 'Missing API key' for any
premium model — which is the exact string the client shows.

Fix: add a request-time gate in OpencodeExecutor.execute() that detects
keyless connections + premium models and returns a clear 402 error with
message 'This model requires an opencode API key — add one in Settings →
Providers.' instead of proxying the raw upstream 401.

Free models (known free catalog +  suffix) continue to work keyless
(deepseek-v4-flash-free, big-pickle, etc.). Users with a valid opencode API
key keep premium access. opencode-go has no free tier — all models require
a key.

* fix(providers): use a free opencode model in the #7993 proxy-routing test

The #8681 keyless-premium gate short-circuits 'grok-code' (a premium
model) with 402 before any fetch happens, so the proxy-egress assertion
never saw a request. Swap to 'deepseek-v4-flash-free' (already applied
to the sibling opencode-proxy-rotation-4954.test.ts in this same PR)
so the test again exercises the proxy-routing path it targets.

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
2026-08-07 18:07:59 -03:00

169 lines
7.4 KiB
TypeScript

import { after, before, describe, it } from "node:test";
import assert from "node:assert/strict";
const { OpencodeExecutor } = await import("../../open-sse/executors/opencode.ts");
const { PROVIDER_MODELS } = await import("../../open-sse/config/providerModels.ts");
function createInput(model, stream = true, credentials = null) {
return {
model,
stream,
credentials,
body: {
model,
stream,
messages: [{ role: "user", content: "hello" }],
},
};
}
function createMockResponse() {
return new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
describe("OpencodeExecutor — premium model keyless gate (#8681)", () => {
let originalFetch: typeof globalThis.fetch;
before(() => {
originalFetch = globalThis.fetch;
globalThis.fetch = (async (_url: string, _options?: RequestInit) => {
return createMockResponse();
}) as typeof globalThis.fetch;
});
after(() => {
globalThis.fetch = originalFetch;
});
describe("isPremiumModel", () => {
it("returns false for known free models on opencode-zen", () => {
// Free models from the opencode (noauth) registry
assert.equal(OpencodeExecutor.isPremiumModel("big-pickle", "opencode-zen"), false);
assert.equal(OpencodeExecutor.isPremiumModel("deepseek-v4-flash-free", "opencode-zen"), false);
});
it("returns false for models ending in -free on opencode-zen", () => {
assert.equal(OpencodeExecutor.isPremiumModel("mimo-v2.5-free", "opencode-zen"), false);
assert.equal(OpencodeExecutor.isPremiumModel("nemotron-3-ultra-free", "opencode-zen"), false);
assert.equal(OpencodeExecutor.isPremiumModel("north-mini-code-free", "opencode-zen"), false);
assert.equal(OpencodeExecutor.isPremiumModel("hy3-free", "opencode-zen"), false);
});
it("returns true for premium models on opencode-zen", () => {
assert.equal(OpencodeExecutor.isPremiumModel("gpt-5", "opencode-zen"), true);
assert.equal(OpencodeExecutor.isPremiumModel("gpt-5-nano", "opencode-zen"), true);
assert.equal(OpencodeExecutor.isPremiumModel("claude-sonnet-4-5", "opencode-zen"), true);
assert.equal(OpencodeExecutor.isPremiumModel("gemini-3-flash", "opencode-zen"), true);
assert.equal(OpencodeExecutor.isPremiumModel("kimi-k2.6", "opencode-zen"), true);
assert.equal(OpencodeExecutor.isPremiumModel("glm-5", "opencode-zen"), true);
});
it("returns true for ALL models on opencode-go (no free tier)", () => {
assert.equal(OpencodeExecutor.isPremiumModel("deepseek-v4-pro", "opencode-go"), true);
assert.equal(OpencodeExecutor.isPremiumModel("kimi-k2.7-code", "opencode-go"), true);
assert.equal(OpencodeExecutor.isPremiumModel("glm-5.2", "opencode-go"), true);
assert.equal(OpencodeExecutor.isPremiumModel("deepseek-v4-flash-free", "opencode-go"), true);
assert.equal(OpencodeExecutor.isPremiumModel("big-pickle", "opencode-go"), true);
assert.equal(OpencodeExecutor.isPremiumModel("mimo-v2.5-free", "opencode-go"), true);
});
it("returns false for free models on the opencode (noauth) provider", () => {
assert.equal(OpencodeExecutor.isPremiumModel("deepseek-v4-flash-free", "opencode"), false);
assert.equal(OpencodeExecutor.isPremiumModel("big-pickle", "opencode"), false);
assert.equal(OpencodeExecutor.isPremiumModel("hy3-free", "opencode"), false);
});
it("returns true for premium models on the opencode (noauth) provider", () => {
assert.equal(OpencodeExecutor.isPremiumModel("gpt-5", "opencode"), true);
assert.equal(OpencodeExecutor.isPremiumModel("claude-sonnet-4-5", "opencode"), true);
});
it("returns true for unknown models on any opencode provider", () => {
assert.equal(OpencodeExecutor.isPremiumModel("unknown-random-model", "opencode-zen"), true);
});
});
describe("execute with keyless credentials", () => {
const zenExecutor = new OpencodeExecutor("opencode-zen");
it("returns 402 for premium model gpt-5 with keyless credentials", async () => {
const result = await zenExecutor.execute(createInput("gpt-5", true, null));
const response = result instanceof Response ? result : result.response;
const body = await response.json() as { error: { message: string } };
assert.equal(response.status, 402);
assert.ok(
body.error.message.includes("API key"),
`Expected message to mention "API key" — got: ${body.error.message}`
);
assert.ok(
!body.error.message.includes("Missing API key"),
"Should NOT be the raw upstream 'Missing API key' message"
);
});
it("returns 402 for premium model claude-sonnet-4-5 with keyless credentials", async () => {
const result = await zenExecutor.execute(createInput("claude-sonnet-4-5", true, null));
const response = result instanceof Response ? result : result.response;
assert.equal(response.status, 402);
});
it("allows free model deepseek-v4-flash-free with keyless credentials", async () => {
// Should reach the upstream fetch (mock returns 200)
const result = await zenExecutor.execute(createInput("deepseek-v4-flash-free", true, null));
const response = result instanceof Response ? result : result.response;
// Should NOT be 402 (the premium gate); should reach the mock fetch
assert.notEqual(response.status, 402);
});
it("allows free model big-pickle with keyless credentials", async () => {
const result = await zenExecutor.execute(createInput("big-pickle", true, null));
const response = result instanceof Response ? result : result.response;
assert.notEqual(response.status, 402);
});
});
describe("execute with valid key credentials", () => {
const zenExecutor = new OpencodeExecutor("opencode-zen");
it("allows premium model gpt-5 with a valid API key", async () => {
// Should reach the upstream fetch (mock returns 200)
const result = await zenExecutor.execute(createInput("gpt-5", true, { apiKey: "valid-key" }));
const response = result instanceof Response ? result : result.response;
assert.notEqual(response.status, 402);
});
it("allows premium model claude-sonnet-4-5 with a valid API key", async () => {
const result = await zenExecutor.execute(
createInput("claude-sonnet-4-5", true, { apiKey: "valid-key" })
);
const response = result instanceof Response ? result : result.response;
assert.notEqual(response.status, 402);
});
});
describe("execute with keyless credentials on opencode-go", () => {
const goExecutor = new OpencodeExecutor("opencode-go");
it("returns 402 for ANY model with keyless credentials (opencode-go has no free tier)", async () => {
const result = await goExecutor.execute(createInput("deepseek-v4-pro", true, null));
const response = result instanceof Response ? result : result.response;
assert.equal(response.status, 402);
});
});
describe("execute with valid key on opencode-go", () => {
const goExecutor = new OpencodeExecutor("opencode-go");
it("allows deepseek-v4-pro with a valid API key", async () => {
const result = await goExecutor.execute(
createInput("deepseek-v4-pro", true, { apiKey: "valid-key" })
);
const response = result instanceof Response ? result : result.response;
assert.notEqual(response.status, 402);
});
});
});