|
2 | 2 | // registered Commander commands. Keep those user-facing descriptions aligned. |
3 | 3 | import { Command } from "commander"; |
4 | 4 | import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { cliCommandCatalog } from "../command-catalog.js"; |
| 6 | +import { isReservedNonPluginCommandRoot } from "../command-registration-policy.js"; |
5 | 7 | import { collectShellCompletionCommandTree } from "../completion-command-tree.js"; |
6 | 8 | import { getCoreCliCommandNames, registerCoreCliByName } from "./command-registry-core.js"; |
7 | 9 | import { createProgramContext } from "./context.js"; |
8 | 10 | import { getCoreCliCommandDescriptors } from "./core-command-descriptors.js"; |
9 | | -import { registerSubCliByName } from "./register.subclis.js"; |
| 11 | +import { registerSubCliByName, registerSubCliCommands } from "./register.subclis.js"; |
10 | 12 | import { getSubCliEntries } from "./subcli-descriptors.js"; |
11 | 13 |
|
| 14 | +const RESERVED_CATALOG_ROOTS = { |
| 15 | + tool: "reserved so plugin registration cannot claim this unregistered root", |
| 16 | + tools: "reserved so plugin registration cannot claim this unregistered root", |
| 17 | +} as const; |
| 18 | + |
| 19 | +const PLUGIN_CATALOG_PATHS = { |
| 20 | + "memory search": "registered and covered by the memory-core plugin", |
| 21 | + "memory status": "registered and covered by the memory-core plugin", |
| 22 | +} as const; |
| 23 | + |
12 | 24 | const JSON_NOT_APPLICABLE = { |
13 | 25 | namespaces: { |
14 | 26 | reason: "command group only; reporting subcommands declare JSON output individually", |
@@ -242,6 +254,16 @@ function supportsJsonOutput(path: string, command: Command): boolean { |
242 | 254 | ); |
243 | 255 | } |
244 | 256 |
|
| 257 | +function collectRegisteredCommandPaths(...programs: Command[]): Set<string> { |
| 258 | + return new Set( |
| 259 | + programs.flatMap((program) => |
| 260 | + collectShellCompletionCommandTree(program).descendants.flatMap((context) => |
| 261 | + context.pathVariants.map((path) => path.join(" ")), |
| 262 | + ), |
| 263 | + ), |
| 264 | + ); |
| 265 | +} |
| 266 | + |
245 | 267 | describe("root command descriptions", () => { |
246 | 268 | beforeEach(() => { |
247 | 269 | vi.stubEnv("OPENCLAW_ENABLE_PRIVATE_QA_CLI", ""); |
@@ -285,6 +307,56 @@ describe("root command descriptions", () => { |
285 | 307 | expect(mismatches, "root help vs registered command description drift").toEqual([]); |
286 | 308 | }); |
287 | 309 |
|
| 310 | + it("keeps startup policy catalog paths registered or explicitly reserved", async () => { |
| 311 | + const program = await registerAllBuiltInCommands(); |
| 312 | + |
| 313 | + // Private QA is a lazy source-checkout command. Its root placeholder proves |
| 314 | + // registration without importing the private build omitted from normal dist. |
| 315 | + vi.stubEnv("OPENCLAW_ENABLE_PRIVATE_QA_CLI", "1"); |
| 316 | + const lazyProgram = new Command().name("openclaw"); |
| 317 | + registerSubCliCommands(lazyProgram, ["node", "openclaw", "--help"]); |
| 318 | + |
| 319 | + const registeredPaths = collectRegisteredCommandPaths(program, lazyProgram); |
| 320 | + const catalogPaths = new Set(cliCommandCatalog.map((entry) => entry.commandPath.join(" "))); |
| 321 | + const reservedPaths = new Set(Object.keys(RESERVED_CATALOG_ROOTS)); |
| 322 | + const pluginPaths = new Set(Object.keys(PLUGIN_CATALOG_PATHS)); |
| 323 | + |
| 324 | + expect( |
| 325 | + Object.entries({ ...RESERVED_CATALOG_ROOTS, ...PLUGIN_CATALOG_PATHS }).filter( |
| 326 | + ([, reason]) => reason.trim().length === 0, |
| 327 | + ), |
| 328 | + "every catalog exception must document why core has no command", |
| 329 | + ).toEqual([]); |
| 330 | + |
| 331 | + const missing = [...catalogPaths].filter( |
| 332 | + (path) => !registeredPaths.has(path) && !reservedPaths.has(path) && !pluginPaths.has(path), |
| 333 | + ); |
| 334 | + expect(missing, "catalog entries with no registered command or reserved-root decision").toEqual( |
| 335 | + [], |
| 336 | + ); |
| 337 | + |
| 338 | + const staleReservedRoots = [...reservedPaths].filter( |
| 339 | + (path) => !catalogPaths.has(path) || !isReservedNonPluginCommandRoot(path), |
| 340 | + ); |
| 341 | + expect( |
| 342 | + staleReservedRoots, |
| 343 | + "reserved catalog roots must remain cataloged and blocked from plugin registration", |
| 344 | + ).toEqual([]); |
| 345 | + |
| 346 | + const stalePluginPaths = [...pluginPaths].filter( |
| 347 | + (path) => !catalogPaths.has(path) || registeredPaths.has(path), |
| 348 | + ); |
| 349 | + expect(stalePluginPaths, "plugin catalog paths must remain plugin-owned").toEqual([]); |
| 350 | + |
| 351 | + const reservedRootsThatNowRegister = [...reservedPaths].filter((path) => |
| 352 | + registeredPaths.has(path), |
| 353 | + ); |
| 354 | + expect( |
| 355 | + reservedRootsThatNowRegister, |
| 356 | + "registered commands must leave the reserved-root exception list", |
| 357 | + ).toEqual([]); |
| 358 | + }); |
| 359 | + |
288 | 360 | it("classifies every built-in command as JSON output or explicitly not applicable", async () => { |
289 | 361 | const program = await registerAllBuiltInCommands(); |
290 | 362 | const contexts = collectShellCompletionCommandTree(program).descendants; |
|
0 commit comments