Files
magpie/internal/gui/tests/plugin-npm-unknown.test.cjs
T
yetone a6217e83d3 plugins: a plugin npm said nothing of offers Install, not Coming soon (zhudabao8433 on Discord)
zhudabao8433 on Discord (#general, 11-01): PowerContext
(@magpie-community/gateway-powercontext) could be installed yesterday and
today reads 即将上线 again.

Cause: PowerContext was never pulled. It is in registry.json since
plugins 1e4c8a2 (10-29) and on the dist branch (0.2.0, every dist commit
since), and not on npm (E404), so magpie learns its version from the
dist branch's ~1.7MB index.json. Coming soon is drawn whenever a listing
has an npm answer with no version, and three paths gave one without
anything confirming the package is missing:
- builds before v0.1.2413 kept a dist read cut off at npmEach (6s) as
  "not on npm" in plugin-npm.json (ferock, e0eb74987). After an update,
  InfoCached (the listings) and Info ("what it said last") still served
  that empty answer while a new ask couldn't read the branch;
- the page turned every listing /api/plugins/npm said nothing of
  (unknown: npm or the branch not read in time) into { weekly: 0 }, i.e.
  Coming soon, and never asked again;
- /api/plugins/market gave every listing &NPM{} even when Info had no
  answer for it.

Fix:
- market.go: a cached "not on npm" over an hour old is unknown
  (npmEntry.missingStale): left out of InfoCached and of Info's seed.
  One said within the hour stands, as before.
- plugins.js: a listing npm said nothing of is marked unknown ({ u: 1 }), which
  offers Install (installDist waits for the branch with its own read);
  the page asks again up to 4 times, 15s apart, and the answer replaces
  it. A listing npm answered with no version still reads Coming soon.
- plugins.go: pluginMarketState leaves NPM nil when Info has no answer.

Old data: an old "not on npm" is unknown once an hour old, for every
user, with nothing to do on their side.

Tests:
- TestStaleMissingIsUnknownWhileDistUnread (internal/plugin): an
  hours-old "not on npm" with the dist branch 503 is in neither
  InfoCached nor Info; a minute-old one stands; once the branch answers,
  Info has 0.2.0.
- plugin-npm-unknown.test.cjs: PowerContext with no npm answer shows
  Install (enabled), Qoder answered without a version shows Coming soon,
  and after the 15s re-ask (page.clock) PowerContext shows v0.2.0. en,
  zh, zh-TW, ja, de; 980 and 420px; Chromium and WebKit.

Run (mote, at this commit rebased on origin/main 36af588e2):
- gofmt -l clean; go vet darwin, GOOS=windows, GOOS=linux -tags nogui on
  ./internal/plugin/... ./internal/gui/...: ok.
- go test -count=1 -timeout 45m ./internal/plugin/...: ok.
  ./internal/gui/...: TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  TestMainWindowCorners/macOS_26_SDK (#3949) red, the same on an
  origin/main 36af588e2 copy; TestCoreScriptBudget green (plugins.js is
  1 byte under main's, a comment cut, no raise).
- node --test plugin-npm-unknown (22/22), plugin-market, i18n-keys: ok.
  plugin-market webkit "the Plugins tab" was red once in the run after
  the Go suite, its output lost; 3 of 3 green after, alone.
- Broken by hand: the Go test is red on origin/main's market.go (an
  hours-old "not on npm" served; want it unknown); plugin-npm-unknown is
  red (waits for Install) on origin/main's plugins.js.
- Earlier GUI batch (70 files: plugin*, market*, i18n*,
  deprecation-notice, list-sort), before the rebase: 968/974; the reds
  are plugin-move-unserved (#4410, #4392, red the same on main) and
  plugin-github-filter chromium en 420px, batch only (#4679).

Not tried: a real slow network from China; zhudabao8433's version is not
known.
2026-11-01 12:12:05 +08:00

102 lines
6.1 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A suggested plugin npm said nothing of (npm, or the community repo's
// dist branch, not read in time) is unknown: its card offers Install, not
// a disabled Coming soon nothing confirmed (zhudabao8433 on Discord:
// PowerContext, only on the dist branch, installable the day before, read
// Coming soon). The page asks again, and the answer that comes shows its
// version. One npm said it doesn't have still reads Coming soon. Every
// language, both engines, wide and narrow; 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 PC = "@magpie-community/gateway-powercontext", QODER = "@magpie-community/opencode-qoder-auth";
const listings = [
{ package: PC, name: "PowerContext", kind: "middleware", community: true, summary: { en: "Recall from your PowerContext service." } },
{ package: QODER, name: "Qoder", icon: "qoder", providers: ["qoder"], community: true, summary: { en: "Your Qoder subscription." }, npm: { weekly: 0 } },
];
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const state = { bun: true, bunVersion: "1.4.2", plugins: [] };
const L = {
en: { install: "Install", soon: "Coming soon" },
zh: { install: "安装", soon: "即将上线" },
"zh-TW": { install: "安裝", soon: "即將上線" },
ja: { install: "インストール", soon: "近日公開" },
de: { install: "Installieren", soon: "Demnächst" },
};
function server(lang, asks) {
return async (route) => {
const url = new URL(route.request().url());
const json = (data) => route.fulfill({ json: data });
if (url.host !== "magpie.test") return route.fulfill({ status: 404, body: "" });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
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" } });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true }, plugins: [] });
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
if (url.pathname === "/api/plugins") return json(state);
if (url.pathname === "/api/plugins/listings") return json({ listings });
if (url.pathname === "/api/plugins/market") return json({ listings, state });
if (url.pathname === "/api/plugins/npm") {
const names = (url.searchParams.get("names") || "").split(",");
asks.push(names);
// the first ask: the dist branch not read in time, nothing said of
// PowerContext; the next: the branch read, its 0.2.0
return json({ npm: asks.length > 1 && names.includes(PC) ? { [PC]: { version: "0.2.0", weekly: 0 } } : {} });
}
if (url.pathname.startsWith("/api/")) return json({});
if (!/^\/[\w./-]*$/.test(url.pathname)) return route.fulfill({ status: 404, body: "" });
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": a plugin npm said nothing of offers Install, not Coming soon", async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of Object.keys(L)) for (const width of [980, 420]) {
await t.test(lang + " " + width, async () => {
const w = L[lang];
const ctx = await browser.newContext({ viewport: { width, height: 820 } });
const page = await ctx.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], asks = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.clock.install();
await page.route("**/*", server(lang, asks));
await page.goto("http://magpie.test/?view=plugins");
const act = (pkg) => page.locator(`#view-plugins .pm-card[data-pkg="${pkg}"] .pm-act`);
await act(PC).waitFor();
// npm's first answer, with nothing of PowerContext, has come
for (let i = 0; i < 100 && !asks.length; i++) await sleep(50);
assert.equal(asks.length, 1, "npm asked once");
await page.waitForFunction(([sel, word]) => document.querySelector(sel)?.textContent === word,
[`#view-plugins .pm-card[data-pkg="${PC}"] .pm-act`, w.install]);
assert.equal(await act(PC).isDisabled(), false, "Install can be clicked");
assert.equal(await act(QODER).innerText(), w.soon, "one npm said it doesn't have is still Coming soon");
assert.equal(await act(QODER).isDisabled(), true);
const box = await act(PC).boundingBox();
assert.ok(box && box.x >= 0 && box.x + box.width <= width, `Install fits: ${JSON.stringify(box)}`);
// asked again, the branch now read: its version shows
await page.clock.fastForward(16000);
for (let i = 0; i < 100 && asks.length < 2; i++) await sleep(50);
assert.ok(asks.length >= 2 && asks[1].includes(PC), "npm asked again of PowerContext: " + JSON.stringify(asks));
await page.locator(`#view-plugins .pm-card[data-pkg="${PC}"] .pm-ver`).waitFor();
assert.match(await page.locator(`#view-plugins .pm-card[data-pkg="${PC}"] .pm-ver`).innerText(), /0\.2\.0/);
assert.equal(await act(PC).innerText(), w.install);
assert.deepEqual(errors, []);
await ctx.close();
});
}
});
}