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 }); } }); test("supports update aliases (upgrade, up) with notice in stderr and exit code 0", async () => { const upgradeResult = await runCli(["upgrade", "0.3.3"]); expect(upgradeResult.exitCode).toBe(0); expect(upgradeResult.isError).toBe(false); expect(upgradeResult.stderr).toContain("Notice: 'upgrade' is an alias for 'seal-use update'"); expect(JSON.parse(upgradeResult.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" }); const upResult = await runCli(["up", "0.3.3"]); expect(upResult.exitCode).toBe(0); expect(upResult.isError).toBe(false); expect(upResult.stderr).toContain("Notice: 'up' is an alias for 'seal-use update'"); expect(JSON.parse(upResult.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" }); }); test("supports case-insensitive update invocations with original command in notice", async () => { const mixedResult = await runCli(["Update", "0.3.3"]); expect(mixedResult.exitCode).toBe(0); expect(mixedResult.stderr).toContain("Notice: 'Update' is an alias for 'seal-use update'"); expect(JSON.parse(mixedResult.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" }); const upperResult = await runCli(["UPGRADE", "0.3.3"]); expect(upperResult.exitCode).toBe(0); expect(upperResult.stderr).toContain("Notice: 'UPGRADE' is an alias for 'seal-use update'"); expect(JSON.parse(upperResult.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" }); }); test("canonical update command does not emit alias notice", async () => { const result = await runCli(["update", "0.3.3"]); expect(result.exitCode).toBe(0); expect(result.stderr).not.toContain("is an alias for 'seal-use update'"); expect(JSON.parse(result.stdout)).toMatchObject({ updated: false, targetVersion: "0.3.3" }); }); });