Files
herdrMario/tests/catalog.test.mjs
T
2026-11-19 20:36:57 +01:00

224 lines
9.0 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
BUILTINS,
commands,
isClaudeCode,
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"));
});
test("isClaudeCode: claude-code and its alias only", () => {
assert.equal(isClaudeCode("claude-code"), true);
assert.equal(isClaudeCode("claude"), true);
for (const name of ["codex", "kiro-cli", "", undefined]) assert.equal(isClaudeCode(name), false, String(name));
});
test("commands: another agent gets its builtins and snippets - no project, no global, no exec", async () => {
let calls = 0;
const exec = async () => {
calls += 1;
return { code: 0, stdout: "", stderr: "" };
};
const global = [{ key: "handoff", desc: "h", group: GROUP_USER, insert: "/handoff " }];
const items = await commands("/x", exec, {
agent: "codex",
global,
snippets: [{ key: "mine", desc: "s" }],
});
assert.equal(calls, 0);
const keys = items.map((item) => item.key);
assert.deepEqual(keys, [...BUILTINS.codex.map((item) => item.key), "mine"]);
assert.ok(!keys.includes("handoff"));
});
test("commands: claude-code still reads the project and takes the global ones", async () => {
const exec = async (command) => {
if (command === "sh") return { code: 0, stdout: ".claude/commands/p.md\n", stderr: "" };
return { code: 1, stdout: "", stderr: "" };
};
const global = [{ key: "handoff", desc: "h", group: GROUP_USER, insert: "/handoff " }];
const items = await commands("/x", exec, { agent: "claude", global });
const keys = items.map((item) => item.key);
assert.ok(keys.includes("handoff"));
assert.ok(keys.includes("clear"));
});