Files
magpie/internal/gui/tests/gateway-key-arrange.test.cjs
T
yetone 333e7f573b gateway: gateway keys are dragged into the order you want, kept with the keys (alextjy on Discord)
alextjy asked on Discord whether gateway keys can be reordered. They
could not: the Gateway page listed them in the order caller-keys.json
held them, and nothing changed that order.

- access: a "move-key" action puts one key at a place in the list,
  under the same file lock as every other key change. Nothing else
  about the key changes, and the sharing key's mirror in settings is
  left alone (its pointer would otherwise land on another key).
- GUI: the Gateway page's key rows are dragged the way search APIs'
  rows are (shared dragRows, a grip shown on hover only, Alt+Up/Down,
  the existing "Drag to reorder" title). A refused move puts the list
  back from the server. New strings in zh, zh-TW, ja and de.
- CLI: `magpie gateway-key move <id> <place>` (from 1); list already
  prints the stored order.
- Usage > Requests: the key filter lists keys in the user's order,
  removed keys after. The Overview stays a ranking by use.

Run (worktree at 14b451c11, then rebased on origin/main):
- gofmt -l clean; go vet (darwin) on access, cli, usageapi, gui;
  GOOS=linux -tags nogui and GOOS=windows go vet on access, cli,
  usageapi (mote).
- go test access, cli, usageapi: ok (mote). internal/gui: every test
  ok except the real-window AppKit tests #4125 lists
  (TestCoveredWindowStaysUp, TestPanelCloseHidesIt and 7 more), killed
  at their deadlines with pboard not running on mote.
- Broken by hand: with move-key removed, TestMoveKeyKeepsOrder,
  TestGatewayKeyCLIMove and TestLedgerKeyFilterInKeyOrder go red; with
  the mirror exclusion removed and the filter left unsorted,
  TestMoveKeyKeepsOrder ("the sharing key's mirror moved") and
  TestLedgerKeyFilterInKeyOrder go red.
- GUI (WSL): gateway-key-arrange 20/20 (chromium and webkit; en, zh,
  zh-TW, ja, de; 1100 and 420px). Red by hand: no arrange (20 red),
  grip always shown (chromium 10/10 red), drop commits nothing (webkit
  10/10 red).
- GUI (WSL), the 68 files the change can reach (every caller-keys
  fixture user, gateway-*, search-*, usage-ledger*, i18n-keys, gui-ja,
  gui-de, gui-zh-tw, key-page, phone-rows, api-key-*): 900/923. The
  reds were run again at origin/main 43966a53f and fail the same there
  (api-key-theme webkit, gateway-key-limit webkit, gateway-key-models
  webkit, search-first, usage-ledger-open-settled, usage-ledger-ttft,
  usage-ledger), each filed: #3307, #3523, #4081, #3795, #4262,
  #3104/#1876, and #4489 (usage-ledger-ttft, new). phone-rows,
  search-keys and connect-snippet-shell reds of the first run passed
  when run again at the head. usage-ledger's "zh: request routing
  links" went red once at the head (Usage view position on return) and
  passed in the first run.

At the rebased head (on 2ac423f4f): TestCoreScriptBudget and the
other Budget/I18n/Asset tests in internal/gui ok (mote);
gateway-key-arrange, i18n-keys and gateway-caller-keys 27/28 in WSL.
The red is gateway-caller-keys webkit en "wider than 560 right after
the resize" (#3797, red alone on main). A probe of that file, printing
the page width and any element past it, read 560/560 with nothing
wider in both engines, in two runs beside the arrange file.

Not tried: dragging in the real app (WebKitGTK, WKWebView, WebView2),
the full Go suite (started after the push).
2026-10-31 03:48:27 +08:00

