Files
magpie/internal/gui/tests/another-magpie.test.cjs
T
yetone b358677305 gui: open a server's magpie in a window of the app (xuanwo on X)
xuanwo runs magpie on a server and wants the desktop app to show that
magpie's routing and usage. Browser access in Settings › Network and
sharing goes the other way: it serves this app's page to a browser.

Settings › Network and sharing has a new section, Another magpie. It takes
an address and a key. You can paste the whole link the server printed
(`magpie daemon install --web`, `magpie web`, or another app's Browser
access), and its k= becomes the key. The app then opens that magpie's own
web page in a window of its own. The window signs in through the server's
own webGuard with ?k=, the same way a browser does, and is traded for its
cookie. Nothing new is served here: the page in the window talks to the
server alone and gets no bound calls from this app.

- remoteAddr:
  - https:// is used when no scheme is typed.
  - Plain http is allowed only to an IP, localhost, a .local name or a
    one-word name, so the key never crosses the internet in the clear.
    This is also the rule a Mac's webview applies.
  - userinfo and other schemes are refused.
- remoteProbe: before the window opens, it asks for the link without
  following redirects. Only a 303 that sets a magpie_web_* cookie counts
  as success. A 401, a 403 (allowed addresses), an untrusted certificate,
  no answer, or any other reply is reported in Settings, translated.
- remote-magpie.json:
  - It sits beside settings.json, mode 0600, and the key is never in
    settings.json.
  - GET /api/remote returns the address and hasKey, never the key.
  - POST with no key reuses the key kept for the same address.
  - POST /api/remote/forget removes the file.
- The routes are on the app's own windows' API only (!isWeb). A host that
  can't open windows refuses.

Run (on mote, at this tree):
- go test -count=1 ./internal/gui/: ok.
- Go tests:
  - TestRemoteAddr
  - TestRemoteOpensTheOtherMagpie: real webHandler; wrong or missing key
    refused, nothing kept; the pasted link opens; 0600; the key is not
    returned; a cookie-jar client following the opened link reaches the
    page behind the key; the kept key is reused; forget.
  - TestRemoteNotMagpie
  - TestRemoteNotOnBrowserPages
- GUI test, new: another-magpie.test.cjs. Covers 5 languages, 360px,
  and a browser page with no section that never asks /api/remote.
