Files
magpie/internal/gui/tests/panel-profiles.test.cjs
T
yetone 13309b07fa panel: a profile saved or deleted shows at once and stays in sight (Image #62: 点击保存没有效果,删除方案也没有效果。你为什么UI UX做这么烂?) — both did their work (profiles.json had the profile), but the page showed nothing of it: every profile action answers with the whole state, which read logins.json (166KB, the accounts' credentials) again for each agent's drift and models, dozens of times — 2.5–4s a state on a real setup — and only then redrew the chips. And with no profiles yet the chip saved came in above + Save current, which the click held where it was on the screen: the hold gave the view room and scrolled it 30px, and the chip went up under the tabs, out of sight. Now the chip saved is there at once, dimmed till magpie answers, the name field closed; × takes the chip away at once; a save or delete refused puts the list back and says why. A view at its top when clicked stays at its top instead of being given room to scroll (what came in above the control moves it down). logins.json is parsed once until it changes (filememo, a copy to each caller): a state went from 2.4s to 0.9s here. Tested with a new case in internal/gui/tests/panel-profiles.test.cjs, Chromium and WebKit, en and zh: magpie answering after 1.5s with no profiles, the chip there and dimmed before the answer, the list unscrolled and the chip below the tabs, × removing it before the answer, a refused save taken off with its reason (fails on main); against a sandbox magpie web in WebKit, the view stays at 0 where main scrolled it to 30. click-scroll passes in both engines; go test -tags nogui ./... (TestClaudeSignedOut flaky under the full run, passes alone), go build for linux and windows
2026-09-29 19:23:14 +08:00

201 lines
11 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.
// Saving the current setup as a profile in the tray panel's Profiles tab:
// "+ Save current", with the list scrolled to its end, opens a name field
// beside it without moving the list (the field at the list's head drew it
// up under the tabs, the field's top cut off) and with one ring, not the
// page's focus ring over its own; field and button stay in sight, and the
// button then reads Save; a click on it saves as Enter does (it did nothing:
// the field lost focus to it and went). Escape closes the field and gives the
// button back. With magpie slow to answer (reading every agent again took it
// seconds) and no profiles yet, the chip saved is there at once, dimmed till
// the answer, under the tabs' line and not scrolled up behind them (the
// click held the button, which the chip came in above); × takes a chip away
// at once; a save refused puts the list back and says why. 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 models = ["model-a", "model-b"].map((m) => ({ value: m, label: m }));
const agent = (id) => ({
id, name: id, path: "/test/" + id, fields: [
{ key: "model", label: "model", value: "model-a", options: models },
],
});
const profile = (name) => ({ name, summary: "" });
function server(lang, saved, { count = 30, delay = 0, refuse = "" } = {}) {
let profiles = Array.from({ length: count }, (_, i) => profile("profile-" + i));
return async (route) => {
const req = route.request(), url = new URL(req.url());
const state = () => ({ agents: [agent("codex"), agent("claude")], profiles, settings: { lang, theme: "light" } });
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: state() });
if (url.pathname === "/api/profile/save") {
const { name } = req.postDataJSON();
await new Promise((r) => setTimeout(r, delay));
if (name === refuse) return route.fulfill({ status: 400, json: { error: "can't write profiles.json" } });
saved.push(name);
profiles = [...profiles, profile(name)];
return route.fulfill({ json: state() });
}
if (url.pathname === "/api/profile/delete") {
const { name } = req.postDataJSON();
await new Promise((r) => setTimeout(r, delay));
profiles = profiles.filter((p) => p.name !== name);
return route.fulfill({ json: state() });
}
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 === "/api/window/fit") return route.fulfill({ status: 204 });
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 });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": saving a profile in the tray panel", 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 = [];
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}-panel-profiles-${i}.png`) });
}
await browser.close();
});
const open = async (lang, saved, opts) => {
const page = await (await browser.newContext({ viewport: { width: 440, height: 420 } })).newPage();
pages.push(page);
page.setDefaultTimeout(5000);
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, saved, opts));
await page.goto("http://magpie.test/?mode=panel");
await page.locator("#profiles .chip, #profiles .hint").first().waitFor({ state: "attached" }); // shown with the tab
await page.locator('[data-ptab="profiles"]').click();
await page.waitForTimeout(300);
return page;
};
const view = (page) => page.locator("#view-agents").evaluate((v) => v.scrollTop);
for (const [lang, add, save] of [["en", "+ Save current", "Save"], ["zh", "+ 保存当前", "保存"]]) {
await t.test(lang, async () => {
const saved = [];
const page = await open(lang, saved);
const button = page.locator("#save");
assert.equal((await button.textContent()).trim(), add);
// the reader scrolls to the button; the field opens beside it, whole,
// with one ring, and nothing moves
const box = await page.locator("#view-agents").boundingBox();
await page.mouse.move(box.x + box.width / 2, box.y + 60);
for (let i = 0; i < 20; i++) { await page.mouse.wheel(0, 40); await page.waitForTimeout(20); }
await page.waitForTimeout(400);
const before = await view(page);
assert(before > 0, "the list must be long enough to scroll");
await button.click();
const field = page.locator(".profiles > .chip-input");
await field.waitFor();
await page.waitForTimeout(300);
assert.equal(await view(page), before, "opening the field must not scroll the list");
const cut = await page.evaluate(() => {
const v = document.querySelector("#view-agents").getBoundingClientRect(), f = document.querySelector(".profiles > .chip-input");
const inView = (e) => { const r = e.getBoundingClientRect(); return r.top >= v.top + 1 && r.bottom <= v.bottom; };
return { field: inView(f), button: inView(document.querySelector("#save")), outline: getComputedStyle(f).outlineStyle, focused: document.activeElement === f };
});
assert(cut.focused, "the field must have focus");
assert(cut.field, "the field must be whole in the view");
assert(cut.button, "the button must stay in the view");
assert.equal(cut.outline, "none", "the field's border is its focus; no ring over it");
assert.equal((await button.textContent()).trim(), save, "with the field open the button saves");
// a click on it saves, and the button is + Save current again
await field.pressSequentially("from click");
await button.click();
await page.locator('#profiles .chip:has-text("from click")').waitFor();
assert.deepEqual(saved, ["from click"]);
assert.equal(await page.locator(".profiles > .chip-input").count(), 0);
assert.equal((await button.textContent()).trim(), add);
// an empty field isn't saved; the click keeps it open, focused
await button.click();
await field.waitFor();
await button.click();
await page.waitForTimeout(250);
assert.equal(await field.count(), 1, "an empty name keeps the field open");
assert(await field.evaluate((f) => document.activeElement === f));
assert.deepEqual(saved, ["from click"]);
// Enter still saves; Escape closes and gives the button back
await field.pressSequentially("from enter");
await field.press("Enter");
await page.locator('#profiles .chip:has-text("from enter")').waitFor();
assert.deepEqual(saved, ["from click", "from enter"]);
await button.click();
await field.waitFor();
await field.press("Escape");
assert.equal(await field.count(), 0);
assert.equal((await button.textContent()).trim(), add);
});
await t.test(lang + ": a slow magpie, from none", async () => {
const saved = [];
const page = await open(lang, saved, { count: 0, delay: 1500, refuse: "refused" });
const button = page.locator("#save"), field = page.locator(".profiles > .chip-input");
const chip = (name) => page.locator("#profiles .chip", { hasText: name });
const where = () => page.evaluate(() => {
const v = document.querySelector("#view-agents"), tabs = document.querySelector("#ptabs").getBoundingClientRect();
const c = document.querySelector("#profiles .chip");
return { scroll: v.scrollTop, below: !c || c.getBoundingClientRect().top >= tabs.bottom, status: document.querySelector("#status").textContent };
});
await button.click();
await field.pressSequentially("hi");
await button.click();
await page.waitForTimeout(200);
assert.equal(await chip("hi").count(), 1, "the chip saved is there before magpie answers");
assert(await chip("hi").evaluate((c) => c.classList.contains("pending")), "and dimmed till it does");
assert.equal(await field.count(), 0, "the field closes at once");
assert.equal((await button.textContent()).trim(), add);
await page.locator("#profiles .chip:not(.pending)").waitFor();
await page.waitForTimeout(300);
let w = await where();
assert.equal(w.scroll, 0, "saving must not scroll the list");
assert(w.below, "the chip saved must not be under the tabs");
assert.match(w.status, /hi/);
assert.deepEqual(saved, ["hi"]);
// × takes it away at once
await chip("hi").hover();
await chip("hi").locator(".x").nth(1).click();
await page.waitForTimeout(200);
assert.equal(await chip("hi").count(), 0, "the chip deleted goes before magpie answers");
await page.locator("#profiles .hint").waitFor({ state: "attached" });
await page.waitForFunction(() => /hi/.test(document.querySelector("#status").textContent) && !document.querySelector("#status").classList.contains("ok"));
assert.equal(await chip("hi").count(), 0);
// a save refused: the chip goes again and the reason is shown
await button.click();
await field.pressSequentially("refused");
await field.press("Enter");
await page.waitForTimeout(200);
assert.equal(await chip("refused").count(), 1);
await page.waitForFunction(() => document.querySelector("#status").classList.contains("err"));
assert.equal(await chip("refused").count(), 0, "a save refused is taken off the list");
assert.match((await where()).status, /profiles\.json/);
assert.equal((await where()).scroll, 0);
});
}
assert.deepEqual(errors, []);
});
}