183 lines
6.6 KiB
TypeScript
183 lines
6.6 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
|
|
import {
|
|
createServer,
|
|
resolveMcpMode,
|
|
HOSE_CLI_TOOL_NAME,
|
|
HOSE_MCP_MODE_ENV,
|
|
tokenizeCommand,
|
|
normalizeCliArgs,
|
|
} from "./server.js";
|
|
import { VERSION } from "./version.js";
|
|
|
|
describe("hose-use MCP server & hose_cli single-gate pattern", () => {
|
|
test("tokenizes complex CLI arguments with quotes and JSON payloads", () => {
|
|
expect(
|
|
tokenizeCommand(`hose-use bill query --code "BX20260901001" --corp ID01MCcOgJl1td`)
|
|
).toEqual([
|
|
"hose-use",
|
|
"bill",
|
|
"query",
|
|
"--code",
|
|
"BX20260901001",
|
|
"--corp",
|
|
"ID01MCcOgJl1td",
|
|
]);
|
|
|
|
expect(
|
|
tokenizeCommand(`hose auth import --raw 'curl "https://app.ekuaibao.com/api?corpId=123" -H "accessToken: abc"'`)
|
|
).toEqual([
|
|
"hose",
|
|
"auth",
|
|
"import",
|
|
"--raw",
|
|
`curl "https://app.ekuaibao.com/api?corpId=123" -H "accessToken: abc"`,
|
|
]);
|
|
});
|
|
|
|
test("normalizes CLI prefix binary names", () => {
|
|
expect(normalizeCliArgs(["hose-use", "version"])).toEqual(["version"]);
|
|
expect(normalizeCliArgs(["hose", "corps", "list"])).toEqual(["corps", "list"]);
|
|
expect(normalizeCliArgs(["/usr/local/bin/hose", "probe"])).toEqual(["probe"]);
|
|
expect(normalizeCliArgs(["corps", "current"])).toEqual(["corps", "current"]);
|
|
});
|
|
|
|
test("resolves HOSE_MCP_MODE from environment variable", () => {
|
|
const original = process.env[HOSE_MCP_MODE_ENV];
|
|
delete process.env[HOSE_MCP_MODE_ENV];
|
|
|
|
try {
|
|
expect(resolveMcpMode()).toBe("single");
|
|
expect(resolveMcpMode("single")).toBe("single");
|
|
expect(resolveMcpMode("all")).toBe("all");
|
|
expect(resolveMcpMode("both")).toBe("all");
|
|
expect(resolveMcpMode("legacy")).toBe("legacy");
|
|
} finally {
|
|
if (original === undefined) delete process.env[HOSE_MCP_MODE_ENV];
|
|
else process.env[HOSE_MCP_MODE_ENV] = original;
|
|
}
|
|
});
|
|
|
|
test("advertises only hose_cli in single mode and executes commands in-process", async () => {
|
|
const server = createServer({ mode: "single" });
|
|
const client = new Client({ name: "hose-use-test-client", 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).toHaveLength(1);
|
|
expect(listed.tools[0]?.name).toBe(HOSE_CLI_TOOL_NAME);
|
|
expect(listed.tools[0]?.inputSchema).toMatchObject({
|
|
type: "object",
|
|
required: ["command"],
|
|
});
|
|
|
|
// Execute 'version'
|
|
const versionResult = await client.callTool({
|
|
name: HOSE_CLI_TOOL_NAME,
|
|
arguments: { command: "hose-use version" },
|
|
});
|
|
expect(versionResult.isError).not.toBe(true);
|
|
const content = versionResult.content as Array<{ type: "text"; text: string }>;
|
|
expect(content[0]?.text).toContain(`hose-use v${VERSION}`);
|
|
|
|
// Execute 'corps list'
|
|
const corpsResult = await client.callTool({
|
|
name: HOSE_CLI_TOOL_NAME,
|
|
arguments: { command: "corps list" },
|
|
});
|
|
expect(corpsResult.isError).not.toBe(true);
|
|
const corpsContent = corpsResult.content as Array<{ type: "text"; text: string }>;
|
|
const text = corpsContent[0]?.text || "";
|
|
expect(text.includes("已配置企业列表") || text.includes("未找到任何企业配置")).toBe(true);
|
|
|
|
// Execute invalid command
|
|
const invalidResult = await client.callTool({
|
|
name: HOSE_CLI_TOOL_NAME,
|
|
arguments: { command: "not-a-real-command-xyz" },
|
|
});
|
|
expect(invalidResult.isError).toBe(true);
|
|
const errContent = invalidResult.content as Array<{ type: "text"; text: string }>;
|
|
expect(errContent[0]?.text).toContain("未知命令域");
|
|
} finally {
|
|
await Promise.allSettled([client.close(), server.close()]);
|
|
}
|
|
});
|
|
|
|
test("advertises both hose_cli and fine-grained tools in all mode", async () => {
|
|
const server = createServer({ mode: "all" });
|
|
const client = new Client({ name: "hose-use-test-all", version: "1.0.0" });
|
|
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
|
|
try {
|
|
await Promise.all([
|
|
server.connect(serverTransport),
|
|
client.connect(clientTransport),
|
|
]);
|
|
|
|
const listed = await client.listTools();
|
|
const toolNames = listed.tools.map((t) => t.name);
|
|
expect(toolNames).toContain(HOSE_CLI_TOOL_NAME);
|
|
expect(toolNames).toContain("bill_search");
|
|
expect(toolNames).toContain("list_corps");
|
|
expect(toolNames).toContain("platform_workflow_list_all");
|
|
expect(toolNames.length).toBeGreaterThan(5);
|
|
} finally {
|
|
await Promise.allSettled([client.close(), server.close()]);
|
|
}
|
|
});
|
|
|
|
test("advertises only fine-grained tools in legacy mode", async () => {
|
|
const server = createServer({ mode: "legacy" });
|
|
const client = new Client({ name: "hose-use-test-legacy", version: "1.0.0" });
|
|
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
|
|
|
|
try {
|
|
await Promise.all([
|
|
server.connect(serverTransport),
|
|
client.connect(clientTransport),
|
|
]);
|
|
|
|
const listed = await client.listTools();
|
|
const toolNames = listed.tools.map((t) => t.name);
|
|
expect(toolNames).not.toContain(HOSE_CLI_TOOL_NAME);
|
|
expect(toolNames).toContain("bill_search");
|
|
expect(toolNames).toContain("list_corps");
|
|
} finally {
|
|
await Promise.allSettled([client.close(), server.close()]);
|
|
}
|
|
});
|
|
|
|
test("returns empty tools when MCP is disabled via configuration", async () => {
|
|
const server = createServer({ enabled: false });
|
|
const client = new Client({ name: "hose-use-test-disabled", 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([]);
|
|
|
|
const callResult = await client.callTool({
|
|
name: HOSE_CLI_TOOL_NAME,
|
|
arguments: { command: "version" },
|
|
});
|
|
expect(callResult.isError).toBe(true);
|
|
const content = callResult.content as Array<{ type: "text"; text: string }>;
|
|
expect(content[0]?.text).toContain("HOSE_USE_MCP_ENABLED=false");
|
|
} finally {
|
|
await Promise.allSettled([client.close(), server.close()]);
|
|
}
|
|
});
|
|
});
|