entry-isolation.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import assert from 'node:assert/strict';
  2. import { mkdir, readFile, writeFile } from 'node:fs/promises';
  3. import { chromium } from 'playwright';
  4. const accounts = JSON.parse(await readFile(new URL('../../.work/frontend-accounts.json', import.meta.url), 'utf8'));
  5. const base = process.env.FRONTEND_URL || 'http://127.0.0.1:18763';
  6. const browser = await chromium.launch({ headless: true, executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE });
  7. const page = await browser.newPage();
  8. const errors = [];
  9. page.on('pageerror', error => errors.push(error.message));
  10. async function route(path) {
  11. await page.evaluate(value => { location.hash = `/${value}`; }, path);
  12. await page.waitForURL(new RegExp(`#/${path}$`));
  13. await page.waitForFunction(() => !document.querySelector('.mobile .content[aria-busy="true"], .merchant-loading'));
  14. }
  15. try {
  16. await page.goto(`${base}/#/customer/login`);
  17. await page.getByLabel('账户', { exact: true }).fill('customer01');
  18. await page.getByLabel('密码', { exact: true }).fill(accounts.password);
  19. await page.getByRole('checkbox').check();
  20. await page.getByRole('button', { name: '登录并继续' }).click();
  21. await page.waitForURL('**/#/customer/chat');
  22. await route('customer/orders');
  23. await page.locator('.order-card').filter({ hasText: accounts.order_id }).getByRole('button', { name: '申请售后', exact: true }).click();
  24. await page.waitForURL('**/#/customer/apply');
  25. await page.getByLabel('故障描述').fill('保留给消费者本人检查的未提交内容');
  26. // 同一个页面切换入口,商家必须通过真实接口自行选择业务上下文。
  27. await page.getByRole('button', { name: '商家后台', exact: true }).click();
  28. const panel = page.locator('.login-panel');
  29. await panel.getByLabel('商家标识').fill(accounts.tenant_id);
  30. await panel.getByLabel('员工账户').fill('staff01');
  31. await panel.getByLabel('密码', { exact: true }).fill(accounts.password);
  32. await panel.getByRole('button', { name: '登录', exact: true }).click();
  33. await page.waitForURL('**/#/merchant/service/queue');
  34. await route('merchant/service/conversation');
  35. await page.getByText('请选择需要处理的会话。', { exact: true }).waitFor();
  36. assert.equal(await page.getByRole('heading', { name: '当前客户订单', exact: true }).count(), 0);
  37. await page.locator('.admin-session').getByRole('button', { name: '退出登录', exact: true }).click();
  38. await page.getByRole('dialog').getByRole('button', { name: '确认操作', exact: true }).click();
  39. await panel.waitFor();
  40. await route('customer/apply');
  41. assert.equal(await page.getByLabel('故障描述').inputValue(), '保留给消费者本人检查的未提交内容');
  42. assert.equal(await page.getByLabel('申请数量', { exact: true }).inputValue(), '1');
  43. assert.deepEqual(errors, []);
  44. await mkdir(new URL('../../.work/frontend-verification/', import.meta.url), { recursive: true });
  45. await writeFile(new URL('../../.work/frontend-verification/entry-isolation.json', import.meta.url), JSON.stringify({ date: new Date().toISOString(), checks: ['消费者与客服业务上下文独立', '商家退出后消费者身份和未提交内容保持'], browserErrors: errors, generatedModelCalls: 0 }, null, 2));
  46. console.log('同页面入口切换及身份数据隔离检查通过。');
  47. } finally {
  48. await browser.close();
  49. }