import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, test } from "bun:test"; import { runCli } from "./runner.js"; describe("CLI runner", () => { test("returns version output without touching the process lifecycle", async () => { const result = await runCli(["version"]); expect(result).toMatchObject({ stderr: "", isError: false, exitCode: 0 }); expect(JSON.parse(result.stdout)).toMatchObject({ name: "seal-use" }); }); test("returns diagnostics and a non-zero exit code for invalid commands", async () => { const result = await runCli(["not-a-command"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(2); expect(result.stderr).toContain("Unknown command: not-a-command"); }); test("returns a semantic usage exit code for invalid JSON", async () => { const result = await runCli(["version", "--json", "not-json"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(2); expect(result.stderr).toContain("Invalid JSON:"); }); test("returns usage exit code 2 for unknown subcommands across standalone domains", async () => { const result = await runCli(["skill", "not-a-subcommand"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(2); expect(result.stderr).toContain("Unknown skill command: not-a-subcommand"); }); test("returns usage exit code 2 for unknown subcommands with an active enterprise", async () => { const tempDir = mkdtempSync(join(tmpdir(), "seal-use-runner-test-")); const originalEnv = process.env.SEAL_USE_ENTERPRISES_DIR; try { process.env.SEAL_USE_ENTERPRISES_DIR = tempDir; writeFileSync( join(tempDir, "test.json"), JSON.stringify({ id: "test", name: "Test Corp", source: { type: "seal", token: "dummy-token", sealUrl: "https://test.sealai.cc" }, seal: { url: "https://test.sealai.cc" } }) ); const result = await runCli(["contacts", "not-a-subcommand", "--corp", "test"]); expect(result.isError).toBe(true); expect(result.exitCode).toBe(2); expect(result.stderr).toContain("Unknown contacts command: not-a-subcommand"); } finally { if (originalEnv === undefined) delete process.env.SEAL_USE_ENTERPRISES_DIR; else process.env.SEAL_USE_ENTERPRISES_DIR = originalEnv; rmSync(tempDir, { recursive: true, force: true }); } }); });