Files
magpie/internal/gui/tests/skill-git-source.test.cjs
T
yetone 9fd80b2128 library: skills install from any git repository, GitLab and a company's own among them (sunset on Discord)
ProbeSkills took only a GitHub source or a folder. It now also takes any
git repository's address: https/http on gitlab.com or a self-hosted
server (any depth of groups), a GitLab or Gitea page of a folder
(/-/tree/<ref>/<path>, /-/blob/..., /src/branch/...), ssh://,
git@host:group/repo.git, a bare host.name/group/repo, and file://.
Such a source is kind "git" (gitSource, skillgit.go).

It is fetched with the user's own git (proc.FindTool), so their
credential helpers and SSH keys reach an intranet host: git init, then
fetch --depth 1 --no-tags -- <url> <ref|HEAD>, checkout FETCH_HEAD into
a magpie-skill-* folder, .git removed; the same keepProbe/maxFetch path
GitHub's tarball takes.

What the user typed is data: an address starting with - and any
transport:: address (ext::, fd::, file::) are refused before git runs;
the address follows --; git runs with protocol.ext.allow=never and
core.symlinks=false, no shell, GIT_TERMINAL_PROMPT=0, and ssh -o
BatchMode=yes unless the user set their own SSH command (GIT_SSH,
GIT_SSH_COMMAND, core.sshCommand), so a sign-in it can't make fails
instead of waiting. An address with a password or token in it is
refused: library.json is shown and backed up. Git missing, a sign-in
refused, an unknown host key, a missing branch and an unreachable host
each say so plainly (gitError).

Updates: Commit keeps the repository head the skill was fetched at. A
check runs git ls-remote; another head is fetched (once per repository
and ref) and the skill's folder hashed, so a commit elsewhere in the
repository leaves it current and moves Commit on. UpdateSkill and the
edited-skill refusal work as for GitHub; EditedError and the page's
Changed here / Update / replace question name the repository instead of
GitHub. findNew, Group again, icons and the market stay GitHub's only
(documented).

Page, TUI and docs: the add box's placeholder in en/zh/zh-TW/ja/de
(zh: owner/repo、GitHub 或 GitLab 链接、任意 git 仓库地址,或本机上的文件夹),
a git skill's source shown as a link (https) or text (SSH), its own
Update and Check for updates; the TUI's prompt; library.md's Skills and
Skill updates rows. GIT_SSH/GIT_SSH_COMMAND are listed in agentenv's
notAnAgent (TestFolderVarsAreListed).

Tests (fail on the old code: gitSource/gitPath don't exist there, and
skill-git-source.test.cjs fails on the old assets' placeholder, checked
by stashing client.js/i18n.js): TestGitSourceReads,
TestInstallFromGitRepo (a local bare repo over file://: probe, install,
check finds an update, update, a commit elsewhere stays current, an
edit here is refused naming the repo), TestGitTreeURLInstallsFolder,
TestGitMissingSaysSo, TestGitSignInRefusedSaysSo (a local httptest 401,
GIT_CONFIG_NOSYSTEM so no Keychain), TestGitRefusesOptionsAndTransports,
TestGitErrorsSaid. No test reaches the network.

Run (worktree at origin/main 224ea7d5b, machine load average ~120-145
from other sessions):
- gofmt -l: clean. go vet darwin, GOOS=linux -tags nogui, GOOS=windows:
  clean.
- set -o pipefail; go test -timeout 45m ./...: 63 packages ok; one red,
  internal/agent TestFolderVarsAreListed (GIT_SSH vars), fixed above
  and rerun alone: ok.
