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

50 lines
2.7 KiB
JavaScript

const assert = require("node:assert/strict");
const { test } = require("node:test");
const { load } = require("./routing-group-source.cjs");
function data() {
const id = "p/base:high#acct";
return { models: [{ id: "p/base", name: "Astra", provider: "p", providerName: "Provider" }],
groups: [{ id: "g", name: "Main", ref: "auto/main", members: [id, "group/nested", "off/base"],
patterns: [{ pattern: "p/glob-*" }], fast: [id], memberInfo: [
{ id, free: true }, { id: "off/base", providerOff: true, name: "Disabled", model: "Lost" },
] }, { id: "nested", name: "Nested", members: [] }],
accounts: { p: [{ id: "p/acct", name: "Account A" }] }, drawers: [], deciders: [] };
}
test("successive queries reuse group search text instead of rebuilding member labels", () => {
const state = data(), g = state.groups[0], api = load(state);
let reads = 0;
const name = state.models[0].name;
Object.defineProperty(state.models[0], "name", { get: () => { reads++; return name; } });
assert.equal(api.groupMatches(g, ["astra"]), true);
const firstReads = reads;
for (const query of ["a", "as", "ast", "astra", "missing", "provider"]) api.groupMatches(g, [query]);
assert.equal(reads, firstReads, "typing another query rebuilt the group's model labels");
});
test("cached search preserves AND, substrings, refs, patterns, accounts and member labels", () => {
const state = data(), api = load(state), g = state.groups[0];
for (const words of [["main", "astra"], ["roup/g"], ["auto/main"], ["p/glob-*"], ["account a"],
["high reasoning", "fast", "free"], ["routing group", "nested"], ["disabled", "lost", "switched off"]]) {
assert.equal(api.groupMatches(g, words), true, words.join(" + "));
}
assert.equal(api.groupMatches(g, ["astra", "missing"]), false);
});
test("search text follows data replacement, language changes and five-minute eviction", () => {
const state = data(), api = load(state), g = state.groups[0];
assert.equal(api.groupMatches(g, ["routing group"]), true);
api.lang("zh");
assert.equal(api.groupMatches(g, ["路由组"]), true);
assert.equal(api.groupMatches(g, ["routing group"]), false);
const next = data();
next.models[0].name = "Sol"; next.accounts.p[0].name = "Account B";
next.groups[1].name = "Renamed"; next.groups[0].memberInfo[0].free = false; next.groups[0].fast = [];
api.refresh(next);
const ng = next.groups[0];
assert.equal(api.groupMatches(ng, ["sol", "account b", "renamed"]), true);
for (const word of ["astra", "account a", "fast", "free"]) assert.equal(api.groupMatches(ng, [word]), false, word);
api.visible(false); api.expire(); api.visible(true);
assert.equal(api.groupMatches(ng, ["sol", "account b"]), true);
});