import { expect, test } from "bun:test"; import { existsSync, mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; test("source and freshly packaged plugin preserve offline review and MCP contracts without a prebuilt dist", () => { const root = resolve(import.meta.dir, "../.."); const temp = mkdtempSync(join(tmpdir(), "hose review plugin ")); try { const packaged = Bun.spawnSync([ process.execPath, join(root, "scripts/build/package-plugin.mjs"), `--out-dir=${join(temp, "artifacts")}`, ], { cwd: temp, env: { ...process.env, BUN_BIN: process.execPath }, timeout: 30_000 }); expect(packaged.exitCode, packaged.stderr.toString()).toBe(0); const version = JSON.parse(readFileSync(join(root, "package.json"), "utf8")).version; const archive = join(temp, "artifacts", `hose-use-plugin-v${version}.tar.gz`); const pluginRoot = join(temp, "unpacked"); mkdirSync(pluginRoot); const extracted = Bun.spawnSync(["tar", "-xzf", archive, "-C", pluginRoot]); expect(extracted.exitCode, extracted.stderr.toString()).toBe(0); expect(readdirSync(pluginRoot).sort()).toEqual([".mcp.json", ".zcode-plugin", "dist", "skills"]); const manifest = JSON.parse(readFileSync(join(pluginRoot, ".zcode-plugin/plugin.json"), "utf8")); expect(manifest.version).toBe(version); expect(manifest.skills).toBe("skills"); const mcp = JSON.parse(readFileSync(join(pluginRoot, manifest.mcpServers), "utf8")).mcpServers["hose-use"]; expect(mcp.command).toBe("bun"); const bundle = mcp.args[0].replace("${CLAUDE_PLUGIN_ROOT}", pluginRoot); expect(existsSync(bundle)).toBe(true); for (const file of readdirSync(join(root, "skills"), { recursive: true })) { expect(existsSync(join(pluginRoot, "skills", file))).toBe(true); } const protocol = Bun.spawnSync([ process.execPath, join(root, "scripts/build/verify-binary.ts"), bundle, "--plugin", ], { cwd: temp, timeout: 30_000 }); expect(protocol.exitCode, protocol.stderr.toString()).toBe(0); const enterprises = join(temp, "enterprises"); mkdirSync(enterprises); writeFileSync(join(enterprises, "smoke.json"), JSON.stringify({ id: "smoke", name: "离线测试企业", env: "test", credentials: { accessToken: "synthetic-offline-token", corpId: "smoke", staffId: "staff" }, })); const env = { ...process.env, HOSE_ENTERPRISES_DIR: enterprises, HOSE_CONFIG_FILE: join(temp, "config.json"), HOSE_ACCOUNTS_FILE: join(temp, "accounts.json"), HOSE_APPROVAL_DB: ":memory:", }; const fixtures = [ { name: "collections", type: "code", code: 'Map m = new java.util.HashMap(); m.put("x", 7); m.size();', value: 1 }, { name: "final-expression", type: "code", code: 'Map m = new HashMap(); m.put("x", 7); m;', value: { x: 7 } }, { name: "javascript", type: "code-js2", code: "return 7;", value: 7 }, { name: "unsupported", type: "code", code: "new java.util.Date();", error: "UnsupportedMvelSyntax" }, ]; for (const fixture of fixtures) { const file = join(temp, `${fixture.name}.script`); writeFileSync(file, fixture.code); for (const entry of [[join(root, "src/cli.ts")], [bundle, "--hose-use-cli"]]) { const result = Bun.spawnSync([ process.execPath, ...entry, "workflow", "eval-script", "--file", file, "--type", fixture.type, "--corp", "smoke", "--stateless", "--json", ], { cwd: pluginRoot, env, timeout: 30000 }); expect(result.exitCode).toBe(fixture.error ? 1 : 0); const report = JSON.parse(result.stdout.toString()); expect(report.success).toBe(!fixture.error); if (fixture.error) expect(report.error.name).toBe(fixture.error); else expect(report.returnValue).toEqual(fixture.value); } } } finally { rmSync(temp, { recursive: true, force: true }); } }, 60_000);