const fs = require('fs'); const path = require('path'); const { pathToFileURL } = require('url'); const { chromium } = require('playwright'); (async () => { const root = process.cwd(); for (const rel of ['.env.example', 'config.js']) if (!fs.existsSync(path.join(root, rel))) throw new Error(`missing ${rel}`); const example = fs.readFileSync(path.join(root, '.env.example'), 'utf8'); for (const setting of ['API_KEY', 'BASE_URL', 'MODEL_NAME']) { if (!new RegExp(`^\\s*(?:export\\s+)?${setting}\\s*=`, 'm').test(example)) { throw new Error(`missing setting ${setting} in .env.example`); } } const browser = await chromium.launch({ headless: true }); const page = await browser.newPage(); await page.goto(pathToFileURL(path.join(root, 'index.html')).href, { waitUntil: 'domcontentloaded' }); if (await page.evaluate(() => typeof window.__setMockMode) !== 'function') throw new Error('window.__setMockMode must be a function for deterministic failure tests'); await page.locator('[data-testid="prompt-input"]').fill('Design a concise note taking app'); await page.locator('[data-testid="send-button"]').click(); await page.waitForFunction(() => /complete|done|finished|完成|已完成/i.test(document.querySelector('[data-testid="generation-status"]')?.textContent || ''), null, { timeout: 5000 }); const title = (await page.locator('[data-testid="generated-title"]').textContent()) || ''; const content = (await page.locator('[data-testid="generated-content"]').textContent()) || ''; if (!title.trim() || !content.trim()) throw new Error('the normal request must display both outputs'); if (/Build a todo app|Develop a cross-platform mobile app/i.test(title + ' ' + content)) { throw new Error('rendered output still contains the old fixed sample text'); } await page.evaluate(() => window.__setMockMode('malformed')); await page.locator('[data-testid="prompt-input"]').fill('parse chunked stream'); await page.locator('[data-testid="send-button"]').click(); await page.waitForFunction(() => /complete|完成|已完成/i.test(document.querySelector('[data-testid="generation-status"]')?.textContent || ''), null, { timeout: 5000 }); const parsed = (await page.locator('[data-testid="generated-content"]').textContent()) || ''; // The specific literal (你好世界) is not disclosed in the instruction or fixture, so only // require that malformed 0:"..." frames are parsed into NON-EMPTY readable content, and // that the raw frame prefixes do not leak into the UI. if (parsed.replace(/\s/g, '').length === 0) throw new Error('malformed 0:"..." chunks should be parsed into non-empty readable content'); if (/\d+:"/.test(parsed)) throw new Error('raw stream frame prefixes should not leak to UI'); await page.evaluate(() => window.__setMockMode('timeout')); await page.locator('[data-testid="prompt-input"]').fill('trigger timeout'); await page.locator('[data-testid="send-button"]').click(); await page.waitForFunction(() => /timeout|failed|error|超时|失败|错误|异常/i.test(document.querySelector('[data-testid="generation-status"]')?.textContent || ''), null, { timeout: 5000 }); await browser.close(); })().catch(err => { console.error(err && err.stack || err); process.exit(1); });