import { readdirSync, readFileSync } from "node:fs"; import { dirname, join, relative, sep } from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; import type { CommandDef } from "citty"; import { stripVTControlCharacters } from "node:util"; import { aliasesAsTyped, GROUPS, HIDDEN_COMMANDS, showUsage } from "./help.js"; import { EXAMPLE_LOADERS, examplesKey } from "./helpExamples.js"; const srcDir = dirname(fileURLToPath(import.meta.url)); function registeredCommands(): string[] { const source = readFileSync(join(srcDir, "cli.ts"), "utf8"); const block = source.match(/const commandLoaders = \{([\s\S]*?)\n\};/)?.[1] ?? ""; const names = [...block.matchAll(/^ {2}"?([\w-]+)"?:/gm)].map(([, name]) => name!); expect(names.length).toBeGreaterThan(40); return names; } // Registered commands whose files carry no examples yet; help prints their options only. const NO_EXAMPLES = new Set(["events"]); function commandSources(dir: string): string[] { return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { const path = join(dir, entry.name); if (entry.isDirectory()) return commandSources(path); return entry.name.endsWith(".ts") && !entry.name.endsWith(".test.ts") ? [path] : []; }); } describe("root help", () => { const listed = GROUPS.flatMap((group) => group.commands.map(([name]) => name)); // fails without the fix: media-use, play, layout, usage and figma were registered but unlisted. it("lists every registered command except the hidden ones", () => { const registered = registeredCommands(); const missing = registered.filter((n) => !listed.includes(n) && !HIDDEN_COMMANDS.includes(n)); expect(missing).toEqual([]); expect(listed.filter((n) => !registered.includes(n))).toEqual([]); expect(new Set(listed).size).toBe(listed.length); for (const hidden of HIDDEN_COMMANDS) expect(listed).not.toContain(hidden); }); it("files billing and usage under Account and marks the deprecated commands", () => { const account = GROUPS.find((g) => g.title === "Account")?.commands.map(([n]) => n); expect(account).toEqual(expect.arrayContaining(["auth", "billing", "usage"])); for (const name of ["validate", "inspect", "layout"]) { const description = GROUPS.flatMap((g) => g.commands).find(([n]) => n === name)?.[1]; expect(description, name).toMatch(/^Deprecated, use check\./); } }); it("says nothing about GSAP skills", async () => { let text = ""; await showUsage({ meta: { name: "hyperframes" } }, undefined, (t) => (text += t)); expect(text).not.toMatch(/GSAP/); expect(text).toContain("media-use"); }); }); describe("command examples", () => { // fails without the fix: examples were imported from a path built at run time, which the // bundle does not contain, so every command's --help lost its examples. it("has a loader for every registered command that has examples", () => { const missing = registeredCommands().filter( (name) => !NO_EXAMPLES.has(name) && !(name in EXAMPLE_LOADERS), ); expect(missing).toEqual([]); }); it("has a loader for every command file that exports examples", () => { const helpSource = readFileSync(join(srcDir, "helpExamples.ts"), "utf8"); const exporting = commandSources(join(srcDir, "commands")) .filter((path) => /^export const examples\b/m.test(readFileSync(path, "utf8"))) .map((path) => `./${relative(srcDir, path).split(sep).join("/").replace(/\.ts$/, ".js")}`); expect(exporting.length).toBeGreaterThan(40); expect(exporting.filter((path) => !helpSource.includes(`import("${path}")`))).toEqual([]); }); it.each(Object.keys(EXAMPLE_LOADERS))( "loads the examples of %s", async (key) => { const examples = await EXAMPLE_LOADERS[key]!(); expect(examples.length).toBeGreaterThan(0); for (const [comment, command] of examples) { expect(comment.trim()).not.toBe(""); expect(command).toMatch(/(^|[|=\s])hyperframes /); } }, 60_000, ); it("files a subcommand's examples under its parent, never the top-level namesake", () => { expect(examplesKey("render", "hyperframes")).toBe("render"); expect(examplesKey("render", "cloud")).toBe("cloud render"); expect(examplesKey("render", "lambda")).toBe("lambda render"); expect("lambda render" in EXAMPLE_LOADERS).toBe(false); }); it("prints a command's examples after its options", async () => { let text = ""; const { meta, args } = (await import("./commands/diff.js")).default; await showUsage({ meta, args }, { meta: { name: "hyperframes" } }, (t) => (text += t)); text = stripVTControlCharacters(text); expect(text).toContain("--at"); expect(text).toContain("Examples:"); expect(text).toContain("hyperframes diff original.mp4 . --at 0.5,2.4,3.1"); }); it("names the current skills, and the names skills update takes", async () => { let text = ""; const skills = (await import("./commands/skills.js")).default; await showUsage(skills, { meta: { name: "hyperframes" } }, (t) => (text += t)); text = stripVTControlCharacters(text); expect(text).not.toMatch(/GSAP/); for (const name of ["hyperframes-registry", "media-use", "figma", "remotion-to-hyperframes"]) { expect(text).toContain(name); } let update = ""; const subCommands = skills.subCommands; const resolved = typeof subCommands === "function" ? await subCommands() : await subCommands; const entry = resolved?.["update"]; const updateCommand: CommandDef = typeof entry === "function" ? await entry() : await entry!; await showUsage(updateCommand, skills, (t) => (update += t)); expect(stripVTControlCharacters(update)).toContain("skills update [OPTIONS] [SKILLS]"); }); }); describe("aliases in help", () => { // fails without the fix: citty printed `-from`, which its parser reads as -f -r -o -m. it("prints a longer alias with two dashes and a one-letter alias with one", async () => { let text = ""; const command: CommandDef = { meta: { name: "cuts" }, args: { start: { type: "string", alias: ["from"], description: "First second" }, output: { type: "string", alias: ["o", "out"], description: "Where to write" }, open: { type: "boolean", alias: "show", default: true, description: "Open it" }, file: { type: "positional", required: false, description: "A file" }, }, }; await showUsage(command, { meta: { name: "measure" } }, (t) => (text += t)); text = stripVTControlCharacters(text); expect(text).toContain("--from, --start="); expect(text).toContain("-o, --out, --output="); expect(text).not.toMatch(/(^|[\s,])-(from|out|show)\b/m); expect(text).not.toContain("--no--"); }); it("leaves the args it is given untouched", () => { const args = { start: { type: "string" as const, alias: ["from"] } }; const typed = aliasesAsTyped(args); expect(typed["start"]).toMatchObject({ alias: ["-from"] }); expect(args.start.alias).toEqual(["from"]); }); });