UXInteract / task_011 /tests /validate_request_audit.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
6.82 kB
const assert = require('node:assert/strict');
const path = require('node:path');
const {pathToFileURL} = require('node:url');
const {chromium} = require('playwright');
const fs = require('node:fs');
(async () => {
const browser = await chromium.launch({headless: true, args: ['--no-sandbox']});
try {
const page = await browser.newPage();
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
const capture = name => page.screenshot({path: `/logs/verifier/visual-audit/${name}.png`, fullPage: true});
const network = [];
await page.route(/^https?:/, route => {network.push(route.request().url()); return route.abort()});
await page.goto(pathToFileURL(path.join('/app', 'index.html')).href);
const get = id => page.locator(`[data-testid="${id}"]`);
assert(await page.evaluate(() => Array.isArray(window.__mockRequests)), 'request audit array missing');
await get('prompt-input').fill(' ');
await get('send-button').click();
assert.equal(await page.evaluate(() => window.__mockRequests.length), 0, 'blank prompt created a request');
await get('config-key').fill('private-key-A');
await get('config-baseurl').fill('https://offline-a.invalid/v1');
await get('config-modelname').fill('model-A');
await get('prompt-input').fill('first calendar brief');
await get('send-button').click();
await page.waitForFunction(() => {
const r = window.__mockRequests[0];
return r && r.status === 'loading' && r.chunks.length > 0;
}, null, {timeout: 3000});
const partial = await get('active-content').innerText();
assert(partial.length > 0, 'no intermediate streamed content');
assert(await get('request-status').isVisible(), 'request status is not visible');
const loadingLabel = await get('request-status').innerText();
assert(loadingLabel.trim(), 'loading state has no visible label');
await capture('01-streaming');
await page.waitForFunction(() => window.__mockRequests[0]?.status === 'complete', null, {timeout: 4000});
const first = await page.evaluate(() => structuredClone(window.__mockRequests[0]));
assert(Number.isInteger(first.id), 'request id must be an integer');
assert.deepEqual([first.key, first.baseURL, first.model, first.prompt],
['private-key-A', 'https://offline-a.invalid/v1', 'model-A', 'first calendar brief']);
assert(first.chunks.length >= 2 && first.chunks.every(chunk => typeof chunk === 'string' && chunk.length > 0));
assert.equal(await get('active-content').innerText(), first.chunks.join(''));
assert.notEqual(await get('request-status').innerText(), loadingLabel, 'visible status did not change on completion');
const completionLabel = (await get('request-status').innerText()).trim().toLowerCase();
assert.equal(await get('request-item').count(), 1);
assert(!(await get('request-history').innerText()).includes('private-key-A'), 'history leaks key');
await get('config-key').fill('private-key-B');
await get('config-baseurl').fill('https://offline-b.invalid/v2');
await get('config-modelname').fill('model-B');
await get('prompt-input').fill('second recipe brief');
await get('send-button').click();
await page.waitForFunction(() => window.__mockRequests[1]?.status === 'complete', null, {timeout: 4000});
const records = await page.evaluate(() => structuredClone(window.__mockRequests));
assert.equal(records.length, 2);
assert.deepEqual(records[0], first, 'editing configuration mutated an older request');
assert(Number.isInteger(records[1].id) && records[1].id > first.id,
'request ids must increase without reuse');
assert.deepEqual([records[1].key, records[1].baseURL, records[1].model, records[1].prompt],
['private-key-B', 'https://offline-b.invalid/v2', 'model-B', 'second recipe brief']);
assert(records[1].chunks.length >= 2 && records[1].chunks.every(chunk => typeof chunk === 'string' && chunk.length > 0));
assert.notEqual(records[1].chunks.join(''), first.chunks.join(''));
assert.equal(await get('request-item').count(), 2);
const visible = await get('request-history').innerText();
for (const value of ['first calendar brief', 'second recipe brief', 'model-A', 'model-B', first.chunks.join(''), records[1].chunks.join('')])
assert(visible.includes(value), `history missing ${value}`);
assert(!visible.includes('private-key-A') && !visible.includes('private-key-B'), 'history leaks key');
for (let index = 0; index < records.length; index++) {
const item = get('request-item').nth(index);
assert(await item.isVisible(), `history item ${index} is hidden`);
const text = await item.innerText();
for (const value of [records[index].prompt, records[index].model, records[index].chunks.join('')])
assert(text.includes(value), `history item ${index} lost its own ${value}`);
const withoutPayload = [records[index].prompt, records[index].model, records[index].baseURL,
records[index].chunks.join('')].reduce((remaining, value) => remaining.replace(value, ''), text);
const visibleLabels = await item.locator('[aria-label]:visible, [title]:visible').evaluateAll(
elements => elements.flatMap(element => [element.getAttribute('aria-label'), element.getAttribute('title')].filter(Boolean)),
);
const accessibleLabel = [await item.getAttribute('aria-label'), await item.getAttribute('title')];
const status = [withoutPayload, ...accessibleLabel, ...visibleLabels].filter(Boolean).join(' ').toLowerCase();
assert((completionLabel && status.includes(completionLabel))
|| /\b(?:complete(?:d)?|done|finished|succeeded|success)\b|完成|成功|[✓✔✅]/.test(status),
`history item ${index} does not display its completed status`);
}
await capture('02-two-requests');
assert.equal(network.length, 0, `unexpected network requests: ${network.join(', ')}`);
await get('clear-button').click();
assert.equal(await get('request-item').count(), 0);
assert.equal(await page.evaluate(() => window.__mockRequests.length), 0);
await capture('03-cleared');
await get('prompt-input').fill('fresh request after clearing');
await get('send-button').click();
await page.waitForFunction(() => window.__mockRequests[0]?.status === 'complete', null, {timeout: 4000});
const fresh = await page.evaluate(() => structuredClone(window.__mockRequests[0]));
assert(fresh.id > records[1].id, 'clearing history reused an earlier id');
assert.equal(await get('request-item').count(), 1, 'cleared history reappeared');
await capture('04-after-clear');
console.log('request audit behavior passed');
} finally {
await browser.close();
}
})().catch(error => {console.error(error); process.exit(1)});