verify.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import assert from 'node:assert/strict';
  2. import { chromium } from 'playwright';
  3. import { fileURLToPath, pathToFileURL } from 'node:url';
  4. import path from 'node:path';
  5. const root = path.dirname(fileURLToPath(import.meta.url));
  6. const base = pathToFileURL(path.join(root, 'index.html')).href;
  7. const browser = await chromium.launch({
  8. channel: process.env.PLAYWRIGHT_CHANNEL || 'chrome',
  9. headless: true,
  10. });
  11. const context = await browser.newContext({
  12. viewport: { width: 390, height: 844 },
  13. reducedMotion: 'reduce',
  14. deviceScaleFactor: 1,
  15. });
  16. const page = await context.newPage();
  17. page.setDefaultTimeout(10000);
  18. const errors = [];
  19. const externalRequests = [];
  20. page.on('pageerror', (error) => errors.push(error.message));
  21. page.on('request', (request) => {
  22. if (/^https?:/.test(request.url())) externalRequests.push(request.url());
  23. });
  24. let checks = 0;
  25. async function check(label, operation) {
  26. await operation();
  27. checks += 1;
  28. console.log(`PASS ${checks}: ${label}`);
  29. }
  30. async function go(route) {
  31. await page.goto(`${base}#/${route}`);
  32. await page.evaluate(() => document.fonts.ready);
  33. }
  34. async function click(action, extra = '') {
  35. const selector = `button[data-action="${action}"]${extra}`;
  36. const overlayButton = page.locator('#overlay-root').locator(selector);
  37. await (await overlayButton.count() ? overlayButton : page.locator(selector)).first().click();
  38. }
  39. async function screenshot(name) {
  40. await page.screenshot({ path: path.join(root, name), style: '.toast { visibility: hidden !important; }' });
  41. }
  42. try {
  43. await go('chat/c1');
  44. await check('会话页隐藏主导航,保留正文与操作入口', async () => {
  45. assert.equal(await page.locator('#tabbar button').count(), 0);
  46. assert.equal(await page.locator('.activity-entry').count(), 1);
  47. assert.equal(await page.locator('.assistant-body').count(), 1);
  48. assert.equal(await page.locator('.process[open]').count(), 0);
  49. });
  50. await screenshot('preview-chat.png');
  51. await check('回车只换行,中文组合输入不提交', async () => {
  52. await page.locator('#draft').fill('第一行');
  53. await page.locator('#draft').press('End');
  54. await page.locator('#draft').press('Enter');
  55. await page.locator('#draft').pressSequentially('第二行');
  56. assert.equal(await page.locator('#draft').inputValue(), '第一行\n第二行');
  57. assert.equal(await page.locator('.user-message').count(), 1);
  58. await page
  59. .locator('#draft')
  60. .dispatchEvent('keydown', { key: 'Enter', isComposing: true, keyCode: 229 });
  61. assert.equal(await page.locator('.user-message').count(), 1);
  62. });
  63. await check('跨会话切换与同分身多会话草稿隔离', async () => {
  64. await click('activity');
  65. assert.equal(await page.locator('.activity-card').count(), 4);
  66. await screenshot('preview-switcher.png');
  67. await click('activity-open', '[data-id="c3"]');
  68. await page.waitForURL('**#/chat/c3');
  69. assert.equal(await page.locator('#draft').inputValue(), '');
  70. await page.locator('#draft').fill('这是另一条会话的草稿');
  71. await click('activity');
  72. await click('activity-open', '[data-id="c1"]');
  73. await page.waitForURL('**#/chat/c1');
  74. assert.equal(await page.locator('#draft').inputValue(), '第一行\n第二行');
  75. });
  76. await check('协作筛选与搜索不混淆在线状态', async () => {
  77. await click('activity');
  78. await click('activity-filter', '[data-value="attention"]');
  79. assert.equal(await page.locator('.activity-card').count(), 1);
  80. assert.match(await page.locator('.activity-card').innerText(), /连接中断/);
  81. await page.getByRole('searchbox', { name: '搜索正在协作的事' }).fill('不存在的内容');
  82. assert.equal(await page.locator('.activity-card').count(), 0);
  83. await page.getByRole('searchbox', { name: '搜索正在协作的事' }).fill('');
  84. await click('activity-filter', '[data-value="all"]');
  85. await click('close');
  86. });
  87. await check('标题候选不会在取消时写回,手动保存同步到列表', async () => {
  88. await click('edit-current-title');
  89. await click('regenerate-title');
  90. assert.equal(await page.locator('#form-value').inputValue(), '移动端第一印象与体验优化');
  91. await click('close');
  92. assert.equal(await page.locator('.header-title strong').innerText(), '把产品的第一印象做好');
  93. await click('edit-current-title');
  94. await page.locator('#form-value').fill('我的产品体验优化');
  95. await screenshot('preview-title.png');
  96. await page.locator('.sheet-form button[type="submit"]').click();
  97. assert.equal(await page.locator('.header-title strong').innerText(), '我的产品体验优化');
  98. });
  99. await check('当前数字生命内新建,保留原会话和工作区', async () => {
  100. await click('quick-current');
  101. await page.waitForFunction(() => location.hash !== '#/chat/c1');
  102. assert.match(await page.locator('.header-center small').innerText(), /小禾/);
  103. assert.match(await page.locator('.blank-chat').innerText(), /产品与灵感/);
  104. assert.equal(await page.locator('#draft').inputValue(), '');
  105. await page.locator('#draft').fill('新会话独立草稿');
  106. await click('drawer');
  107. await click('open-chat', '[data-id="c1"]');
  108. await page.waitForURL('**#/chat/c1');
  109. assert.equal(await page.locator('#draft').inputValue(), '第一行\n第二行');
  110. });
  111. await check('附件仅本地暂存,发送和回复明确标记演示', async () => {
  112. await page
  113. .locator('#file-input')
  114. .setInputFiles({ name: '体验调研.txt', mimeType: 'text/plain', buffer: Buffer.from('demo') });
  115. assert.match(await page.locator('.attachment-list').innerText(), /体验调研/);
  116. await page.locator('#draft').fill('<img src=x onerror="alert(1)">');
  117. await click('send');
  118. assert.equal(await page.locator('.send-button').isDisabled(), true);
  119. await page.waitForFunction(
  120. () =>
  121. !document.querySelector('.send-button').disabled ||
  122. document.querySelector('.send-button').getAttribute('aria-label') === '发送消息',
  123. );
  124. assert.equal(await page.locator('.user-bubble img').count(), 0);
  125. assert.match(await page.locator('.assistant-body').last().innerText(), /本地模拟回复/);
  126. assert.equal(await page.locator('#draft').inputValue(), '');
  127. assert.equal(await page.locator('.attachment-chip').count(), 0);
  128. });
  129. await check('侧栏按数字生命分组,不引入全局任务入口', async () => {
  130. await click('drawer');
  131. await click('drawer-view', '[data-value="agents"]');
  132. assert.equal(await page.locator('.drawer-agent-group').count(), 3);
  133. assert.equal(await page.locator('.drawer-nav button').count(), 4);
  134. await screenshot('preview-drawer.png');
  135. await page.keyboard.press('Escape');
  136. assert.equal(await page.locator('[role="dialog"]').count(), 0);
  137. });
  138. await check('弹层支持焦点约束、Escape 与下拉关闭', async () => {
  139. await click('activity');
  140. const buttons = page.locator('#overlay-root button');
  141. await buttons.last().focus();
  142. await page.keyboard.press('Tab');
  143. assert.equal(await buttons.first().evaluate((element) => element === document.activeElement), true);
  144. const handle = await page.locator('.sheet-handle').boundingBox();
  145. await page.mouse.move(handle.x + handle.width / 2, handle.y + 12);
  146. await page.mouse.down();
  147. await page.mouse.move(handle.x + handle.width / 2, handle.y + 120, { steps: 6 });
  148. await page.mouse.up();
  149. assert.equal(await page.locator('[role="dialog"]').count(), 0);
  150. assert.equal(await page.locator('#app').evaluate((element) => element.inert), false);
  151. });
  152. await check('文件/成果面板与真实格式区分,预览可返回', async () => {
  153. await click('details');
  154. await click('detail-tab', '[data-value="file"]');
  155. await click('preview-file');
  156. assert.equal(await page.locator('.preview-document').count(), 1);
  157. assert.match(await page.locator('[data-action="download-demo"]').innerText(), /TXT/);
  158. await click('details', '[data-tab="file"]');
  159. await click('close');
  160. });
  161. await check('完成提醒可直接返回对应会话', async () => {
  162. await click('activity');
  163. await click('complete-demo');
  164. assert.equal(await page.locator('#completion-banner').count(), 1);
  165. await screenshot('preview-completion.png');
  166. await click('completion-open');
  167. await page.waitForURL('**#/chat/c4');
  168. assert.match(await page.locator('.header-center small').innerText(), /已完成/);
  169. });
  170. await go('messages');
  171. await check('工作区默认折叠、名称去重、删除不删会话', async () => {
  172. await click('message-filter', '[data-value="workspace"]');
  173. assert.equal(await page.locator('.workspace-toggle[aria-expanded="false"]').count(), 2);
  174. await click('create-workspace');
  175. await page.locator('#form-value').fill('产品与灵感');
  176. await page.locator('.sheet-form button[type="submit"]').click();
  177. assert.match(await page.locator('.form-error').innerText(), /同名/);
  178. await page.locator('#form-value').fill('临时验证区');
  179. await page.locator('.sheet-form button[type="submit"]').click();
  180. assert.equal(await page.locator('.workspace').count(), 3);
  181. await click('workspace-menu', '[data-id="w1"]');
  182. await click('delete-workspace');
  183. await click('confirm-delete-workspace');
  184. assert.equal(await page.locator('.workspace').count(), 2);
  185. await click('message-filter', '[data-value="all"]');
  186. assert.match(await page.locator('#conversation-results').innerText(), /我的产品体验优化/);
  187. });
  188. await check('搜索保留输入焦点,主导航为四栏', async () => {
  189. const search = page.getByRole('searchbox', { name: '搜索会话或数字生命' });
  190. await search.fill('不存在');
  191. assert.match(await page.locator('#conversation-results').innerText(), /没有找到/);
  192. assert.equal(await search.evaluate((element) => element === document.activeElement), true);
  193. assert.equal(await page.locator('#tabbar button').count(), 4);
  194. });
  195. await go('mine');
  196. await check('深色、配色和字号只持久化原型偏好', async () => {
  197. await click('theme');
  198. assert.equal(await page.locator('#device.dark').count(), 1);
  199. await click('palette', '[data-value="blue"]');
  200. await click('font');
  201. await click('choose-font', '[data-value="18"]');
  202. await click('close');
  203. assert.match(await page.locator('[data-action="font"]').innerText(), /大号/);
  204. await page.reload();
  205. assert.equal(await page.locator('#device.dark').count(), 1);
  206. const stored = await page.evaluate(() =>
  207. JSON.parse(localStorage.getItem('fmode_mobile_proposal_v1')),
  208. );
  209. assert.equal(stored.palette, 'blue');
  210. assert.equal(stored.font, 18);
  211. });
  212. await check('骨架、空态、错误和重试可以完整体验', async () => {
  213. await click('preview-states');
  214. await click('list-state', '[data-value="loading"]');
  215. assert.equal(await page.locator('.skeleton-row').count(), 5);
  216. await page.waitForSelector('.conversation-row');
  217. await page.goto(`${base}#/mine`);
  218. await click('preview-states');
  219. await click('list-state', '[data-value="error"]');
  220. await click('retry-list');
  221. assert.ok((await page.locator('.conversation-row').count()) > 0);
  222. });
  223. await page.evaluate(() => localStorage.removeItem('fmode_mobile_proposal_v1'));
  224. await page.reload();
  225. await check('320 到 1440 宽度无横向溢出,输入框始终在视窗内', async () => {
  226. for (const [width, height] of [
  227. [320, 568],
  228. [360, 740],
  229. [390, 844],
  230. [430, 932],
  231. [768, 1024],
  232. [1440, 1000],
  233. ]) {
  234. await page.setViewportSize({ width, height });
  235. for (const route of ['home', 'chat/c1', 'messages', 'contacts', 'workbench', 'mine']) {
  236. await go(route);
  237. const dimensions = await page.evaluate(() => ({
  238. viewport: innerWidth,
  239. width: document.documentElement.scrollWidth,
  240. composer: document.querySelector('#composer-area').getBoundingClientRect().bottom,
  241. height: innerHeight,
  242. }));
  243. assert.ok(
  244. dimensions.width <= dimensions.viewport + 1,
  245. `${width}px ${route}: horizontal overflow`,
  246. );
  247. assert.ok(
  248. dimensions.composer <= dimensions.height + 1,
  249. `${width}px ${route}: composer outside viewport`,
  250. );
  251. }
  252. }
  253. });
  254. await page.setViewportSize({ width: 1440, height: 1000 });
  255. await go('chat/c1');
  256. await screenshot('preview-desktop.png');
  257. await page.setViewportSize({ width: 390, height: 844 });
  258. await go('home');
  259. await screenshot('preview-home.png');
  260. await go('workbench');
  261. await screenshot('preview-workbench.png');
  262. assert.deepEqual(errors, [], 'Browser runtime errors');
  263. assert.deepEqual(externalRequests, [], 'Prototype must not make external requests');
  264. console.log(
  265. `PASS ${checks} groups; 36 responsive routes; no runtime errors or external requests.`,
  266. );
  267. } finally {
  268. await browser.close();
  269. }