Files
stack/packages/webui/tests/triage-settings-browser.test.mjs

263 lines
21 KiB
JavaScript

// #1873 triage-settings: Settings > Triage in a real browser. The owner sees "not set", picks a model,
// a harness from that model's list, a cap and a time zone, and saves in three clicks; a refusal is one
// line and keeps the fields; a stale revision offers a re-read; a second Save while one runs does
// nothing; anyone /api/config does not call the owner gets no Triage page and no route read.
import test from 'node:test';
import assert from 'node:assert/strict';
import { w15Fixture } from './w15-fixture.mjs';
const T = '2026-11-18T12:00:00.000Z';
const NOTE = 'Saving a route does not start Triage. The key and the wiring are separate.';
const choices = { status: 'ready', models: [
{ key: 'down', label: 'Down', available: false, harnesses: ['claude-code'] },
{ key: 'glm-flash', label: 'GLM Flash', available: true, harnesses: ['pi'] },
{ key: 'sonnet', label: 'Sonnet', available: true, harnesses: ['claude-code', 'pi'] },
] };
const unset = { route: null, routeMessage: 'No Triage route is set.', revision: 0, choices, send: true, note: NOTE };
const route = (over = {}) => ({ model: 'sonnet', harness: 'pi', cap: 200000, reset: '00:00', timeZone: 'Europe/Berlin', revision: 1, updatedAt: T, ...over });
const config = (over = {}) => ({ send: true, devOwner: true, sso: true, proxyAdmin: true, proxyAdminSend: true, ...over });
const scenario = (read = unset, extra = {}, cfg = {}) => ({ '/api/config': config(cfg), '/api/triage/route': read, ...extra });
const writes = b => b.evaluate('window.__writes.filter(w=>w.path==="/api/triage/route").map(w=>({method:w.method,body:JSON.parse(w.body)}))');
// The fixture answers in the page, so no resource timing exists: count the route reads in a wrapper.
const countReads = b => b.call('Page.addScriptToEvaluateOnNewDocument', { source: '(()=>{window.__routeReads=0;const prev=window.fetch;window.fetch=(i,init)=>{try{if(new URL(typeof i==="string"?i:i.url,location.href).pathname==="/api/triage/route"&&(init?.method??"GET")==="GET")window.__routeReads++;}catch{}return prev(i,init);};})()' });
const reads = b => b.evaluate('window.__routeReads');
const pick = (b, id, value) => b.evaluate(`(()=>{const el=document.getElementById(${JSON.stringify(id)});el.value=${JSON.stringify(value)};el.dispatchEvent(new Event('input',{bubbles:true}));el.dispatchEvent(new Event('change',{bubbles:true}));})()`);
const type = (b, id, value) => b.evaluate(`(()=>{const el=document.getElementById(${JSON.stringify(id)});el.value=${JSON.stringify(value)};el.dispatchEvent(new Event('input',{bubbles:true}));})()`);
const text = (b, id) => b.evaluate(`document.getElementById(${JSON.stringify(id)})?.textContent ?? null`);
const value = (b, id) => b.evaluate(`document.getElementById(${JSON.stringify(id)})?.value ?? null`);
const options = (b, id) => b.evaluate(`[...document.getElementById(${JSON.stringify(id)}).options].map(o=>[o.value,o.textContent,o.disabled])`);
async function fill(b, { model = 'sonnet', harness = 'claude-code', cap = '150000', zone = 'America/Chicago' } = {}) {
await pick(b, 'triage-route-model', model);
if (harness !== null) await pick(b, 'triage-route-harness', harness);
await type(b, 'triage-route-cap', cap);
await type(b, 'triage-route-zone', zone);
}
test('the owner sees Triage in Settings, "not set" before a first save, the catalogue choices and the note', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario() });
await countReads(b);
await b.navigate(f.base + '/#/settings');
await wait('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\')');
await b.evaluate('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\').click()');
await wait('document.getElementById("triage-route-stored")');
assert.match(await text(b, 'triage-route-stored'), /^Not set\. No Triage route is set\.$/);
assert.ok(await reads(b) >= 1, 'the owner page reads the route (the counter works)');
assert.equal(await text(b, 'triage-route-note'), NOTE);
assert.equal(await text(b, 'settings-page-title'), 'Triage');
assert.deepEqual(await options(b, 'triage-route-model'), [['', 'Choose a model', false], ['down', 'Down (down) (unavailable)', true], ['glm-flash', 'GLM Flash (glm-flash)', false], ['sonnet', 'Sonnet (sonnet)', false]]);
assert.deepEqual(await options(b, 'triage-route-harness'), [['', 'Choose a model first', false]]);
assert.equal(await value(b, 'triage-route-cap'), '', 'no default cap');
assert.equal(await value(b, 'triage-route-zone'), '', 'no default time zone');
assert.equal(await value(b, 'triage-route-reset'), '00:00');
assert.equal(await b.evaluate('document.getElementById("triage-route-reset").readOnly'), true);
assert.equal(await b.evaluate('document.querySelector(".settings-readonly")'), null);
assert.equal(await b.evaluate('document.getElementById("triage-route-catalogue")'), null);
// The harness list follows the model; a model with one harness picks it.
await pick(b, 'triage-route-model', 'glm-flash');
assert.deepEqual(await options(b, 'triage-route-harness'), [['', 'Choose a harness', false], ['pi', 'pi', false]]);
assert.equal(await value(b, 'triage-route-harness'), 'pi');
await pick(b, 'triage-route-model', 'sonnet');
assert.deepEqual((await options(b, 'triage-route-harness')).map(o => o[0]), ['', 'claude-code', 'pi']);
assert.equal(await value(b, 'triage-route-harness'), 'pi', 'a harness the new model takes is kept');
assert.deepEqual(await writes(b), []);
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
assert.deepEqual(await b.evaluate('window.__errors'), []);
});
test('Save posts the fields and the read revision once, then shows the stored route; three clicks from Settings', { timeout: 90000 }, async t => {
const saved = route({ harness: 'claude-code', cap: 150000, timeZone: 'America/Chicago' });
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(unset, { 'POST /api/triage/route': { body: { route: saved, revision: 1, note: NOTE } } }) });
await b.navigate(f.base + '/#/settings');
await wait('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\')');
await b.evaluate('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\').click()');
await wait('document.getElementById("triage-route-model")');
await fill(b, { zone: ' America/Chicago ' });
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('document.getElementById("triage-route-line")?.textContent==="Saved. Revision 1."');
assert.equal(await b.evaluate('document.activeElement?.id'), 'triage-route-line', 'focus lands on the saved line, not the page body');
assert.deepEqual(await writes(b), [{ method: 'POST', body: { model: 'sonnet', harness: 'claude-code', cap: 150000, timeZone: 'America/Chicago', revision: 0 } }]);
assert.match(await text(b, 'triage-route-stored'), /Stored route: sonnet on claude-code, cap 150,000 tokens a day, reset 00:00 America\/Chicago\. Revision 1/);
assert.equal(await value(b, 'triage-route-cap'), '150000');
// The next save carries the new revision.
await type(b, 'triage-route-cap', '9000');
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('window.__writes.filter(w=>w.path==="/api/triage/route").length===2');
assert.equal((await writes(b))[1].body.revision, 1);
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
});
test('a stored route fills the fields; each refusal is one plain line and keeps the fields', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario({ ...unset, route: route(), routeMessage: undefined, revision: 1 }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-model")?.value==="sonnet"');
assert.equal(await value(b, 'triage-route-harness'), 'pi');
assert.equal(await value(b, 'triage-route-cap'), '200000');
assert.equal(await value(b, 'triage-route-zone'), 'Europe/Berlin');
assert.match(await text(b, 'triage-route-stored'), /^Stored route: sonnet on pi, cap 200,000 tokens a day, reset 00:00 Europe\/Berlin\. Revision 1, saved 2026-11-18T12:00:00\.000Z\.$/);
for (const [error, message, status] of [['triage-zone-unknown', 'The time zone is not one this Console knows.', 400], ['model-harness-incompatible', 'That harness does not run this model.', 400], ['model-provider-unavailable', 'The model provider is not available now.', 503]]) {
await b.evaluate(`Object.assign(window.__scenario, {'POST /api/triage/route': {status: ${status}, body: {error: ${JSON.stringify(error)}, message: ${JSON.stringify(message)}}}})`);
await fill(b, { zone: 'Mars/Olympus', cap: '777' });
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait(`document.getElementById("triage-route-line")?.textContent===${JSON.stringify(message)}`);
assert.equal(await value(b, 'triage-route-model'), 'sonnet');
assert.equal(await value(b, 'triage-route-harness'), 'claude-code');
assert.equal(await value(b, 'triage-route-cap'), '777');
assert.equal(await value(b, 'triage-route-zone'), 'Mars/Olympus');
assert.equal(await b.evaluate('document.getElementById("triage-route-reload")'), null, 'only a conflict offers a re-read');
assert.equal(await b.evaluate('document.getElementById("triage-route-save").disabled'), false);
}
assert.match(await text(b, 'triage-route-stored'), /Revision 1/, 'a refusal leaves the stored line alone');
// A refusal with no readable answer still says one line.
await b.evaluate(`Object.assign(window.__scenario, {'POST /api/triage/route': {status: 500, body: {}}})`);
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('document.getElementById("triage-route-line")?.textContent==="The route was refused. Nothing was saved."');
assert.equal(await value(b, 'triage-route-cap'), '777');
// Leaving the page and coming back reads the route again and keeps what was typed.
await b.evaluate('location.hash="#/settings/appearance"');
await wait('!document.getElementById("triage-route")');
await b.evaluate('location.hash="#/settings/triage"');
await wait('document.getElementById("triage-route-cap")');
assert.equal(await value(b, 'triage-route-cap'), '777');
assert.equal(await value(b, 'triage-route-zone'), 'Mars/Olympus');
assert.match(await text(b, 'triage-route-stored'), /Revision 1/);
});
test('a stale revision says so, keeps the fields, and Reload route reads the stored route again', { timeout: 90000 }, async t => {
const message = 'The Triage route changed since this page read it. Reload the route, then save again.';
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(unset, { 'POST /api/triage/route': { status: 409, body: { error: 'triage-route-stale', message, revision: 2 } } }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-model")');
await fill(b, { cap: '5000' });
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait(`document.getElementById("triage-route-line")?.textContent===${JSON.stringify(message)}`);
assert.equal(await value(b, 'triage-route-cap'), '5000');
assert.equal(await b.evaluate('document.activeElement?.id'), 'triage-route-reload');
await b.evaluate(`window.__scenario['/api/triage/route'] = ${JSON.stringify({ ...unset, route: route({ revision: 2, model: 'glm-flash', cap: 42 }), routeMessage: undefined, revision: 2 })}`);
await b.evaluate('document.getElementById("triage-route-reload").click()');
await wait('document.getElementById("triage-route-model")?.value==="glm-flash"');
assert.equal(await value(b, 'triage-route-cap'), '42');
assert.match(await text(b, 'triage-route-stored'), /Revision 2/);
assert.equal(await b.evaluate('document.getElementById("triage-route-reload")'), null);
await b.evaluate(`window.__scenario['POST /api/triage/route'] = {body: {route: ${JSON.stringify(route({ revision: 3, model: 'glm-flash', cap: 42 }))}, revision: 3, note: ${JSON.stringify(NOTE)}}}`);
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('document.getElementById("triage-route-line")?.textContent==="Saved. Revision 3."');
assert.equal((await writes(b)).at(-1).body.revision, 2, 'the save after a re-read carries the stored revision');
});
test('an untouched page follows the store; typed fields keep the revision they were read at, so a later save is stale', { timeout: 90000 }, async t => {
const stored = (over, revision) => ({ ...unset, route: route({ revision, ...over }), routeMessage: undefined, revision });
const message = 'The Triage route changed since this page read it. Reload the route, then save again.';
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(stored({}, 1), { 'POST /api/triage/route': { status: 409, body: { error: 'triage-route-stale', message, revision: 3 } } }) });
const away = async () => {
await b.evaluate('location.hash="#/settings/appearance"');
await wait('!document.getElementById("triage-route")');
await b.evaluate('location.hash="#/settings/triage"');
await wait('document.getElementById("triage-route-cap")');
};
const dot = 'document.querySelector(\'#settings-nav a[href="#/settings/triage"] .settings-dot\') !== null';
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-model")?.value==="sonnet"');
// Saved elsewhere while this page sat untouched: the page shows the new route, with no unsaved mark.
await b.evaluate(`window.__scenario['/api/triage/route'] = ${JSON.stringify(stored({ model: 'glm-flash', cap: 42 }, 2))}`);
await away();
assert.equal(await value(b, 'triage-route-model'), 'glm-flash');
assert.equal(await value(b, 'triage-route-cap'), '42');
assert.match(await text(b, 'triage-route-stored'), /Revision 2/);
assert.equal(await b.evaluate(dot), false);
// Typed at revision 2, then saved elsewhere again: the typed fields stay and the save sends revision 2.
await type(b, 'triage-route-cap', '9000');
await b.evaluate(`window.__scenario['/api/triage/route'] = ${JSON.stringify(stored({ model: 'sonnet', cap: 1 }, 3))}`);
await away();
assert.equal(await value(b, 'triage-route-model'), 'glm-flash');
assert.equal(await value(b, 'triage-route-cap'), '9000');
assert.match(await text(b, 'triage-route-stored'), /Revision 3/);
assert.equal(await b.evaluate(dot), true, 'the typed fields are marked unsaved');
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait(`document.getElementById("triage-route-line")?.textContent===${JSON.stringify(message)}`);
const last = (await writes(b)).at(-1).body;
assert.equal(last.revision, 2, 'the save carries the revision the typed fields were read at, not the newest read');
assert.equal(last.model, 'glm-flash');
assert.equal(last.cap, 9000);
});
test('a second Save while one runs does nothing', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(unset, { 'POST /api/triage/route': { body: { route: route(), revision: 1, note: NOTE } } }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-model")');
// Hold the POST until the test lets it go.
await b.evaluate('(()=>{const prev=window.fetch;window.__posts=0;window.fetch=(input,init)=>init?.method==="POST"?(window.__posts++,new Promise(r=>{window.__release=()=>r(prev(input,init));})):prev(input,init);})()');
await fill(b);
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('typeof window.__release==="function"');
assert.equal(await text(b, 'triage-route-save'), 'Saving…');
assert.equal(await b.evaluate('document.getElementById("triage-route-save").disabled'), true);
// A click on the disabled button, a form submit (Enter) and a direct requestSubmit all do nothing.
await b.evaluate('document.getElementById("triage-route-save").click()');
await b.evaluate('document.getElementById("triage-route-form").requestSubmit()');
await b.evaluate('document.getElementById("triage-route-form").dispatchEvent(new Event("submit",{bubbles:true,cancelable:true}))');
assert.equal(await b.evaluate('window.__writes.filter(w=>w.path==="/api/triage/route").length'), 0, 'the held POST is not recorded yet');
assert.equal(await b.evaluate('window.__posts'), 1, 'no second POST left the page');
await b.evaluate('window.__release()');
await wait('document.getElementById("triage-route-line")?.textContent==="Saved. Revision 1."');
assert.equal((await writes(b)).length, 1);
});
test('a read-only Console shows the route with Save off and posts nothing', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario({ ...unset, send: false }, {}, { send: false, proxyAdminSend: false }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-save")');
assert.equal(await b.evaluate('document.getElementById("triage-route-save").disabled'), true);
assert.match(await text(b, 'triage-route-readonly'), /read-only/);
assert.equal(await b.evaluate('document.querySelector(".settings-readonly")'), null, 'the panel says read-only once, from the route\'s own answer');
await b.evaluate('document.getElementById("triage-route-form").dispatchEvent(new Event("submit",{bubbles:true,cancelable:true}))');
assert.deepEqual(await writes(b), []);
});
test('a catalogue that is not ready says so and offers no model', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario({ ...unset, choices: { status: 'bus-unavailable', models: [] } }) });
await b.navigate(f.base + '/#/settings/triage');
await wait('document.getElementById("triage-route-catalogue")');
assert.match(await text(b, 'triage-route-catalogue'), /No organization bus offered a model catalogue \(bus-unavailable\)/);
assert.deepEqual(await options(b, 'triage-route-model'), [['', 'Choose a model', false]]);
});
test('a non-owner sees no Triage section and the page reads no route', { timeout: 90000 }, async t => {
const { f, b, wait } = await w15Fixture(t, { scenario: scenario(unset, {}, { proxyAdmin: undefined, proxyAdminSend: undefined, user: true }) });
await countReads(b);
await b.navigate(f.base + '/#/settings');
await wait('document.querySelector(\'#settings-nav a[href="#/settings/appearance"]\')');
assert.equal(await b.evaluate('document.querySelector(\'#settings-nav a[href="#/settings/triage"]\')'), null);
await b.navigate(f.base + '/#/settings/triage');
await wait('document.querySelector(".nf")');
assert.equal(await b.evaluate('document.getElementById("triage-route")'), null);
assert.equal(await reads(b), 0, 'no route read for a non-owner');
assert.deepEqual(await writes(b), []);
});
// Shots for the build report: run with MOSAIC_UX_SHOTS=<dir>. Not set, filled and one refusal, at 390
// and 1440, light and dark.
test('shots', { timeout: 300000, skip: !process.env.MOSAIC_UX_SHOTS }, async t => {
const { f, b, wait, capture } = await w15Fixture(t, { slice: 'triage', scenario: scenario(unset, { 'POST /api/triage/route': { status: 400, body: { error: 'triage-zone-unknown', message: 'The time zone is not one this Console knows.' } } }) });
const results = [];
for (const scheme of ['light', 'dark']) {
await b.call('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: scheme }] });
for (const width of [390, 1440]) {
await b.viewport(width, width === 390 ? 844 : 900);
// A fresh document each round, so the kept fields of the last round do not carry over.
await b.navigate(`${f.base}/?shot=${scheme}-${width}#/settings/triage`);
await wait('document.getElementById("triage-route-model") && !!window.MosaicAppearance');
await b.evaluate(`window.MosaicAppearance.ready.then(() => window.MosaicAppearance.set({ mode: ${JSON.stringify(scheme)} }))`);
// No sideways scroll at either width.
assert.equal(await b.evaluate('document.documentElement.scrollWidth <= document.documentElement.clientWidth'), true, `${scheme} ${width} scrolls sideways`);
results.push(await capture(`notset-${scheme}`, width, '#/settings/triage'));
await fill(b);
results.push(await capture(`filled-${scheme}`, width, '#/settings/triage'));
await type(b, 'triage-route-zone', 'Mars/Olympus');
await b.evaluate('document.getElementById("triage-route-save").click()');
await wait('document.getElementById("triage-route-line")?.textContent.includes("time zone")');
results.push(await capture(`refusal-${scheme}`, width, '#/settings/triage'));
}
}
for (const r of results) { assert.deepEqual(r.errors, [], r.full); assert.deepEqual(r.unexpected, [], r.full); }
});