Files
magpie/internal/gui/tests/routing-group-source.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

32 lines
2.0 KiB
JavaScript

// The real routing lookup/search functions, without a DOM or a live API.
// Counting model reads tests algorithmic work without a wall-clock limit.
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const assets = process.env.MAGPIE_GROUP_ASSETS || path.resolve(__dirname, "../assets");
const source = fs.readFileSync(path.join(assets, "plugins/routing/client.js"), "utf8");
const app = fs.readFileSync(path.join(assets, "app.js"), "utf8");
const section = (s, a, b) => { const start = s.indexOf(a); return start < 0 ? "" : s.slice(start, s.indexOf(b, start)); };
function load(data) {
let visible = true, timer = null, timerDelay = 0;
const context = vm.createContext({
groups: data, locale: "en", shown: () => visible, gShown: () => false,
setTimeout: (fn, ms) => { timer = fn; timerDelay = ms; return 1; }, clearTimeout: () => { timer = null; },
t: (s, vars) => Object.entries(vars || {}).reduce((s, [k, v]) => s.replaceAll("{" + k + "}", v), context.locale === "zh" && s === "routing group" ? "路由组" : s),
});
vm.runInContext([
section(app, "function modelTells(", "// tellTag:"),
section(source, " // Built on first lookup,", " // poolOf is"),
section(source, " const LEVELS =", " // a group's editor,"),
section(source, " const offOf =", " function renderGroups("),
section(source, " function groupMatches(", " // rowOrFlag:"),
`globalThis.api = { splitMember, modelOf, memberAccount, memberLabel, subOf, offOf, freeIn, groupMatches,
refresh: (x) => { groups = x; }, lang: (x) => { locale = x; },
cache: () => typeof groupIndex === "undefined" ? null : groupIndex,
keep: () => { if (typeof keepGroupCaches === "function") keepGroupCaches(); } };`,
].join("\n"), context);
return { ...context.api, visible: (on) => { visible = on; context.api.keep(); },
expire: () => { const fn = timer; timer = null; fn?.(); }, delay: () => timerDelay, pending: () => !!timer };
}
module.exports = { load };