Files
magpie/internal/gui/tests/routing-context-layout.test.cjs
T
Zhu Wang dae2618ade gui: make the Routing context lazy and align wide cards (#1354)
* gui: make the Routing context lazy and align wide cards (#1354)

Rebuild the context and wide-layout change on main e1b70c13 following
the Oct 13 review. Keep the complete request story, original heading,
visible stage caption and main's unchanged line/logo retention.

Use one context header, construct its body only while open, migrate the
existing fold choice and retain live card updates and the text viewer.
Align the stage/story and overview/contents splits within the page's
existing width cap. Keep the narrow stage sizing and prevent flex shrink
from moving the header when context contents appear.

Three-platform vet and builds and the sandboxed nogui suite passed.
Base assets fail the new context test because a closed card constructs
the detail grid (1 instead of 0). Focused disclosure checks passed;
the complete GUI suite is still running and is tracked in the PR body.

* gui tests: exercise the new Routing layout with user scrolling

Open the lazy context explicitly for its retained-animation check and wait for page entrance animations as well as cells. Reach the group Cancel button and history day with user wheel input rather than relying on forbidden click scrolling. Until the baseline contains the new Routing wrapper, keep its intentional desktop layout out of the unchanged-page pixel comparison; the focused layout matrix covers it and mobile fit still checks Routing.

Validation: group-fold and held-animations passed all 8 cases; phone-rows passed all 10 cases across five languages and both engines; mobile-web passed all 30 cases. git diff --check and JavaScript syntax passed. The complete GUI suite is still being recorded on unchanged d6d5738a assets. Library switch failures are independently reproduced on e1b70c13 assets and tracked by upstream #1609; they are not claimed as passing.

* gui tests: scroll to narrow Routing history before selecting it

The visible full request story can leave a historical request below a narrow window. Reach the day and request row with wheel input before selecting them, while retaining the app scroll guard and all cancellation and failure-colour assertions.

Validation: all 37 checks in routing-agent-gone and routing-agent-hue-not-red pass across both engines, four/five languages and 420/1100px widths. The whole GUI run had exposed the two WebKit German 420px click-scroll failures. git diff --check and JavaScript syntax passed.

* gui: keep context disclosure usable while the next prompt is read

Keep the last available live prompt separately from the render key. Opening or closing the unified header can redraw that carried context while a new request has no prompt yet; clearing a render key for a disclosure no longer removes the entire card. Explicitly picked requests still start fresh, and unavailable or completed requests clear the carried data.

Validation: before the fix, the new pending-prompt disclosure regression fails because the carried context becomes invisible. After the fix, all 134 context disclosure, saved-choice, Usage-link, live patch, every-frame and provider-logo checks pass in Chromium and WebKit across five languages and desktop/phone widths. Native macOS, Linux nogui and Windows builds, syntax and git diff --check pass. The complete GUI run is recorded separately with unresolved existing failures linked in the PR description.
2026-10-14 15:27:59 +08:00

114 lines
8.2 KiB
JavaScript

// The context header is lazy and stable; Routing keeps its full story.
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 = process.env.MAGPIE_CONTEXT_ASSETS || path.resolve(__dirname, '../assets');
const now = new Date().toISOString();
const account = { id: 'codex', provider: 'codex', name: 'Codex', who: 'demo@example.com', icon: 'codex-color', kind: 'account', model: 'gpt-6-luna' };
const prompt = { window: 272000, tokens: 43900, counted: true, turns: 1,
parts: [{ kind: 'system', tokens: 10000, items: [{ name: 'prompt', tokens: 10000 }] },
{ kind: 'tools', tokens: 9500, items: [{ name: 'exec_command', tokens: 9500 }] },
{ kind: 'memory', tokens: 5300, items: [{ name: '/demo/AGENTS.md', tag: 'project', tokens: 5300 }] },
{ kind: 'results', tokens: 14500, items: [{ name: 'exec_command', tag: 'result', n: 4, tokens: 14500 }] },
{ kind: 'chat', tokens: 4600, items: [{ name: '1', tag: 'turn', tokens: 4600 }] }] };
function request(id, extra = {}) {
return { id, seq: id, time: now, agent: 'codex', model: 'codex/gpt-6-luna', provider: 'codex',
kind: 'ambient_suggestions', order: [account], tries: [{ id: account.id, model: account.model, effort: 'medium', start: now, done: true, status: 200, ms: 5900, ttft: 5700 }],
done: true, status: 200, ms: 5900, ttft: 5700, tokens: 44100, out: 200, prompt,
usage: [{ in: 3200, cache_read: 40700, out: 200 }], ...extra };
}
function serve(lang, feed) {
return async (r) => {
const url = new URL(r.request().url()), json = (data) => r.fulfill({ json: data });
if (url.pathname === '/boot.js') return r.fulfill({ contentType: 'text/javascript', body: `window.bootPrefs={lang:"${lang}",theme:"dark",web:true};` });
if (url.pathname === '/wails/runtime.js') return r.fulfill({ contentType: 'text/javascript', body: 'export const Window={};' });
if (url.pathname === '/api/state') return json({ agents: [{ id: 'codex', name: 'Codex', fields: [] }], profiles: [], settings: { lang, theme: 'dark' } });
if (url.pathname === '/api/gateway/trace') {
if (!url.searchParams.has('wait')) { feed.count = 1; feed.next = null; }
const routes = url.searchParams.has('wait') ? await new Promise((resolve) => { feed.next = resolve; }) : [request(100)];
return json({ mine: true, now, seq: routes[0]?.id || 100, totals: { requests: 1, rerouted: 0, errors: 0 }, routes });
}
if (url.pathname === '/api/gateway/history') return json({ days: [], routes: [] });
if (url.pathname === '/api/providers') return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } });
if (url.pathname === '/api/groups') return json({ groups: [] });
if (url.pathname.startsWith('/api/')) return json({});
const file = path.join(assets, url.pathname === '/' ? 'index.html' : url.pathname);
await r.fulfill({ body: await fs.readFile(file), contentType: { '.html': 'text/html', '.js': 'text/javascript', '.css': 'text/css', '.svg': 'image/svg+xml', '.png': 'image/png' }[path.extname(file)] });
};
}
for (const engine of process.env.BROWSER ? [process.env.BROWSER] : ['chromium', 'webkit']) {
for (const lang of ['en', 'zh', 'zh-TW', 'ja', 'de']) {
for (const width of [2048, 1440, 900, 860, 420, 360]) {
test(`${engine} ${lang} ${width}px: a single optional context header preserves the routing story`, async (t) => {
const browser = await (engine === 'webkit' ? webkit.launch() : chromium.launch({ channel: 'chromium' }));
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width, height: 1250 }, reducedMotion: 'reduce' });
page.setDefaultTimeout(5000);
const feed = {}, errors = [];
page.on('pageerror', (e) => errors.push(e.message));
await page.route('**/*', serve(lang, feed));
await page.goto('http://magpie.test/?view=routing');
const toggle = page.locator('.rt-ctx-toggle');
await page.locator('.rt-ctx').waitFor();
assert.equal(await page.locator('.rt-ctx .ctx-waffle').count(), 0, 'closed context does not build the detail grid');
assert.equal(await toggle.getAttribute('aria-expanded'), 'false');
assert.match(await toggle.innerText(), /43\.9K.*272K.*16%.*93%/s);
assert.equal(await page.locator('.rt-cap').isVisible(), true, 'the stage caption stays visible');
assert.equal(await page.locator('.rt-steps li.why').isVisible(), true, 'routing reasons stay visible by default');
const translated = await page.evaluate(() => t('How the last request was routed'));
assert.ok((await page.locator('.rt-log-head').innerText()).includes(translated), 'the descriptive story heading is preserved');
await page.waitForTimeout(500);
await toggle.hover();
const before = await toggle.evaluate((e) => e.getBoundingClientRect().top);
await toggle.click();
await page.locator('.rt-ctx .ctx-waffle').waitFor();
assert.equal(await page.locator('.rt-ctx .ctx-title').count(), 1, 'one header serves both states');
assert.equal(await page.locator('.ctx-summary-numbers').count(), 0, 'expanded figures appear only in the body');
assert.equal(await page.locator('.ctx-waffle > i').count(), 400);
assert.equal(await toggle.getAttribute('aria-expanded'), 'true');
assert.ok(Math.abs(await toggle.evaluate((e) => e.getBoundingClientRect().top) - before) < 1, `opening keeps the header under the pointer: ${before} -> ${await toggle.evaluate((e) => e.getBoundingClientRect().top)}`);
await page.waitForTimeout(100);
const layout = await page.evaluate(() => {
const root = document.querySelector('.rt'), log = root.querySelector('.rt-log').getBoundingClientRect();
const stage = root.querySelector('.rt-stage').getBoundingClientRect(), contents = root.querySelector('.ctx-contents').getBoundingClientRect();
const head = root.querySelector('.ctx-contents-head');
return { wide: root.classList.contains('min1100'), pair: root.classList.contains('min800'),
log: log.left, stage: stage.right, separator: contents.left,
nowrap: getComputedStyle(head).flexWrap, over: document.documentElement.scrollWidth > innerWidth };
});
if (layout.wide) {
assert.ok(layout.log >= layout.stage, 'stage and request story share a row');
assert.ok(Math.abs(layout.log - layout.separator) <= 1, 'both column separators align');
}
assert.equal(layout.nowrap, 'nowrap', 'content label and filters share a row');
assert.equal(layout.over, false, 'the page fits the window');
await toggle.click();
assert.equal(await page.locator('.ctx-waffle').count(), 0, 'closing releases detail contents');
await toggle.focus();
await page.keyboard.press('Enter');
await page.locator('.ctx-waffle').waitFor();
assert.equal(await page.evaluate(() => localStorage.getItem('magpie.routingContext')), '1');
await page.reload();
await page.locator('.ctx-waffle').waitFor();
assert.equal(await page.locator('.rt-ctx-toggle').getAttribute('aria-expanded'), 'true', 'reload restores the preference');
if (width === 900) {
for (let i = 0; i < 60 && !feed.next; i++) await page.waitForTimeout(50);
assert.ok(feed.next, 'the next trace request is waiting');
const next = feed.next; feed.next = null;
next([request(101, { done: false, prompt: undefined, usage: [], tries: [] })]);
await page.waitForFunction(() => document.querySelectorAll('.rt-req').length === 2);
await page.locator('.rt-ctx-toggle').click();
assert.equal(await page.locator('.rt-ctx').isVisible(), true, 'a disclosure keeps the carried context while the new prompt is being read');
assert.equal(await page.locator('.ctx-waffle').count(), 0);
await page.locator('.rt-ctx-toggle').click();
await page.locator('.ctx-waffle').waitFor();
assert.equal(await page.locator('.rt-ctx-toggle').getAttribute('aria-expanded'), 'true');
}
assert.deepEqual(errors, []);
});
}
}
}