OmniRoute/open-sse/mcp-server/__tests__/dbHealthTool.test.ts
diegosouzapw 48467bc514 feat(core): add db health checks and provider interoperability
Add automated SQLite health diagnostics with optional auto-repair,
startup and scheduled execution, authenticated API routes, an MCP tool,
and dashboard visibility for status and repair actions.

Improve provider compatibility by adding Cursor usage fetching and
v3.1.0 parity headers, introducing a per-connection Codex Responses
store opt-in with session fallback, fixing Codex non-stream combo and
SSE translation behavior, sanitizing Gemini googleSearch tool payloads
and Qwen thinking tool_choice handling, and hardening cleanup and call
log storage paths.
2026-04-13 13:11:30 -03:00

70 lines
2.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
import { createMcpServer } from "../server.ts";
import { MCP_TOOL_MAP, dbHealthCheckInput } from "../schemas/tools.ts";
const mockFetch = vi.fn();
vi.stubGlobal("fetch", mockFetch);
vi.mock("../audit.ts", () => ({
logToolCall: vi.fn().mockResolvedValue(undefined),
}));
describe("omniroute_db_health_check MCP tool", () => {
let client: Client;
beforeEach(async () => {
mockFetch.mockReset();
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
const server = createMcpServer();
await server.connect(serverTransport);
client = new Client({ name: "test-client", version: "1.0.0" });
await client.connect(clientTransport);
});
afterEach(async () => {
await client.close();
});
it("is registered in the MCP tool map", () => {
expect(MCP_TOOL_MAP["omniroute_db_health_check"]).toBeDefined();
expect(MCP_TOOL_MAP["omniroute_db_health_check"]?.phase).toBe(2);
});
it("validates empty input and explicit autoRepair requests", () => {
expect(dbHealthCheckInput.safeParse({}).success).toBe(true);
expect(dbHealthCheckInput.safeParse({ autoRepair: true }).success).toBe(true);
expect(dbHealthCheckInput.safeParse({ autoRepair: "yes" }).success).toBe(false);
});
it("dispatches to /api/v1/db/health using POST when autoRepair=true", async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
json: async () => ({
isHealthy: false,
issues: [{ type: "broken_reference", table: "combos", description: "broken", count: 1 }],
repairedCount: 1,
backupCreated: true,
autoRepair: true,
checkedAt: new Date().toISOString(),
}),
});
const result = await client.callTool({
name: "omniroute_db_health_check",
arguments: { autoRepair: true },
});
expect(result.isError).toBeFalsy();
expect(mockFetch).toHaveBeenCalledWith(
expect.stringContaining("/api/v1/db/health"),
expect.objectContaining({ method: "POST" })
);
const content = result.content[0] as { type: string; text: string };
const payload = JSON.parse(content.text);
expect(payload.repairedCount).toBe(1);
expect(payload.backupCreated).toBe(true);
});
});