97 lines
6.1 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// Gateway keys in the order the user puts them in (alextjy on Discord): a
// key is dragged by its row, anywhere off its buttons, to another place, the
// grip in its margin shown only on the row under the pointer; the order is
// sent once as move-key and Saved is said; Alt+↑/↓ on a focused row moves it
// a place, the keyboard staying on it; an order that can't be saved is put
// back as it is kept; and the page opened again lists the keys in the order
// saved. In every language, Chromium and WebKit, 1100px and 420px wide, with
// the API faked.
const assert = require("node:assert/strict");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { fixture } = require("./fixtures/caller-keys.cjs");
const saved = { en: "Gateway key order saved", zh: "网关密钥的顺序已保存", "zh-TW": "閘道金鑰的順序已儲存", ja: "ゲートウェイキーの順番を保存しました", de: "Reihenfolge der Gateway-Schlüssel gespeichert" };
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) for (const width of [1100, 420]) {
test(`${engine} ${lang} ${width}px: gateway keys are dragged into order`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 900 } })).newPage();
page.setDefaultTimeout(6000);
page.setDefaultNavigationTimeout(60000);
const errors = [], events = [], options = { lan: true };
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", fixture(lang, "light", events, options));
await page.goto("http://magpie.test/?view=gateway");
const rows = page.locator("#gatewayKeys .acc[data-key]");
await rows.last().waitFor();
const order = () => rows.evaluateAll((rs) => rs.map((r) => r.dataset.key).join(","));
const waitOrder = (want) => page.waitForFunction((want) => [...document.querySelectorAll("#gatewayKeys .acc[data-key]")].map((r) => r.dataset.key).join(",") === want, want);
const moves = () => events.filter((e) => e.action !== "ledger");
assert.equal(await order(), "laptop,server,work");
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), "nothing wider than the window");
// the grip shows only on the row under the pointer
const grip = (i) => rows.nth(i).locator(".grip").evaluate((g) => getComputedStyle(g).opacity);
await page.mouse.move(2, 2);
await page.waitForFunction(() => getComputedStyle(document.querySelector("#gatewayKeys .acc[data-key] .grip")).opacity === "0");
await rows.nth(1).hover({ position: { x: 40, y: 10 } });
await page.waitForFunction(() => getComputedStyle(document.querySelectorAll("#gatewayKeys .acc[data-key] .grip")[1]).opacity === "1");
assert.equal(await grip(0), "0", "only the row under the pointer has its grip");
const box = await rows.nth(1).locator(".grip").boundingBox(), row = await rows.nth(1).boundingBox();
assert(box.x >= row.x && box.x + box.width <= (await rows.nth(1).locator(".dot").boundingBox()).x, "the grip is in the row's margin, before its tick");
const clear = () => page.evaluate(() => { const s = document.querySelector("#status"); s.className = "status"; s.textContent = ""; });
const said = () => page.waitForFunction(() => document.querySelector("#status")?.classList.contains("ok")).then(() => page.locator("#status").textContent());
const y0 = await page.evaluate(() => scrollY);
// drag Work, by its masked key, above Laptop
await clear();
const from = await rows.nth(2).locator(".plan").boundingBox(), to = await rows.nth(0).boundingBox();
const fx = from.x + from.width / 2, fy = from.y + from.height / 2;
await page.mouse.move(fx, fy);
await page.mouse.down();
for (let i = 1; i <= 12; i++) await page.mouse.move(fx, fy + (to.y + 4 - fy) * i / 12);
await page.mouse.up();
await waitOrder("work,laptop,server");
assert.equal((await said()).trim(), saved[lang]);
assert.deepEqual(moves(), [{ action: "move-key", body: { key: "work", index: 0 } }], "letting go sent the order once, and clicked nothing");
// Alt+↓ on Work moves it a place, the keyboard staying on it
await clear();
await rows.nth(0).focus();
await page.keyboard.press("Alt+ArrowDown");
await waitOrder("laptop,work,server");
await said();
assert.deepEqual(moves().at(-1), { action: "move-key", body: { key: "work", index: 1 } });
await page.waitForFunction(() => document.activeElement?.matches?.('#gatewayKeys .acc[data-key="work"]') && document.activeElement.isConnected);
// an order that can't be saved is put back as it is kept
options.refuseMove = true;
await rows.nth(2).focus();
await page.keyboard.press("Alt+ArrowUp");
await page.waitForFunction(() => document.querySelector("#status")?.classList.contains("err"));
await waitOrder("laptop,work,server");
options.refuseMove = false;
assert.equal(moves().length, 3);
// a click on a row's buttons still does what it did, and moves nothing
await rows.nth(0).locator(".tick").click();
await page.waitForFunction(() => document.querySelector('#gatewayKeys .acc[data-key="laptop"]')?.classList.contains("off"));
assert.equal(moves().at(-1).action, "off-key");
assert.equal(await order(), "laptop,work,server");
assert.equal(await page.evaluate(() => scrollY), y0, "arranging moved the page");
// opened again, the keys are in the order saved
await page.reload();
await rows.last().waitFor();
assert.equal(await order(), "laptop,work,server");
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), "nothing wider than the window");
assert.deepEqual(errors, []);
});
}
}