import assert from 'node:assert/strict'; import { chromium } from 'playwright'; import { fileURLToPath, pathToFileURL } from 'node:url'; import path from 'node:path'; const directory = path.dirname(fileURLToPath(import.meta.url)); const base = pathToFileURL(path.join(directory, 'index.html')).href; const browser = await chromium.launch({ channel: 'chrome', headless: true }); const context = await browser.newContext({ viewport: { width: 390, height: 844 }, deviceScaleFactor: 1, reducedMotion: 'reduce', }); const page = await context.newPage(); page.setDefaultTimeout(8000); const errors = []; const requests = []; page.on('pageerror', (error) => errors.push(error.message)); page.on('request', (request) => { if (/^https?:/.test(request.url())) requests.push(request.url()); }); const phone = page.locator('#polished'); const click = async (action, selector = '') => { const query = `[data-action="${action}"]${selector}`; const overlay = phone.locator('.overlay').locator(query); await ((await overlay.count()) ? overlay : phone.locator(query)).first().click(); }; const go = async (route) => { await page.goto(`${base}#/${route}`); await page.evaluate(() => document.fonts.ready); }; let checks = 0; async function check(name, operation) { await operation(); checks++; console.log(`PASS ${checks}: ${name}`); } const shot = async (name) => { await page.mouse.move(0, 0); return page.screenshot({ path: path.join(directory, name), fullPage: true, style: '.toast{visibility:hidden!important}', }); }; try { await go('messages'); await check('当前会话结构、四栏导航和前后对照', async () => { assert.equal(await phone.locator('.tabbar button').count(), 4); assert.deepEqual(await phone.locator('.segmented button').allTextContents(), [ '全部', '工作区', ]); assert.equal(await phone.locator('.conversation').count(), 6); assert.equal(await page.locator('#baseline .conversation').count(), 6); assert.equal(await page.locator('#baseline').isVisible(), false); await page.locator('.mobile-review [data-view="baseline"]').click(); assert.equal(await page.locator('#baseline').isVisible(), true); await page.locator('.mobile-review [data-view="polished"]').click(); }); await check('即时搜索与空状态', async () => { await phone.locator('[data-field="search"]').fill('Nova'); assert.equal(await phone.locator('.conversation').count(), 2); await phone.locator('[data-field="search"]').fill('不存在的关键词'); assert.equal(await phone.locator('.empty h3').textContent(), '没有找到相关会话'); await phone.locator('[data-field="search"]').fill(''); }); await shot('preview-messages.png'); await check('工作区折叠、新建、重命名、删除保留会话', async () => { await click('filter', '[data-value="workspace"]'); assert.equal(await phone.locator('.group-toggle[aria-expanded="false"]').count(), 2); await click('group', '[data-id="w1"]'); assert.equal( await phone.locator('.group-toggle[data-id="w1"]').getAttribute('aria-expanded'), 'true', ); await click('workspace-new'); await phone.locator('[data-field="workspace"]').fill('设计评审'); await phone.locator('[data-form="workspace"] button[type="submit"]').click(); assert.equal(await phone.locator('.group-toggle').count(), 3); const group = phone .locator('.list') .filter({ has: page.locator('.group-toggle', { hasText: '设计评审' }) }); await group.locator('[data-action="workspace-rename"]').click(); await phone.locator('[data-field="workspace"]').fill('设计迭代'); await phone.locator('[data-form="workspace"] button[type="submit"]').click(); await phone .locator('.list') .filter({ has: page.locator('.group-toggle', { hasText: '设计迭代' }) }) .locator('[data-action="workspace-delete"]') .click(); await click('confirm-workspace-delete'); assert.equal(await phone.locator('.group-toggle').count(), 2); await phone .locator('.list') .filter({ has: page.locator('.group-toggle[data-id="w1"]') }) .locator('[data-action="workspace-delete"]') .click(); await click('confirm-workspace-delete'); await click('filter', '[data-value="all"]'); assert.equal(await phone.locator('.conversation').count(), 6); }); await go('chat/c1'); await check('保留回复卡片、过程折叠和两份文件', async () => { assert.equal(await phone.locator('.tabbar').count(), 0); assert.equal(await phone.locator('.assistant-card').count(), 1); assert.equal(await phone.locator('.file-card').count(), 2); assert.equal(await phone.locator('details[open]').count(), 0); await phone.locator('.process summary').click(); assert.equal(await phone.locator('details[open]').count(), 1); await phone.locator('.process summary').click(); await click('files'); await click('preview', '[data-id="ppt"]'); assert.equal(await phone.locator('.preview-document').count(), 1); await page.keyboard.press('Escape'); assert.equal(await phone.locator('.overlay').count(), 0); }); await shot('preview-chat.png'); await check('标题校验、预设建议与安全文本渲染', async () => { await click('title'); await phone.locator('[data-field="title"]').fill(' '); await phone.locator('[data-form="title"] button[type="submit"]').click(); assert.equal(await phone.locator('.form-error').textContent(), '名称不能为空'); await click('suggest-title'); assert.equal(await phone.locator('.candidate').count(), 2); await click('use-title'); await phone.locator('[data-field="title"]').fill(''); await phone.locator('[data-form="title"] button[type="submit"]').click(); assert.equal(await phone.locator('.appbar h2').textContent(), ''); assert.equal(await phone.locator('img').count(), 0); await click('title'); await phone.locator('[data-field="title"]').fill('季度经营复盘报告'); await phone.locator('[data-form="title"] button[type="submit"]').click(); }); await check('Enter 换行、IME 安全和同一数字生命不同会话草稿隔离', async () => { const draft = phone.locator('[data-field="draft"]'); await draft.fill('第一行'); await draft.press('Enter'); await draft.pressSequentially('第二行'); assert.equal(await draft.inputValue(), '第一行\n第二行'); await draft.dispatchEvent('keydown', { key: 'Enter', isComposing: true }); assert.equal(await phone.locator('.user-message').count(), 1); await click('activity'); await click('chat', '[data-id="c6"]'); assert.equal(await phone.locator('[data-field="draft"]').inputValue(), ''); await phone.locator('[data-field="draft"]').fill('另一条 Nova 草稿'); await click('activity'); await click('chat', '[data-id="c1"]'); assert.equal(await phone.locator('[data-field="draft"]').inputValue(), '第一行\n第二行'); await click('activity'); await shot('preview-switcher.png'); await click('close'); }); await check('附件、本地发送、后台完成提醒与返回结果', async () => { await phone .locator('[data-field="attachment"]') .setInputFiles({ name: '季度资料.txt', mimeType: 'text/plain', buffer: Buffer.from('demo') }); assert.equal(await phone.locator('.attachment-chip').count(), 1); await click('send'); assert.equal(await phone.locator('[data-field="draft"]').inputValue(), ''); assert.equal(await phone.locator('[data-action="send"]').isDisabled(), true); await click('activity'); await click('chat', '[data-id="c6"]'); await page.waitForTimeout(1400); assert.match(await phone.locator('.toast').textContent(), /已完成/); await phone.locator('.toast [data-action="chat"]').click(); assert.equal(await phone.locator('.assistant-card').count(), 2); assert.match(await phone.locator('.assistant-card').last().textContent(), /未请求 AI/); }); await check('当前容器快速新建、归档与恢复', async () => { await click('chat-menu'); await click('quick-new'); assert.equal(await phone.locator('.appbar h2').textContent(), '新对话'); assert.match(await phone.locator('.appbar p').textContent(), /Nova/); await click('chat-menu'); await click('archive'); await click('drawer'); await click('archived'); assert.match(await phone.locator('.sheet').textContent(), /新对话/); await click('restore'); assert.match(await phone.locator('.sheet').textContent(), /没有已归档会话/); await click('close'); }); await go('contacts'); await check('通讯录原有三段筛选、同事展开和容器重命名', async () => { assert.deepEqual(await phone.locator('.segmented button').allTextContents(), [ '全部', '数字生命', '同事', ]); await click('contact-filter', '[data-value="people"]'); await click('person', '[data-id="p1"]'); assert.equal(await phone.locator('.person-children .conversation').count(), 2); await click('agent', '[data-id="nova"]'); assert.equal(await phone.locator('.appbar h2').textContent(), '容器设置'); assert.equal(await phone.locator('.tabbar').count(), 0); await click('rename-agent'); await phone.locator('[data-field="agent-name"]').fill('Nova 分析助手'); await phone.locator('[data-form="agent"] button[type="submit"]').click(); assert.match(await phone.locator('.settings-card').textContent(), /Nova 分析助手/); }); await go('workbench'); await check('工作台原有状态筛选、指标缺失与通道充值入口', async () => { assert.deepEqual(await phone.locator('.segmented button').allTextContents(), [ '全部', '在线', '离线', ]); assert.equal(await phone.locator('.life-card').count(), 5); await click('work-filter', '[data-value="offline"]'); assert.equal(await phone.locator('.life-card').count(), 1); assert.match(await phone.locator('.life-card').textContent(), /指标未上报/); await click('enter-agent'); assert.match(await phone.locator('.toast').textContent(), /当前离线/); await click('channel'); assert.match(await phone.locator('.sheet').textContent(), /不修改通道配置/); await click('close'); await click('recharge'); assert.match(await phone.locator('.sheet').textContent(), /不会产生支付/); await click('close'); await click('work-filter', '[data-value="all"]'); }); await shot('preview-workbench.png'); await go('mine'); await check('保留四套配色、深色、字号和脱敏 Token', async () => { assert.equal(await phone.locator('.palette-grid button').count(), 4); assert.equal(await phone.locator('[data-action="copy-token"]').isDisabled(), true); await click('token'); assert.match(await phone.locator('.settings-card').first().textContent(), /NOT-A-REAL/); await click('palette', '[data-id="ice"]'); assert.equal(await phone.getAttribute('data-palette'), 'ice'); await click('dark'); assert.match(await phone.getAttribute('class'), /dark/); await phone.locator('[data-field="font"]').fill('17'); assert.equal( await phone.evaluate((element) => element.style.getPropertyValue('--chat-size')), '17px', ); await page.reload(); assert.match(await phone.getAttribute('class'), /dark/); await click('dark'); await click('palette', '[data-id="platinum"]'); await phone.locator('[data-field="font"]').fill('14'); }); await go('messages'); await check('侧栏焦点约束、Escape 关闭与原型提醒', async () => { await click('drawer'); assert.equal(await phone.locator('.app-shell').getAttribute('inert'), ''); await shot('preview-drawer.png'); const focusable = phone.locator('.drawer button'); await focusable.last().focus(); await page.keyboard.press('Tab'); assert.equal( await focusable.first().evaluate((element) => element === document.activeElement), true, ); await page.keyboard.press('Escape'); assert.equal(await phone.locator('.overlay').count(), 0); await click('chat', '[data-id="c1"]'); await click('activity'); await click('demo-complete'); assert.match(await phone.locator('.toast').textContent(), /演示/); }); await check('新建入口保留品牌与起始输入', async () => { await go('home'); assert.equal(await phone.locator('.home-logo').textContent(), 'fmode'); await phone.locator('[data-field="draft"]').fill('帮我做一份报告'); await click('send'); await click('create-chat', '[data-id="nova"]'); assert.equal(await phone.locator('[data-field="draft"]').inputValue(), '帮我做一份报告'); }); await check('响应式布局、横向溢出与固定输入区', async () => { let screens = 0; for (const [width, height] of [ [320, 640], [375, 667], [390, 844], [430, 932], [768, 1024], [1440, 1130], ]) { await page.setViewportSize({ width, height }); for (const route of [ 'messages', 'chat/c1', 'contacts', 'workbench', 'mine', 'home', 'agent/nova', ]) { await go(route); assert.equal( await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false, `document ${width} ${route}`, ); const overflow = await phone.evaluate( (element) => element.scrollWidth > element.clientWidth, ); assert.equal(overflow, false, `phone ${width} ${route}`); if (route === 'chat/c1') { const geometry = await phone.evaluate((element) => ({ phone: element.getBoundingClientRect().bottom, send: element.querySelector('.send-button').getBoundingClientRect().bottom, scroll: element.querySelector('.page-content').clientHeight, })); assert.ok(geometry.send <= geometry.phone, `send ${width}`); assert.ok(geometry.scroll > 150, `chat height ${width}`); } screens++; } } console.log(` ${screens} viewport/route combinations checked`); }); await page.evaluate(() => localStorage.clear()); await page.reload(); await go('messages'); await shot('preview-comparison.png'); await go('chat/c1'); await shot('preview-chat-comparison.png'); await page.setViewportSize({ width: 390, height: 844 }); await go('messages'); await shot('preview-messages.png'); await go('chat/c1'); await shot('preview-chat.png'); await go('workbench'); await shot('preview-workbench.png'); await go('contacts'); await shot('preview-contacts.png'); await go('mine'); await shot('preview-mine.png'); await go('home'); await shot('preview-home.png'); await go('chat/c1'); await click('activity'); await shot('preview-switcher.png'); await click('close'); await click('back'); await click('drawer'); await shot('preview-drawer.png'); await click('close'); await check('无脚本错误、无外部网络请求', async () => { assert.deepEqual(errors, []); assert.deepEqual(requests, []); }); console.log(`PASS: ${checks} interaction groups; standalone offline prototype verified.`); } finally { await browser.close(); }