File size: 3,697 Bytes
ef987bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
const entryUrl = pathToFileURL(path.join(process.cwd(), 'index.html')).href;
const seed = Number.parseInt(process.env.ENHANCED_SEED || '32031', 10);
function assert(condition, message) {
if (!condition) throw new Error(message);
}
async function runCase(browser, name, validate) {
const context = await browser.newContext({ viewport: { width: 1000, height: 700 } });
const networkAttempts = [];
await context.route('**/*', async (route) => {
const url = route.request().url();
if (/^https?:/i.test(url)) {
networkAttempts.push(url);
await route.abort('blockedbyclient');
return;
}
await route.continue();
});
await context.addInitScript(({ fixedSeed }) => {
let state = fixedSeed >>> 0;
Math.random = () => {
state = (state * 1664525 + 1013904223) >>> 0;
return state / 0x100000000;
};
}, { fixedSeed: seed });
const page = await context.newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on('pageerror', (error) => errors.push(error.message));
try {
await page.goto(entryUrl, { waitUntil: 'domcontentloaded' });
await validate(page);
assert(networkAttempts.length === 0, `${name}: external network request attempted`);
assert(errors.length === 0, `${name}: browser errors: ${errors.join(' | ')}`);
console.log(`${name} passed`);
} finally {
await context.close();
}
}
async function assertNamesAppearOnce(page, names) {
for (const name of names) {
const count = await page.locator(`[data-component-name="${name}"]`).count();
assert(count === 1, `${name} must occur exactly once as a data-component-name value; got ${count}`);
}
}
(async () => {
assert(Number.isInteger(seed), 'ENHANCED_SEED must be a fixed integer');
const browser = await chromium.launch({
headless: true,
args: ['--no-sandbox', '--disable-background-networking', '--disable-component-update', '--no-first-run'],
});
try {
// ENHANCED_CASE_1: all required layout component names occur exactly once.
await runCase(browser, 'ENHANCED_CASE_1', async (page) => {
await assertNamesAppearOnce(page, ['AppShell', 'NavigationSidebar', 'MainContentArea']);
});
// ENHANCED_CASE_2: all required prompt component names occur exactly once.
await runCase(browser, 'ENHANCED_CASE_2', async (page) => {
await assertNamesAppearOnce(page, ['PromptResponseBlock', 'PromptInput', 'AIContentArea']);
});
// ENHANCED_CASE_3: the prescribed component names are exposed as registry keys.
await runCase(browser, 'ENHANCED_CASE_3', async (page) => {
const required = ['AppShell', 'NavigationSidebar', 'MainContentArea', 'PromptResponseBlock', 'PromptInput', 'AIContentArea'];
const registryState = await page.evaluate((names) => {
const registry = window.componentRegistry;
return {
isObject: registry !== null && typeof registry === 'object' && !Array.isArray(registry),
ownKeys: registry && typeof registry === 'object'
? names.filter((name) => Object.prototype.hasOwnProperty.call(registry, name))
: [],
};
}, required);
assert(registryState.isObject, 'window.componentRegistry must be an object');
assert(required.every(name => registryState.ownKeys.includes(name)),
`window.componentRegistry must expose all six prescribed names; found ${registryState.ownKeys.join(', ')}`);
});
} finally {
await browser.close();
}
})().catch((error) => {
console.error(error && error.stack || error);
process.exit(1);
});
|