Files
magpie/internal/gui/tests/library-rtk-upgrade.test.cjs
T
yetone aad799eae6 library: an RTK release winget or Homebrew doesn't have yet says "v0.51.0 is out · waiting for winget" with no Upgrade, until it does (#1025, Fermin214)
On Windows, RTK installed by winget was 0.50.0 and GitHub had 0.51.0, which winget didn't yet. Library → RTK showed "v0.51.0 可升级" with Upgrade; Upgrade ran winget upgrade, which rightly said "winget's RTK is 0.50.0 so far…", and the card went back to "可升级" with Upgrade. The card compared rtk's version only with GitHub's latest release, never with what the package manager that installed it has, and the upgrade's answer was forgotten as soon as the card was read again.

RTKView.CheckLatest now also asks, when the latest release is newer than this rtk and it was installed by winget or Homebrew, what that package manager has: winget show --id rtk-ai.rtk --exact (the version is the first one after the line naming the package, as winget's labels are in the system's language), and brew info --json=v2 rtk without auto-update, as the Agents page's CLI updates read Homebrew. An upgrade through either records the version it left as the newest that package manager has. The answer is kept six hours (a failure fifteen minutes) and is asked again after that. When it has nothing newer than this rtk, the view says waiting (winget or Homebrew) and waitingHas. The card then shows "v{v} is out · waiting for {via}" (zh "v{v} 已发布 · 等待 {via}", ja, de), with a tooltip saying what it has, and no Upgrade button. Once it has the release, Upgrade is back. A package manager that can't be asked, or doesn't say, leaves Upgrade offered as before. GET /api/library/rtk runs the whole check within its 3s budget. magpie library rtk prints the same. Cargo (built from rtk's repository) and rtk's own script (its GitHub releases) can't lag, so they are unchanged. The Agents page's CLI updates already compare with the installing channel's own latest (Homebrew's or npm's), so they don't have this bug. The Japanese "v{v} is out" is shortened to "v{v} 公開済み", because with Upgrade beside it the card head overflowed at 440px.

TestRTKWaitsForWinget (a fake winget whose show prints Chinese labels: unaskable leaves Upgrade; asked, it waits; the reporter's upgrade waits with the "so far" note, and so does the card read again; winget given 0.51.0 and asked again, Upgrade is back and works) and the extended TestRTKUpgradeBrew (Homebrew's 0.49.0 after brew upgrade waits, on the card read again too) fail without the change (waiting <nil>) and pass. TestRTKChannelVersion covers winget show in English, German and Chinese with spinner frames, no package, and brew info's JSON. -race -count=10 on them. library-rtk-upgrade.test.cjs's new test (Upgrade finds winget behind: the waiting tag, no button, the note; a reload still waits; winget has it: Upgrade back; card head and page fit at 440px) fails without the library.js change and passes on Chromium and WebKit in en, zh, ja and de. i18n-keys, gui-ja, gui-de and the library-rtk-* tests pass on both engines. go test -tags nogui ./internal/..., go vet, gofmt -l, darwin, linux (nogui) and windows builds pass. Not tried with a real winget or on Windows: winget show's output is taken from its documented format, and the version line is assumed to follow the package line in every language.
2026-10-06 20:56:52 +08:00

