Files
magpie/site/install.test.mjs
T
yetone 2cfb8351d0 install.sh: a Linux desktop without WebKitGTK 4.1 has it installed through sudo, then gets the desktop app (shuhai)
shuhai on Discord, on f68ddca37's hint: "为什么不能帮用户直接装好?" On a
desktop without WebKitGTK 4.1 the installer put in the terminal build and
told the user to `sudo apt install libwebkit2gtk-4.1-0` and run it again.

Now, on a desktop without the libraries (and with --gui), when someone is
at the terminal, the script installs WebKitGTK 4.1 (GTK 3 comes with it)
with the distribution's package manager, says first in one line what and
why, and installs the desktop app:
- apt-get install -y libwebkit2gtk-4.1-0 (Debian/Ubuntu family), and when
  that fails, apt-get update and once more (lists missing or stale);
- dnf install -y webkit2gtk4.1 (Fedora/RHEL family);
- pacman -S --needed --noconfirm webkit2gtk-4.1 (Arch family; no -Sy, so
  no partial upgrade);
- zypper --non-interactive install libwebkit2gtk-4_1-0 (openSUSE).
Through sudo, which asks for the password itself; as root, without it.
"At the terminal" is /dev/tty opening (in a subshell, since a failed
redirection would end the script): curl | sh gives the script the pipe
for stdin, so every package command reads /dev/tty too, never the script.
No terminal, no sudo, the package manager missing, Void/Gentoo/another
distribution, or the install failing: the terminal build and the command,
as before, with "didn't work (see above)" when it was tried. --tui and
MAGPIE_CLI=1 install no package; a headless computer neither.
MAGPIE_TTY points the check at another file for the tests.
Docs: reference.md Install, README en/zh, docs start (en/zh/ja).

Run:
- sh -n, dash -n; shellcheck -s sh: only SC2010 at the uninstall's
  ls | grep, there before.
