147 lines
5.6 KiB
JavaScript
147 lines
5.6 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
BUILTINS,
|
|
DEFAULT_PROFILES,
|
|
PROFILES_FILE,
|
|
parseProfiles,
|
|
resolveProfile,
|
|
} from "../herdr/herdr-composer/mods/herdr-composer/profiles.js";
|
|
|
|
test("PROFILES_FILE is composer-agents.json", () => {
|
|
assert.equal(PROFILES_FILE, "composer-agents.json");
|
|
});
|
|
|
|
test("resolveProfile: defaults, case-insensitive, unknown and default are null", () => {
|
|
assert.equal(resolveProfile([], "claude").id, "claude-code");
|
|
assert.equal(resolveProfile([], "Claude-Code").id, "claude-code");
|
|
assert.equal(resolveProfile([], "codex").id, "codex");
|
|
assert.equal(resolveProfile([], "kiro-cli"), null);
|
|
assert.equal(resolveProfile([], "default"), null);
|
|
assert.equal(resolveProfile([], ""), null);
|
|
});
|
|
|
|
test("resolveProfile: match is case-insensitive for a user profile", () => {
|
|
const { profiles } = parseProfiles('{"agents":[{"id":"kiro","match":["kiro-cli"]}]}');
|
|
assert.equal(resolveProfile(profiles, "Kiro-CLI").id, "kiro");
|
|
assert.equal(resolveProfile(profiles, " kiro-cli ").id, "kiro");
|
|
});
|
|
|
|
test("resolveProfile: a user profile with the same id replaces the default whole", () => {
|
|
const { profiles } = parseProfiles(
|
|
'{"agents":[{"id":"claude-code","match":["claude-code"],"builtins":[{"key":"x"}]}]}',
|
|
);
|
|
const profile = resolveProfile(profiles, "claude-code");
|
|
assert.deepEqual(profile.builtins, [{ key: "x", desc: "" }]);
|
|
assert.deepEqual(profile.sources, []);
|
|
});
|
|
|
|
test("parseProfiles: no file is normal", () => {
|
|
assert.deepEqual(parseProfiles(null), { profiles: [], problem: null });
|
|
assert.deepEqual(parseProfiles(undefined), { profiles: [], problem: null });
|
|
});
|
|
|
|
test("parseProfiles: BOM and CRLF are fine, prefix kept", () => {
|
|
const text =
|
|
'\uFEFF{"agents":[{"id":"kiro","match":["kiro-cli"],"sources":[{"scope":"home","path":".kiro/prompts","pattern":"*.md","prefix":"@"}]}]}\r\n';
|
|
const { profiles, problem } = parseProfiles(text);
|
|
assert.equal(problem, null);
|
|
assert.equal(profiles.length, 1);
|
|
assert.deepEqual(profiles[0].sources, [
|
|
{ scope: "home", path: ".kiro/prompts", pattern: "*.md", prefix: "@" },
|
|
]);
|
|
});
|
|
|
|
test("parseProfiles: broken JSON", () => {
|
|
assert.deepEqual(parseProfiles("{nope"), { profiles: [], problem: "nieczytelny JSON" });
|
|
});
|
|
|
|
test("parseProfiles: agents is not a list", () => {
|
|
assert.deepEqual(parseProfiles('{"agents":{}}'), { profiles: [], problem: "brak listy agents" });
|
|
assert.deepEqual(parseProfiles("{}"), { profiles: [], problem: "brak listy agents" });
|
|
});
|
|
|
|
test("parseProfiles: bad sources are dropped, the good one stays", () => {
|
|
const text = JSON.stringify({
|
|
agents: [
|
|
{
|
|
id: "a",
|
|
match: ["a"],
|
|
sources: [
|
|
{ scope: "project", path: "../x", pattern: "*.md" },
|
|
{ scope: "home", path: "C:/x", pattern: "*.md" },
|
|
{ scope: "project", path: ".a/cmds", pattern: "*.md" },
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const { profiles, problem } = parseProfiles(text);
|
|
assert.equal(profiles[0].sources.length, 1);
|
|
assert.equal(profiles[0].sources[0].prefix, "/");
|
|
assert.equal(problem, "pominięto 2 wpisów");
|
|
});
|
|
|
|
test("parseProfiles: absolute and traversal path forms are rejected", () => {
|
|
for (const path of ["/x", "C:/x", "C:\\x", "\\\\x", "a/../b", "a\\..\\b", ".."]) {
|
|
const text = JSON.stringify({
|
|
agents: [{ id: "a", match: ["a"], sources: [{ scope: "home", path, pattern: "*.md" }] }],
|
|
});
|
|
assert.deepEqual(parseProfiles(text).profiles[0].sources, [], path);
|
|
}
|
|
});
|
|
|
|
test("parseProfiles: a profile without match is dropped, the next stays", () => {
|
|
const text = JSON.stringify({ agents: [{ id: "bad" }, { id: "ok", match: ["ok"] }] });
|
|
const { profiles, problem } = parseProfiles(text);
|
|
assert.deepEqual(profiles.map((p) => p.id), ["ok"]);
|
|
assert.equal(problem, "pominięto 1 wpisów");
|
|
});
|
|
|
|
test("parseProfiles: unknown pattern, scope and keyless builtin are dropped", () => {
|
|
const text = JSON.stringify({
|
|
agents: [
|
|
{
|
|
id: "a",
|
|
match: ["a"],
|
|
builtins: [{ key: "ok", desc: "d" }, { desc: "no key" }],
|
|
sources: [
|
|
{ scope: "home", path: "p", pattern: "**/*.md" },
|
|
{ scope: "nowhere", path: "p", pattern: "*.md" },
|
|
],
|
|
},
|
|
],
|
|
});
|
|
const { profiles, problem } = parseProfiles(text);
|
|
assert.deepEqual(profiles[0].builtins, [{ key: "ok", desc: "d" }]);
|
|
assert.deepEqual(profiles[0].sources, []);
|
|
assert.equal(problem, "pominięto 3 wpisów");
|
|
});
|
|
|
|
test("parseProfiles: claude-plugins source is kept for home only", () => {
|
|
const text = JSON.stringify({
|
|
agents: [{ id: "a", match: ["a"], sources: [{ scope: "home", kind: "claude-plugins" }] }],
|
|
});
|
|
assert.deepEqual(parseProfiles(text).profiles[0].sources, [{ scope: "home", kind: "claude-plugins" }]);
|
|
});
|
|
|
|
test("DEFAULT_PROFILES: claude-code keeps the builtins and the five sources", () => {
|
|
const cc = DEFAULT_PROFILES[0];
|
|
assert.equal(cc.id, "claude-code");
|
|
assert.deepEqual(cc.match, ["claude-code", "claude"]);
|
|
assert.deepEqual(
|
|
cc.builtins.map((b) => b.key),
|
|
BUILTINS["claude-code"].map((b) => b.key),
|
|
);
|
|
assert.deepEqual(cc.sources, [
|
|
{ scope: "project", path: ".claude/commands", pattern: "*.md", prefix: "/" },
|
|
{ scope: "project", path: ".claude/skills", pattern: "*/SKILL.md", prefix: "/" },
|
|
{ scope: "home", path: ".claude/commands", pattern: "*.md", prefix: "/" },
|
|
{ scope: "home", path: ".claude/skills", pattern: "*/SKILL.md", prefix: "/" },
|
|
{ scope: "home", kind: "claude-plugins" },
|
|
]);
|
|
const codex = DEFAULT_PROFILES[1];
|
|
assert.equal(codex.id, "codex");
|
|
assert.deepEqual(codex.sources, []);
|
|
assert.equal(codex.builtins.length, 4);
|
|
});
|