UXInteract / task_010 /tests /validate_enhanced_cases.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
7.68 kB
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { pathToFileURL } = require('node:url');
const { chromium } = require('playwright');
const complete = /complete|done|finished|完成/i;
const failed = /fail|error|timeout|失败|错误|超时/i;
const loading = /\bloading\b|加载中/i;
function withoutEcho(text, prompt) {
return text.toLowerCase().replaceAll(prompt.toLowerCase(), '')
.replace(/\b(?:request|reply|response|result|title)\s*#?\s*\d+\b/gi, ' ')
.replace(/\b[a-z]\b/gi, ' ')
.replace(/[^\p{L}\p{M}\p{N}]+/gu, ' ').trim();
}
async function state(page) {
const result = await page.evaluate(() => {
const node = name => document.querySelector(`[data-testid="${name}"]`);
const title = node('generated-title'), reply = node('generated-content'), status = node('generation-status');
const busy = [reply, ...reply.querySelectorAll('[aria-busy="true"]')].some(element =>
element.getAttribute('aria-busy') === 'true' && element.getClientRects().length);
return {title: title.innerText.trim(), content: reply.textContent.trim(), visibleContent: reply.innerText.trim(), status: status.innerText.trim(), busy,
leak: document.body.innerText.includes('ux-audit-private-48cf83')};
});
assert(!result.leak, 'configuration key must not be rendered, including in error states');
return result;
}
async function submit(page, prompt) {
await page.getByTestId('prompt-input').fill(prompt);
await page.getByTestId('send-button').click();
}
async function untilTerminal(page) {
// Time is controlled, but the implementation chooses its own chunk intervals.
for (let elapsed = 0; elapsed <= 5000; elapsed += 10) {
const current = await state(page);
if (complete.test(current.status) || failed.test(current.status)) return current;
await page.clock.runFor(10);
}
throw new Error('request did not reach a terminal state within the public 5s bound');
}
async function capture(page, name) {
const dir = '/logs/verifier/visual';
fs.mkdirSync(dir, { recursive: true });
await page.screenshot({ path: path.join(dir, `${name}.png`), fullPage: true });
}
(async () => {
const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
try {
const page = await browser.newPage({ viewport: { width: 1000, height: 700 } });
await page.route(/^https?:/, route => route.abort());
await page.clock.install({ time: new Date('2026-01-01T00:00:00Z') });
await page.goto(pathToFileURL(path.resolve('index.html')).href);
await page.clock.pauseAt(new Date('2026-01-01T00:01:00Z'));
await page.evaluate(() => { window.aiConfig.key = 'ux-audit-private-48cf83'; });
const firstPrompt = 'Amber lighthouse restoration: recommend practical next steps';
const secondPrompt = 'Violet submarine inspection: recommend practical next steps';
await submit(page, firstPrompt);
let initial;
let elapsed = 0;
for (; elapsed <= 5000; elapsed += 10) {
const current = await state(page);
if (loading.test(current.title) && (loading.test(current.content) || current.busy)) {
initial = current;
break;
}
if (complete.test(current.status) || failed.test(current.status)) break;
await page.clock.runFor(10);
}
assert(initial, 'both output areas need a visible loading state');
await capture(page, '01-loading');
const contents = new Set();
let first;
for (; elapsed <= 5000; elapsed += 10) {
const current = await state(page);
if (complete.test(current.status)) { first = current; break; }
assert(!failed.test(current.status), 'normal stream must not fail');
if (current.visibleContent && current.visibleContent !== initial.visibleContent) {
if (!contents.size) await capture(page, '02-intermediate');
contents.add(current.visibleContent);
}
await page.clock.runFor(10);
}
assert(first, 'normal stream must complete within 5s');
assert(first.title && first.content, 'both generated outputs must be non-empty');
assert([...contents].some(content => content !== first.visibleContent && first.visibleContent.includes(content)),
'content must visibly grow through an intermediate state before completion');
await capture(page, '03-complete');
await submit(page, secondPrompt);
const second = await untilTerminal(page);
assert(complete.test(second.status));
for (const field of ['title', 'content']) {
const original = withoutEcho(first[field], firstPrompt);
const changed = withoutEcho(second[field], secondPrompt);
assert(original && changed && original !== changed,
`${field} must adapt to the prompt beyond quoting it or changing a counter`);
}
const thirdPrompt = 'Amber lighthouse restoration: draft a risk register and mitigations';
await submit(page, thirdPrompt);
const third = await untilTerminal(page);
assert(complete.test(third.status), 'follow-up request on the same subject must complete');
assert.notEqual(withoutEcho(third.content, thirdPrompt), withoutEcho(first.content, firstPrompt),
'different requests about the same subject need different response content');
for (const field of ['key', 'baseurl', 'modelname']) {
await page.evaluate(field => { window.__savedConfig = {...window.aiConfig}; window.aiConfig[field] = ' '; }, field);
await submit(page, `missing ${field}`);
assert(failed.test((await untilTerminal(page)).status), `missing ${field} must fail`);
await page.evaluate(() => Object.assign(window.aiConfig, window.__savedConfig));
await submit(page, `recovered ${field}`);
assert(complete.test((await untilTerminal(page)).status), `${field} recovery must succeed`);
}
await page.evaluate(() => window.__setMockMode('timeout'));
await submit(page, 'timeout case');
assert(failed.test((await untilTerminal(page)).status));
await capture(page, '04-timeout');
await page.evaluate(() => window.__setMockMode('normal'));
await submit(page, 'recovered after timeout');
assert(complete.test((await untilTerminal(page)).status));
const text = '中文 "quoted"\nnew line \\ path';
await page.evaluate(text => window.__setMockChunks(['0:' + JSON.stringify(text), ' and plain tail']), text);
await submit(page, 'frame parsing');
const parsed = await untilTerminal(page);
assert(complete.test(parsed.status));
assert([parsed.content, parsed.visibleContent].includes(text + ' and plain tail'),
'frame escaping and concatenation must preserve text');
await capture(page, '05-decoded-frames');
const valid = 'Recovered text after an invalid frame';
await page.evaluate(text => window.__setMockChunks(['0:"unterminated', '0:' + JSON.stringify(text)]), valid);
await submit(page, 'test mixed valid and invalid frames');
const mixed = await untilTerminal(page);
if (complete.test(mixed.status)) {
assert.equal(mixed.content, valid, 'skipping an invalid frame must preserve only the valid response');
} else {
assert(failed.test(mixed.status), 'an invalid frame must be skipped or terminate with a visible error');
}
const key = await page.evaluate(() => window.aiConfig.key);
assert(!(await page.locator('body').innerText()).includes(key), 'configuration key must not be rendered');
console.log('variable timing, real intermediate content, all config fields, timeout recovery and frame parsing passed');
} finally {
await browser.close();
}
})().catch(error => { console.error(error); process.exit(1); });