ui-review-isolation.mjs 1.4 KB

1234567891011121314151617181920212223242526272829
  1. /** 本地 UI 取证隔离与几何检查,供审查脚本和故障夹具共用。 */
  2. export function isUiAssetRequest(request, origin) {
  3. const url = new URL(request.url());
  4. return url.origin === origin && !/^\/(?:api(?:-platform)?|parse)(?:\/|$)/i.test(url.pathname);
  5. }
  6. export async function assertExpectedUiPage(page, { origin, pathname, selector, timeout = 10000 }) {
  7. const actual = new URL(page.url());
  8. if (actual.origin !== origin || decodeURIComponent(actual.pathname) !== pathname) {
  9. throw new Error('原型路由发生重定向:' + actual.pathname + ',预期 ' + pathname);
  10. }
  11. await page.locator(selector).first().waitFor({ state: 'attached', timeout });
  12. const angular = await page.locator(selector).first().evaluate(el =>
  13. !!document.querySelector('app-root') && !!globalThis.ng?.getComponent(el));
  14. if (!angular) throw new Error('不是当前 Angular Mock 页面,停止操作');
  15. }
  16. export async function uiLayoutMetrics(page) {
  17. const expectedWidth = page.viewportSize().width;
  18. return page.evaluate(expected => {
  19. const root = document.documentElement;
  20. const width = root.clientWidth;
  21. return {
  22. viewport: expected, width, scrollWidth: root.scrollWidth,
  23. overflow: root.scrollWidth > width + 1 || innerWidth > width + 1 || Math.abs(width - expected) > 1,
  24. content: document.body.innerText.trim().length, path: location.pathname,
  25. };
  26. }, expectedWidth);
  27. }