Files
hose-use/scripts/verify-binary.ts
T

49 lines
3.3 KiB
TypeScript

#!/usr/bin/env bun
import assert from "node:assert/strict";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { resolve, join } from "node:path";
import { spawnSync } from "node:child_process";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
const binary = resolve(process.argv[2] || "dist/bin/hose-use");
const plugin = process.argv.includes("--plugin");
const command = plugin ? process.execPath : binary;
const prefix = plugin ? [binary] : [];
const cwd = mkdtempSync(join(tmpdir(), "hose-binary-smoke-"));
// The binary must work without Bun on PATH or a source checkout in cwd.
const env = { PATH: process.platform === "win32" ? process.env.SystemRoot || "C:\\Windows" : "/usr/bin:/bin", HOSE_USE_MCP_ENABLED: "true", HOSE_CONFIG_FILE: join(cwd, "config.json"), HOSE_ENTERPRISES_DIR: cwd };
const client = new Client({ name: "hose-binary-smoke", version: "1.0.0" });
const transport = new StdioClientTransport({ command, args: plugin ? prefix : ["mcp", "serve"], cwd, env, stderr: "pipe" });
let stderr = "";
transport.stderr?.on("data", chunk => { stderr += chunk.toString(); });
const deadline = setTimeout(() => { void client.close(); }, 15_000);
try {
const version = spawnSync(command, [...prefix, ...(plugin ? ["--hose-use-cli"] : []), "version", "--json"], { cwd, env, encoding: "utf8", timeout: 5000 });
assert.equal(version.status, 0, version.stderr);
assert.ok(JSON.parse(version.stdout).version);
writeFileSync(join(cwd, "smoke.json"), JSON.stringify({ id: "smoke", name: "Smoke", source: "manual", credentials: { accessToken: "fixture", corpId: "smoke", staffId: "smoke:staff" } }));
const sandbox = spawnSync(command, [...prefix, ...(plugin ? ["--hose-use-cli"] : []), "workflow", "eval-script", "--code", 'return { total: items.reduce((a, b) => a + b, 0), host: typeof process };', "--input", '{"items":[1,2,3]}', "--corp", "smoke"], { cwd, env, encoding: "utf8", timeout: 5000 });
assert.equal(sandbox.status, 0, sandbox.stderr);
assert.match(sandbox.stdout, /"total": 6/);
assert.match(sandbox.stdout, /"host": "undefined"/);
await client.connect(transport, { timeout: 5000 });
const tools = await client.listTools();
assert.deepEqual(tools.tools.map(tool => tool.name), ["hose_cli"]);
const result = await client.callTool({ name: "hose_cli", arguments: { command: "version --json" } });
assert.ok(!result.isError, JSON.stringify(result));
const text = (result.content as Array<{ type: string; text?: string }>).find(c => c.type === "text")?.text;
assert.equal(JSON.parse(text || "").version, JSON.parse(version.stdout).version);
const invalid = await client.callTool({ name: "hose_cli", arguments: { command: "bill list --limitt 5 --json" } });
assert.equal(invalid.isError, true);
assert.match(JSON.stringify(invalid.content), /未知选项/);
console.log(plugin ? "Plugin bundle version and MCP stdio verified with its Bun runtime." : "Binary version and MCP stdio verified without Bun or source files.");
} catch (error) {
throw new Error(`Binary smoke failed: ${error instanceof Error ? error.message : error}\n${stderr}`);
} finally {
clearTimeout(deadline);
await client.close();
rmSync(cwd, { recursive: true, force: true });
}