Files
OmniRoute/tests/unit/qoder-executor.test.mjs
Diego Rodrigues de Sa e Souza 1442c47bbb chore(release): v3.5.6 — email masking, model toggle, OpenRouter registries & bug fixes (#1080)
* fix(minimax): switch auth from x-api-key to Authorization Bearer (#1076)

Integrated into release/v3.5.6 — MiniMax auth fix with authHeader consistency normalization

* feat(CI,i18n): autogenerate language files + Add missing strings (#1071)

Integrated into release/v3.5.6 — i18n translations for memory, skills, and missing keys across 31 languages

* fix(ci): restore i18n continue-on-error, remove auto-commit race condition

* fix(husky): load nvm in hooks for VS Code compatibility

* fix(husky): gracefully skip hooks when npm is not in PATH

* fix: convert OpenAI function tool_choice to Claude tool format (#1072)

* fix: prevent EPIPE feedback loop filling logs at GB/s (#1006)

* fix: fallback to native fetch when undici dispatcher fails (#1054)

* fix: improve Qoder PAT validation with actionable error messages (#966)

- Add QODER_PERSONAL_ACCESS_TOKEN env var fallback for both validation and execution
- Pre-flight ping check to diagnose connectivity issues (Docker/proxy)
- Detect encrypted auth blobs from ~/.qoder/.auth/user and guide to website PAT
- Clear error messages for auth failures with link to integrations page
- Treat non-auth 4xx as auth-pass (request format issue, not token issue)
- Update tests to cover new validation paths (23 tests, all passing)

* feat: Improve the Chinese translation (#1079)

Integrated into release/v3.5.6

* chore(release): v3.5.6 — i18n updates and credential security fixes

* fix(ci): resolve e2e and docs-sync pipeline failures

* fix(security): bump next to 16.2.3 to resolve SNYK-JS-NEXT-15954202

* fix: guard Memory/Cache UI against null toLocaleString crash (#1083)

* fix: translate OpenAI tool_choice type 'function' to Claude 'tool' format (#1072)

* fix: pass custom baseUrl in provider API key validation (#1078)

* docs: update CHANGELOG with v3.5.6 bug fixes and security patches

* docs: rewrite implement-features workflow with 5-phase harvest-research-report-plan-execute pipeline

* docs: organize _ideia/ into viable/defer/notfit + add Phase 2.5 auto-response workflow

* docs: implementation plans for #1025, #750, #960, #1046 + close already-implemented #833, #973, #982

* feat: mask email addresses in dashboard for privacy (#1025)

* feat: add OpenRouter and GitHub to embedding/image provider registries (#960)

* feat: add model visibility toggle and search filter to provider page (#750)

* docs: move implemented features to notfit, update task plans status

* chore: untrack _ideia/ and _tasks/ from git — private/internal only

* chore(release): bump to v3.5.6 — changelog, docs, version sync & any-budget fix

* fix: remove explicit .ts extension in qoderCli import that caused 500 error in production build

---------

Co-authored-by: Jean Brito <jeanfbrito@gmail.com>
Co-authored-by: zenobit <zenobit@disroot.org>
Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
Co-authored-by: Ethan Hunt <136065060+only4copilot@users.noreply.github.com>
2026-04-09 15:55:59 -03:00

235 lines
7.8 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { QoderExecutor } from "../../open-sse/executors/qoder.ts";
import {
buildQoderPrompt,
getStaticQoderModels,
mapQoderModelToLevel,
normalizeQoderPatProviderData,
parseQoderCliFailure,
validateQoderCliPat,
} from "../../open-sse/services/qoderCli.ts";
test("QoderExecutor: constructor sets provider to qoder", () => {
const executor = new QoderExecutor();
assert.equal(executor.getProvider(), "qoder");
});
test("QoderExecutor: buildHeaders inherits configured user agent, auth and stream headers", () => {
const executor = new QoderExecutor();
assert.deepEqual(executor.buildHeaders({ apiKey: "pat" }, true), {
"Content-Type": "application/json",
"User-Agent": "Qoder-Cli",
Authorization: "Bearer pat",
Accept: "text/event-stream",
});
assert.deepEqual(executor.buildHeaders({ accessToken: "token" }, false), {
"Content-Type": "application/json",
"User-Agent": "Qoder-Cli",
Authorization: "Bearer token",
});
});
test("QoderExecutor: buildUrl uses the live qoder.com API base", () => {
const executor = new QoderExecutor();
assert.equal(
executor.buildUrl("qoder-rome-30ba3b", false),
"https://api.qoder.com/v1/chat/completions"
);
});
test("normalizeQoderPatProviderData forces PAT + qodercli transport", () => {
assert.deepEqual(normalizeQoderPatProviderData({ region: "sa-east-1" }), {
region: "sa-east-1",
authMode: "pat",
transport: "qodercli",
});
});
test("mapQoderModelToLevel maps static models to qodercli levels", () => {
assert.equal(mapQoderModelToLevel("qoder-rome-30ba3b"), "qmodel");
assert.equal(mapQoderModelToLevel("deepseek-r1"), "ultimate");
assert.equal(mapQoderModelToLevel("qwen3-max"), "performance");
assert.equal(mapQoderModelToLevel(""), null);
});
test("getStaticQoderModels exposes the static if/* catalog seed", () => {
const models = getStaticQoderModels();
assert.ok(models.some((model) => model.id === "qoder-rome-30ba3b"));
assert.ok(models.some((model) => model.id === "deepseek-r1"));
});
test("buildQoderPrompt flattens transcript and warns against local tools", () => {
const prompt = buildQoderPrompt({
messages: [
{ role: "system", content: "Follow the user request." },
{
role: "user",
content: [{ type: "text", text: "Reply with OK." }],
},
{
role: "assistant",
tool_calls: [
{
type: "function",
function: { name: "pwd", arguments: "{}" },
},
],
content: "",
},
],
tools: [{ type: "function", function: { name: "pwd" } }],
});
assert.match(prompt, /Conversation transcript:/);
assert.match(prompt, /USER:\nReply with OK\./);
assert.match(prompt, /TOOL_CALL pwd: \{\}/);
assert.match(prompt, /Do not call those tools yourself\./);
});
test("parseQoderCliFailure classifies auth, upstream and timeout failures", () => {
assert.deepEqual(parseQoderCliFailure("Invalid API key"), {
status: 401,
message: "Invalid API key",
code: "upstream_auth_error",
});
assert.deepEqual(parseQoderCliFailure("command not found: qodercli"), {
status: 502,
message: "command not found: qodercli",
code: "upstream_error",
});
assert.deepEqual(parseQoderCliFailure("request timed out"), {
status: 504,
message: "request timed out",
code: "timeout",
});
});
test("validateQoderCliPat succeeds when the validation endpoint returns OK", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
const urlStr = String(url);
// Handle ping check
if (urlStr.includes("/ping")) {
return new Response("pong", { status: 200 });
}
assert.match(urlStr, /api1\.qoder\.sh\/algo\/api\/v2\/service\/pro\/sse\/agent_chat_generation/);
assert.equal(options.method, "POST");
assert.match(String(options.headers.Authorization), /^Bearer COSY\./);
return new Response("{}", { status: 200 });
};
try {
const result = await validateQoderCliPat({ apiKey: "pat_test" });
assert.deepEqual(result, { valid: true, error: null, unsupported: false });
} finally {
globalThis.fetch = originalFetch;
}
});
test("validateQoderCliPat returns auth failures with actionable error", async () => {
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url) => {
if (String(url).includes("/ping")) return new Response("pong", { status: 200 });
return new Response("Invalid API key", { status: 401 });
};
try {
const result = await validateQoderCliPat({ apiKey: "pat_bad" });
assert.equal(result.valid, false);
assert.match(result.error, /Authentication failed/);
assert.equal(result.unsupported, false);
} finally {
globalThis.fetch = originalFetch;
}
});
test("QoderExecutor: missing tokens return an authentication error response", async () => {
const executor = new QoderExecutor();
const { response, url } = await executor.execute({
model: "qoder-rome-30ba3b",
body: { messages: [{ role: "user", content: "hi" }] },
stream: false,
credentials: {},
});
assert.equal(url, "https://dashscope.aliyuncs.com");
assert.equal(response.status, 401);
const payload = await response.json();
assert.equal(payload.error.code, "token_required");
});
test("QoderExecutor: non-stream calls target DashScope and map alias models", async () => {
const executor = new QoderExecutor();
const originalFetch = globalThis.fetch;
globalThis.fetch = async (url, options) => {
assert.equal(String(url), "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions");
assert.equal(options.method, "POST");
assert.equal(options.headers.Authorization, "Bearer pat_test");
assert.equal(options.headers["x-dashscope-authtype"], "qwen-oauth");
const parsedBody = JSON.parse(String(options.body));
assert.equal(parsedBody.model, "coder-model");
return new Response(
JSON.stringify({
id: "chatcmpl-qoder",
object: "chat.completion",
choices: [
{
index: 0,
message: { role: "assistant", content: "OK" },
finish_reason: "stop",
},
],
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
};
try {
const { response, url, transformedBody } = await executor.execute({
model: "qwen3.5-plus",
body: { messages: [{ role: "user", content: "Reply with OK only." }] },
stream: false,
credentials: { apiKey: "pat_test" },
});
assert.equal(url, "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions");
assert.equal(transformedBody.model, "coder-model");
assert.equal(response.status, 200);
const payload = await response.json();
assert.equal(payload.object, "chat.completion");
assert.equal(payload.choices[0].message.role, "assistant");
assert.equal(payload.choices[0].message.content, "OK");
} finally {
globalThis.fetch = originalFetch;
}
});
test("QoderExecutor: stream calls pass through successful SSE responses", async () => {
const executor = new QoderExecutor();
const originalFetch = globalThis.fetch;
globalThis.fetch = async () =>
new Response('data: {"choices":[{"delta":{"content":"O"}}]}\n\ndata: [DONE]\n\n', {
status: 200,
headers: { "Content-Type": "text/event-stream" },
});
try {
const { response } = await executor.execute({
model: "qoder-rome-30ba3b",
body: { messages: [{ role: "user", content: "Reply with OK only." }] },
stream: true,
credentials: { apiKey: "pat_test" },
});
assert.equal(response.status, 200);
const body = await response.text();
assert.match(body, /"content":"O"/);
assert.match(body, /\[DONE\]/);
} finally {
globalThis.fetch = originalFetch;
}
});