Files
magpie/internal/gui/tests/group-index.test.cjs
T
yetoneandliu-kan 923cc8747f routing: filtering groups reuses indexed lookups and cards (liu-kan, #6744)
Ported from liu-kan's routing_mod_filter branch at 5aca86598 (#6744).
Typing in the Routing groups filter re-ran every member's lookups as a
linear .find over all models, deciders, drawers and accounts, and redrew
the whole page each keystroke. Now a lazy index (Maps built first-match
like .find, splitMember and memberInfo cached, search text cached per
group and locale) is rebuilt whenever `groups` is reassigned (it never
is mutated in place) and dropped 5 minutes after both Routing pages are
hidden. The filter's input redraws only the group list from cached
cards, falling back to a full draw when groups or the locale changed.
No look, matching or saved config change; no new strings; app.js
untouched (client.js is outside the core budget).

Measured (mote, Playwright, 4000 models, 20 groups x 20 members, 16
keystrokes, ms per keystroke incl. layout):
  chromium: before mean 4767.6 / median 5086.3, after 0.37 / 0.2
  webkit:   before mean 4813 / median 5075,     after 0.88 / 1

Run:
- go test -count=1 ./internal/gui/ on 2bc25a2d6 (mote): ok 137.9s. Main since
  (1f04ed219) touches no routing file.
- New and touched GUI files (group-filter-render, group-index,
  group-search-cache, routing-group-plans) on 2bc25a2d6 on mote:
  54 pass, 0 fail.
- make test-ui (both engines) on mica before the rebase onto 2bc25a2d6
  (main's two routing commits since touch the stage only): the run
  stopped after 453 of 1542 files, 5788 results. Its 34 red files were
  each run on origin/main and on this branch: the same tests fail on
  both (main 2bc25a2d6: 378 pass / 204 fail; branch identical but one
  context-cap Settings flake, #6514). Each has an issue: #6010, #5814,
  #5982, #6369, #6371, #6365, #5925, #4959, #6444, #6212, #6543,
  #6029, #6179, #6658, #3293, #6372, #5972, #6514, #5801, #4870,
  #6475, #6424, #6156, #6201, #6660, #5013, #6268, #6293, #5049
  (group-editor-selected), #5594 (group-new-head), #3479, #6414, #4086.
- The other 1089 files: a run on mica went bad under another session's
  suite (load 117) and was stopped; a whole suite at 2bc25a2d6 is
  queued on mote behind the suite lock, followed up after push.

Not tried: the real app, a Linux WebKitGTK app, Windows.

Co-authored-by: liu-kan <liukan@126.com>
2026-11-15 20:25:50 +08:00

60 lines
3.2 KiB
JavaScript

const assert = require("node:assert/strict");
const { test } = require("node:test");
const { load } = require("./routing-group-source.cjs");
const model = (id, name = id) => ({ id, name, provider: "p", providerName: "Provider" });
const data = () => ({ models: [model("p/base"), model("p/literal:high"), model("p/base", "duplicate")],
drawers: [model("p/image:high")], deciders: [model("p/judge")], groups: [
{ id: "nested", name: "Nested", members: [] }, { id: "hidden", hidden: true, members: [] },
{ id: "off", members: [], memberInfo: [{ id: "gone/model", providerOff: true, name: "Gone", model: "Model" }] },
], accounts: { p: [{ id: "p/acct", name: "Account A" }] } });
test("routing members use bounded model reads, including effort, account and missing IDs", () => {
const models = Array.from({ length: 1000 }, (_, i) => model("p/model-" + i));
let reads = 0;
for (const m of models) { const id = m.id; Object.defineProperty(m, "id", { get: () => { reads++; return id; } }); }
const api = load({ models, groups: [], drawers: [], deciders: [] });
for (let i = 0; i < 10; i++) {
assert.equal(api.modelOf("p/model-999:high#acct"), models[999]);
assert.equal(api.modelOf("p/missing:high"), undefined);
}
assert.ok(reads < 10000, `member lookups rescanned the model catalog: ${reads} ID reads`);
});
test("indexed members preserve literal IDs, effort order, account names and first match", () => {
const state = data(), api = load(state);
assert.deepEqual(Array.from(api.splitMember("p/base:HIGH+low# acct ")), ["p/base", "low+high", "acct"]);
assert.deepEqual(Array.from(api.splitMember("p/literal:high#acct")), ["p/literal:high", "", "acct"]);
assert.deepEqual(Array.from(api.splitMember("p/image:high")), ["p/image:high", "", ""]);
assert.equal(api.modelOf("p/base"), state.models[0], "a duplicate must keep find's first match");
assert.equal(api.modelOf("p/judge:high"), state.deciders[0]);
assert.equal(api.modelOf("p/image:high"), state.drawers[0]);
assert.equal(api.memberAccount("p/base:high#acct").name, "Account A");
assert.equal(api.subOf("group/nested").name, "Nested");
assert.equal(api.subOf("group/hidden"), undefined);
assert.equal(api.offOf("gone/model").name, "Gone");
const next = data(); next.models[0].name = "Renamed"; next.accounts.p[0].name = "Account B";
api.refresh(next);
assert.equal(api.modelOf("p/base").name, "Renamed");
assert.equal(api.memberAccount("p/base#acct").name, "Account B");
});
test("routing indexes are lazy, expire after five minutes away and rebuild from current data", () => {
const state = data(), api = load(state);
assert.equal(api.cache(), null);
api.modelOf("p/base:high");
assert.ok(api.cache(), "a lookup must build the index");
const first = api.cache();
api.visible(false);
assert.equal(api.delay(), 300000);
assert.equal(api.pending(), true);
api.visible(true);
assert.equal(api.pending(), false, "returning cancels expiry");
assert.equal(api.cache(), first);
api.visible(false); api.expire();
assert.equal(api.cache(), null, "expiry must release the index");
state.models[0].name = "After expiry";
api.visible(true);
assert.equal(api.modelOf("p/base").name, "After expiry");
assert.notEqual(api.cache(), first);
});