- After later edits, rerun ./internal/library ./internal/cli
  ./internal/tui ok; internal/gui TestMainWindowCorners red once at
  load 144 (76.5s, AppKit window test this change doesn't touch),
  passed alone (28.6s); internal/agent timed out at 45m in that
  combined rerun at load ~140 (it ran 962s in the full run) and passed
  alone (1808s, load ~100).
- GUI: the 88 test files that load the library page, chromium and
  webkit: 1420/1420 pass; skill-git-source + skill-update-edited rerun
  after the last client.js edit: 20/20.
- Real use in a sandbox HOME (GIT_CONFIG_NOSYSTEM=1): a local bare repo
  over file:// probed, installed, checked current, a new commit found as
  update, updated to v2; ext:: and --upload-pack refused with nothing
  run; https://gitlab.com/gitlab-org/ai/skills probed (52 skills), its
  /-/tree/main/skills/glab installed and checked current; a private
  gitlab.com address said "git couldn't sign in to gitlab.com ...".
- Not tried: a real self-hosted GitLab with credentials, ssh:// against
  a real host, Windows.
2026-10-19 18:16:57 +08:00

125 lines
8.2 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// sunset on Discord: skills from GitLab, and from a company's own GitLab.
// The add box says any git repository's address works. A skill from one
// (kind "git") shows where it came from (a link for an https address, the
// address as text for an SSH one), has its own Update naming the
// repository, and Check for updates is offered for it. A probe of one
// names the repository. In English and Chinese, in Chromium and WebKit,
// wide and narrow. 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/sunset";
const agent = (id, name) => ({ id, name, icon: "", skills: `${HOME}/.${id}/skills` });
const HTTPS = "https://gitlab.corp.example/team/skills/-/tree/HEAD/skills/pdf";
const SSH = "git@gitlab.corp.example:team/private.git";
const lib = () => ({
dir: `${HOME}/.magpie/library`, backups: `${HOME}/.magpie/backups`, home: HOME,
agents: [agent("claude", "Claude Code")],
instructions: { agents: [] }, servers: [], foundServers: [], projects: [], foundSkills: [], newSkills: [],
checkDue: false,
skills: [
{ name: "pdf", description: "PDFs", kind: "git", source: HTTPS, agents: ["claude"] },
{ name: "review", description: "Reviews", kind: "git", source: SSH, agents: ["claude"], edited: true },
],
});
const probe = { source: "https://gitlab.corp.example/team/skills", kind: "git", candidates: [{ path: "skills/a", name: "a", description: "a", have: false }] };
function server(lang, posts) {
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, theme: "light" } } });
if (url.pathname === "/api/library") return route.fulfill({ json: lib() });
if (url.pathname === "/api/library/skills/probe") { posts.push(req.postDataJSON()); return route.fulfill({ json: probe }); }
if (url.pathname.startsWith("/api/library/skills/")) { posts.push(url.pathname.split("/").pop()); return route.fulfill({ json: { ...lib(), result: { changed: [] } } }); }
if (url.pathname === "/api/plugins") return route.fulfill({ json: { plugins: [] } });
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: { ph: "owner/repo, a GitHub or GitLab link, any git repository's address, or a folder on this computer", upd: "Update from gitlab.corp.example/team/skills", updSSH: "Update from " + SSH, check: "Check for updates", found: "1 skill in gitlab.corp.example/team/skills",
tip: "You changed it since it was fetched from " + SSH + ". Updating asks before it replaces your changes.", ask: "Replace your changes to review with the version in " + SSH + "?" },
zh: { ph: "owner/repo、GitHub 或 GitLab 链接、任意 git 仓库地址,或本机上的文件夹", upd: "从 gitlab.corp.example/team/skills 更新", updSSH: "从 " + SSH + " 更新", check: "检查更新", found: null,
tip: "从 " + SSH + " 获取后你在本地改动过它。更新前会先询问,不会直接覆盖你的改动。", ask: "用 " + SSH + " 中的版本替换你对 review 的改动?" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": a skill from a git repository shows it, updates from it, and the add box says so", 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();
});
for (const width of [900, 400]) for (const lang of ["en", "zh"]) {
await t.test(`${lang} at ${width}px`, async () => {
const w = words[lang], posts = [];
const ctx = await browser.newContext({ viewport: { width, height: 900 } });
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("**/*", server(lang, posts));
await page.goto("http://magpie.test/");
await page.locator('button[data-view="library"]').click();
const v = page.locator("#view-library");
const src = v.locator('input[data-lib="source"]');
await src.waitFor();
assert.equal(await src.getAttribute("placeholder"), w.ph);
const row = (name) => v.locator(".lib-skill", { has: page.locator(".name", { hasText: new RegExp("^" + name) }) });
await row("pdf").waitFor();
const link = row("pdf").locator("a.lib-srclink");
assert.equal(await link.getAttribute("href"), HTTPS);
assert.equal((await link.textContent()).trim(), HTTPS.replace("https://", ""));
const ssh = row("review").locator(".lib-src .lib-srclink");
assert.equal(await ssh.evaluate((e) => e.tagName), "SPAN", "an SSH address isn't a link");
assert.equal((await ssh.textContent()).trim(), SSH);
assert.equal(await row("pdf").locator(".lib-rowacts button.lib-icon").first().getAttribute("title"), w.upd);
assert.equal((await row("review").locator(".lib-rowacts button.lib-icon").first().getAttribute("title")).split("\n")[0], w.updSSH);
const check = v.locator(".row-head button.lib-updall", { hasText: w.check });
assert.equal(await check.count(), 1, "Check for updates is offered");
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth), true, "no sideways scroll");
await row("pdf").locator(".lib-rowacts button.lib-icon").first().click();
await page.waitForTimeout(200);
assert.deepEqual(posts, ["update"]);
// one changed here names the repository it would be replaced from
assert.equal(await row("review").locator(".lib-tag.lib-edited").getAttribute("title"), w.tip);
await row("review").locator(".lib-rowacts button.lib-icon").first().click();
const modal = page.locator(".lib-update-edited");
await modal.waitFor();
assert.equal((await modal.locator(".ehead b").textContent()).trim(), w.ask);
assert.match(await modal.locator(".lib-confirm").textContent(), new RegExp(SSH.replace(/[.]/g, "\\.")));
assert.deepEqual(posts, ["update"], "asking sends nothing");
await page.keyboard.press("Escape");
await modal.waitFor({ state: "detached" });
await src.fill("https://gitlab.corp.example/team/skills");
await src.press("Enter");
const head = v.locator(".lib-probehead .note");
await head.waitFor();
assert.equal(posts[1].source, "https://gitlab.corp.example/team/skills");
if (w.found) assert.equal((await head.textContent()).trim(), w.found);
else assert.match(await head.textContent(), /gitlab\.corp\.example\/team\/skills/);
await ctx.close();
});
}
assert.deepEqual(errors, []);
});
}