import { describe, expect, it, spyOn } from "bun:test"; import { Command, type CommandEntry, Flags, run } from "@oh-my-pi/pi-utils/cli"; class GoodCommand extends Command { static description = "prints good things"; static flags = { verbose: Flags.boolean({ description: "be loud" }), }; async run(): Promise {} } describe("run() per-command help", () => { // Contract: `omp --help` must load only the requested command module. // Loading the whole table would let any unrelated command whose import // hangs or crashes take down every per-command help invocation. it("loads only the requested command", async () => { let brokenLoads = 0; const commands: CommandEntry[] = [ { name: "good", load: async () => GoodCommand }, { name: "broken", load: async () => { brokenLoads++; throw new Error("import-time crash"); }, }, ]; const writes: string[] = []; const stdoutSpy = spyOn(process.stdout, "write").mockImplementation(chunk => { writes.push(String(chunk)); return true; }); try { await run({ bin: "omp", version: "0.0.0", argv: ["good", "--help"], commands }); } finally { stdoutSpy.mockRestore(); } expect(brokenLoads).toBe(0); expect(writes.join("")).toContain("prints good things"); expect(writes.join("")).toContain("--verbose"); }); });