205 lines
11 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// #741: on Windows, Library → RTK → Upgrade showed only a red, cut-short
// "winget upgrade --id rtk-ai.rtk --exact --silent ..." in the status pill —
// no result, no exit code, no version. While it runs, the card now says what
// it runs; a failure stays on the card with the tool's exit code and what it
// said (the backend's message, faked here), and the card is read again for
// the version it left; a success says the new version, which the card
// shows. 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 = "C:/Users/aimer";
const UPGRADE = "winget upgrade --id rtk-ai.rtk --exact --silent --accept-package-agreements --accept-source-agreements --disable-interactivity";
const WHY = "winget failed (exit code 0x8A150101): RTK is in use — close the agents running it and try again";
const agent = (id, name) => ({ id, name, icon: "", skills: `${HOME}/.${id}/skills`, mcp: `${HOME}/.${id}/mcp.json` });
const lib = () => ({
dir: `${HOME}/.magpie/library`, backups: `${HOME}/.magpie/backups`, home: HOME,
agents: [agent("codex", "Codex")],
instructions: { agents: [], sets: [] }, foundServers: [], projects: [], foundSkills: [], skills: [], servers: [],
});
const rtkView = (version) => ({
path: `${HOME}/AppData/Local/Microsoft/WinGet/Links/rtk.exe`, version, latest: "0.51.0", upgrade: UPGRADE, url: "https://www.rtk-ai.app",
agents: [{ id: "codex", name: "Codex", icon: "", on: true }],
});
function server(lang, state) {
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/plugins") return route.fulfill({ json: { plugins: [] } });
if (url.pathname === "/api/library") return route.fulfill({ json: lib() });
if (url.pathname === "/api/library/rtk") { state.reads++; return route.fulfill({ json: rtkView(state.version) }); }
if (url.pathname === "/api/library/rtk/upgrade") {
await state.gate;
if (state.fail) return route.fulfill({ status: 400, json: { error: WHY } });
state.version = "0.51.0";
return route.fulfill({ json: rtkView(state.version) });
}
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: { button: "Upgrade", running: `Running ${UPGRADE} — this can take a few minutes.`, failed: `The upgrade failed: ${WHY}`, pill: "RTK's upgrade failed — the RTK card says why", done: "RTK is now 0.51.0" },
zh: { button: "升级", running: `正在运行 ${UPGRADE},可能需要几分钟。`, failed: `升级失败:${WHY}`, pill: "RTK 升级失败,原因见 RTK 卡片", done: "RTK 已升级到 0.51.0" },
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": an RTK upgrade says how it went", 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 lang of ["en", "zh"]) {
await t.test(lang, async () => {
const w = words[lang];
let open;
const state = { version: "0.43.0", fail: true, reads: 0, gate: new Promise((r) => { open = r; }) };
const ctx = await browser.newContext({ viewport: { width: 980, height: 800 } });
await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", "rtk"); } catch {} });
const page = await ctx.newPage();
page.setDefaultTimeout(5000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, state));
await page.goto("http://magpie.test/");
await page.locator('button[data-view="library"]').click();
const v = page.locator("#view-library");
const button = v.locator(".lib-cardhead button");
await button.waitFor();
assert.equal(await button.textContent(), w.button);
// while it runs: what it runs, on the card
await button.click();
const running = v.locator(".lib-card p", { hasText: UPGRADE });
await running.waitFor();
assert.equal(await running.textContent(), w.running);
// it fails: why, on the card, kept past the pill; the card read again
const reads = state.reads;
open();
const err = v.locator(".lib-rtk-err");
await err.waitFor();
assert.equal(await err.textContent(), w.failed);
assert.equal(await page.locator("#status").textContent(), w.pill);
for (let i = 0; i < 50 && state.reads === reads; i++) await page.waitForTimeout(20);
assert(state.reads > reads, "the card wasn't read again after the failure");
assert.equal(await running.count(), 0);
// again, and it works: the error goes, the new version is shown
state.fail = false;
await v.locator(".lib-cardhead button").click();
await page.waitForFunction((want) => document.querySelector("#status").textContent === want, w.done).catch(() => {});
assert.equal(await page.locator("#status").textContent(), w.done);
assert.equal(await err.count(), 0);
assert.equal(await v.locator(".lib-cardhead .note").first().textContent(), "v0.51.0");
await ctx.close();
});
}
assert.deepEqual(errors, []);
});
}
// #1025 (Fermin214): RTK 0.51.0 was out on GitHub while winget had 0.50.0,
// the one installed. Upgrade ran winget upgrade, which said so, and the card
// went back to "v0.51.0 is out" with Upgrade. Now the card says the release
// is out and waits for winget, with no Upgrade button — from the upgrade's
// answer and when the card is read again — and offers Upgrade again once
// winget has it. In every language, at 440px.
const waitWords = {
en: { out: "v0.51.0 is out", waiting: "v0.51.0 is out · waiting for winget", button: "Upgrade" },
zh: { out: "v0.51.0 可升级", waiting: "v0.51.0 已发布 · 等待 winget", button: "升级" },
ja: { out: "v0.51.0 公開済み", waiting: "v0.51.0 公開済み · winget 待ち", button: "アップグレード" },
de: { out: "v0.51.0 ist da", waiting: "v0.51.0 ist da · wartet auf winget", button: "Aktualisieren" },
};
const NOTE = "winget's RTK is 0.50.0 so far; RTK 0.51.0 is out, and winget usually has it within a few days";
function waitServer(lang, state) {
const base = server(lang, state);
return async (route) => {
const url = new URL(route.request().url());
const view = () => ({ ...rtkView("0.50.0"), ...(state.wingetHas ? {} : { waiting: "winget", waitingHas: "0.50.0" }) });
if (url.pathname === "/api/library/rtk") { state.reads++; return route.fulfill({ json: state.checked ? view() : rtkView("0.50.0") }); }
if (url.pathname === "/api/library/rtk/upgrade") { state.checked = true; return route.fulfill({ json: { ...view(), note: NOTE } }); }
return base(route);
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": RTK waits for winget to have its release", async (t) => {
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 lang of ["en", "zh", "ja", "de"]) {
await t.test(lang, async () => {
const w = waitWords[lang];
const state = { reads: 0, checked: false, wingetHas: false };
const ctx = await browser.newContext({ viewport: { width: 440, height: 800 } });
await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", "rtk"); } catch {} });
const page = await ctx.newPage();
page.setDefaultTimeout(5000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", waitServer(lang, state));
const open = async () => {
await page.goto("http://magpie.test/");
await page.locator('button[data-view="library"]').click();
await page.locator("#view-library .lib-cardhead .lib-tag").waitFor();
};
const head = page.locator("#view-library .lib-cardhead").first();
const fits = async () => {
const box = await head.evaluate((h) => ({ sw: h.scrollWidth, cw: h.clientWidth, doc: document.documentElement.scrollWidth, vw: innerWidth }));
assert(box.sw <= box.cw + 1, `card head overflows: ${JSON.stringify(box)}`);
assert(box.doc <= box.vw, `page scrolls sideways: ${JSON.stringify(box)}`);
};
// before any check: out, with Upgrade
await open();
assert.equal(await head.locator(".lib-tag").textContent(), w.out);
assert.equal(await head.locator("button").textContent(), w.button);
// the upgrade finds winget has nothing newer: waiting, no button
await head.locator("button").click();
await page.waitForFunction((want) => document.querySelector("#view-library .lib-cardhead .lib-tag")?.textContent === want, w.waiting);
assert.equal(await head.locator("button").count(), 0);
await page.locator("#view-library .lib-rtk-note", { hasText: NOTE }).waitFor();
await fits();
// the card read again (a new page) still waits
const reads = state.reads;
await open();
assert(state.reads > reads);
assert.equal(await head.locator(".lib-tag").textContent(), w.waiting);
assert.equal(await head.locator("button").count(), 0);
await fits();
// winget has 0.51.0 now: Upgrade is back
state.wingetHas = true;
await open();
assert.equal(await head.locator(".lib-tag").textContent(), w.out);
assert.equal(await head.locator("button").textContent(), w.button);
await fits();
await ctx.close();
});
}
assert.deepEqual(errors, []);
});
}