- GUI suite: every GUI file that draws Settings › Network (18, the new one included), plus
  i18n-keys, i18n-en-no-cjk and gui-de/ja/zh-tw. Chromium and WebKit:
  307/307 pass. Also remote-magpie.test.cjs (the Remote magpie
  provider's add sheet), run with the new file at the rebased head.
- gofmt -l: clean.
- go vet: darwin, GOOS=linux -tags nogui, and GOOS=windows on
  internal/gui; ok.

Broken by hand:
- A 303 without the magpie cookie taken as signed in:
  TestRemoteNotMagpie red.
- remoteRoutes outside !isWeb: TestRemoteNotOnBrowserPages red (200).
- renderRemote drawn on a browser's page, and errors not translated:
  another-magpie.test.cjs red in zh/zh-TW/ja/de and the browser case,
  in both engines.

Not tried:
- The real Wails window on macOS, Windows or Linux.
- A real server over https.
- Links on the remote page that open a new window (target=_blank,
  window.open) inside the remote window.
- Settings › Text size does not rezoom a remote window that is already
  open.
2026-11-10 18:39:20 +08:00

147 lines
9.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// Settings › Network and sharing › Another magpie (xuanwo on X: a magpie on a
// server, its routing and usage, opened from the desktop app). The address
// and the key are posted to /api/remote, which opens the window; an error
// from it is said in the page's language; once kept, the key field shows it
// is kept (never the key) and Forget posts /api/remote/forget. A browser on
// the page (web) has no such section and never asks for it. Nothing wider
// than a 360px window. Every language, Chromium and WebKit; 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 { reach, watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const KEY = "0123456789abcdef0123456789abcdef";
function server(lang, web, posts, gets) {
let kept = { url: "", hasKey: false };
return async (route) => {
const req = route.request();
const url = new URL(req.url());
const json = (data, status = 200) => route.fulfill({ status, json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:${web},sideGateway:{url:"http://127.0.0.1:3425",running:true,boot:true}};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" }, web });
if (url.pathname === "/api/remote") {
if (req.method() === "GET") { gets.n++; return json(kept); }
const body = JSON.parse(req.postData());
posts.push(body);
if (body.key === "wrong-wrong-wrong-wrong") return json({ error: "that magpie turned the key away: use the key in the link it printed (MAGPIE_WEB_KEY, or the k= in it)" }, 400);
if (/^http:\/\/magpie\.example\.com/.test(body.url)) return json({ error: "magpie.example.com is a name on the internet: open it over https://, or the key crosses the network unencrypted" }, 400);
kept = { url: body.url.replace(/\?k=.*/, ""), hasKey: true };
return json(kept);
}
if (url.pathname === "/api/remote/forget") { posts.push("forget"); kept = { url: "", hasKey: false }; return json(kept); }
if (url.pathname === "/api/web-page") return json({ mode: "", port: 3430, serving: false });
if (url.pathname === "/api/settings") return json({ lang, theme: "light", gateway: "http://127.0.0.1:3425", lan: false, lanURLs: [], version: "0.1.900", dir: "/config/magpie", fx: { rate: 7.2, at: new Date().toISOString(), stale: false } });
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/groups") return json({ groups: [], models: [] });
if (url.pathname.startsWith("/api/")) return 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 });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": Another magpie opens a server's magpie with its key", 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 pages = [];
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
for (const [i, p] of pages.entries()) await p.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-another-magpie-${i}.png`), fullPage: true });
}
await browser.close();
});
const open = async (lang, web, width = 900) => {
const page = await (await browser.newContext({ viewport: { width, height: 700 }, reducedMotion: "reduce" })).newPage();
pages.push(page);
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], posts = [], gets = { n: 0 };
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, web, posts, gets));
await watchSettingsReads(page);
await page.goto("http://localhost:3430/?view=settings&tab=network");
await settingsDrawn(page);
const tr = (s, vars) => page.evaluate(([s, vars]) => t(s, vars), [s, vars]);
return { page, errors, posts, gets, tr };
};
// posts are counted, not a time waited
const until = async (page, f) => { for (let i = 0; i < 250 && !f(); i++) await page.waitForTimeout(20); assert(f()); };
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
await t.test(lang, async () => {
const { page, errors, posts, tr } = await open(lang, false);
const head = page.locator("#remoteHead");
const list = page.locator("#remoteList");
await list.locator(".row.pref").first().waitFor();
assert.equal((await head.textContent()).trim(), await tr("Another magpie"));
if (lang !== "en") assert.notEqual(await tr("Another magpie"), "Another magpie", "translated");
assert.equal(await list.locator(".row.pref").count(), 2);
const addr = list.locator("input.remote-url"), key = list.locator("input.remote-key");
assert.equal(await key.getAttribute("type"), "password");
assert.equal(await list.getByRole("button", { name: await tr("Forget") }).count(), 0, "nothing kept: no Forget");
// a name on the internet over http: said in the page's language
await reach(page, addr);
await addr.fill("http://magpie.example.com:3430");
await key.fill(KEY);
const openBtn = list.getByRole("button", { name: await tr("Open"), exact: true });
await openBtn.click();
await page.locator("#status").filter({ hasText: await tr("{host} is a name on the internet: open it over https://, or the key crosses the network unencrypted", { host: "magpie.example.com" }) }).waitFor();
// a wrong key
await addr.fill("https://magpie.example.com");
await key.fill("wrong-wrong-wrong-wrong");
await openBtn.click();
await page.locator("#status").filter({ hasText: await tr("That magpie turned the key away: use the key in the link it printed") }).waitFor();
// the link it printed, with Enter
await key.fill("");
await addr.fill("https://magpie.example.com/?k=" + KEY);
await addr.press("Enter");
await until(page, () => posts.some((p) => p.url === "https://magpie.example.com/?k=" + KEY));
assert.deepEqual(posts.at(-1), { url: "https://magpie.example.com/?k=" + KEY, key: "" });
const forget = list.getByRole("button", { name: await tr("Forget") });
await forget.waitFor();
assert.equal(await addr.inputValue(), "https://magpie.example.com/", "kept without its key");
assert.equal(await key.inputValue(), "", "the key is never put back");
assert.equal(await key.getAttribute("placeholder"), await tr("Kept"));
assert.equal(await page.locator("#setPage-network select").count(), 0, "no native select");
await forget.click();
await until(page, () => posts.includes("forget"));
await page.waitForFunction(() => !document.querySelector("#remoteList input.remote-url").value);
assert.deepEqual(errors, []);
});
}
await t.test("360px wide, German", async () => {
const { page, errors, tr } = await open("de", false, 360);
const list = page.locator("#remoteList");
await list.locator("input.remote-url").waitFor();
await list.locator("input.remote-url").fill("https://magpie.example.com/?k=" + KEY);
await list.getByRole("button", { name: await tr("Open"), exact: true }).click();
await list.getByRole("button", { name: await tr("Forget") }).waitFor();
const over = await list.evaluate((l) => [...l.querySelectorAll(".row.pref")].filter((r) => r.scrollWidth > r.clientWidth + 1 || r.getBoundingClientRect().right > innerWidth + 1).map((r) => r.textContent.slice(0, 40)));
assert.deepEqual(over, [], "rows wider than the window");
assert.deepEqual(errors, []);
});
await t.test("a browser on the page has no Another magpie", async () => {
const { page, errors, gets } = await open("en", true);
await page.locator("#portList .row").first().waitFor();
assert.equal(await page.locator("#remoteHead").isVisible(), false);
assert.equal(await page.locator("#remoteList .row").count(), 0);
assert.equal(gets.n, 0, "never asks /api/remote");
assert.deepEqual(errors, []);
});
});
}