import { afterEach, describe, expect, test } from "bun:test"; import { spawnSync } from "node:child_process"; import { chmodSync, copyFileSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { VERSION } from "./version.js"; const CLI_PATH = join(import.meta.dirname, "cli.ts"); const tempDirs: string[] = []; function runCli(args: string[], env: Record = {}) { return spawnSync(process.execPath, [CLI_PATH, ...args], { encoding: "utf-8", env: { ...process.env, ...env } }); } afterEach(() => { for (const dir of tempDirs.splice(0)) { rmSync(dir, { recursive: true, force: true }); } }); describe("CLI version & help handling", () => { test.each([ ["version"], ["v"], ["-v"], ["-version"], ["--version"], ["-V"], ["--v"], ["--V"] ])("version command alias '%s' returns version JSON", (flag) => { const result = runCli([flag]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed.name).toBe("seal-use"); expect(parsed.version).toBe(VERSION); expect(parsed).toHaveProperty("channel"); expect(parsed).toHaveProperty("isRelease"); }); test("version output includes structured dev channel and git information", () => { const result = runCli(["v"]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed.name).toBe("seal-use"); expect(typeof parsed.channel).toBe("string"); expect(typeof parsed.isRelease).toBe("boolean"); if (!parsed.isRelease) { expect(parsed.channel).toBe("dev"); if (parsed.git) { expect(parsed.git).toHaveProperty("baseVersion"); expect(parsed.git).toHaveProperty("commitsAhead"); expect(parsed.git).toHaveProperty("commit"); } } }); test("help command prints standard help banner", () => { const result = runCli(["help"]); expect(result.status).toBe(0); expect(result.stdout).toContain("seal-use CLI"); expect(result.stdout).toContain("Usage:"); expect(result.stdout).toContain("seal-use mcp"); expect(result.stdout).toContain("seal-use probe []"); expect(result.stdout).toContain("seal-use users search "); expect(result.stdout).toContain("seal-use users delegation "); expect(result.stdout).toContain("seal-use webhook get|find "); expect(result.stdout).toContain("seal-use webhook create --name --result-url "); expect(result.stdout).toContain("seal-use corps add-link "); expect(result.stdout).toContain("seal-use auth link [--note ] [--expire ] [--corp ]"); expect(result.stdout).toContain("[--secret |--rotate-secret]"); }); test("invalid command displays unknown command error with help hint", () => { const result = runCli(["invalid-command"]); expect(result.status).toBe(2); expect(result.stderr).toContain("Unknown command: invalid-command. Run 'seal-use help' for available commands."); }); test("requires --corp in the default stateless mode", () => { const env = makeConfigEnv(); const result = runCli(["corps", "current"], env); // 13: enterprise-selection failure — the command line is well-formed, the // local config/argument is incomplete. expect(result.status).toBe(13); expect(result.stderr).toContain("Stateless mode is enabled"); expect(result.stderr).toContain("--corp "); }); test("allows an explicit corp in stateless mode", () => { const env = makeConfigEnv(); const result = runCli(["corps", "current", "--corp", "corp-a"], env); expect(result.status).toBe(0); expect(JSON.parse(result.stdout)).toMatchObject({ id: "corp-a" }); }); test("keeps the documents domain reachable in stateless mode", () => { const env = makeConfigEnv(); const result = runCli(["documents", "get", "--corp", "corp-a"], env); expect(result.status).toBe(2); expect(result.stderr).toContain("Usage: seal-use documents get "); }); test("requires explicit confirmation on documents delete", () => { const env = makeConfigEnv(); const result = runCli(["documents", "delete", "doc-123", "--corp", "corp-a"], env); expect(result.status).toBe(10); expect(result.stderr).toContain("documents delete requires --yes or --confirm"); expect(result.stderr).toContain("--dry-run"); }); test("does not hijack --version option when passed to subcommands like rules", () => { const env = makeConfigEnv(); const result = runCli(["rules", "get", "--version", "latest", "--corp", "corp-a"], env); expect(result.status).toBe(2); expect(result.stderr).toContain("Usage: seal-use rules get "); }); test("can enable stateful mode and select a default corp", () => { const env = makeConfigEnv(); const mode = runCli(["config", "mode", "stateful", "--corp", "corp-a"], env); const current = runCli(["corps", "current"], env); expect(mode.status).toBe(0); expect(JSON.parse(mode.stdout)).toMatchObject({ mode: "stateful", defaultCorpId: "corp-a" }); expect(current.status).toBe(0); expect(JSON.parse(current.stdout)).toMatchObject({ id: "corp-a" }); }); test("removes the default when switching back to stateless mode", () => { const env = makeConfigEnv(); runCli(["config", "mode", "stateful", "--corp", "corp-a"], env); const mode = runCli(["config", "mode", "stateless"], env); const current = runCli(["corps", "current"], env); expect(mode.status).toBe(0); expect(JSON.parse(mode.stdout)).toMatchObject({ mode: "stateless" }); expect(JSON.parse(mode.stdout)).not.toHaveProperty("defaultCorpId"); expect(current.status).toBe(13); expect(current.stderr).toContain("Stateless mode is enabled"); }); test("update command runs in source mode when executed from bun", () => { // Exercise the CLI's source-update control flow without fetching Git refs, // installing packages, or running hooks in the developer's working tree. // Native shims work with spawnSync on Windows as well as POSIX (no shell). const dir = mkdtempSync(join(tmpdir(), "cli-source-update-")); tempDirs.push(dir); const source = join(dir, "tool-shim.ts"); const suffix = process.platform === "win32" ? ".exe" : ""; const executable = join(dir, `shim${suffix}`); const log = join(dir, "commands.jsonl"); const realGit = Bun.which("git"); expect(realGit).not.toBeNull(); writeFileSync(source, ` import { basename } from "node:path"; import { appendFileSync } from "node:fs"; import { spawnSync } from "node:child_process"; const command = basename(process.execPath).replace(/\\.exe$/, ""); const args = process.argv.slice(2); if (command === "git" && !args.includes("pull")) { const result = spawnSync(${JSON.stringify(realGit)}, args, { encoding: "utf8" }); if (result.stdout) process.stdout.write(result.stdout); if (result.stderr) process.stderr.write(result.stderr); process.exitCode = result.status ?? 1; } else { appendFileSync(${JSON.stringify(log)}, JSON.stringify({ command, args }) + "\\n"); } `); const build = spawnSync(process.execPath, ["build", "--compile", source, "--outfile", executable], { encoding: "utf8" }); expect(build.status).toBe(0); for (const name of ["git", "bun"]) { const path = join(dir, `${name}${suffix}`); copyFileSync(executable, path); chmodSync(path, 0o755); } const result = runCli(["update"], { PATH: `${dir}${process.platform === "win32" ? ";" : ":"}${process.env.PATH ?? ""}` }); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed.updated).toBe(true); expect(parsed.mode).toBe("source"); expect(parsed.version).toBe(VERSION); const calls = readFileSync(log, "utf8").trim().split("\n").map(line => JSON.parse(line)); expect(calls).toEqual([ { command: "git", args: ["-C", join(import.meta.dirname, ".."), "pull", "--ff-only"] }, { command: "bun", args: ["install"] } ]); }, 20000); test("migrate command without target flags shows usage error", () => { const result = runCli(["migrate"]); expect(result.status).toBe(2); expect(result.stderr).toContain("Usage: seal-use migrate --system | --user"); }); test("update help shows system and user migration options", () => { const result = runCli(["update", "--help"]); expect(result.status).toBe(0); expect(result.stdout).toContain("--system"); expect(result.stdout).toContain("--user"); }); test("mcp-config outputs valid mcp configuration json", () => { const result = runCli(["mcp-config"]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed.mcpServers["seal-use"]).toMatchObject({ command: "seal-use", args: ["mcp"] }); }); test("skill dump outputs embedded skill markdown", () => { const result = runCli(["skill", "dump"]); expect(result.status).toBe(0); expect(result.stdout).toContain("name: seal-use"); expect(result.stdout).toContain("# Seal Use"); }); test("skill status outputs installation directories", () => { const result = runCli(["skill", "status"]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed.global).toHaveProperty("dir"); expect(parsed.local).toHaveProperty("dir"); }); test("skill install writes files to specified directory", () => { const tmp = mkdtempSync(join(tmpdir(), "cli-skill-install-")); tempDirs.push(tmp); const result = runCli(["skill", "install", "--dir", tmp]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed.installed).toBe(true); expect(parsed.targetDir).toBe(tmp); expect(parsed.fileCount).toBeGreaterThan(5); }); test("update check subcommand outputs structured check payload", () => { const result = runCli(["update", "check"]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed).toHaveProperty("currentVersion"); expect(parsed).toHaveProperty("latestVersion"); expect(parsed).toHaveProperty("updateAvailable"); expect(parsed).toHaveProperty("behindCount"); expect(parsed).toHaveProperty("pendingReleases"); expect(Array.isArray(parsed.pendingReleases)).toBe(true); }, 20000); test("changelog command outputs release history list", () => { const result = runCli(["changelog", "--limit", "5"]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed.currentVersion).toBe(VERSION); expect(Array.isArray(parsed.releases)).toBe(true); }, 20000); test("rejects downgrade to older version when target version is lower", () => { const result = runCli(["update", "0.3.3"]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed.updated).toBe(false); expect(parsed.currentVersion).toBe(VERSION); expect(parsed.targetVersion).toBe("0.3.3"); expect(parsed.message).toContain("Cannot downgrade seal-use"); }); test("treats latest keyword as latest release without downgrade error", () => { const result = runCli(["update", "latest", "--check"]); expect(result.status).toBe(0); const parsed = JSON.parse(result.stdout.trim()); expect(parsed).toHaveProperty("latestVersion"); expect(parsed.updated).toBe(false); }, 20000); }); function makeConfigEnv(): Record { const root = mkdtempSync(join(tmpdir(), "seal-home-cli-")); tempDirs.push(root); const enterprises = join(root, "enterprises"); mkdirSync(enterprises, { recursive: true }); writeFileSync(join(enterprises, "corp-a.json"), JSON.stringify({ id: "corp-a", name: "Corp A", source: { type: "seal", token: "seal-token", sealUrl: "https://corp-a.seal.test" } })); return { SEAL_HOME_ENTERPRISES_DIR: enterprises, SEAL_HOME_CONFIG_FILE: join(root, "config.json") }; }