Files
seal-use/src/server.test.ts
T

228 lines
8.8 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { spawnSync } from "node:child_process";
import { tmpdir } from "node:os";
import { fileURLToPath } from "node:url";
import {
createServer,
resolveMcpEnabled,
resolveServerCliLauncher,
SEAL_USE_MCP_TOOL_NAME,
SEAL_USE_MCP_WRITE_TOOL_NAME,
SEAL_MCP_ENABLED_ENV,
tokenizeCommand
} from "./server.js";
describe("seal-use MCP server", () => {
test("the source MCP launcher executes the CLI without relying on the caller's cwd", () => {
const launcher = resolveServerCliLauncher(fileURLToPath(import.meta.resolve("./server.ts")), false);
const result = spawnSync(launcher.command, [...launcher.args!, "version"], {
cwd: tmpdir(), encoding: "utf8", env: { ...process.env, SEAL_DISABLE_BG_UPDATE: "1" }
});
expect(result.status).toBe(0);
expect(JSON.parse(result.stdout).name).toBe("seal-use");
});
test("the compiled MCP launcher never forwards a virtual source file to the executable", () => {
const launcher = resolveServerCliLauncher("/$bunfs/root/src/server.ts", true, "/usr/local/bin/seal-use");
expect(launcher).toEqual({ command: "/usr/local/bin/seal-use", args: [] });
});
test("tokenizes quoted JSON without invoking a shell", () => {
expect(tokenizeCommand(
`seal-use rules search "差旅 发票" --corp corp-a`
)).toEqual([
"seal-use",
"rules",
"search",
"差旅 发票",
"--corp",
"corp-a"
]);
});
test("defaults MCP tools to disabled and parses the plugin boolean setting", () => {
const original = process.env[SEAL_MCP_ENABLED_ENV];
delete process.env[SEAL_MCP_ENABLED_ENV];
try {
expect(resolveMcpEnabled()).toBe(false);
expect(resolveMcpEnabled("false")).toBe(false);
expect(resolveMcpEnabled("0")).toBe(false);
expect(resolveMcpEnabled("true")).toBe(true);
expect(resolveMcpEnabled("1")).toBe(true);
expect(resolveMcpEnabled("yes")).toBe(true);
} finally {
if (original === undefined) delete process.env[SEAL_MCP_ENABLED_ENV];
else process.env[SEAL_MCP_ENABLED_ENV] = original;
}
});
test("advertises the read-only and write tools and executes version through the CLI runner", async () => {
const server = createServer({ enableCli: true });
const client = new Client({ name: "seal-use-test", version: "1.0.0" });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
try {
await Promise.all([
server.connect(serverTransport),
client.connect(clientTransport)
]);
const listed = await client.listTools();
expect(listed.tools.map((tool) => tool.name)).toEqual([SEAL_USE_MCP_TOOL_NAME, SEAL_USE_MCP_WRITE_TOOL_NAME]);
expect(listed.tools[0]?.inputSchema).toMatchObject({
type: "object",
required: ["command"],
additionalProperties: false
});
// The read-only tool promises it cannot mutate, so a client may auto-approve it.
expect(listed.tools[0]?.annotations).toMatchObject({
readOnlyHint: true,
destructiveHint: false,
openWorldHint: true
});
// The write tool can reach mutating verbs, so it must not be advertised as safe to auto-run
// or safe to retry.
expect(listed.tools[1]?.annotations).toMatchObject({
readOnlyHint: false,
destructiveHint: true,
idempotentHint: false,
openWorldHint: true
});
const result = await client.callTool({
name: SEAL_USE_MCP_TOOL_NAME,
arguments: { command: "seal-use version" }
});
const content = result.content as Array<{ type: "text"; text: string }>;
const text = content[0];
expect(result.isError).not.toBe(true);
expect(text?.type).toBe("text");
if (text?.type === "text") {
expect(JSON.parse(text.text)).toMatchObject({ name: "seal-use" });
}
const invalid = await client.callTool({
name: SEAL_USE_MCP_TOOL_NAME,
arguments: { command: "not-a-command" }
});
expect(invalid.isError).toBe(true);
const invalidContent = invalid.content as Array<{ type: "text"; text: string }>;
const errorText = invalidContent[0];
expect(errorText?.type).toBe("text");
if (errorText?.type === "text") {
expect(errorText.text).toContain(SEAL_USE_MCP_WRITE_TOOL_NAME);
}
} finally {
await Promise.allSettled([client.close(), server.close()]);
}
});
test("the read-only tool refuses mutating and unclassified commands", async () => {
const server = createServer({ enableCli: true });
const client = new Client({ name: "seal-use-policy-test", version: "1.0.0" });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
const callRead = async (command: string) => {
const result = await client.callTool({ name: SEAL_USE_MCP_TOOL_NAME, arguments: { command } });
const content = result.content as Array<{ type: "text"; text: string }>;
return { isError: result.isError, text: content[0]?.text ?? "" };
};
try {
await Promise.all([
server.connect(serverTransport),
client.connect(clientTransport)
]);
for (const command of [
"seal-use corps mark corp-a --env production --confirm",
"seal-use rules push",
"seal-use rules create --description test --scope all",
"seal-use documents create --title t --purpose p --content c",
"seal-use documents update doc-1 --content c --dry-run --yes",
"seal-use documents delete doc-1 --dry-run",
"seal-use simulation create --mode hosecloud",
"seal-use deploy hosecloud sync sign-key",
"seal-use users reset-password corp-a:u1",
"seal-use webhook replay",
"seal-use update",
"seal-use mcp",
"seal-use totally-unknown-area list",
""
]) {
const refused = await callRead(command);
expect({ command, isError: refused.isError }).toEqual({ command, isError: true });
}
// Read-only commands must get past the policy; `version` is a top-level command with no
// registry row, and `corp` is an area alias the CLI dispatches as `corps`.
for (const command of [
"seal-use version",
"seal-use corps list",
"seal-use corp list",
"seal-use documents list --corp corp-a",
"seal-use documents list --summary --corp corp-a",
"seal-use documents get doc-1 --out ./body.txt --corp corp-a",
"seal-use expenses --sn SN-FIXTURE --corp missing-fixture-corp"
]) {
const allowed = await callRead(command);
expect({ command, refusedByPolicy: allowed.text.includes("only runs read-only commands") }).toEqual({
command,
refusedByPolicy: false
});
}
// A read-only command must survive the policy check and fail (if at all) for its own
// reasons — here the corp is unknown — never because the policy refused it.
const allowed = await callRead("seal-use approval-runs get does-not-exist --corp missing");
expect(allowed.text).not.toContain(`only runs read-only commands`);
} finally {
await Promise.allSettled([client.close(), server.close()]);
}
});
test("the write tool accepts mutating commands that the read-only tool refuses", async () => {
const server = createServer({ enableCli: true });
const client = new Client({ name: "seal-use-write-test", version: "1.0.0" });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
try {
await Promise.all([
server.connect(serverTransport),
client.connect(clientTransport)
]);
const result = await client.callTool({
name: SEAL_USE_MCP_WRITE_TOOL_NAME,
arguments: { command: "seal-use corps mark missing-corp --env production --confirm" }
});
const content = result.content as Array<{ type: "text"; text: string }>;
const text = content[0]?.text ?? "";
// It must get past the policy and reach the CLI, which then fails on its own terms.
expect(text).not.toContain("only runs read-only commands");
} finally {
await Promise.allSettled([client.close(), server.close()]);
}
});
test("does not advertise tools when MCP is disabled", async () => {
const server = createServer({ enableCli: false });
const client = new Client({ name: "seal-home-disabled-test", version: "1.0.0" });
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
try {
await Promise.all([
server.connect(serverTransport),
client.connect(clientTransport)
]);
const listed = await client.listTools();
expect(listed.tools).toEqual([]);
} finally {
await Promise.allSettled([client.close(), server.close()]);
}
});
});