P1 of docs/subsystems/plugin-runtime.md. P0 (3ccbe6bc1) gave built-in plugins a manifest, plugins.yml and a switch; their pages were still app.js's own code. Now the window has a kernel a plugin draws its page through, and the Gateway page moved into plugins/gateway to prove it. What changed: - internal/gui/assets/kernel.js (new): window.magpie. A plugin registers with magpie.plugin(id, setup); ctx.slots.add("page", part) puts its markup where index.html's <template data-slot="page"> is. The shell calls its hooks through magpie.call, behind an error boundary: a hook that throws, or whose promise fails, is logged and its page shows "This page stopped working", what broke and Try again, while every other page goes on. ctx.ui is the kit (button, switch, the app menu, a settings row) and ui.form draws a plugin's settings from their schema. ctx.config / onConfig carry the settings, from boot.js before the first paint and from Plugins › Built-in after. - internal/pluginrt/schema.go (new): a plugin's settings are a Go struct (Manifest.Settings); SchemaOf makes the JSON Schema the form is drawn from, and Validate is what SetConfig (config.go) takes into plugins.yml. Compose drops a hand-edited value the schema refuses. - plugins/gateway: the Gateway page as a plugin, Essential and in use while a gateway key is there. One setting, callsPage (10/20/50/100, 20 by default). Its client is plugins/gateway/{keys.js,client.js}, moved out of app.js (and caller-keys.js) unchanged in behaviour. - internal/gui: builtins.go takes POST {id, config} beside {id, on} and lists each plugin's schema and config; boot.js carries pluginConfig; /plugins/<id>/… files are served. Plugins › Built-in draws a plugin's settings rows under its switch. - Docs: plugin-runtime.md has "The window's kernel (P1)"; gui-shell.md, subsystems/README.md and tests/README.md updated. What was run: - go test ./... (sandbox HOME, pinned caches): all packages ok; gofmt -l clean; go vet on darwin, GOOS=linux -tags nogui and GOOS=windows. - New tests: TestSchemaOf, TestValidate, TestSetConfig, TestBuiltinConfig, TestBuiltinClientsLoaded; plugin-kernel.test.cjs (a page that throws breaks only itself and Try again draws it, at 1100 and 420 in en, zh, zh-TW, ja, de; callsPage from boot.js and from Plugins › Built-in), both engines. - Broken by hand, each turned its test red: the boundary's catch removed; Try again not putting the page's markup back; the Gateway page's onConfig not re-reading callsPage; boot.js's pluginConfig ignored; SetConfig skipping Validate. - The full GUI suite in Chromium and WebKit (node --test *.test.cjs). One red was this change's own. providers-fetching-retry lifts providersWhileFetching out of app.js, which now asks providersPage, and the lift lacked it. The test now lifts that too, and gains "a poll under the Gateway page, a plugin's page that reads with Providers, reads". That case goes red with providersPage dropped from the poll's guard. - The reds that were on origin/maina75de4c21too are fixed in their own commits after this one. Two of them came from P0's Built-in tab: nav-sub-unroll and plugin-check-github-limit.
159 lines
6.7 KiB
JavaScript
159 lines
6.7 KiB
JavaScript
// Run with `node --test internal/gui/tests/providers-fetching-retry.test.cjs`;
|
|
// no browser is needed. While the backend says the accounts' lists are on
|
|
// their way (fetching), the page asks again every few seconds — the timer is
|
|
// the only thing that brings them in, so a read that failed, or a reload that
|
|
// did, must put the next one back rather than ending the asking. The guards
|
|
// that pause it (another page, an open editor, a hidden window, lists that are
|
|
// in) still hold, and only one timer is ever pending. providers-fetching.test.cjs
|
|
// checks the same feature in a browser; this one lifts the real
|
|
// providersWhileFetching out of app.js and drives it with faked timers and a
|
|
// faked api(), so it runs without Playwright.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
const { test } = require("node:test");
|
|
|
|
const APP = process.env.MAGPIE_APP_JS || path.resolve(__dirname, "../assets/app.js");
|
|
const source = fs.readFileSync(APP, "utf8").replace(/\r\n/g, "\n");
|
|
|
|
// lift a top-level function out of app.js whole: from its `function` line to
|
|
// the `}` at the left margin that closes it
|
|
function lift(header) {
|
|
const start = source.indexOf(header);
|
|
assert.notEqual(start, -1, `app.js no longer has ${header}`);
|
|
const end = source.indexOf("\n}\n", start);
|
|
assert.notEqual(end, -1, `${header} is not closed at the left margin`);
|
|
return source.slice(start, end + 2);
|
|
}
|
|
const providersSource = lift("function providersWhileFetching() {");
|
|
// a plugin's page that reads with Providers (the Gateway page) is polled too
|
|
const pageLine = source.match(/^const providersPage = .*$/m);
|
|
assert.ok(pageLine, "app.js no longer has providersPage");
|
|
|
|
// values cross the vm's realm boundary (its Object is not the test's), so what
|
|
// is handed to assert is copied into this one first
|
|
const plain = (v) => JSON.parse(JSON.stringify(v));
|
|
|
|
// page() is the Providers page with the globals the function reads, timers
|
|
// that only fire when the test says so, and a fake api()/loadProviders().
|
|
// fire() runs the pending timer and waits for the read it starts.
|
|
function page() {
|
|
const script = [
|
|
`
|
|
let view = "providers", editing = null, adding = false;
|
|
const magpie = { page: (v) => (v === "gateway" ? { loads: "providers" } : undefined) };
|
|
`,
|
|
pageLine[0],
|
|
`
|
|
const document = { hidden: false };
|
|
let providers = { fetching: true, providers: [], gateway: { calls: 0 } };
|
|
const timers = [];
|
|
let seq = 0, apiCalls = 0, reloads = 0, reloadFails = false;
|
|
let answer = () => Promise.resolve(null);
|
|
function setTimeout(fn, ms) { const t = { id: ++seq, ms, cancelled: false, fired: false, fn }; timers.push(t); return t; }
|
|
function clearTimeout(t) { if (t) t.cancelled = true; }
|
|
function api(path) { apiCalls++; return answer(path); }
|
|
// as the real loadProviders ends by asking again, so does this one — unless it
|
|
// throws first, which is the reload that failed
|
|
async function loadProviders() { reloads++; if (reloadFails) throw new Error("reload failed"); providersWhileFetching(); }
|
|
`,
|
|
providersSource,
|
|
`
|
|
globalThis.page = {
|
|
arm: () => { providersWhileFetching(); return timers.filter((t) => !t.cancelled && !t.fired).length; },
|
|
pending: () => timers.filter((t) => !t.cancelled && !t.fired).length,
|
|
fire: async () => { const t = timers.filter((x) => !x.cancelled && !x.fired).pop(); t.fired = true; await t.fn(); },
|
|
counts: () => ({ api: apiCalls, reloads }),
|
|
fetching: (v) => { providers.fetching = v; },
|
|
set: (k, v) => { if (k === "view") view = v; else if (k === "editing") editing = v; else if (k === "adding") adding = v; else if (k === "hidden") document.hidden = v; else if (k === "reloadFails") reloadFails = v; },
|
|
answers: (payload) => { answer = () => Promise.resolve(payload); },
|
|
refuses: () => { answer = () => Promise.reject(new Error("no answer")); },
|
|
};
|
|
`,
|
|
].join("\n");
|
|
const context = vm.createContext({});
|
|
vm.runInContext(script, context, { filename: "providersWhileFetching.js" });
|
|
return vm.runInContext("page", context);
|
|
}
|
|
|
|
const changed = () => ({ fetching: true, providers: [{ id: "a" }], gateway: { calls: 0 } });
|
|
const unchanged = () => ({ fetching: false, providers: [], gateway: { calls: 0 } });
|
|
|
|
test("a read that failed is asked again, one timer at a time", async () => {
|
|
const p = page();
|
|
p.refuses();
|
|
assert.equal(p.arm(), 1, "the first poll was not set");
|
|
await p.fire(); // the read fails
|
|
assert.equal(p.pending(), 1, "a failed read ended the asking: nothing is set to try again");
|
|
await p.fire(); // and it fails again
|
|
assert.equal(p.pending(), 1, "the asking did not go on");
|
|
assert.deepEqual(plain(p.counts()), { api: 2, reloads: 0 });
|
|
});
|
|
|
|
test("an answer that changed the list reloads it, and the reload's timer is the only one", async () => {
|
|
const p = page();
|
|
p.answers(changed());
|
|
p.arm();
|
|
await p.fire();
|
|
assert.deepEqual(plain(p.counts()), { api: 1, reloads: 1 }, "the changed list was not reloaded");
|
|
assert.equal(p.pending(), 1, "the reload left no timer, or left two");
|
|
});
|
|
|
|
test("a reload that failed is asked again too", async () => {
|
|
const p = page();
|
|
p.answers(changed());
|
|
p.set("reloadFails", true);
|
|
p.arm();
|
|
await p.fire();
|
|
assert.deepEqual(plain(p.counts()), { api: 1, reloads: 1 });
|
|
assert.equal(p.pending(), 1, "a failed reload ended the asking");
|
|
});
|
|
|
|
test("an answer that says the lists are in stops the asking", async () => {
|
|
const p = page();
|
|
p.answers(unchanged());
|
|
p.arm();
|
|
await p.fire();
|
|
assert.equal(p.pending(), 0, "the asking went on after the lists were in");
|
|
});
|
|
|
|
test("nothing is set while the lists are not on their way", () => {
|
|
const p = page();
|
|
p.fetching(false);
|
|
assert.equal(p.arm(), 0);
|
|
});
|
|
|
|
test("arming twice leaves one timer", () => {
|
|
const p = page();
|
|
assert.equal(p.arm(), 1);
|
|
assert.equal(p.arm(), 1, "a second arming added a second timer");
|
|
});
|
|
|
|
test("a poll under the Gateway page, a plugin's page that reads with Providers, reads", async () => {
|
|
const p = page();
|
|
p.refuses();
|
|
p.arm();
|
|
p.set("view", "gateway");
|
|
await p.fire();
|
|
assert.equal(plain(p.counts()).api, 1, "it read nothing under the Gateway page");
|
|
assert.equal(p.pending(), 1);
|
|
});
|
|
|
|
for (const [guard, set] of [
|
|
["another page", (p) => p.set("view", "agents")],
|
|
["an open editor", (p) => p.set("editing", "p1")],
|
|
["a provider being added", (p) => p.set("adding", true)],
|
|
["a hidden window", (p) => p.set("hidden", true)],
|
|
]) {
|
|
test(`a poll under ${guard} reads nothing and sets nothing`, async () => {
|
|
const p = page();
|
|
p.refuses();
|
|
p.arm();
|
|
set(p);
|
|
await p.fire();
|
|
assert.deepEqual(plain(p.counts()), { api: 0, reloads: 0 }, "it read under " + guard);
|
|
assert.equal(p.pending(), 0, "it kept asking under " + guard);
|
|
});
|
|
}
|