import { describe, expect, test } from "bun:test"; import { readFileSync, readdirSync } from "node:fs"; import { join } from "node:path"; import { HELP_REGISTRY } from "./help.js"; import { KNOWN_AREAS } from "./runner.js"; import { SEAL_MUTATING_TOOLS, SEAL_READ_ONLY_TOOLS, sealTools } from "../domains/seal/tools.js"; import { isReadOnlyCliCommand, listReadOnlyCommands } from "./read-only.js"; describe("CLI read-only classification", () => { test("the tool registry is classified exhaustively and without overlap", () => { const all = sealTools.map((tool) => tool.name); const classified = [...SEAL_READ_ONLY_TOOLS, ...SEAL_MUTATING_TOOLS]; // A newly added tool must fail here rather than silently defaulting into either bucket. expect(classified.slice().sort()).toEqual(all.slice().sort()); for (const name of SEAL_READ_ONLY_TOOLS) { expect({ name, alsoMutating: SEAL_MUTATING_TOOLS.has(name) }).toEqual({ name, alsoMutating: false }); } }); // Actions the dispatcher accepts that carry no help-registry row, so they are documented // nowhere else. Kept explicit: anything not a registry entry and not listed here fails below. const ACTIONS_WITHOUT_REGISTRY_ENTRY = new Set([ "approval-runs search", "auth diagnose", "auth status", "auth test", "auth doctor", "doctor auth" ]); test("every allow-listed command exists in the help registry", () => { const byArea = new Map( HELP_REGISTRY.map((area) => [ area.area, area.commands.flatMap((c) => [c.name, ...(c.aliases ?? [])]) ]) ); for (const { area, action } of listReadOnlyCommands()) { if (ACTIONS_WITHOUT_REGISTRY_ENTRY.has(`${area} ${action}`)) continue; expect({ area, dispatchable: KNOWN_AREAS.has(area) }).toEqual({ area, dispatchable: true }); expect({ area, action, known: byArea.get(area)?.includes(action) }).toEqual({ area, action, known: true }); } }); test("allows read-only commands, including through area aliases and positional arguments", () => { for (const command of [ ["corps", "list"], ["corp", "list"], ["corp", "get", "corp-a"], ["approval-runs", "get", "01a09dac-c688-767b-8eb4-37b945095585"], ["approval-runs", "list", "--corp", "corp-a"], ["deploy", "hosecloud", "status", "--corp", "corp-a"], ["preferences", "get"], ["doctor", "tenant", "--corp", "corp-a"], ["openapi"], ["version"], ["mcp-config"] ]) { expect({ command, readOnly: isReadOnlyCliCommand(command) }).toEqual({ command, readOnly: true }); } }); test("refuses mutating, unclassified, and server-lifetime commands", () => { for (const command of [ ["corps", "mark", "corp-a", "--env", "production"], ["corps", "switch", "corp-a"], ["corps", "extract-keys", "corp-a"], ["corps", "sync-test", "corp-a"], ["rules", "push"], ["rules", "clear"], ["simulation", "create", "--mode", "hosecloud"], ["deploy", "hosecloud", "sync", "sign-key"], ["deploy", "hosecloud", "configure"], ["webhook", "delete", "hook-1"], ["webhook", "replay"], ["users", "reset-password", "corp-a:u1"], ["users", "create"], ["config", "test-dir", "corp-a"], ["skill", "install"], ["service", "start"], ["service", "run"], ["mcp"], ["http"], ["update"], ["stop"], ["auth", "keys"], ["analytics", "analyze", "record-1"], ["tool", "seal_approval_rule_create", "--json", "{}"], ["tool", "seal_webhook_delete", "--json", "{}"], ["tool", "seal_insight_record_analyze", "--json", "{}"], ["tool"], ["totally-unknown", "list"], [] ]) { expect({ command, readOnly: isReadOnlyCliCommand(command) }).toEqual({ command, readOnly: false }); } }); test("routes `seal-use tool` through the tool registry's own classification", () => { for (const command of [ ["tool", "seal_approval_search", "--json", "{}"], ["tool", "seal_approval_rules_list", "--corp", "corp-a"], ["tool", "seal_whoami"], ["tool", "seal_approval_run_flow_get", "--json", '{"sn":"B26002352"}'] ]) { expect({ command, readOnly: isReadOnlyCliCommand(command) }).toEqual({ command, readOnly: true }); } }); test("matches whole tokens, so a read prefix cannot smuggle a mutation", () => { for (const command of [ ["corps", "listing"], ["corps", "list-extra"], ["approval-runs", "getter"], ["rules", "listp"] ]) { expect({ command, readOnly: isReadOnlyCliCommand(command) }).toEqual({ command, readOnly: false }); } }); test("a bare allow-listed command does not cover its subcommands", () => { expect(isReadOnlyCliCommand(["doctor"])).toBe(true); expect(isReadOnlyCliCommand(["doctor", "guide"])).toBe(true); expect(isReadOnlyCliCommand(["doctor", "unknown-subcommand"])).toBe(false); // `openapi` is allow-listed as the bare form only. expect(isReadOnlyCliCommand(["openapi", "extra"])).toBe(false); }); test("refuses command lines that cannot be parsed", () => { expect(isReadOnlyCliCommand(["--json"])).toBe(false); }); }); // The two classifications are maintained by hand, in different files: the CLI allow-list here and // SEAL_READ_ONLY_TOOLS in the tool registry. Nothing stopped them from disagreeing, and they did — // `analytics analyze` was allow-listed while its tool POSTs, and three read-only approval-runs verbs // were simply missing. This derives one from the other and fails on any new disagreement. describe("CLI allow-list agrees with the tool registry", () => { // `deploy` only accepts `deploy hosecloud `; the short `deploy ` form throws // "未知的发布目标系统" at runtime, so these pairs are unreachable rather than refused. const UNREACHABLE_CLI_FORMS = new Set(["deploy requirements", "deploy list", "deploy status"]); test("every CLI verb dispatching to a read-only tool is itself read-only", () => { const files = readdirSync(join(import.meta.dir, "commands")).filter( (f) => f.endsWith(".ts") && !f.endsWith(".test.ts") ); const pairs: Array<{ area: string; action: string; tool: string }> = []; for (const file of files) { const area = file.replace(/\.ts$/, ""); const source = readFileSync(join(import.meta.dir, "commands", file), "utf8"); const pattern = /action === "([a-z0-9-]+)"[\s\S]{0,600}?runTool\(\s*"(seal_[a-z_0-9]+)"/g; let match; while ((match = pattern.exec(source))) { pairs.push({ area, action: match[1]!, tool: match[2]! }); } } // If the dispatch shape changes this silently finds nothing, which would make the test vacuous. expect(pairs.length).toBeGreaterThan(50); const disagreements = pairs .filter((pair) => SEAL_READ_ONLY_TOOLS.has(pair.tool)) .filter((pair) => !UNREACHABLE_CLI_FORMS.has(`${pair.area} ${pair.action}`)) .filter((pair) => !isReadOnlyCliCommand([pair.area, pair.action])) .map((pair) => `${pair.area} ${pair.action} -> ${pair.tool}`); expect(disagreements).toEqual([]); }); });