UXInteract / task_009 /tests /validate_enhanced_cases.js
DojiSum's picture
Publish the current 141-task UXInteract release
ef987bb verified
Raw History Blame Contribute Delete
9.51 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 auditDir = '/logs/verifier/visual';
const locator = (page, name) => page.getByTestId(name);
async function open(browser) {
const page = await browser.newPage({ viewport: { width: 1200, height: 800 } });
await page.route(/^https?:/, route => route.abort());
await page.goto(pathToFileURL(path.resolve('index.html')).href);
await page.evaluate(() => {
window.__requests = [];
window.__setMockGenerate((prompt, id) => new Promise((resolve, reject) => {
window.__requests.push({ prompt, id, resolve, reject });
}));
});
return page;
}
async function send(page, index, prompt) {
await locator(page, 'prompt-input').nth(index).fill(prompt);
await locator(page, 'send-button').nth(index).click();
await page.waitForFunction(value => window.__requests.some(r => r.prompt === value), prompt);
}
async function settle(page, prompt, text, reject = false) {
await page.evaluate(({ prompt, text, reject }) => {
const request = window.__requests.find(r => r.prompt === prompt);
if (!request) throw new Error(`missing request: ${prompt}`);
if (reject) request.reject(new Error(text));
else request.resolve(text);
}, { prompt, text, reject });
}
async function pending(page, index) {
const block = locator(page, 'prompt-response-block').nth(index);
const response = locator(page, 'response').nth(index);
assert(await response.isVisible(), 'pending response area must be visible');
const text = await response.innerText();
const busy = await block.evaluate(el => el.getAttribute('aria-busy') === 'true' ||
[...el.querySelectorAll('[aria-busy="true"]')].some(node => {
const box = node.getBoundingClientRect();
const style = getComputedStyle(node);
return box.width > 0 && box.height > 0 && style.visibility !== 'hidden';
}));
assert(busy || /loading|generating|pending|加载|生成中|处理中/i.test(text),
'pending request needs a visible loading indicator (localized text or aria-busy)');
}
async function responseIs(page, index, expected) {
await page.waitForFunction(({ index, expected }) =>
document.querySelectorAll('[data-testid="response"]')[index]?.textContent.trim() === expected,
{ index, expected });
}
async function assertSameBlockId(page, initialPrompt, laterPrompt) {
const stable = await page.evaluate(([first, later]) => {
const initial = window.__requests.find(request => request.prompt === first);
const current = window.__requests.find(request => request.prompt === later);
return initial?.id != null && current?.id === initial.id;
}, [initialPrompt, laterPrompt]);
assert(stable, 'resubmission must retain the original block ID passed to the generator');
}
async function sidebarFrame(page) {
return locator(page, 'sidebar').screenshot({animations: 'disabled', caret: 'hide'});
}
async function waitForSidebarChange(page, previous) {
const deadline = Date.now() + 3000;
do {
const frame = await sidebarFrame(page);
if (!frame.equals(previous)) return frame;
await page.waitForTimeout(40);
} while (Date.now() < deadline);
assert.fail('sidebar must visibly indicate the block status change');
}
async function completeFirstBlockSidebar(browser) {
const page = await open(browser);
try {
const initial = await sidebarFrame(page);
await send(page, 0, 'alpha pending');
const firstPending = await waitForSidebarChange(page, initial);
await locator(page, 'new-block-placeholder').click();
const newDraft = await waitForSidebarChange(page, firstPending);
await send(page, 1, 'beta pending');
const bothPending = await waitForSidebarChange(page, newDraft);
await settle(page, 'alpha pending', 'first isolated result');
await responseIs(page, 0, 'first isolated result');
await pending(page, 1);
return await waitForSidebarChange(page, bothPending);
} finally {
await page.close();
}
}
async function capture(page, name) {
fs.mkdirSync(auditDir, { recursive: true });
await page.screenshot({ path: path.join(auditDir, `${name}.png`), fullPage: true });
}
(async () => {
const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
try {
const page = await open(browser);
const placeholder = locator(page, 'new-block-placeholder');
assert.equal(await locator(page, 'prompt-input').count(), 1);
assert.equal(await placeholder.isVisible(), false, 'initial placeholder must be hidden');
await locator(page, 'send-button').first().click();
assert.equal(await page.evaluate(() => window.__requests.length), 0, 'empty send must be ignored');
await locator(page, 'prompt-input').first().fill('draft only');
assert.equal(await placeholder.isVisible(), false, 'draft alone must not unlock a block');
await capture(page, '01-draft');
const initialSidebar = await sidebarFrame(page);
await send(page, 0, 'alpha pending');
await pending(page, 0);
const firstPending = await waitForSidebarChange(page, initialSidebar);
assert.equal(await placeholder.filter({ visible: true }).count(), 1);
await placeholder.click();
const newDraft = await waitForSidebarChange(page, firstPending);
assert.equal(await locator(page, 'prompt-input').count(), 2);
assert(await locator(page, 'prompt-input').nth(1).evaluate(el => el === document.activeElement));
await locator(page, 'prompt-input').nth(1).fill('beta draft');
assert.equal(await placeholder.isVisible(), false, 'a new draft blocks the next placeholder');
await send(page, 1, 'beta pending');
await pending(page, 0);
await pending(page, 1);
assert.equal(await placeholder.filter({ visible: true }).count(), 1);
assert.equal(await page.evaluate(() => new Set(window.__requests.map(r => r.id)).size), 2,
'concurrent blocks must have distinct IDs');
await capture(page, '02-concurrent');
const bothPending = await waitForSidebarChange(page, newDraft);
await settle(page, 'beta pending', 'second isolated result');
await responseIs(page, 1, 'second isolated result');
await pending(page, 0);
const secondComplete = await waitForSidebarChange(page, bothPending);
const firstComplete = await completeFirstBlockSidebar(browser);
// Both states have one result and one pending request. A sidebar showing
// only totals would look the same without distinguishing the two blocks.
assert(!firstComplete.equals(secondComplete) && !firstComplete.equals(bothPending),
'sidebar must distinguish which of the two blocks completed');
await placeholder.click();
const draft = locator(page, 'prompt-input').nth(2);
await draft.fill('third block: edited but unsent');
await settle(page, 'alpha pending', 'first isolated result');
await responseIs(page, 0, 'first isolated result');
await responseIs(page, 1, 'second isolated result');
assert(await draft.evaluate(el => el === document.activeElement),
'completion in another block must not steal focus');
assert.equal(await draft.inputValue(), 'third block: edited but unsent',
'completion in another block must preserve the unsent draft');
assert.equal(await page.evaluate(() => window.__requests.length), 2,
'completion in another block must not submit the draft');
assert.equal(await placeholder.isVisible(), false, 'unsent draft must still block the new-block control');
await capture(page, '03-out-of-order-completion');
await send(page, 2, 'gamma pending');
await settle(page, 'gamma pending', 'third isolated result');
await responseIs(page, 2, 'third isolated result');
await send(page, 0, 'request that fails');
await assertSameBlockId(page, 'alpha pending', 'request that fails');
await pending(page, 0);
await settle(page, 'request that fails', 'offline failure', true);
await page.waitForFunction(() => /fail|error|失败|错误/i.test(
document.querySelector('[data-testid="response"]').textContent));
await responseIs(page, 1, 'second isolated result');
assert(await placeholder.isVisible(), 'failure must not permanently block creation');
await capture(page, '04-local-failure');
await send(page, 0, 'retry in original block');
await assertSameBlockId(page, 'alpha pending', 'retry in original block');
await pending(page, 0);
await settle(page, 'retry in original block', 'recovered result');
await responseIs(page, 1, 'second isolated result');
assert.equal(await locator(page, 'prompt-response-block').count(), 3, 'retry must reuse the block');
await responseIs(page, 0, 'recovered result');
await placeholder.click();
assert.equal(await locator(page, 'prompt-response-block').count(), 4);
assert.equal(await placeholder.isVisible(), false);
await page.setViewportSize({ width: 375, height: 812 });
assert(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1),
'narrow viewport must not overflow horizontally');
await capture(page, '05-mobile-retry-and-new-block');
await page.close();
console.log('independent lifecycle, stable IDs, draft retention, per-block sidebar status, and responsive cases passed');
} finally {
await browser.close();
}
})().catch(error => { console.error(error); process.exit(1); });