File size: 9,055 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
const fs = require('node:fs');
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});
const entryUrl = pathToFileURL(path.join(process.cwd(), 'index.html')).href;
const seed = Number.parseInt(process.env.ENHANCED_SEED || '28031', 10);
function assert(condition, message) {
if (!condition) throw new Error(message);
}
async function runCase(browser, name, viewport, validate) {
const context = await browser.newContext({ viewport });
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);
await page.screenshot({path: `/logs/verifier/visual-audit/${name}.png`, fullPage: true});
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 waitForCollapsedSidebar(page, selector, initialVisibleWidth) {
await page.evaluate(({ selector, initialVisibleWidth }) => new Promise((resolve, reject) => {
const deadline = performance.now() + 4500;
let lastWidth = initialVisibleWidth;
let stableFrames = 0;
let reduced = false;
const sample = () => {
const element = document.querySelector(selector);
if (!element) {
reject(new Error('fixture sidebar must exist'));
return;
}
const rect = element.getBoundingClientRect();
const style = getComputedStyle(element);
const visibleWidth = Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
reduced = visibleWidth < 40 || style.display === 'none' || style.visibility === 'hidden';
stableFrames = reduced && Math.abs(visibleWidth - lastWidth) < 0.01 ? stableFrames + 1 : 0;
lastWidth = visibleWidth;
if (reduced && stableFrames >= 4) resolve();
else if (performance.now() >= deadline) reject(new Error('sidebar did not reach a stable collapsed state'));
else requestAnimationFrame(sample);
};
sample();
}), { selector, initialVisibleWidth });
}
(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: the sidebar and content remain in a left-to-right layout.
await runCase(browser, 'ENHANCED_CASE_1', { width: 1200, height: 800 }, async (page) => {
const boxes = await page.evaluate(() => {
const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
const main = document.querySelector('[data-testid="main-content"]')?.getBoundingClientRect();
return sidebar && main ? {
sidebar: { left: sidebar.left, right: sidebar.right, top: sidebar.top, bottom: sidebar.bottom },
main: { left: main.left, top: main.top, bottom: main.bottom },
} : null;
});
assert(boxes, 'fixture sidebar and main content must exist');
assert(boxes.sidebar.left <= 1, 'sidebar must stay against the left edge');
assert(boxes.main.left >= boxes.sidebar.right - 1, 'main content must be to the right of the sidebar');
assert(Math.min(boxes.sidebar.bottom, boxes.main.bottom) > Math.max(boxes.sidebar.top, boxes.main.top),
'sidebar and main content must vertically overlap instead of stacking');
});
// ENHANCED_CASE_2: activating the provided toggle visibly collapses the sidebar.
await runCase(browser, 'ENHANCED_CASE_2', { width: 900, height: 700 }, async (page) => {
const sidebar = page.locator('[data-testid="sidebar"]');
const before = await sidebar.boundingBox();
assert(before && before.width > 1, 'sidebar must initially be visible');
await page.locator('[data-testid="sidebar-toggle"]').click();
await waitForCollapsedSidebar(page, '[data-testid="sidebar"]', before.width);
const collapsed = await sidebar.evaluate((element, initialWidth) => {
const rect = element.getBoundingClientRect();
const style = getComputedStyle(element);
const visibleWidth = Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
return visibleWidth < 40 || style.display === 'none' || style.visibility === 'hidden';
}, before.width);
assert(collapsed, 'sidebar must collapse after its toggle is clicked');
});
// ENHANCED_CASE_3: responsive extremes preserve narrow usability and wide centering.
await runCase(browser, 'ENHANCED_CASE_3', { width: 420, height: 700 }, async (page) => {
const narrow = await page.evaluate(() => {
const mainElement = document.querySelector('[data-testid="main-content"]');
const main = mainElement?.getBoundingClientRect();
const minWidth = mainElement ? Number.parseFloat(getComputedStyle(mainElement).minWidth) : NaN;
return main ? {
mainLeft: main.left,
mainRight: main.right,
mainWidth: main.width,
minWidth,
scrollWidth: document.documentElement.scrollWidth,
clientWidth: document.documentElement.clientWidth,
} : null;
});
assert(narrow, 'main content must exist at a narrow viewport');
assert(Number.isFinite(narrow.minWidth) && narrow.minWidth > 0,
'main content must define a fixed positive minimum width');
assert(narrow.mainWidth + 1 >= narrow.minWidth,
'rendered main content must honor its fixed minimum width');
assert(narrow.mainLeft >= -1 && narrow.mainRight <= narrow.clientWidth + 1,
'main content must remain fully visible, not clipped to hide overflow');
assert(narrow.scrollWidth <= narrow.clientWidth + 1, 'narrow viewport must not expose horizontal page scrolling');
await page.screenshot({path: '/logs/verifier/visual-audit/narrow-content.png', fullPage: true});
const sidebar = page.locator('[data-testid="sidebar"]');
const initiallyOpen = await sidebar.evaluate(element => {
const rect = element.getBoundingClientRect(), style = getComputedStyle(element);
return style.display !== 'none' && style.visibility !== 'hidden' && rect.right > 40 && rect.left < innerWidth;
});
if (!initiallyOpen) {
await page.locator('[data-testid="sidebar-toggle"]').click();
await page.waitForFunction(() => {
const el = document.querySelector('[data-testid="sidebar"]');
const rect = el.getBoundingClientRect(), style = getComputedStyle(el);
return style.display !== 'none' && style.visibility !== 'hidden' && rect.right > 40;
}, null, {timeout: 3000});
}
await page.screenshot({path: '/logs/verifier/visual-audit/narrow-open.png', fullPage: true});
await page.locator('[data-testid="sidebar-toggle"]').click();
await waitForCollapsedSidebar(page, '[data-testid="sidebar"]', 240);
await page.screenshot({path: '/logs/verifier/visual-audit/narrow-closed.png', fullPage: true});
await page.setViewportSize({ width: 1800, height: 900 });
await page.goto(entryUrl, { waitUntil: 'domcontentloaded' });
const wide = await page.evaluate(() => {
const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
const main = document.querySelector('[data-testid="main-content"]')?.getBoundingClientRect();
return sidebar && main ? {
sidebarLeft: sidebar.left,
sidebarRight: sidebar.right,
mainCenter: main.left + main.width / 2,
viewportWidth: document.documentElement.clientWidth,
} : null;
});
assert(wide, 'sidebar and main content must exist at a wide viewport');
assert(wide.sidebarLeft <= 1, 'wide viewport sidebar must remain left aligned');
const availableCenter = (wide.sidebarRight + wide.viewportWidth) / 2;
assert(Math.abs(wide.mainCenter - availableCenter) <= 2, 'main content must be centered in the area beside the sidebar');
});
} finally {
await browser.close();
}
})().catch((error) => {
console.error(error && error.stack || error);
process.exit(1);
});
|