Files
magpie/internal/gui/tests/library-memory-select.test.cjs
T
yetone 6a253f8af7 memory: pick several memories and keep, archive or delete them at once (RealZeroP27 on X)
Library -> Memory reviewed one memory at a time: To review held
candidates that had to be opened and kept or deleted one by one.

- Select (beside the filters, shown when two or more are listed) puts a
  box on each memory. A click picks one, Shift-click the range from the
  last one clicked, up/down move between the boxes (Shift picks as it
  goes), Cmd/Ctrl+A picks all shown, Escape ends it. A search or filter
  unpicks what it hides.
- The bar under the list (the library's lib-pickbar, as Skills' Select)
  keeps (To review), archives (In use) or restores (Archived) the picked
  ones, and deletes them on a second click.
- POST /api/memory/status {ids, status}: memory.SetStatus changes them in
  one write, each as Edit by the user (an archived inferred memory is
  dismissed, so it isn't proposed again). Unknown ids are skipped; none
  found, or a bad status, is a 400 and changes nothing.
  Delete reuses POST /api/memory/remove {ids}.

Run (mote, at this head):
- go test ./internal/memory ./internal/memdistill ./internal/library: ok
- go test -run 'Memory|Distill|Dream' ./internal/gui: ok
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet
  ./..., and the three builds: ok. gofmt -l: clean.
- library-memory-select.test.cjs (new): 22/22, chromium and webkit,
  en/zh/zh-TW/ja/de at 980 and 420 wide (no native select, no sideways
  overflow).
- Every library-*, i18n-* and gui-* GUI file (67), both engines: 1309 of 1309 pass.
- Rebased on 2219fdd49: i18n-keys, library-memory, library-memory-select
  and library-bili 132/132; go test ./internal/memory and -run Memory
  ./internal/gui ok (at d374168d0; only a
  client_icons test changed in those packages since).

Broken by hand:
- SetStatus without editIn: TestSetStatusOfSome red ("after keeping two:
  candidate candidate candidate candidate"), and
  TestMemoryStatusRouteActsOnSome red.
- memPick with no Shift range: the GUI test red (got [c1, c4], wanted
  c1..c4).

Not tried: the real app with a real memory.json, the WebKitGTK Linux app,
Windows' WebView2.
2026-11-10 16:00:30 +08:00

183 lines
11 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The Library's Memory tab picks several memories and acts on them at once
// (RealZeroP27 on X): Select puts a box on each row, a click picks one and
// a Shift-click every one from the last picked, ↑ ↓ move between the boxes
// and ⌘A / Ctrl+A picks them all, and the bar under the list keeps the ones
// to review, archives the ones in use, and deletes on a second click, each
// in one request. Escape ends it. Every language, wide and narrow,
// Chromium and WebKit; 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 HOME = "/Users/mem";
const library = {
dir: `${HOME}/.magpie/library`, backups: `${HOME}/.magpie/backups`, home: HOME,
agents: [{ id: "codex", name: "Codex", icon: "", instructions: `${HOME}/.codex/AGENTS.md` }],
instructions: { shared: "", sets: [{ id: "default", name: "", active: true, text: "" }], agents: [] },
foundServers: [], projects: [], foundSkills: [], skills: [], servers: [],
};
const at = (d) => `2026-11-0${d}T10:00:00Z`;
function memories() {
const guessed = ["Deploys go out on Fridays", "Prefers pnpm", "Answers in Chinese", "Runs tests on mote", "Ships on Tuesdays"];
return {
memories: [
{ id: "a1", text: "Use pnpm, never npm, in this repo", kind: "rule", scope: "global", origin: "user", status: "active", created: at(1), updated: at(2) },
{ id: "a2", text: "Never push to main", kind: "rule", scope: "global", origin: "user", status: "active", created: at(1), updated: at(1) },
// newest first in the list: c1 … c5
...guessed.map((text, i) => ({ id: "c" + (i + 1), text, kind: "fact", scope: "global", origin: "inferred", status: "candidate", sources: [{ agent: "codex", at: at(3) }], created: at(3), updated: at(9 - i) })),
],
projects: {}, digest: true, agents: [],
};
}
function server(lang, state, sent) {
return async (route) => {
const url = new URL(route.request().url());
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: { agents: [], profiles: [], settings: { lang, theme: "light" } } });
if (url.pathname === "/api/plugins") return route.fulfill({ json: { plugins: [] } });
if (url.pathname === "/api/library") return route.fulfill({ json: library });
if (url.pathname.startsWith("/api/memory/distill")) return route.fulfill({ json: { on: false, model: "", auto: "", running: false, models: [] } });
if (url.pathname.startsWith("/api/memory")) {
if (route.request().method() === "POST") {
const body = route.request().postDataJSON();
sent.push({ path: url.pathname, body });
if (url.pathname === "/api/memory/remove") state.memories = state.memories.filter((m) => !body.ids.includes(m.id));
else if (url.pathname === "/api/memory/status") for (const m of state.memories) if (body.ids.includes(m.id)) m.status = body.status;
}
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.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 });
};
}
const view = "#view-library";
const review = { en: "To review", zh: "待确认", "zh-TW": "待確認", ja: "確認待ち", de: "Zu prüfen" };
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": the Memory tab keeps, archives and deletes the memories picked at once", 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 = [];
t.after(async () => {
if (errors.length) console.log(errors);
await browser.close();
});
for (const lang of Object.keys(review)) for (const width of [980, 420]) {
await t.test(`${lang} ${width}`, async () => {
const ctx = await browser.newContext({ viewport: { width, height: 860 }, reducedMotion: "reduce" });
await ctx.addInitScript(() => { try { localStorage.setItem("magpie.libTab", "memory"); } catch {} });
const page = await ctx.newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
page.on("pageerror", (e) => errors.push(e.message));
const sent = [];
const state = memories();
await page.route("**/*", server(lang, state, sent));
await page.goto("http://magpie.test/");
await page.locator('button[data-view="library"]').click();
const rows = page.locator(`${view} .lib-mems .lib-mem`);
await rows.first().waitFor();
const count = (n) => page.waitForFunction((n) => document.querySelectorAll("#view-library .lib-mems .lib-mem").length === n, n);
const picked = () => page.evaluate(() => [...document.querySelectorAll("#view-library .lib-mems .lib-mem.picked")].map((r) => r.dataset.id));
const fits = async () => {
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth + 1), "the page scrolls sideways");
const body = await page.locator(`${view} .lib-body`).boundingBox();
for (const b of await page.locator(`${view} .lib-membar > *, ${view} .lib-mempickbar > *`).all()) {
const r = await b.boundingBox();
if (r) assert.ok(r.x + r.width <= body.x + body.width + 1, "a control is cut off");
}
};
await page.locator(`${view} .lib-memstatus .opt`, { hasText: review[lang] }).click();
await count(5);
// no boxes until Select
assert.equal(await page.locator(`${view} .lib-mems .lib-pickbox`).count(), 0);
await page.locator(`${view} .lib-memselect`).click();
await page.locator(`${view} .lib-mempickbar`).waitFor();
assert.equal(await page.locator(`${view} .lib-mems .lib-pickbox`).count(), 5);
assert.equal(await page.locator(`${view} select`).count(), 0, "a native select");
assert.equal(await page.locator(`${view} .lib-mempickbar .lib-memto`).count(), 0, "an action with nothing picked");
await fits();
// a click picks one and opens nothing; a Shift-click every one from it
await rows.nth(0).click();
assert.equal(await page.locator(`${view} .lib-memdetail`).count(), 0, "a picked row opened");
assert.deepEqual(await picked(), ["c1"]);
await rows.nth(3).click({ modifiers: ["Shift"] });
assert.deepEqual(await picked(), ["c1", "c2", "c3", "c4"]);
assert.match(await page.locator(`${view} .lib-mempickbar .lib-pickn`).textContent(), /4/);
// a Shift-click selects no text
assert.equal(await page.evaluate(() => String(getSelection())), "");
// the keys: ↓ to the last box, Space picks it, ⌘A / Ctrl+A unpicks every one and picks them again
await page.keyboard.press("ArrowDown");
assert.equal(await page.evaluate(() => document.activeElement.closest(".lib-mem")?.dataset.id), "c5");
await page.keyboard.press("Space");
assert.deepEqual(await picked(), ["c1", "c2", "c3", "c4", "c5"]);
assert.ok(await page.locator(`${view} .lib-mempickall`).isChecked());
await page.keyboard.press("ControlOrMeta+a");
assert.deepEqual(await picked(), []);
await page.keyboard.press("ControlOrMeta+a");
assert.equal((await picked()).length, 5);
// Shift+↑ unpicks as it goes back from an unpicked one
await rows.nth(4).click();
assert.equal((await picked()).length, 4);
await page.keyboard.press("Shift+ArrowUp");
assert.deepEqual(await picked(), ["c1", "c2", "c3"]);
await fits();
// kept, the three in one request
await page.locator(`${view} .lib-mempickbar .lib-memto`).click();
await count(2);
assert.deepEqual(sent.filter((x) => x.path === "/api/memory/status").map((x) => x.body), [{ ids: ["c1", "c2", "c3"], status: "active" }]);
assert.equal(await page.locator(`${view} .lib-mems .lib-pickbox`).count(), 2, "Select ended with memories left");
assert.deepEqual(await picked(), []);
// deleted on a second click
await rows.nth(0).click();
await rows.nth(1).click({ modifiers: ["Shift"] });
await page.locator(`${view} .lib-mempickbar .lib-memdel`).click();
assert.equal(sent.filter((x) => x.path === "/api/memory/remove").length, 0, "deleted on the first click");
assert.match(await page.locator(`${view} .lib-mempickbar .lib-memdel`).textContent(), /2/);
await fits();
await page.locator(`${view} .lib-mempickbar .lib-memdel`).click();
await count(0);
assert.deepEqual(sent.findLast((x) => x.path === "/api/memory/remove").body, { ids: ["c4", "c5"] });
// an empty list ends Select
assert.equal(await page.locator(`${view} .lib-mempickbar`).count(), 0);
// in use: archived at once; Escape ends Select
await page.locator(`${view} .lib-memstatus .opt`).first().click();
await count(5);
await page.locator(`${view} .lib-memselect`).click();
await page.locator(`${view} .lib-mempickbar`).waitFor();
await page.locator(`${view} .lib-mems .lib-mem[data-id="a1"]`).click();
await page.locator(`${view} .lib-mems .lib-mem[data-id="a2"]`).click();
await page.locator(`${view} .lib-mempickbar .lib-memto`).click();
await count(3);
assert.deepEqual(sent.findLast((x) => x.path === "/api/memory/status").body, { ids: ["a1", "a2"], status: "archived" });
await rows.first().click();
await page.keyboard.press("Escape");
await page.locator(`${view} .lib-mempickbar`).waitFor({ state: "detached" });
assert.equal(await page.locator(`${view} .lib-mems .lib-pickbox`).count(), 0);
// and a row opens again
await rows.first().click();
await page.locator(`${view} .lib-memdetail`).waitFor();
await ctx.close();
});
}
assert.deepEqual(errors, []);
});
}