const fs = require('fs'); const path = require('path'); const { pathToFileURL } = require('url'); const { chromium } = require('playwright'); function assert(condition, message) { if (!condition) throw new Error(message); } function findHtml(dir) { for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { if (['tests', 'logs', 'node_modules', '.git'].includes(entry.name)) continue; const file = path.join(dir, entry.name); if (entry.isDirectory()) { const nested = findHtml(file); if (nested) return nested; } else if (entry.name.toLowerCase().endsWith('.html')) { return file; } } return null; } async function openFixture(browser, target) { const page = await browser.newPage({ viewport: { width: 900, height: 700 } }); const attemptedNetwork = []; await page.route(/^https?:\/\//, route => { attemptedNetwork.push(route.request().url()); return route.abort('blockedbyclient'); }); await page.goto(pathToFileURL(target).href, { waitUntil: 'domcontentloaded' }); const input = page.locator('textarea:visible, [contenteditable="true"]:visible, input:not([type="button"]):not([type="submit"]):visible').first(); assert(await input.count(), 'chat page must expose a visible editable input'); return { page, input, attemptedNetwork }; } async function outputText(page) { return page.locator('[data-testid="response"]:visible, [data-testid="title"]:visible, [data-testid="status"]:visible, output:visible, [aria-live]:visible, [role="status"]:visible') .allTextContents().then(parts => parts.join(' ').trim()); } async function inputValue(input) { return input.evaluate(el => 'value' in el ? el.value : (el.textContent || '')); } async function waitForCompletedOutput(page, timeout) { await page.waitForFunction(() => /complete|done|finished|完成|已发送/i.test( document.querySelector('[data-testid="status"]')?.textContent || ''), null, { timeout }); } (async () => { const target = path.join(process.cwd(), 'index.html'); assert(target, 'no HTML entrypoint found'); const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] }); try { // ENHANCED_CASE_1: pressing Enter submits the current prompt and produces a completed response. { const { page, input, attemptedNetwork } = await openFixture(browser, target); await input.fill('enhanced enter submission'); await input.press('Enter'); await waitForCompletedOutput(page, 5000); const response = await outputText(page); assert(response.length > 0, 'Enter must produce a non-empty response'); assert(!/AI is generating response\.\.\./i.test(response), 'completed response must not remain stuck in loading state'); assert(!(await inputValue(input)).includes('\n'), 'plain Enter should submit rather than insert a newline'); assert(attemptedNetwork.length === 0, 'enhanced case must run without network access'); await page.close(); } // ENHANCED_CASE_2: Shift+Enter inserts a newline, does not submit, and preserves/grows input height. { const { page, input, attemptedNetwork } = await openFixture(browser, target); const beforeOutput = await outputText(page); await input.fill(''); await page.waitForTimeout(50); const beforeBox = await input.boundingBox(); for(let i=0;i<12;i++){await input.type(`line ${i}`);if(i<11)await input.press('Shift+Enter')} await page.waitForTimeout(180); const value = await inputValue(input); const afterBox = await input.boundingBox(); assert(value.split('\n').length >= 12, 'Shift+Enter must create content beyond the initial textarea capacity'); assert(await outputText(page) === beforeOutput, 'Shift+Enter must not submit the prompt'); assert(beforeBox && afterBox && afterBox.height > beforeBox.height, 'multiline input must grow above its empty initial height'); assert(attemptedNetwork.length === 0, 'enhanced case must run without network access'); await page.close(); } // ENHANCED_CASE_3: the visible Send button still submits a readable reply. { const { page, input, attemptedNetwork } = await openFixture(browser, target); await input.fill('give a concise productivity answer'); await page.getByTestId('send-button').click(); await waitForCompletedOutput(page, 5000); const reply = page.getByTestId('response'); const response = await reply.innerText(); assert(await reply.isVisible(), 'Send button reply must be visible'); assert(/[a-z\u3400-\u9fff]/i.test(response) && !response.includes('\uFFFD'), 'response must contain readable text'); assert(response.trim().length > 0, 'completed response must remain visible through a public response hook'); assert(!/AI is generating response\.\.\./i.test(await page.locator('body').innerText()), 'page must leave the generating state after completion'); assert(attemptedNetwork.length === 0, 'enhanced case must run without network access'); await page.close(); } // ENHANCED_CASE_4: title and response must belong to the latest prompt, not a fixed fixture. { const {page, input} = await openFixture(browser, target); await input.fill('calendar planning'); await input.press('Enter'); await waitForCompletedOutput(page, 5000); const firstTitle = await page.locator('[data-testid="title"]').innerText(); const firstResponse = await page.locator('[data-testid="response"]').innerText(); await input.fill('recipe shopping'); await input.press('Enter'); await waitForCompletedOutput(page, 5000); const secondTitle = await page.locator('[data-testid="title"]').innerText(); const secondResponse = await page.locator('[data-testid="response"]').innerText(); assert(firstTitle && secondTitle && firstTitle !== secondTitle, 'different prompts need different direct titles'); assert(firstResponse && secondResponse && firstResponse !== secondResponse, 'responses must depend on the submitted prompt'); await page.close(); } // ENHANCED_CASE_5: the response box must show an intermediate chunk before completion. { const page = await browser.newPage(); await page.route(/^https?:\/\//, route => route.abort('blockedbyclient')); await page.clock.install(); await page.goto(pathToFileURL(target).href); await page.clock.pauseAt(new Date(Date.now() + 60000)); await page.locator('[data-testid="chat-input"]').fill('streaming chronology'); await page.locator('[data-testid="chat-input"]').press('Enter'); assert(/generating|loading|生成中/i.test(await page.locator('[data-testid="status"]').innerText()), 'send must show loading'); // The contract gives a 5-second budget, not the oracle's private 60/70 ms timing. let partial = ''; let complete = ''; for (let elapsed = 0; elapsed < 5000; elapsed += 10) { await page.clock.runFor(10); const text = await page.locator('[data-testid="response"]').innerText(); const status = await page.locator('[data-testid="status"]').innerText(); if (/complete|done|完成/i.test(status)) { complete = text; break; } if (text.trim()) partial = text; } assert(partial.trim(), 'a readable streamed chunk must precede completion'); assert(complete.length > partial.length, 'completion must extend the streamed content within 5 seconds'); fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true}); await page.screenshot({path: '/logs/verifier/visual-audit/stream-complete.png', fullPage: true}); await page.close(); } } finally { await browser.close(); } console.log('enhanced cases passed for task 13'); })().catch(err => { console.error(err && err.stack || err); process.exit(1); });