mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-25 08:32:11 +03:00
Validated on the resolved merge against the current release tip: pack-artifact-policy + cli-mcp-call-commands + cli-resilience-commands + cli-skills-commands + model-hide-multikey-11300 39/39, typecheck:core clean, eslint clean. Resolved a pt-BR.json wording conflict against #11322 (kept the tip's wording, semantically identical). Drains the real lint-fallout from the wave that was blocking the release-green verdict — dead code + newly-enforced React-Compiler hook rules. Thank you @jonlwheat2-gif!
228 lines
7.5 KiB
TypeScript
228 lines
7.5 KiB
TypeScript
import test from "node:test";
|
|
import { makeMcpStreamFetch } from "./helpers/mcpStreamMock.ts";
|
|
import assert from "node:assert/strict";
|
|
|
|
const SKILLS_DATA = [
|
|
{ id: "sk_pdf", name: "PDF Parser", type: "sandbox", version: "1.0.0", enabled: true },
|
|
{ id: "sk_img", name: "Image Resize", type: "custom", version: "2.1.0", enabled: false },
|
|
];
|
|
|
|
const EXECUTIONS_DATA = [
|
|
{
|
|
id: "ex_001",
|
|
skillId: "sk_pdf",
|
|
status: "completed",
|
|
startedAt: "2026-05-10T10:00:00Z",
|
|
duration: 342,
|
|
},
|
|
{
|
|
id: "ex_002",
|
|
skillId: "sk_pdf",
|
|
status: "failed",
|
|
startedAt: "2026-05-09T12:00:00Z",
|
|
duration: 100,
|
|
error: "timeout",
|
|
},
|
|
];
|
|
|
|
const MARKETPLACE_DATA = [
|
|
{
|
|
id: "pkg_pdf",
|
|
name: "PDF Toolkit",
|
|
category: "documents",
|
|
version: "1.0.0",
|
|
downloads: 1200,
|
|
rating: 4.5,
|
|
author: "acme",
|
|
},
|
|
];
|
|
|
|
function makeResp(data: unknown, status = 200) {
|
|
const obj = {
|
|
ok: status < 400,
|
|
status,
|
|
exitCode: status < 400 ? 0 : 1,
|
|
json: () => Promise.resolve(data),
|
|
text: () => Promise.resolve(JSON.stringify(data)),
|
|
headers: new Headers(),
|
|
};
|
|
obj.json = obj.json.bind(obj);
|
|
obj.text = obj.text.bind(obj);
|
|
return obj;
|
|
}
|
|
|
|
async function captureStdout(fn: () => Promise<void>): Promise<string> {
|
|
const chunks: string[] = [];
|
|
const orig = process.stdout.write.bind(process.stdout);
|
|
process.stdout.write = (c: string | Uint8Array) => {
|
|
if (typeof c === "string") chunks.push(c);
|
|
return true;
|
|
};
|
|
try {
|
|
await fn();
|
|
} finally {
|
|
process.stdout.write = orig;
|
|
}
|
|
return chunks.join("");
|
|
}
|
|
|
|
function makeCmd(output = "json") {
|
|
return { optsWithGlobals: () => ({ output, quiet: output !== "table" }) };
|
|
}
|
|
|
|
test("runSkillsList retorna lista de skills", async () => {
|
|
const origFetch = globalThis.fetch;
|
|
globalThis.fetch = (() => Promise.resolve(makeResp({ items: SKILLS_DATA }))) as any;
|
|
|
|
const { runSkillsList } = await import("../../bin/cli/commands/skills.mjs");
|
|
const out = await captureStdout(() => runSkillsList({}, makeCmd() as any));
|
|
|
|
globalThis.fetch = origFetch;
|
|
const parsed = JSON.parse(out);
|
|
assert.ok(Array.isArray(parsed));
|
|
assert.equal(parsed[0].id, "sk_pdf");
|
|
});
|
|
|
|
test("runSkillsList filtra por --enabled", async () => {
|
|
let capturedUrl = "";
|
|
const origFetch = globalThis.fetch;
|
|
globalThis.fetch = ((url: string) => {
|
|
capturedUrl = url;
|
|
return Promise.resolve(makeResp({ items: [SKILLS_DATA[0]] }));
|
|
}) as any;
|
|
|
|
const { runSkillsList } = await import("../../bin/cli/commands/skills.mjs");
|
|
await captureStdout(() => runSkillsList({ enabled: true }, makeCmd() as any));
|
|
|
|
globalThis.fetch = origFetch;
|
|
assert.ok(capturedUrl.includes("enabled=true"));
|
|
});
|
|
|
|
test("runSkillsGet busca /api/skills/:id", async () => {
|
|
let capturedUrl = "";
|
|
const origFetch = globalThis.fetch;
|
|
globalThis.fetch = ((url: string) => {
|
|
capturedUrl = url;
|
|
return Promise.resolve(makeResp(SKILLS_DATA[0]));
|
|
}) as any;
|
|
|
|
const { runSkillsGet } = await import("../../bin/cli/commands/skills.mjs");
|
|
const out = await captureStdout(() => runSkillsGet("sk_pdf", {}, makeCmd() as any));
|
|
|
|
globalThis.fetch = origFetch;
|
|
assert.ok(capturedUrl.includes("/api/skills/sk_pdf"));
|
|
const parsed = JSON.parse(out);
|
|
assert.equal(parsed.id, "sk_pdf");
|
|
});
|
|
|
|
test("runSkillsEnable usa JSON-RPC tools/call", async () => {
|
|
const calls: unknown[] = [];
|
|
const origFetch = globalThis.fetch;
|
|
globalThis.fetch = makeMcpStreamFetch({ toolResult: { ok: true } });
|
|
const inner = globalThis.fetch;
|
|
globalThis.fetch = ((url: unknown, init: unknown) => {
|
|
calls.push({ url: String(url), init });
|
|
return inner(url, init);
|
|
}) as any;
|
|
|
|
const { runSkillsEnable } = await import("../../bin/cli/commands/skills.mjs");
|
|
const out = await captureStdout(() => runSkillsEnable("sk_pdf", {}, makeCmd() as any));
|
|
|
|
globalThis.fetch = origFetch;
|
|
assert.ok(calls.some((x) => String(x.url).includes("/api/mcp/stream")));
|
|
const callBody = JSON.parse(calls.find((x) => String(x.init?.body || "").includes("tools/call"))?.init?.body || "{}");
|
|
assert.equal(callBody.method, "tools/call");
|
|
assert.equal(callBody.params.name, "omniroute_skills_enable");
|
|
assert.equal(callBody.params.arguments.skillId, "sk_pdf");
|
|
assert.equal(callBody.params.arguments.enabled, true);
|
|
assert.ok(out.includes("sk_pdf"));
|
|
});
|
|
|
|
test("runSkillsExecute usa JSON-RPC tools/call", async () => {
|
|
const calls: unknown[] = [];
|
|
const origFetch = globalThis.fetch;
|
|
globalThis.fetch = makeMcpStreamFetch({ toolResult: { result: "ok", output: "parsed" } });
|
|
const inner = globalThis.fetch;
|
|
globalThis.fetch = ((url: unknown, init: unknown) => {
|
|
calls.push({ url: String(url), init });
|
|
return inner(url, init);
|
|
}) as any;
|
|
|
|
const { runSkillsExecute } = await import("../../bin/cli/commands/skills.mjs");
|
|
await captureStdout(() =>
|
|
runSkillsExecute("sk_pdf", { input: '{"file":"doc.pdf"}' }, makeCmd() as any)
|
|
);
|
|
|
|
globalThis.fetch = origFetch;
|
|
const callBody = JSON.parse(calls.find((x) => String(x.init?.body || "").includes("tools/call"))?.init?.body || "{}");
|
|
assert.equal(callBody.method, "tools/call");
|
|
assert.equal(callBody.params.name, "omniroute_skills_execute");
|
|
assert.equal(callBody.params.arguments.skillId, "sk_pdf");
|
|
assert.deepEqual(callBody.params.arguments.input, { file: "doc.pdf" });
|
|
});
|
|
|
|
test("runSkillsExecutions filtra por skill e status", async () => {
|
|
let capturedUrl = "";
|
|
const origFetch = globalThis.fetch;
|
|
globalThis.fetch = ((url: string) => {
|
|
capturedUrl = url;
|
|
return Promise.resolve(makeResp({ items: EXECUTIONS_DATA }));
|
|
}) as any;
|
|
|
|
const { runSkillsExecutions } = await import("../../bin/cli/commands/skills.mjs");
|
|
const out = await captureStdout(() =>
|
|
runSkillsExecutions({ skill: "sk_pdf", limit: 20, status: "completed" }, makeCmd() as any)
|
|
);
|
|
|
|
globalThis.fetch = origFetch;
|
|
assert.ok(capturedUrl.includes("skillId=sk_pdf"));
|
|
assert.ok(capturedUrl.includes("status=completed"));
|
|
const parsed = JSON.parse(out);
|
|
assert.ok(Array.isArray(parsed));
|
|
});
|
|
|
|
test("runMarketplaceSearch retorna pacotes com query e filtros", async () => {
|
|
let capturedUrl = "";
|
|
const origFetch = globalThis.fetch;
|
|
globalThis.fetch = ((url: string) => {
|
|
capturedUrl = url;
|
|
return Promise.resolve(makeResp({ items: MARKETPLACE_DATA }));
|
|
}) as any;
|
|
|
|
const { runMarketplaceSearch } = await import("../../bin/cli/commands/skills.mjs");
|
|
const out = await captureStdout(() =>
|
|
runMarketplaceSearch("pdf", { limit: 30, category: "documents" }, makeCmd() as any)
|
|
);
|
|
|
|
globalThis.fetch = origFetch;
|
|
assert.ok(capturedUrl.includes("q=pdf"));
|
|
assert.ok(capturedUrl.includes("category=documents"));
|
|
const parsed = JSON.parse(out);
|
|
assert.ok(Array.isArray(parsed));
|
|
assert.equal(parsed[0].id, "pkg_pdf");
|
|
});
|
|
|
|
test("runMarketplaceInstall --yes envia POST sem confirmação", async () => {
|
|
let capturedBody: unknown = null;
|
|
const origFetch = globalThis.fetch;
|
|
globalThis.fetch = ((_url: string, init: unknown) => {
|
|
capturedBody = JSON.parse(init?.body ?? "{}");
|
|
return Promise.resolve(makeResp({ skillId: "sk_pdf_installed" }));
|
|
}) as any;
|
|
|
|
const { runMarketplaceInstall } = await import("../../bin/cli/commands/skills.mjs");
|
|
const out = await captureStdout(() =>
|
|
runMarketplaceInstall(
|
|
"pkg_pdf",
|
|
{ yes: true, version: "latest", enable: true },
|
|
makeCmd() as any
|
|
)
|
|
);
|
|
|
|
globalThis.fetch = origFetch;
|
|
assert.equal(capturedBody.packageId, "pkg_pdf");
|
|
assert.equal(capturedBody.version, "latest");
|
|
assert.equal(capturedBody.enable, true);
|
|
assert.ok(out.includes("sk_pdf_installed"));
|
|
});
|