Files
magpie/site/install.test.mjs
T
yetone 90518de261 install.sh: --uninstall takes the mimeapps.list xdg-mime made when nothing else is in it
After 305c624ed a curl-piped install and --uninstall in a fresh HOME
left ~/.config/mimeapps.list holding only the "[Default Applications]"
header xdg-mime wrote at install. A list with no entry left (only
section headers and blank lines) now goes; one with any line of the
user's stays as it is, less magpie's.

Run at 305c624ed + this: node --test site/install.test.mjs
worker.test.mjs stable.test.mjs on mote (53 pass); install.test.mjs in
WSL magpie-desktop (14 pass); dash -n ok. Real run in WSL with the real
0.1.2345 release, sandbox HOME: with a text/html line of the user's the
list keeps it, and the rest of the run is as 305c624ed says.

Broken by hand: the rm taken out turns the --uninstall and --purge
tests red (the list is still there).

Not tried: a mimeapps.list with comments only (a # line counts as an
entry and keeps the file).
2026-10-31 16:13:55 +08:00

295 lines
15 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, statSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const installer = fileURLToPath(new URL("./public/install.sh", import.meta.url));
function install(t, { termux = true, arch = "aarch64", override = false, prefixOnly = false, missing = false, corrupt = false, desktop = false, github = false, args = [], env: extra = {}, tamper = "" } = {}) {
const dir = mkdtempSync(join(tmpdir(), "magpie-install-"));
t.after(() => rmSync(dir, { recursive: true, force: true }));
const tools = join(dir, "tools");
const home = join(dir, "home");
// forward slashes on Windows too: install.sh knows Termux by
// */com.termux/files/usr, which a backslashed path never matches
const prefix = join(dir, "com.termux/files/usr").replaceAll("\\", "/");
const bin = override ? join(dir, "custom bin") : termux ? join(prefix, "bin") : join(home, ".local/bin");
mkdirSync(tools, { recursive: true });
mkdirSync(bin, { recursive: true });
writeFileSync(join(bin, "magpie"), "old installation");
const payload = "test binary\n";
const cpu = arch === "aarch64" ? "arm64" : "amd64";
const file = termux ? `magpie-cli-android-${cpu}` : desktop ? `magpie-linux-${cpu}` : `magpie-cli-linux-${cpu}`;
const assets = {};
// A Linux asset is present even when Android's is missing: never use it.
for (const name of [file, `magpie-cli-linux-${cpu}`]) {
if (missing && name === file) continue;
assets[name] = { url: github ? `https://github.com/yetone/magpie-releases/releases/download/vtest/${name}` : `https://download.invalid/${name}`, sha256: corrupt ? "0".repeat(64) : createHash("sha256").update(payload).digest("hex") };
}
const requests = join(dir, "requests");
writeFileSync(join(tools, "uname"), `#!/bin/sh\ncase "$1" in -m) echo '${arch}';; -s) echo Linux;; esac\n`, { mode: 0o755 });
writeFileSync(join(tools, "ldconfig"), "#!/bin/sh\necho libwebkit2gtk-4.1.so.0\n", { mode: 0o755 });
// xdg-mime as xdg-utils writes the default: one line in mimeapps.list
writeFileSync(join(tools, "xdg-mime"), `#!/bin/sh
[ "$1" = default ] || exit 0
mkdir -p "$XDG_CONFIG_HOME"
printf '[Default Applications]\\n' >> "$XDG_CONFIG_HOME/mimeapps.list"
printf '%s=%s\\n' "$3" "$2" >> "$XDG_CONFIG_HOME/mimeapps.list"
`, { mode: 0o755 });
writeFileSync(join(tools, "curl"), `#!${process.execPath}
const fs = require("node:fs");
const args = process.argv.slice(2);
const url = args.at(-1);
fs.appendFileSync(process.env.INSTALL_REQUESTS, url + String.fromCharCode(10));
fs.appendFileSync(process.env.INSTALL_ARGS, JSON.stringify(args) + String.fromCharCode(10));
const tamper = process.env.INSTALL_TAMPER;
if (url.endsWith("/api/latest")) process.stdout.write(process.env.INSTALL_FEED);
else fs.writeFileSync(args[args.indexOf("-o") + 1], tamper && url.startsWith(tamper) ? "changed by the mirror" : process.env.INSTALL_PAYLOAD);
`, { mode: 0o755 });
const env = {
...process.env,
HOME: home,
PREFIX: termux ? prefix : "",
TERMUX_VERSION: termux && !prefixOnly ? "test" : "",
MAGPIE_BIN_DIR: override ? bin : "",
MAGPIE_CLI: desktop ? "" : "1",
PATH: tools + ":" + process.env.PATH,
XDG_CONFIG_HOME: join(dir, "config"),
XDG_DATA_HOME: join(dir, "share"),
INSTALL_REQUESTS: requests,
INSTALL_FEED: JSON.stringify({ version: "test", assets }),
INSTALL_PAYLOAD: payload,
INSTALL_ARGS: join(dir, "args"),
INSTALL_TAMPER: tamper,
MAGPIE_PROXY: "",
MAGPIE_MIRROR: "",
...extra,
};
const read = (p) => (existsSync(p) ? readFileSync(p, "utf8") : "");
const run = (args) => {
rmSync(requests, { force: true });
rmSync(env.INSTALL_ARGS, { force: true });
const result = spawnSync("sh", [installer, ...args], { env, encoding: "utf8" });
const calls = read(env.INSTALL_ARGS).trim().split("\n").filter(Boolean).map((l) => JSON.parse(l));
return { ...result, requests: read(requests), calls };
};
return { ...run(args), run, bin, file, payload, home, env, share: env.XDG_DATA_HOME, config: env.XDG_CONFIG_HOME };
}
test("Termux downloads Android arm64 into PREFIX/bin even with WebKit installed", (t) => {
const r = install(t, { desktop: true });
assert.equal(r.status, 0, r.stderr);
assert.equal(readFileSync(join(r.bin, "magpie"), "utf8"), r.payload);
// NTFS keeps no execute bit, so Windows reports none
if (process.platform !== "win32") assert.ok(statSync(join(r.bin, "magpie")).mode & 0o111);
assert.match(r.requests, /magpie-cli-android-arm64/);
assert.doesNotMatch(r.requests, /magpie-linux-|icon-256/);
assert.equal(existsSync(join(r.share, "applications/magpie.desktop")), false);
assert.match(r.stdout, /run it: magpie web/);
assert.doesNotMatch(r.stdout, /install WebKit/);
});
test("Termux prefix detection and amd64 target", (t) => {
const r = install(t, { prefixOnly: true, arch: "x86_64" });
assert.equal(r.status, 0, r.stderr);
assert.match(r.requests, /magpie-cli-android-amd64/);
});
test("Termux respects a custom bin directory containing spaces", (t) => {
const r = install(t, { override: true });
assert.equal(r.status, 0, r.stderr);
assert.equal(readFileSync(join(r.bin, "magpie"), "utf8"), r.payload);
});
test("missing Android release never falls back to Linux", (t) => {
const r = install(t, { missing: true });
assert.notEqual(r.status, 0);
assert.match(r.stderr, /no magpie-cli-android-arm64.*build in Termux/);
assert.equal(readFileSync(join(r.bin, "magpie"), "utf8"), "old installation");
assert.equal(r.requests.trim().split("\n").length, 1);
});
test("a checksum mismatch leaves the existing installation intact", (t) => {
const r = install(t, { corrupt: true });
assert.notEqual(r.status, 0);
assert.match(r.stderr, /does not match its checksum/);
assert.equal(readFileSync(join(r.bin, "magpie"), "utf8"), "old installation");
});
test("ordinary Linux still selects its CLI or desktop build", (t) => {
for (const desktop of [false, true]) {
const r = install(t, { termux: false, desktop });
assert.equal(r.status, 0, r.stderr);
assert.match(r.requests, desktop ? /magpie-linux-arm64/ : /magpie-cli-linux-arm64/);
assert.equal(existsSync(join(r.share, "applications/magpie.desktop")), desktop);
// the dock binds the window (app_id/WM_CLASS "magpie") to the entry (#2626)
if (desktop) assert.match(readFileSync(join(r.share, "applications/magpie.desktop"), "utf8"), /^StartupWMClass=magpie$/m);
}
});
// akic404 on Discord: installing without a way round the firewall. A proxy
// and a GitHub mirror can be given; neither is used unless given.
test("no proxy and no mirror unless given", (t) => {
const r = install(t, { termux: false, github: true });
assert.equal(r.status, 0, r.stderr);
for (const a of r.calls) assert.ok(!a.includes("--proxy"), JSON.stringify(a));
assert.match(r.requests, /^https:\/\/github\.com\/yetone\/magpie-releases\//m);
});
test("--proxy takes every download through it, as does MAGPIE_PROXY", (t) => {
for (const [opts, want] of [
[{ args: ["--proxy", "socks5h://127.0.0.1:1080"] }, "socks5h://127.0.0.1:1080"],
[{ args: ["--proxy=http://127.0.0.1:7890"] }, "http://127.0.0.1:7890"],
[{ env: { MAGPIE_PROXY: "http://10.0.0.1:3128" } }, "http://10.0.0.1:3128"],
]) {
const r = install(t, { termux: false, desktop: true, github: true, ...opts });
assert.equal(r.status, 0, r.stderr);
assert.equal(r.calls.length, 3); // the feed, the file, the icon
for (const a of r.calls) assert.equal(a[a.indexOf("--proxy") + 1], want, JSON.stringify(a));
}
const bad = install(t, { termux: false, args: ["--proxy", "ftp://x"] });
assert.notEqual(bad.status, 0);
assert.match(bad.stderr, /--proxy is an address/);
assert.equal(bad.calls.length, 0);
});
test("--mirror fetches the file through the mirror, the feed and its checksum from the site", (t) => {
const r = install(t, { termux: false, github: true, args: ["--mirror", "https://gh.mirror.example"] });
assert.equal(r.status, 0, r.stderr);
const [feed, file] = r.requests.trim().split("\n");
assert.equal(feed, "https://usemagpie.ai/api/latest");
assert.equal(file, "https://gh.mirror.example/https://github.com/yetone/magpie-releases/releases/download/vtest/magpie-cli-linux-arm64");
assert.equal(readFileSync(join(r.bin, "magpie"), "utf8"), r.payload);
assert.match(r.stdout, /through https:\/\/gh\.mirror\.example\//);
const env = install(t, { termux: false, github: true, env: { MAGPIE_MIRROR: "https://gh.mirror.example/" } });
assert.equal(env.status, 0, env.stderr);
assert.match(env.requests, /^https:\/\/gh\.mirror\.example\/https:\/\/github\.com\//m);
});
test("a file the mirror changed is refused and the installed magpie kept", (t) => {
const r = install(t, { termux: false, github: true, args: ["--mirror", "https://gh.mirror.example/"], tamper: "https://gh.mirror.example/" });
assert.notEqual(r.status, 0);
assert.match(r.stderr, /from https:\/\/gh\.mirror\.example\/ does not match its checksum from https:\/\/usemagpie\.ai; not installed/);
assert.equal(readFileSync(join(r.bin, "magpie"), "utf8"), "old installation");
const bad = install(t, { termux: false, args: ["--mirror", "gh.mirror.example"] });
assert.notEqual(bad.status, 0);
assert.match(bad.stderr, /--mirror is an http\(s\) address/);
});
// pota on Discord: the script installed magpie and had no way to take it
// off. --uninstall takes off what it put there and keeps the user's
// settings; --purge has magpie disconnect the agents and delete them first.
function installedDesktop(t, opts = {}) {
const r = install(t, { termux: false, desktop: true, ...opts });
assert.equal(r.status, 0, r.stderr);
const exe = join(r.bin, "magpie");
// magpie itself, as far as the uninstall asks it anything
writeFileSync(exe, `#!/bin/sh
echo "$*" >> "$HOME/magpie-calls"
[ "$1 $2" = "daemon uninstall" ] && rm -f "$XDG_CONFIG_HOME/systemd/user/magpie.service"
[ "$1 $2" = "uninstall --yes" ] && { [ -z "$MAGPIE_FAIL" ] || exit 1; rm -rf "$XDG_CONFIG_HOME/magpie" "$XDG_DATA_HOME/applications/magpie.desktop"; }
exit 0
`, { mode: 0o755 });
const settings = join(r.config, "magpie", "settings.json");
mkdirSync(join(r.config, "magpie"), { recursive: true });
writeFileSync(settings, "{}");
const calls = () => (existsSync(join(r.home, "magpie-calls")) ? readFileSync(join(r.home, "magpie-calls"), "utf8") : "");
return { ...r, exe, settings, calls, entry: join(r.share, "applications/magpie.desktop"), icon: join(r.share, "icons/hicolor/256x256/apps/magpie.png"), mime: join(r.config, "mimeapps.list") };
}
test("--uninstall takes off the command, menu entry, icon and magpie:// default, and keeps the settings", (t) => {
const r = installedDesktop(t);
for (const p of [r.exe, r.entry, r.icon]) assert.ok(existsSync(p), p);
writeFileSync(r.mime, readFileSync(r.mime, "utf8") + "text/html=firefox.desktop\n");
assert.match(readFileSync(r.mime, "utf8"), /^x-scheme-handler\/magpie=magpie\.desktop$/m);
const u = r.run(["--uninstall"]);
assert.equal(u.status, 0, u.stderr);
for (const p of [r.exe, r.entry, r.icon]) assert.equal(existsSync(p), false, p);
assert.equal(readFileSync(r.mime, "utf8"), "[Default Applications]\ntext/html=firefox.desktop\n");
// with no line of the user's, the list xdg-mime made goes with it
const bare = installedDesktop(t);
const bu = bare.run(["--uninstall"]);
assert.equal(bu.status, 0, bu.stderr);
assert.equal(existsSync(bare.mime), false);
assert.ok(existsSync(r.settings), "the settings stay");
assert.equal(r.calls(), "", "magpie isn't asked to uninstall");
assert.equal(u.calls.length, 0, "nothing is downloaded");
assert.match(u.stdout, /kept your settings.*--purge/);
// again: nothing there, nothing wrong
const again = r.run(["--uninstall"]);
assert.equal(again.status, 0, again.stderr);
assert.match(again.stdout, /no magpie installed by this script/);
});
test("--uninstall takes the service and login entry that start this magpie, never another's", (t) => {
const r = installedDesktop(t);
const unit = join(r.config, "systemd/user/magpie.service");
const login = join(r.config, "autostart/magpie.desktop");
mkdirSync(join(r.config, "systemd/user"), { recursive: true });
mkdirSync(join(r.config, "autostart"), { recursive: true });
writeFileSync(unit, `[Service]\nExecStart="${r.exe}" serve\n`);
writeFileSync(login, `[Desktop Entry]\nExec="${r.exe}" --autostart\n`);
const u = r.run(["--uninstall"]);
assert.equal(u.status, 0, u.stderr);
assert.equal(existsSync(unit), false);
assert.equal(existsSync(login), false);
assert.match(r.calls(), /^daemon uninstall$/m);
// another magpie's (one still there) are left alone
const o = installedDesktop(t);
const other = join(o.home, "elsewhere/magpie");
mkdirSync(join(o.home, "elsewhere"), { recursive: true });
writeFileSync(other, "", { mode: 0o755 });
mkdirSync(join(o.config, "systemd/user"), { recursive: true });
mkdirSync(join(o.config, "autostart"), { recursive: true });
const ounit = join(o.config, "systemd/user/magpie.service");
const ologin = join(o.config, "autostart/magpie.desktop");
writeFileSync(ounit, `[Service]\nExecStart="${other}" serve\n`);
writeFileSync(ologin, `[Desktop Entry]\nExec="${other}" --autostart\n`);
writeFileSync(o.entry, `[Desktop Entry]\nExec=${other} %u\n`);
const ou = o.run(["--uninstall"]);
assert.equal(ou.status, 0, ou.stderr);
for (const p of [ounit, ologin, o.entry]) assert.ok(existsSync(p), p);
assert.equal(existsSync(o.exe), false);
});
test("--purge has magpie uninstall itself first, then takes off the install", (t) => {
const r = installedDesktop(t);
const u = r.run(["--purge"]);
assert.equal(u.status, 0, u.stderr);
assert.match(r.calls(), /^uninstall --yes$/m);
for (const p of [r.exe, r.entry, r.icon, r.settings]) assert.equal(existsSync(p), false, p);
// magpie took the menu entry itself; its icon and magpie:// default go too
assert.equal(existsSync(r.mime), false);
assert.doesNotMatch(u.stdout, /kept your settings/);
// magpie failing to: the command stays, to run it again
const f = installedDesktop(t, { env: { MAGPIE_FAIL: "1" } });
const fu = f.run(["--uninstall", "--purge"]);
assert.notEqual(fu.status, 0);
assert.match(fu.stderr, /magpie uninstall didn't finish/);
for (const p of [f.exe, f.entry, f.settings]) assert.ok(existsSync(p), p);
// no magpie to ask: refused, the settings kept
const n = installedDesktop(t);
rmSync(n.exe);
const nu = n.run(["--purge"]);
assert.notEqual(nu.status, 0);
assert.match(nu.stderr, /no magpie at .* --uninstall alone/);
assert.ok(existsSync(n.settings));
});
test("--uninstall in Termux takes off PREFIX/bin/magpie", (t) => {
const r = install(t, {});
assert.equal(r.status, 0, r.stderr);
const u = r.run(["--uninstall"]);
assert.equal(u.status, 0, u.stderr);
assert.equal(existsSync(join(r.bin, "magpie")), false);
assert.match(u.stdout, /removed .*com\.termux\/files\/usr\/bin\/magpie/);
});