Files
plugins/packages/nowledge-mem/page-layout.test.mjs
T
yetone 234e073c60 nowledge-mem test: headless Chrome no longer hangs on its component updater (#972)
The "narrow settings footer" test spawns Chrome with --dump-dom. Chrome 151
(Chrome for Testing, and the runner's stable) intermittently never exits:
its SODA on-demand component update and the component updater/breakpad
start-up stall, so the process outlived the test's 30s and was killed.
This failed the publish run 43122260448 (page-chat 0.1.14) and 52e31c4a.

Cause fixed, not the timeout: the test now starts Chrome with the speech
(SODA), component-update, breakpad, field-trial and default-app start-up
work turned off, so --dump-dom exits as soon as the page is dumped.

Run (WSL magpie-desktop via wsl-run, Chrome for Testing 151 as `chromium`,
HOME=$(mktemp -d), bun 1.4.2):
- old code: red, timed out at 30s (reproduced; plain chrome --dump-dom of a
  trivial page hung 15s in 4 of 4 runs with only the SODA flag)
- with the flags: green 3 of 3, ~0.8s each; the flag set exited in 0.4-1s in
  8 of 8 bare chrome runs
Not run: the full bun test suite; GitHub's own runner.
2026-11-12 08:06:44 +08:00

65 lines
4.7 KiB
JavaScript

// This small layout regression runs only with Linux Chrome. It uses a data
// URL, a temporary profile and no network or user browser state.
import { expect, test } from "bun:test"
import { mkdtemp, rm } from "node:fs/promises"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { runInNewContext } from "node:vm"
const chrome = process.platform === "linux" &&
(Bun.which("google-chrome") || Bun.which("google-chrome-stable") || Bun.which("chromium") || Bun.which("chromium-browser"))
test.skipIf(!chrome)("narrow settings footer keeps translated actions inside the dialog", async () => {
const read = (name) => Bun.file(new URL(`./page/${name}`, import.meta.url)).text()
const [html, css, dictionary] = await Promise.all([read("settings.html"), read("style.css"), read("i18n.js")])
const footer = html.match(/<footer>[\s\S]*?<\/footer>/)?.[0]
expect(footer).toBeTruthy()
const { messages } = runInNewContext(`${dictionary}\nnowledgeMemI18n`)
const translations = Object.fromEntries(["en", "zh", "zh-TW", "ja", "de"].map((lang) => [lang, {
saveHelp: messages[lang].saveHelp, cancel: messages[lang].cancel, save: messages[lang].save,
}]))
const script = `
const translations = ${JSON.stringify(translations)};
const footer = ${JSON.stringify(footer)};
const results = [];
for (const language of Object.keys(translations)) for (const width of [320, 380, 900]) for (const theme of ['light', 'dark']) {
const host = document.createElement('div'); host.style.width = width + 'px';
host.style.colorScheme = theme;
if (theme === 'dark') { host.style.setProperty('--magpie-card', '#222'); host.style.setProperty('--magpie-fg', '#eee'); }
host.innerHTML = '<dialog open><form><div class="dialog-body"><small id="key-help">Help</small><div class="field-wrap"><label>Key environment</label></div></div>' + footer + '</form></dialog>';
host.querySelectorAll('[data-t]').forEach((el) => { el.textContent = translations[language][el.dataset.t]; });
document.body.append(host);
const box = host.querySelector('dialog').getBoundingClientRect();
const form = host.querySelector('form').getBoundingClientRect();
const foot = host.querySelector('footer').getBoundingClientRect();
const actions = host.querySelector('.actions').getBoundingClientRect();
const save = host.querySelector('#save').getBoundingClientRect();
const gap = host.querySelector('.field-wrap').getBoundingClientRect().top - host.querySelector('#key-help').getBoundingClientRect().bottom;
const inside = (r, p) => r.left >= p.left - 1 && r.right <= p.right + 1;
results.push({language, width, theme, fits: inside(form, box) && inside(foot, box) && inside(actions, foot) && inside(save, foot), gap});
}
document.getElementById('results').textContent = JSON.stringify(results);
`
const page = `<!doctype html><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src 'unsafe-inline'; script-src 'unsafe-inline'"><style>${css}\ndialog { position: relative; inset: auto; margin: 0; }</style><pre id="results"></pre><script>${script}</script>`
const temp = await mkdtemp(join(tmpdir(), "nowledge-layout-"))
try {
const proc = Bun.spawn({
cmd: [chrome, "--headless=new", "--no-sandbox", "--disable-gpu", "--disable-dev-shm-usage", "--disable-background-networking", "--disable-extensions", "--disable-sync", "--disable-features=OnDeviceSpeechRecognition,SODA,OptimizationHints,MediaRouter,Translate", "--disable-component-update", "--disable-breakpad", "--disable-field-trial-config", "--no-service-autorun", "--disable-default-apps", "--no-first-run", "--no-default-browser-check", "--password-store=basic", "--host-resolver-rules=MAP * ~NOTFOUND", `--user-data-dir=${temp}/profile`, "--window-size=1200,1600", "--dump-dom", `data:text/html;charset=utf-8,${encodeURIComponent(page)}`],
env: { HOME: temp, PATH: "/usr/bin:/bin", XDG_CONFIG_HOME: `${temp}/config`, XDG_CACHE_HOME: `${temp}/cache` },
stdout: "pipe", stderr: "pipe",
})
const [out, err, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited])
expect({ code, error: code ? err : "" }).toEqual({ code: 0, error: "" })
const result = out.match(/<pre id="results">([^<]+)<\/pre>/)?.[1]
expect(result).toBeTruthy()
const rows = JSON.parse(result)
expect(rows).toHaveLength(30)
for (const row of rows) {
expect({ language: row.language, width: row.width, fits: row.fits }).toEqual({ language: row.language, width: row.width, fits: true })
expect(row.gap).toBeGreaterThanOrEqual(11)
}
} finally {
await rm(temp, { recursive: true, force: true })
}
}, 30000)