Opening Settings reads the settings twice: the page's own read (showView), then the window's load() once the state is in. Each read redraws every row. A test that found a row after the first draw, and then called scrollIntoViewIfNeeded or boundingBox on it, failed when the second read landed in between. The errors were "Element is not attached", or a null box. #1653 fixed the three masking-rules tests and listed these 37 in #1659. Each of the 37 now calls watchSettingsReads(page) before every page.goto, and settingsDrawn(page) right after it. Right after goto is exact for ?view=settings: at boot, show(view) asks for showView's read before load() runs. load() sets load.done in the same run that then asks for its own read, so settingsDrawn's "load.done and every read asked for drawn" holds only once the second read is drawn. For a test opened on another page that clicks into Settings later, load is done by then, the click makes the only read, and the wait passes at once. watchSettingsReads now watches a page once, however many times it is opened, since some of these tests open one page several times. The page is unchanged. Reproduced with a generic injection (scratchpad inject1659.cjs, loaded with NODE_OPTIONS=--require). A route added at each goto holds the page's second GET /api/settings. It lets the read go just as the test calls scrollIntoViewIfNeeded or boundingBox, or waits on the page with waitForFunction (which settingsDrawn is), with a 4s timer as a backstop. This is #1653's injection, made generic. Before, injected, both engines, all languages: - 5 files red with the issue's error: - github-token: null box. - member-model-setting, searcher-pick, search-first and update-wait: "Element is not attached to the DOM". - 4 files red on a check of a row drawn from the first read: model-suffix-own, otel, readable-focus and warm-at-times. - 28 green. After, injected: 36 of the 37 files green, every held read let go by settingsDrawn's wait. otel stays red in WebKit at 360px. It is red the same way with no injection, on origin/main's own copy of the test. That cause is separate (the narrow nav laid out after setViewportSize returns) and has its own commit next. Plain (no injection), both engines, all 37 files: 460 cases passed and 2 failed, both otel's WebKit 360px check above. The 8 files red under injection before, injected again after, 5 rounds: 390 of 390 cases passed.
180 lines
9.6 KiB
JavaScript
180 lines
9.6 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||
// Settings › Gateway port (Magic_zero on Discord: 3425 was taken by another
|
||
// program, and a port of one's own is easier to tell apart): any port from
|
||
// 1024 to 65535 can be typed and applied. One out of range is refused in the
|
||
// page; one another program has is refused with the gateway's reason, in the
|
||
// page's language, what was typed kept; a free one moves the gateway and
|
||
// says how many agents moved with it, and the row then says the new URL.
|
||
// With MAGPIE_ADDR set the field is off and says why. The field is an
|
||
// input, not a native <select>; applying never scrolls the page; no
|
||
// coloured left border. English and Chinese, 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 { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
|
||
|
||
const assets = path.resolve(__dirname, "../assets");
|
||
|
||
function settingsPayload(over) {
|
||
return {
|
||
theme: "light", lang: "en", tray: "panel", quotaLeft: false, currency: "usd",
|
||
dock: false, dockWindow: false, proxy: "", redact: false, redactPersonal: false, redactWords: [],
|
||
codexWarmup: "", claudeWarmup: "", codexWarmAt: "", claudeWarmAt: "", workbuddyCheckin: false, noStats: false,
|
||
trayUsage: "", trayUsageEvery: 3, vision: "", imageGen: "",
|
||
version: "0.1.900", dir: "/config/magpie", gateway: "http://127.0.0.1:3425",
|
||
proxyNow: "none", proxySource: "none", login: false,
|
||
visionModels: [], imageGenModels: [], workbuddyCheckins: [],
|
||
lan: false, lanURLs: [],
|
||
fx: { rate: 7.2, at: new Date().toISOString(), stale: false },
|
||
...over,
|
||
};
|
||
}
|
||
|
||
function server(lang, over, posts) {
|
||
let s = settingsPayload({ lang, ...over });
|
||
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: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" }, fx: s.fx });
|
||
if (url.pathname === "/api/settings/port") {
|
||
const { port } = JSON.parse(req.postData());
|
||
posts.push(port);
|
||
if (port === 3500) return json({ error: "port 3500 is in use by another program: pick another one" }, 400);
|
||
s = { ...s, port, gateway: "http://127.0.0.1:" + port };
|
||
return json({ settings: s, port: { port, moved: ["Claude Code", "Codex"] } });
|
||
}
|
||
if (url.pathname === "/api/settings") return json(s);
|
||
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 });
|
||
};
|
||
}
|
||
|
||
const words = {
|
||
en: {
|
||
name: "Gateway port", apply: "Apply",
|
||
now: (u) => "Agents magpie connected move with it. Now " + u,
|
||
range: "A port is a number from 1024 to 65535",
|
||
taken: "Port 3500 is in use by another program: pick another one",
|
||
moved: "Gateway on port 3591; 2 agents moved with it",
|
||
env: "MAGPIE_ADDR=127.0.0.1:3426 sets the gateway’s address: unset it to set the port here",
|
||
},
|
||
zh: {
|
||
name: "网关端口", apply: "应用",
|
||
now: (u) => "改了之后,已接入 magpie 的 Agent 会一起换过去。当前 " + u,
|
||
range: "端口是 1024 到 65535 之间的数字",
|
||
taken: "端口 3500 已被其他程序占用,请换一个",
|
||
moved: "网关已换到端口 3591,2 个 Agent 已跟着换过去",
|
||
env: "MAGPIE_ADDR=127.0.0.1:3426 已指定网关地址:去掉这个环境变量后才能在这里设端口",
|
||
},
|
||
};
|
||
|
||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||
test(engine + ": the gateway's port is typed in Settings and agents move with it", 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}-gateway-port-${i}.png`) });
|
||
}
|
||
await browser.close();
|
||
});
|
||
|
||
const open = async (lang, over) => {
|
||
const page = await (await browser.newContext({ viewport: { width: 900, height: 360 }, reducedMotion: "reduce" })).newPage();
|
||
pages.push(page);
|
||
page.setDefaultTimeout(5000);
|
||
const errors = [], posts = [];
|
||
page.on("pageerror", (e) => errors.push(e.message));
|
||
await page.route("**/*", server(lang, over, posts));
|
||
await watchSettingsReads(page);
|
||
await page.goto("http://localhost:3430/?view=settings&tab=network");
|
||
await settingsDrawn(page); // rows found are the ones that stay (#1659)
|
||
await page.locator("#portList .port-row").waitFor();
|
||
return { page, errors, posts };
|
||
};
|
||
|
||
for (const lang of ["en", "zh"]) {
|
||
const w = words[lang];
|
||
await t.test(lang, async () => {
|
||
{
|
||
const { page, errors, posts } = await open(lang);
|
||
const row = page.locator("#portList .port-row");
|
||
const field = row.locator("input.gateway-port");
|
||
const sub = row.locator(".sub");
|
||
const apply = row.getByRole("button", { name: w.apply });
|
||
assert.equal((await row.locator(".name").textContent()).trim(), w.name);
|
||
assert.equal(await field.inputValue(), "3425");
|
||
assert.equal((await sub.textContent()).trim(), w.now("http://127.0.0.1:3425"));
|
||
assert.equal(await page.locator("#portList select").count(), 0, "no native select");
|
||
const border = await row.evaluate((r) => getComputedStyle(r).borderLeftWidth);
|
||
assert(border === "0px" || border === "", "no left border accent: " + border);
|
||
|
||
// scrolled with the wheel, as a reader would, until the row is in view
|
||
const scroll = () => page.locator("#view-settings").evaluate((v) => v.scrollTop);
|
||
await page.locator("#view-settings").hover();
|
||
for (let i = 0; i < 40; i++) {
|
||
const box = await apply.boundingBox();
|
||
if (box && box.y > 80 && box.y + box.height < 340 && (await scroll()) > 0) break;
|
||
await page.mouse.wheel(0, 120);
|
||
await page.waitForTimeout(30);
|
||
}
|
||
await page.waitForTimeout(500); // the wheel's scroll come to rest
|
||
const before = await scroll();
|
||
assert(before > 0, "the settings must be scrolled to the row");
|
||
|
||
// out of range: said in the page, nothing sent
|
||
await field.fill("80");
|
||
assert.equal(await scroll(), before, "typing moved the page");
|
||
await apply.click();
|
||
await page.waitForFunction((want) => document.querySelector("#portList .sub")?.textContent.trim() === want, w.range);
|
||
assert(await row.locator(".sub").evaluate((e) => e.classList.contains("err")));
|
||
assert.deepEqual(posts, []);
|
||
assert.equal(await field.inputValue(), "80", "what was typed is kept");
|
||
assert.equal(await scroll(), before, "a port out of range moved the page");
|
||
|
||
// taken by another program: the gateway's reason, translated
|
||
await field.fill("3500");
|
||
await apply.click();
|
||
await page.waitForFunction((want) => document.querySelector("#portList .sub")?.textContent.trim() === want, w.taken);
|
||
assert.equal(await field.inputValue(), "3500");
|
||
assert.deepEqual(posts, [3500]);
|
||
assert.equal(await scroll(), before, "a taken port moved the page");
|
||
|
||
// a free one, by Enter: moved, and the agents with it
|
||
await field.fill("3591");
|
||
await field.press("Enter");
|
||
await page.waitForFunction((want) => document.querySelector("#status")?.textContent.trim() === want, w.moved);
|
||
await page.waitForFunction((want) => document.querySelector("#portList .sub")?.textContent.trim() === want, w.now("http://127.0.0.1:3591"));
|
||
assert.equal(await field.inputValue(), "3591");
|
||
assert.deepEqual(posts, [3500, 3591]);
|
||
assert.equal(await scroll(), before, "applying moved the page");
|
||
assert.deepEqual(errors, []);
|
||
}
|
||
// MAGPIE_ADDR comes first: the field is off and says why
|
||
{
|
||
const { page, errors, posts } = await open(lang, { addrEnv: "127.0.0.1:3426", gateway: "http://127.0.0.1:3426" });
|
||
const row = page.locator("#portList .port-row");
|
||
assert(await row.locator("input.gateway-port").isDisabled());
|
||
assert(await row.getByRole("button", { name: w.apply }).isDisabled());
|
||
assert.equal((await row.locator(".sub").textContent()).trim(), w.env);
|
||
assert.deepEqual(posts, []);
|
||
assert.deepEqual(errors, []);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|