Files
OmniRoute/tests/unit/cli-resilience-commands.test.ts
Jack Smith 14d70a755b fix: resolve CLI mcp call protocol issues (BUG-001) (#10960)
Validated on the combined 8-PR board: all seven CLI command suites green (combo-suggest, compression, mcp-call, oneproxy, resilience, skills + the shared mcpStreamMock helper) within the board's 88/88, typecheck:core clean, gates within baseline. The CLI mcp call protocol fixes (BUG-001) land with full regression coverage across the affected commands. Thank you @YunyunZhai — and thank you for your patience while this one waited for review.
2026-08-23 18:11:50 -03:00

140 lines
4.5 KiB
TypeScript

import test from "node:test";
import { makeMcpResp, makeMcpStreamFetch } from "./helpers/mcpStreamMock.ts";
import assert from "node:assert/strict";
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("resilience status busca /api/resilience", async () => {
let capturedUrl = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string) => {
capturedUrl = url;
return Promise.resolve(makeResp({ breakers: [], cooldowns: [] }));
}) as any;
await (globalThis.fetch as any)("/api/resilience");
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("/api/resilience"));
});
test("resilience breakers busca include=breakers", async () => {
let capturedUrl = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string) => {
capturedUrl = url;
return Promise.resolve(makeResp({ breakers: [{ provider: "openai", state: "closed" }] }));
}) as any;
await (globalThis.fetch as any)("/api/resilience?include=breakers");
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("include=breakers"));
});
test("resilience cooldowns busca include=cooldowns", async () => {
let capturedUrl = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string) => {
capturedUrl = url;
return Promise.resolve(makeResp({ cooldowns: [] }));
}) as any;
await (globalThis.fetch as any)("/api/resilience?include=cooldowns");
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("include=cooldowns"));
});
test("resilience lockouts busca /api/resilience/model-cooldowns", async () => {
let capturedUrl = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((url: string) => {
capturedUrl = url;
return Promise.resolve(makeResp({ items: [] }));
}) as any;
await (globalThis.fetch as any)("/api/resilience/model-cooldowns");
globalThis.fetch = origFetch;
assert.ok(capturedUrl.includes("/api/resilience/model-cooldowns"));
});
test("resilience reset envia provider e body correto", async () => {
let capturedBody: any = null;
let capturedMethod = "";
const origFetch = globalThis.fetch;
globalThis.fetch = ((_url: string, opts: any) => {
capturedMethod = opts?.method ?? "GET";
if (opts?.body) capturedBody = JSON.parse(opts.body);
return Promise.resolve(makeResp({ reset: true }));
}) as any;
await (globalThis.fetch as any)("/api/resilience/reset", {
method: "POST",
body: JSON.stringify({ provider: "openai", connectionId: "conn-1", allCooldowns: false }),
});
globalThis.fetch = origFetch;
assert.equal(capturedMethod, "POST");
assert.equal(capturedBody.provider, "openai");
assert.equal(capturedBody.connectionId, "conn-1");
});
test("resilience profile set usa JSON-RPC tools/call", async () => {
let capturedCall: any = null;
const origFetch = globalThis.fetch;
globalThis.fetch = makeMcpStreamFetch({ toolResult: {} });
const inner = globalThis.fetch;
globalThis.fetch = ((url: any, init: any) => {
if (String(url).includes("/api/mcp/stream") && String(init?.body || "").includes("tools/call")) {
capturedCall = JSON.parse(init.body);
}
return inner(url, init);
}) as any;
const { mcpCallTool } = await import("../../bin/cli/mcpClient.mjs");
await mcpCallTool("omniroute_set_resilience_profile", { profile: "balanced" });
globalThis.fetch = origFetch;
assert.equal(capturedCall.method, "tools/call");
assert.equal(capturedCall.params.name, "omniroute_set_resilience_profile");
assert.equal(capturedCall.params.arguments.profile, "balanced");
});
test("resilience.mjs pode ser importado sem erro", async () => {
const mod = await import("../../bin/cli/commands/resilience.mjs");
assert.equal(typeof mod.registerResilience, "function");
});