"Add from folder" pointed at a skill already in ~/.config/magpie/library/skills emptied every file of it: InstallSkills' symlink failed (it was there) and its fallback copied the folder onto itself, O_TRUNC first. A folder from elsewhere by a name the library's folder already had was copied into it the same way. Such a folder is now refused ("…is in the library's folder already: bring it in from the skills found there"), anything by that name in the library's folder is never written into, the copy fallback runs only when the symlink failed for another reason, and copyDir refuses a destination that is its source or inside it.
A skill put in the library's folder by hand, not in library.json, was shown nowhere: foundSkills now scans the library's folder too, first, and such a one gets FoundSkill.Library (magpie's links to it in the agents count as the agents having it). Bringing it in lists it where it is: nothing moved or linked, Source nil for a folder of its own, {folder, its real path} for a link. The found row says "in the library's folder, not listed" with its path, and Bring in that it is listed where it is (en, zh).
Codex, Gemini CLI and the others that read ~/.agents/skills themselves got a ~/.codex/skills/X link to a library skill whose real folder is in ~/.agents/skills, and saw it twice. A user's own entry there (not magpie's link) that is the very folder the library's is now counts as the skill being in the shared folder: no link of their own is made, magpie's made before are taken away, and a byte copy of their own is kept aside with the backups as before. Nothing in ~/.agents/skills is moved or removed. SkillView.Always names the agents that read that folder, and their chips are lit, disabled, and say "Codex reads ~/.agents/skills itself, where this skill is kept — it has it whatever is ticked here" (en, zh); a click on another chip or All neither sends nor darkens them.
A skill's kind and "linked from" are now what its library entry is, not what was written when it came in: a linked folder moved in by hand is the library's own, shown without "linked from", and its Remove says the folder goes to the backups, as removeSkill does (which now also counts a junction as a link, as the page does).
TestAddFromLibraryFolderKeepsIt (the skill's folder and the library's skills folder as the source, another folder by that name, copyDir onto and into itself: bytes unchanged), TestFoundInLibraryFolder, TestSharedSkillNotLinkedTwice and TestSkillKindIsWhatTheFolderIs are new and fail without the change (the first with SKILL.md and run.sh emptied); TestSharedAgentsSkills now asserts Codex and Gemini CLI get no second link (Pi keeps its) and are Always. library-own-folder.test.cjs (WebKit, en and zh) is new and fails without the change but for its Remove case; library-all, library-items, library-loading, library-row-open-scroll, library-toggle-status, click-scroll, import-all-skills, own-skill, every-skill, remove-all-skills, by-agent and market-have pass in WebKit. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
183 lines
10 KiB
JavaScript
183 lines
10 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// #595: a skill put in the library's own folder by hand is shown, said to
|
|
// be there, and brought in where it is; a library skill kept in
|
|
// ~/.agents/skills is lit, and can't be clicked, for the agents that read
|
|
// that folder themselves, and a click on another chip or All never sends
|
|
// them; a skill's Remove says what really happens to its folder. No click
|
|
// moves the page. In English and Chinese. No backend: the API is faked here.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs/promises");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
const { chromium, webkit } = require("playwright");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const HOME = "/Users/loosheng";
|
|
const LIB = `${HOME}/.config/magpie/library`;
|
|
const agent = (id, name, icon, more = {}) => ({ id, name, icon, skills: `${HOME}/.${id}/skills`, mcp: `${HOME}/.${id}/mcp.json`, ...more });
|
|
const base = () => ({
|
|
dir: LIB, backups: `${HOME}/.config/magpie/backups`, home: HOME,
|
|
agents: [
|
|
agent("claude", "Claude Code", "claudecode-color"),
|
|
agent("codex", "Codex", "codex-color"),
|
|
agent("gemini", "Gemini CLI", "gemini-color"),
|
|
agent("pi", "Pi", "pi", { mcp: "" }),
|
|
],
|
|
instructions: { agents: [], sets: [] }, foundServers: [], projects: [], servers: [],
|
|
skills: [
|
|
// brought in from ~/.agents/skills: Codex and Gemini CLI read it
|
|
{ name: "lint", kind: "folder", description: "Lint", source: `${HOME}/.agents/skills/lint`, agents: ["claude"], always: ["codex", "gemini"] },
|
|
// the library's own folder (a linked one moved in by hand)
|
|
{ name: "notes", kind: "", description: "Notes", agents: [] },
|
|
],
|
|
foundSkills: [{ name: "hand", description: "By hand", agents: [], library: `${LIB}/skills/hand` }],
|
|
});
|
|
|
|
function server(lang, posts) {
|
|
let lib = base();
|
|
return async (route) => {
|
|
const req = route.request(), url = new URL(req.url());
|
|
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
|
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return route.fulfill({ json: { agents: [], profiles: [], settings: { lang } } });
|
|
if (url.pathname === "/api/plugins") return route.fulfill({ json: { plugins: [] } });
|
|
if (url.pathname === "/api/library") return route.fulfill({ json: lib });
|
|
if (url.pathname === "/api/library/skills/agents") {
|
|
const body = req.postDataJSON();
|
|
posts.push(body);
|
|
lib = structuredClone(lib);
|
|
lib.skills.find((x) => x.name === body.name).agents = body.agents;
|
|
return route.fulfill({ json: { ...lib, problems: [], result: { changed: [], problems: [] } } });
|
|
}
|
|
if (url.pathname === "/api/library/skills/import") {
|
|
const body = req.postDataJSON();
|
|
posts.push({ import: body.name });
|
|
lib = structuredClone(lib);
|
|
lib.foundSkills = [];
|
|
lib.skills.push({ name: body.name, kind: "", description: "By hand", agents: [] });
|
|
return route.fulfill({ json: { ...lib, problems: [], result: { changed: [], problems: [] } } });
|
|
}
|
|
if (url.pathname === "/api/usage/quotas") return route.fulfill({ json: [] });
|
|
if (url.pathname === "/api/groups") return route.fulfill({ json: { groups: [] } });
|
|
if (url.pathname === "/api/providers") return route.fulfill({ json: { providers: [], gateway: { running: true } } });
|
|
if (url.pathname.startsWith("/api/")) return route.fulfill({ json: {} });
|
|
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
|
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
|
await route.fulfill({ body: await fs.readFile(file), contentType });
|
|
};
|
|
}
|
|
|
|
const words = {
|
|
en: {
|
|
here: "in the library's folder, not listed",
|
|
bring: "Lists it in the library where it is, nothing moved: you can give it to any agent",
|
|
always: (a) => `${a} reads ~/.agents/skills itself, where this skill is kept — it has it whatever is ticked here`,
|
|
stays: "It is taken out of every agent it was given to. The folder it was linked from stays where it is.",
|
|
moved: "It is taken out of every agent it was given to, and its folder is moved to magpie's backups.",
|
|
cancel: "Cancel",
|
|
},
|
|
zh: {
|
|
here: "在资源库文件夹中,但未列入",
|
|
bring: "在原处列入资源库,不移动任何文件:你可以把它给任何 Agent",
|
|
always: (a) => `${a} 自己会读取 ~/.agents/skills,这个技能就放在那里 — 无论这里是否勾选,它都有这个技能`,
|
|
stays: "它会从所有已启用的 Agent 中移除。它链接的原文件夹保持不动。",
|
|
moved: "它会从所有已启用的 Agent 中移除,文件夹会移到 magpie 的备份中。",
|
|
cancel: "取消",
|
|
},
|
|
};
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
test(engine + ": skills in the library's folder and in ~/.agents/skills", async (t) => {
|
|
assert(["chromium", "webkit"].includes(engine), "BROWSER must be chromium or webkit");
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
const errors = [];
|
|
t.after(async () => {
|
|
if (errors.length) console.log(errors);
|
|
await browser.close();
|
|
});
|
|
const open = async (lang, posts) => {
|
|
const ctx = await browser.newContext({ viewport: { width: 980, height: 800 } });
|
|
await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", "skills"); } catch {} });
|
|
const page = await ctx.newPage();
|
|
page.setDefaultTimeout(5000);
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.route("http://magpie.test/**", server(lang, posts));
|
|
await page.goto("http://magpie.test/");
|
|
await page.locator('button[data-view="library"]').click();
|
|
await page.locator("#view-library .lib-row").first().waitFor();
|
|
return page;
|
|
};
|
|
const row = (page, name) => page.locator("#view-library .lib-row").filter({ has: page.locator(".name", { hasText: new RegExp("^" + name) }) });
|
|
const click = async (page, loc) => {
|
|
const top = await page.evaluate(() => document.querySelector("#view-library").scrollTop);
|
|
await loc.click();
|
|
assert.equal(await page.evaluate(() => document.querySelector("#view-library").scrollTop), top);
|
|
};
|
|
const lit = (r) => r.locator(".lib-ag[data-agent]").evaluateAll((cs) => cs.filter((c) => c.getAttribute("aria-pressed") === "true").map((c) => c.dataset.agent));
|
|
|
|
for (const lang of ["en", "zh"]) {
|
|
const w = words[lang];
|
|
await t.test(lang + ": a skill in the library's folder is shown, and brought in where it is", async () => {
|
|
const posts = [];
|
|
const page = await open(lang, posts);
|
|
const r = row(page, "hand");
|
|
await r.waitFor();
|
|
assert.equal(await r.locator(".lib-src > span").first().textContent(), w.here);
|
|
assert.match(await r.locator(".lib-src").first().textContent(), /library\/skills\/hand/);
|
|
const b = r.locator("button.action");
|
|
assert.equal(await b.getAttribute("title"), w.bring);
|
|
await click(page, b);
|
|
await page.waitForFunction(() => ![...document.querySelectorAll("#view-library .lib-row .name")].some((n) => n.textContent === "hand" && n.closest(".lib-row").querySelector("button.action")));
|
|
assert.deepEqual(posts, [{ import: "hand" }]);
|
|
await page.close();
|
|
});
|
|
|
|
await t.test(lang + ": the agents reading ~/.agents/skills have a skill kept there, whatever is ticked", async () => {
|
|
const posts = [];
|
|
const page = await open(lang, posts);
|
|
const r = row(page, "lint");
|
|
assert.deepEqual(await lit(r), ["claude", "codex", "gemini"]);
|
|
for (const [id, name] of [["codex", "Codex"], ["gemini", "Gemini CLI"]]) {
|
|
const c = r.locator(`.lib-ag[data-agent="${id}"]`);
|
|
assert.equal(await c.isDisabled(), true);
|
|
assert.equal(await c.getAttribute("title"), w.always(name));
|
|
}
|
|
assert.equal(await r.locator('.lib-ag[data-agent="pi"]').isDisabled(), false);
|
|
// a click on Pi's chip sends Claude Code and Pi, not the two that
|
|
// have it anyway
|
|
await click(page, r.locator('.lib-ag[data-agent="pi"]'));
|
|
await page.waitForFunction(() => document.querySelector("#status")?.textContent);
|
|
assert.deepEqual(posts.map((p) => [...p.agents].sort()), [["claude", "pi"]]);
|
|
// All takes it from the agents it can, and leaves the two lit
|
|
await click(page, r.locator(".lib-ag.all"));
|
|
await page.waitForFunction((n) => n === 2, posts.length).catch(() => {});
|
|
await page.waitForTimeout(300);
|
|
assert.equal(posts.length, 2);
|
|
assert.deepEqual(posts[1].agents, []);
|
|
assert.deepEqual(await lit(r), ["codex", "gemini"]);
|
|
// the row's own click (open the SKILL.md) wasn't set off
|
|
assert.equal(await page.locator("#modal").isHidden(), true);
|
|
await page.close();
|
|
});
|
|
|
|
await t.test(lang + ": Remove says what happens to the skill's folder", async () => {
|
|
const page = await open(lang, []);
|
|
for (const [name, text] of [["lint", w.stays], ["notes", w.moved]]) {
|
|
await click(page, row(page, name).locator(".lib-icon.danger"));
|
|
assert.equal(await page.locator("#modal .lib-confirm").first().textContent(), text);
|
|
await page.locator("#modal button", { hasText: new RegExp("^" + w.cancel + "$") }).click();
|
|
await page.locator("#modal").waitFor({ state: "hidden" });
|
|
}
|
|
// nothing on the rows has a left-border accent
|
|
const borders = await page.locator("#view-library .lib-row, #view-library .lib-row *").evaluateAll((els) => els.filter((e) => {
|
|
const s = getComputedStyle(e);
|
|
return parseFloat(s.borderLeftWidth) >= 2 && s.borderLeftStyle !== "none" && s.borderLeftColor !== s.borderRightColor;
|
|
}).length);
|
|
assert.equal(borders, 0);
|
|
await page.close();
|
|
});
|
|
}
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|