import test from "node:test"; import assert from "node:assert/strict"; import { BUILTINS, commands, globalCommands, } from "../herdr/herdr-composer/mods/herdr-composer/catalog.js"; import { GROUPS, GROUP_USER, GROUP_PLUGIN, } from "../herdr/herdr-composer/mods/herdr-composer/suggest.js"; const HOME = "C:/Users/Test"; /** * A fake machine: `files` maps a path to its text. `exec` answers the POSIX * listing of catalog.js (powershell is "not installed"), `readText` reads. */ function machine(files, { listError = false } = {}) { const exec = async (command, args) => { if (command !== "sh") throw new Error("not found"); if (listError) return { code: 2, stdout: "", stderr: "boom" }; const rest = args.slice(3); const lines = []; for (let i = 0; i + 2 < rest.length; i += 3) { const [index, kind, path] = rest.slice(i, i + 3); for (const file of Object.keys(files)) { const name = file.slice(file.lastIndexOf("/") + 1); const dir = file.slice(0, file.lastIndexOf("/")); if (kind === "file" && file === path) lines.push(`${index}\t${file}`); if (kind === "md" && dir === path && name.endsWith(".md")) lines.push(`${index}\t${file}`); if (kind === "skills" && name === "SKILL.md" && dir.slice(0, dir.lastIndexOf("/")) === path) lines.push(`${index}\t${file}`); } } return { code: 0, stdout: lines.join("\n"), stderr: "" }; }; const readText = async (path) => files[path] ?? null; return { exec, readText }; } const skill = (name, extra = "") => `---\nname: ${name}\ndescription: Opis ${name}\n${extra}---\nTreść\n`; function plugins(enabled, installed) { return { [`${HOME}/.claude/settings.json`]: JSON.stringify({ enabledPlugins: enabled }), [`${HOME}/.claude/plugins/installed_plugins.json`]: JSON.stringify({ version: 2, plugins: Object.fromEntries( Object.entries(installed).map(([id, path]) => [id, [{ installPath: path }]]), ), }), }; } test("GROUPS has Pluginy right after Użytkownik", () => { assert.equal(GROUPS[GROUPS.indexOf(GROUP_USER) + 1], "Pluginy"); assert.equal(GROUP_PLUGIN, "Pluginy"); }); test("claude-code builtins: the full list, unique keys", () => { const keys = BUILTINS["claude-code"].map((item) => item.key); for (const key of ["context", "help", "mcp", "rewind", "clear"]) assert.ok(keys.includes(key), key); assert.equal(new Set(keys).size, keys.length); assert.deepEqual(keys, [...keys].sort()); }); test("globalCommands: an enabled plugin gives plugin:name (default skills dir)", async () => { const dir = "C:/p/superpowers/7.0.2"; const { exec, readText } = machine({ ...plugins({ "superpowers@official": true }, { "superpowers@official": "C:\\p\\superpowers\\7.0.2" }), [`${dir}/skills/brainstorming/SKILL.md`]: skill("brainstorming"), }); const items = await globalCommands(HOME, exec, readText); assert.equal(items.length, 1); assert.equal(items[0].key, "superpowers:brainstorming"); assert.equal(items[0].group, GROUP_PLUGIN); assert.equal(items[0].insert, "/superpowers:brainstorming "); assert.equal(items[0].desc, "Opis brainstorming"); }); test("globalCommands: disabled or unlisted plugin gives nothing", async () => { const dir = "C:/p/a/1"; const { exec, readText } = machine({ ...plugins({ "a@m": false }, { "a@m": dir, "b@m": "C:/p/b/1" }), [`${dir}/skills/x/SKILL.md`]: skill("x"), ["C:/p/b/1/skills/y/SKILL.md"]: skill("y"), }); assert.deepEqual(await globalCommands(HOME, exec, readText), []); }); test("globalCommands: plugin.json skills array takes only the named dirs", async () => { const dir = "C:/p/mp/1"; const { exec, readText } = machine({ ...plugins({ "mp@m": true }, { "mp@m": dir }), [`${dir}/.claude-plugin/plugin.json`]: JSON.stringify({ skills: ["./skills/eng/tdd"] }), [`${dir}/skills/eng/tdd/SKILL.md`]: skill("tdd"), [`${dir}/skills/eng/other/SKILL.md`]: skill("other"), [`${dir}/skills/direct/SKILL.md`]: skill("direct"), }); const keys = (await globalCommands(HOME, exec, readText)).map((item) => item.key); assert.deepEqual(keys, ["mp:tdd"]); }); test("globalCommands: plugin commands dir and user-invocable false", async () => { const dir = "C:/p/c/1"; const { exec, readText } = machine({ ...plugins({ "c@m": true }, { "c@m": dir }), [`${dir}/commands/ship.md`]: "---\ndescription: Wysyła\n---\n", [`${dir}/skills/hidden/SKILL.md`]: skill("hidden", "user-invocable: false\n"), [`${dir}/skills/shown/SKILL.md`]: skill("shown"), }); const keys = (await globalCommands(HOME, exec, readText)).map((item) => item.key).sort(); assert.deepEqual(keys, ["c:ship", "c:shown"]); }); test("globalCommands: name: in the front matter wins over the folder name", async () => { const { exec, readText } = machine({ [`${HOME}/.claude/skills/folder/SKILL.md`]: skill("real-name"), }); const items = await globalCommands(HOME, exec, readText); assert.deepEqual(items.map((item) => item.key), ["real-name"]); }); test("globalCommands: a user command gives handoff in Użytkownik", async () => { const { exec, readText } = machine({ [`${HOME}/.claude/commands/handoff.md`]: "---\ndescription: Przekazanie\n---\n", }); const items = await globalCommands(HOME, exec, readText); assert.equal(items.length, 1); assert.equal(items[0].key, "handoff"); assert.equal(items[0].group, GROUP_USER); assert.equal(items[0].insert, "/handoff "); }); test("globalCommands: no settings.json, broken json: user items only, no throw", async () => { const none = machine({ [`${HOME}/.claude/commands/a.md`]: "x" }); assert.deepEqual((await globalCommands(HOME, none.exec, none.readText)).map((i) => i.key), ["a"]); const broken = machine({ [`${HOME}/.claude/settings.json`]: "{nope", [`${HOME}/.claude/plugins/installed_plugins.json`]: "[[", [`${HOME}/.claude/commands/a.md`]: "x", }); assert.deepEqual((await globalCommands(HOME, broken.exec, broken.readText)).map((i) => i.key), ["a"]); }); test("globalCommands: a listing failure goes to onReadError, or throws", async () => { const { exec, readText } = machine({}, { listError: true }); const errors = []; const items = await globalCommands(HOME, exec, readText, { onReadError: (err) => errors.push(err) }); assert.deepEqual(items, []); assert.equal(errors.length, 1); await assert.rejects(() => globalCommands(HOME, exec, readText)); }); test("paths with spaces and Polish letters are not put in the PowerShell script as text", async () => { const home = "D:/Użytkownik Zażółć/x"; let script = ""; const exec = async (command, args) => { if (command === "powershell") { script = args[args.length - 1]; return { code: 0, stdout: "", stderr: "" }; } throw new Error("no"); }; await globalCommands(home, exec, async () => null); assert.ok(script.length > 0); assert.ok(!script.includes("Zażółć")); }); test("commands: options.global joins after the project, first key wins", async () => { const exec = async () => ({ code: 0, stdout: "", stderr: "" }); const global = [ { key: "clear", desc: "dup", group: GROUP_USER, insert: "/clear " }, { key: "handoff", desc: "h", group: GROUP_USER, insert: "/handoff " }, ]; const items = await commands("/x", exec, { agent: "claude-code", global }); assert.equal(items.filter((item) => item.key === "clear").length, 1); assert.equal( items.find((item) => item.key === "clear").desc, BUILTINS["claude-code"].find((item) => item.key === "clear").desc, ); assert.ok(items.some((item) => item.key === "handoff")); });