const { chromium } = require('playwright');
|
const fs = require('node:fs');
|
const path = require('node:path');
|
const assert = require('node:assert/strict');
|
|
(async () => {
|
const root = path.resolve('mom-pro2-before/dist');
|
const script = fs.readFileSync(path.join(root, 'startup-check.js'), 'utf8');
|
const diagnostic = fs.readFileSync(path.join(root, 'browser-check.html'), 'utf8');
|
assert.ok(fs.readFileSync(path.join(root, 'index.html'), 'utf8').includes('data-startup-check'));
|
const browser = await chromium.launch({ headless: true, channel: 'msedge' });
|
try {
|
const page = await browser.newPage({ viewport: { width: 480, height: 800 } });
|
page.on('pageerror', error => console.error('Browser error:', error.message));
|
await page.route('http://startup.test/**', route => {
|
const pathname = new URL(route.request().url()).pathname;
|
if (pathname.endsWith('startup-check.js')) return route.fulfill({ contentType: 'application/javascript', body: script });
|
if (pathname.endsWith('browser-check.html')) return route.fulfill({ contentType: 'text/html', body: diagnostic });
|
return route.fulfill({ contentType: 'text/html', body: '<div id="__app-loading__">启动中</div><script data-startup-check src="/nested/startup-check.js"></script>' });
|
});
|
await page.clock.install();
|
await page.goto('http://startup.test/nested/');
|
await page.evaluate(() => window.dispatchEvent(new ErrorEvent('error', { message: "Unexpected token '?'" })));
|
await page.clock.fastForward(31000);
|
assert.match(await page.locator('#mom-startup-error').innerText(), /Unexpected token/);
|
assert.equal(await page.locator('#mom-startup-error a').getAttribute('href'), 'http://startup.test/nested/browser-check.html');
|
await page.evaluate(() => window.__MOM_STARTUP__.finish());
|
assert.equal(await page.locator('#mom-startup-error').count(), 0);
|
await page.goto('http://startup.test/nested/');
|
await page.evaluate(() => window.__MOM_STARTUP__.finish());
|
await page.clock.fastForward(31000);
|
assert.equal(await page.locator('#mom-startup-error').count(), 0);
|
await page.goto('http://startup.test/nested/');
|
await page.evaluate(() => window.__MOM_STARTUP__.fail(new Error('<script>bad</script>')));
|
assert.match(await page.locator('#mom-startup-error').innerText(), /<script>bad<\/script>/);
|
assert.equal(await page.locator('#mom-startup-error script').count(), 0);
|
await page.locator('#mom-startup-error a').click();
|
assert.equal(await page.locator('#checks tr').count(), 10);
|
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth));
|
console.log('PASS: diagnostic page, startup failure/timeout, success cleanup, safe text, nested base URL');
|
} finally { await browser.close(); }
|
})().catch(error => { console.error(error); process.exitCode = 1; });
|