- site/install.test.mjs, 5 new tests (fake sudo, id and package managers;
  every distro's command, root without sudo, no tty, no sudo on PATH,
  Void, NixOS, a failing install, apt's update-and-retry, --gui, --tui,
  MAGPIE_CLI, headless): red on origin/main's install.sh (the 5 new fail,
  19 pass), green after. CI's site step (sh -n + node --test
  site/*.test.mjs) at this head, rebased on origin/main: 63/63 on mote
  (macOS sh) and 63/63 on magpie-desktop through wsl-run (/bin/sh = dash).
- Real installs in fresh containers (Docker on xiaomi-gpu, amd64), a user
  u with sudo, DISPLAY=:0, `cat install.sh | sh` (stdin a pipe), the
  terminal kept with setpriv (su -c starts a session without one),
  v0.1.2399/2400 from the real feed; then ldd (nothing missing),
  `magpie version`, and the package query:
  - Debian 12, sudo with a password, typed by expect at sudo's prompt,
    package lists deleted first: install failed "Unable to locate", apt-get
    update, retry ok, desktop app, libwebkit2gtk-4.1-0 installed;
  - Ubuntu 24.04 (NOPASSWD): libwebkit2gtk-4.1-0, desktop app;
  - Fedora 44, password via expect: webkit2gtk4.1, desktop app;
  - Arch (archlinux:latest): webkit2gtk-4.1 2.54.2, desktop app;
  - openSUSE Tumbleweed and Leap 15.6: libwebkit2gtk-4_1-0, desktop app;
  - Debian 12 as root: apt-get without sudo, desktop app;
  - Ubuntu 24.04 --gui: installed, desktop app; --tui: terminal build,
    nothing installed;
  - Debian 12 without -t (no terminal): terminal build + the apt line, no
    install tried; without sudo at a terminal: the same;
  - Ubuntu 20.04 (no WebKitGTK 4.1 in focal): install, update, retry all
    fail "Unable to locate", then the terminal build and the "didn't work
    (see above)" hint.

Not tried: a real desktop VM (the containers fake one with DISPLAY=:0);
the installed app wasn't opened, only `magpie version`. Void and Gentoo
stay hint-only (Gentoo would build WebKit from source). Debian 11 failed
in the container's own setup (archives unreachable), so focal stood in
for a distribution without 4.1. A held dpkg lock (unattended-upgrades)
would fail the install and fall back; not reproduced. On focal the hint
still names libwebkit2gtk-4.1-0, which focal lacks, as before.
2026-10-31 21:52:54 +08:00

519 lines
26 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, symlinkSync } 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));
// systemPath: a folder of links to what the script runs, without sudo, for
// a computer that has none
function systemPath(dir) {
const sys = join(dir, "sys");
mkdirSync(sys, { recursive: true });
for (const n of ["sh", "dash", "bash", "sed", "head", "tr", "grep", "cut", "mktemp", "rm", "mkdir", "install", "cat", "sha256sum", "shasum", "ls", "rmdir", "mv", "ln", "chmod", "env", "perl"]) {
const w = spawnSync("sh", ["-c", `command -v ${n}`], { encoding: "utf8" }).stdout.trim();
if (w.startsWith("/")) symlinkSync(w, join(sys, n));
}
return sys;
}
function install(t, { termux = true, arch = "aarch64", override = false, prefixOnly = false, missing = false, corrupt = false, desktop = false, github = false, args = [], env: extra = {}, tamper = "", libs = desktop, cli = !desktop, distro = "ID=ubuntu\nID_LIKE=debian\n", sessions = [], login = "", tty = false, uid = 1000, sudo = true, pmFails = false } = {}) {
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 new Set([file, `magpie-cli-linux-${cpu}`, `magpie-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 });
// ldconfig -p as glibc prints it: the libraries the desktop app needs
// when libs, and another library always (a cache that was read)
const cache = ["libc.so.6", ...(libs ? ["libgtk-3.so.0", "libwebkit2gtk-4.1.so.0"] : [])]
.map((n) => `\t${n} (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/${n}`).join("\n");
writeFileSync(join(tools, "ldconfig.txt"), cache + "\n");
writeFileSync(join(tools, "ldconfig"), `#!/bin/sh\ncat "${join(tools, "ldconfig.txt").replaceAll("\\", "/")}"\n`, { mode: 0o755 });
// logind: the graphical session this user is logged in to, if any
// (INSTALL_LOGIN "<id> <type>"); logind names a tty login when there is
// no graphical one
writeFileSync(join(tools, "loginctl"), `#!/bin/sh
set -- "$@" "" "" "" "" ""
id=\${INSTALL_LOGIN%% *} type=\${INSTALL_LOGIN#* }
[ "$1 $3 $4 $5" = "show-user -p Display --value" ] && [ -n "$id" ] && echo "$id"
[ "$1 $2 $3 $4 $5" = "show-session $id -p Type --value" ] && [ -n "$id" ] && echo "$type"
exit 0
`, { mode: 0o755 });
// the package managers, sudo and id: each call logged ("sudo" before a
// command sudo ran), and an install puts the libraries in ldconfig's
// cache unless it fails
const pmLog = join(dir, "pm-log");
const ldTxt = join(tools, "ldconfig.txt").replaceAll("\\", "/");
for (const pm of ["apt-get", "dnf", "pacman", "zypper"]) {
writeFileSync(join(tools, pm), `#!/bin/sh
echo "\${INSTALL_SUDO:+sudo }${pm} $*" >> "$INSTALL_PM_LOG"
case "$*" in update|"") exit 0 ;; esac
[ -z "$INSTALL_PM_FAILS" ] || exit 100
[ "$INSTALL_PM_FAILS_ONCE" != 1 ] || [ -e "$INSTALL_PM_LOG.once" ] || { : > "$INSTALL_PM_LOG.once"; exit 100; }
printf '\\tlibgtk-3.so.0 (libc6,x86-64) => /usr/lib/libgtk-3.so.0\\n\\tlibwebkit2gtk-4.1.so.0 (libc6,x86-64) => /usr/lib/libwebkit2gtk-4.1.so.0\\n' >> "${ldTxt}"
`, { mode: 0o755 });
}
if (sudo) writeFileSync(join(tools, "sudo"), `#!/bin/sh
INSTALL_SUDO=1 exec "$@"
`, { mode: 0o755 });
writeFileSync(join(tools, "id"), `#!/bin/sh
case "$1" in -u) echo ${uid};; *) echo tester;; esac
`, { mode: 0o755 });
const dataDirs = join(dir, "data-dirs");
for (const s of sessions) {
mkdirSync(join(dataDirs, s.split("/")[0]), { recursive: true });
writeFileSync(join(dataDirs, s), "[Desktop Entry]\n");
}
writeFileSync(join(dir, "os-release"), distro);
// 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: cli ? "1" : "",
// a headless shell unless a test gives it a desktop
DISPLAY: "",
WAYLAND_DISPLAY: "",
XDG_CURRENT_DESKTOP: "",
XDG_SESSION_DESKTOP: "",
DESKTOP_SESSION: "",
XDG_SESSION_TYPE: "",
XDG_DATA_DIRS: dataDirs,
INSTALL_LOGIN: login,
MAGPIE_OS_RELEASE: join(dir, "os-release"),
// no terminal unless a test gives it one: the run never reaches a real
// sudo or package manager
MAGPIE_TTY: tty ? join(dir, "tty") : join(dir, "no-tty"),
INSTALL_PM_LOG: pmLog,
INSTALL_PM_FAILS: pmFails ? "1" : "",
PATH: tools + ":" + (sudo ? process.env.PATH : systemPath(dir)),
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,
};
if (tty) writeFileSync(join(dir, "tty"), "");
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, pm: read(pmLog) };
};
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/);
});
// shuhai on Discord: on a Linux desktop the script installed the terminal
// build, with one line at the end about WebKitGTK. The build is chosen with
// nothing asked: the desktop app when its libraries are there; on a
// desktop without them, the terminal build and, said last, the command
// that installs them on this distribution; --gui and --tui choose instead.
function linux(t, opts) {
const r = install(t, { termux: false, cli: false, ...opts });
assert.equal(r.status, 0, r.stderr);
const gui = /magpie-linux-arm64/.test(r.requests);
assert.notEqual(gui, /magpie-cli-linux-arm64/.test(r.requests), r.requests);
assert.equal(existsSync(join(r.share, "applications/magpie.desktop")), gui);
return { ...r, gui };
}
const last = (r, n) => r.stdout.trimEnd().split("\n").slice(-n).join("\n");
test("Linux with GTK 3 and WebKitGTK 4.1 gets the desktop app, with nothing to install", (t) => {
for (const env of [{ DISPLAY: ":0" }, { WAYLAND_DISPLAY: "wayland-0" }, {}]) {
const r = linux(t, { libs: true, env });
assert.ok(r.gui, JSON.stringify(env));
assert.doesNotMatch(r.stdout, /WebKitGTK|apt install/);
}
});
test("a desktop without WebKitGTK gets the terminal build and its distribution's command, said last", (t) => {
const desktops = [
{ env: { DISPLAY: ":0" } },
{ env: { WAYLAND_DISPLAY: "wayland-0" } },
{ env: { XDG_CURRENT_DESKTOP: "XFCE" } },
{ env: { XDG_SESSION_TYPE: "wayland" } },
// ssh into a computer whose user is logged in to its desktop
{ login: "2 x11" },
{ login: "c3 wayland" },
// or that has a desktop to log in to
{ sessions: ["xsessions/xfce.desktop"] },
{ sessions: ["wayland-sessions/gnome.desktop"] },
];
for (const opts of desktops) {
const r = linux(t, { libs: false, ...opts });
assert.equal(r.gui, false, JSON.stringify(opts));
assert.match(r.stdout, /has a desktop, but the desktop app needs GTK 3 and WebKitGTK 4\.1/);
assert.match(last(r, 4), /sudo apt install libwebkit2gtk-4\.1-0/, JSON.stringify(opts));
assert.match(last(r, 4), /install\.sh \| sh -s -- --gui/);
}
for (const [distro, want] of [
["ID=fedora\n", /sudo dnf install webkit2gtk4\.1/],
['ID="linuxmint"\nID_LIKE="ubuntu debian"\n', /sudo apt install libwebkit2gtk-4\.1-0/],
["ID=endeavouros\nID_LIKE=arch\n", /sudo pacman -S --needed webkit2gtk-4\.1/],
['ID="opensuse-tumbleweed"\nID_LIKE="opensuse suse"\n', /sudo zypper install libwebkit2gtk-4_1-0/],
["ID=nixos\n", /install WebKitGTK 4\.1 \(webkit2gtk 4\.1\) from your distribution/],
]) {
const r = linux(t, { libs: false, distro, env: { DISPLAY: ":0" } });
assert.match(r.stdout, want, distro);
}
});
test("a headless Linux without WebKitGTK gets the terminal build and no desktop advice", (t) => {
// no login at all, or a tty login (an ssh shell's, on a server or in WSL)
for (const login of ["", "135 tty"]) {
const r = linux(t, { libs: false, login });
assert.equal(r.gui, false);
assert.match(r.stdout, /installed the terminal build/);
assert.doesNotMatch(r.stdout, /WebKitGTK|apt install|--gui/, login);
}
});
test("--gui installs the desktop app before its libraries, and says what installs them", (t) => {
const r = linux(t, { libs: false, args: ["--gui"] });
assert.ok(r.gui);
assert.match(last(r, 2), /aren't installed; it opens once they are/);
assert.match(last(r, 2), /sudo apt install libwebkit2gtk-4\.1-0/);
// the flag wins over MAGPIE_CLI=1
const over = linux(t, { libs: true, cli: true, args: ["--gui"] });
assert.ok(over.gui);
assert.doesNotMatch(over.stdout, /aren't installed/);
});
test("--tui and MAGPIE_CLI=1 install the terminal build on a desktop with WebKitGTK", (t) => {
for (const opts of [{ args: ["--tui"] }, { cli: true }]) {
const r = linux(t, { libs: true, env: { DISPLAY: ":0" }, ...opts });
assert.equal(r.gui, false, JSON.stringify(opts));
assert.doesNotMatch(r.stdout, /WebKitGTK/);
}
const help = install(t, { termux: false, args: ["--help"] });
assert.equal(help.status, 0);
assert.match(help.stdout, /--gui .*desktop app/);
assert.match(help.stdout, /--tui .*terminal build/);
});
// shuhai on Discord: "为什么不能帮用户直接装好?" A desktop without
// WebKitGTK 4.1 has it installed with the distribution's package manager,
// through sudo, when someone is at the terminal (curl | sh: /dev/tty, not
// stdin), and then gets the desktop app. Anything else keeps the terminal
// build and the command.
test("a desktop without WebKitGTK at a terminal has it installed through sudo and gets the desktop app", (t) => {
for (const [distro, want] of [
["ID=ubuntu\nID_LIKE=debian\n", "sudo apt-get install -y libwebkit2gtk-4.1-0"],
["ID=fedora\n", "sudo dnf install -y webkit2gtk4.1"],
["ID=endeavouros\nID_LIKE=arch\n", "sudo pacman -S --needed --noconfirm webkit2gtk-4.1"],
['ID="opensuse-tumbleweed"\nID_LIKE="opensuse suse"\n', "sudo zypper --non-interactive install libwebkit2gtk-4_1-0"],
]) {
const r = linux(t, { libs: false, tty: true, distro, env: { DISPLAY: ":0" } });
assert.ok(r.gui, distro + r.stdout);
assert.equal(r.pm, want + "\n", distro);
// what and why, said before sudo asks
assert.match(r.stdout, new RegExp(`needs WebKitGTK 4\\.1, so installing it: ${want.replace(/[.+]/g, "\\$&")} \\(sudo may ask for your password\\)`));
assert.doesNotMatch(r.stdout, /aren't installed|didn't work|run this again/);
}
});
test("as root the package manager runs without sudo", (t) => {
const r = linux(t, { libs: false, tty: true, uid: 0, env: { DISPLAY: ":0" } });
assert.ok(r.gui);
assert.equal(r.pm, "apt-get install -y libwebkit2gtk-4.1-0\n");
assert.doesNotMatch(r.stdout, /sudo/);
});
test("with no terminal, no sudo, an unknown distribution or a failed install, the terminal build and the command", (t) => {
for (const opts of [
{ tty: false },
{ tty: true, sudo: false },
{ tty: true, distro: "ID=void\n" },
{ tty: true, distro: "ID=nixos\n" },
]) {
const r = linux(t, { libs: false, env: { DISPLAY: ":0" }, ...opts });
assert.equal(r.gui, false, JSON.stringify(opts));
assert.equal(r.pm, "", JSON.stringify(opts));
assert.doesNotMatch(r.stdout, /installing it|didn't work/);
assert.match(r.stdout, /has a desktop, but the desktop app needs GTK 3 and WebKitGTK 4\.1, which aren't installed/);
}
const f = linux(t, { libs: false, tty: true, pmFails: true, env: { DISPLAY: ":0" } });
assert.equal(f.gui, false);
// apt read its lists again and tried once more
assert.equal(f.pm, "sudo apt-get install -y libwebkit2gtk-4.1-0\nsudo apt-get update\nsudo apt-get install -y libwebkit2gtk-4.1-0\n");
assert.match(last(f, 5), /installing GTK 3 and WebKitGTK 4\.1 for the desktop app didn't work/);
assert.match(last(f, 4), /sudo apt install libwebkit2gtk-4\.1-0/);
});
test("a stale apt list is read again and the install tried once more", (t) => {
const r = linux(t, { libs: false, tty: true, env: { DISPLAY: ":0", INSTALL_PM_FAILS_ONCE: "1" } });
assert.ok(r.gui, r.stdout);
assert.equal(r.pm, "sudo apt-get install -y libwebkit2gtk-4.1-0\nsudo apt-get update\nsudo apt-get install -y libwebkit2gtk-4.1-0\n");
});
test("--gui installs WebKitGTK at a terminal too; --tui, MAGPIE_CLI=1 and a headless computer never do", (t) => {
const g = linux(t, { libs: false, tty: true, args: ["--gui"] });
assert.ok(g.gui);
assert.equal(g.pm, "sudo apt-get install -y libwebkit2gtk-4.1-0\n");
assert.doesNotMatch(g.stdout, /aren't installed/);
for (const opts of [{ args: ["--tui"], env: { DISPLAY: ":0" } }, { cli: true, env: { DISPLAY: ":0" } }, {}]) {
const r = linux(t, { libs: false, tty: true, ...opts });
assert.equal(r.gui, false, JSON.stringify(opts));
assert.equal(r.pm, "", JSON.stringify(opts));
}
});