videogen/apps/web/app/api/mcp/route.test.ts
caelum0x d56639a594
Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Menivor — open source release (MIT), free to use and fork
Paid, hosted product at menivor.com, now retired as a company project.
Fresh squashed history of the cleaned working tree (secret-scanned,
no .env, no customer data, no outreach logs). Built on
MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
2026-12-04 02:10:26 +03:00

122 lines
4.7 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import { NextRequest } from "next/server";
import { POST } from "./route";
function rpc(body: unknown, headers: Record<string, string> = {}): NextRequest {
return new NextRequest("https://menivor.com/api/mcp", {
method: "POST",
headers: { "content-type": "application/json", ...headers },
body: JSON.stringify(body),
});
}
afterEach(() => vi.unstubAllGlobals());
describe("POST /api/mcp", () => {
it("initializes with a supported protocol version", async () => {
const res = await POST(
rpc({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-03-26" } }),
);
const body = await res.json();
expect(res.status).toBe(200);
expect(body.result.protocolVersion).toBe("2025-03-26");
expect(body.result.serverInfo.name).toBe("menivor");
expect(body.result.capabilities.tools).toBeDefined();
});
it("lists tools without a key", async () => {
const res = await POST(rpc({ jsonrpc: "2.0", id: 2, method: "tools/list" }));
const body = await res.json();
const names = body.result.tools.map((tool: { name: string }) => tool.name);
expect(names).toEqual(
expect.arrayContaining(["create_reel", "render_html", "get_job", "ad_from_url", "schedule_publish", "wallet_balance"]),
);
});
it("acknowledges notifications with 202", async () => {
const res = await POST(rpc({ jsonrpc: "2.0", method: "notifications/initialized" }));
expect(res.status).toBe(202);
});
it("refuses tools/call without a key", async () => {
const res = await POST(
rpc({ jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "wallet_balance", arguments: {} } }),
);
expect(res.status).toBe(401);
expect(res.headers.get("WWW-Authenticate")).toContain("Bearer");
expect((await res.json()).error.code).toBe(-32001);
});
it("forwards the bearer key to the REST API and returns structured content", async () => {
const fetchMock = vi.fn(async () =>
new Response(JSON.stringify({ status: 200, data: { available_micros: 5_000_000 } }), { status: 200 }),
);
vi.stubGlobal("fetch", fetchMock);
const res = await POST(
rpc(
{ jsonrpc: "2.0", id: 4, method: "tools/call", params: { name: "wallet_balance", arguments: {} } },
{ authorization: "Bearer mk_test_123" },
),
);
const body = await res.json();
expect(body.result.isError).toBe(false);
expect(body.result.structuredContent).toEqual({ available_micros: 5_000_000 });
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
expect(url).toMatch(/\/wallet$/);
expect((init.headers as Record<string, string>)["x-api-key"]).toBe("mk_test_123");
});
it("render_html starts a reel from the heading and paragraphs, and does not send hidden script text", async () => {
const fetchMock = vi.fn(async () => new Response(JSON.stringify({ data: { job_id: "job_1" } }), { status: 200 }));
vi.stubGlobal("fetch", fetchMock);
const secret = "sk-live-secret";
const res = await POST(
rpc(
{
jsonrpc: "2.0",
id: 6,
method: "tools/call",
params: {
name: "render_html",
arguments: {
html: `<h1>Ship the reel</h1><script>const key = "${secret}"</script><p>Visible line.</p>`,
},
},
},
{ authorization: "Bearer mk_test_123" },
),
);
const body = await res.json();
expect(body.result.isError).toBe(false);
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
expect(url).toMatch(/\/jobs$/);
const sent = String(init.body);
expect(sent).toContain("Ship the reel");
expect(sent).toContain("Visible line.");
expect(sent).not.toContain(secret);
});
it("reports invalid arguments as a tool error without calling upstream", async () => {
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
const res = await POST(
rpc(
{ jsonrpc: "2.0", id: 5, method: "tools/call", params: { name: "get_job", arguments: { job_id: "../x" } } },
{ "x-api-key": "k" },
),
);
const body = await res.json();
expect(body.result.isError).toBe(true);
expect(fetchMock).not.toHaveBeenCalled();
});
it("rejects unknown methods and malformed JSON", async () => {
const unknown = await (await POST(rpc({ jsonrpc: "2.0", id: 6, method: "resources/list" }))).json();
expect(unknown.error.code).toBe(-32601);
const bad = await POST(
new NextRequest("https://menivor.com/api/mcp", { method: "POST", body: "{not json" }),
);
expect(bad.status).toBe(400);
});
});