Webdev-Bench / tasks /c12 /repo /scripts /validate.mjs
Roy-DOU's picture
Add files using upload-large-folder tool
41235a1 verified
Raw History Blame Contribute Delete
3.9 kB
import { chromium } from 'playwright-core'
import { writeFileSync } from 'node:fs'
const BASE_URL = process.env.APP_URL ?? 'http://localhost:4173/'
const results = {
timestamp: new Date().toISOString(),
baseUrl: BASE_URL,
checks: [],
}
const check = (name, passed, details) => {
results.checks.push({ name, passed, details })
console.log(`${passed ? 'PASS' : 'FAIL'} ${name} — ${details}`)
}
const browser = await chromium.launch({
args: ['--no-sandbox', '--disable-dev-shm-usage', '--force-device-scale-factor=1'],
})
const page = await browser.newPage({ viewport: { width: 1920, height: 1080 } })
const errors = []
page.on('pageerror', (err) => errors.push(String(err)))
page.on('console', (msg) => msg.type() === 'error' && errors.push(msg.text()))
await page.goto(BASE_URL, { waitUntil: 'networkidle', timeout: 60000 })
const root = await page.$eval('#root', (el) => el.children.length)
check('react-mounted', root > 0, `#root has ${root} child node(s)`)
const sections = await page.$$eval('.project', (els) => els.length)
check('project-sections', sections === 9, `${sections} project sections (expected 9)`)
const cells = await page.$$eval('.cell', (els) => els.length)
check('grid-cells', cells === 72, `${cells} grid cells (expected 72)`)
const hatch = await page.$$eval('.cell .hatch', (els) => els.length)
check('hatch-cells', hatch === 33, `${hatch} hatched placeholder cells (expected 33)`)
const imgs = await page.$$eval('.cell img', (els) => ({
total: els.length,
loaded: els.filter((img) => img.complete && img.naturalWidth > 0).length,
}))
check('images-loaded', imgs.total === 39 && imgs.loaded === 39, `${imgs.loaded}/${imgs.total} images loaded (expected 39/39)`)
const navLinks = await page.$$eval('.site-nav .nav-link', (els) => els.map((a) => a.textContent.trim()))
check('nav-links', navLinks.length === 5, `nav: ${navLinks.join(' | ')}`)
const height = await page.evaluate(() => document.documentElement.scrollHeight)
const target = 12474
const heightOk = Math.abs(height - target) / target < 0.05
check('page-height', heightOk, `rendered ${height}px vs reference ~${target}px (${((height / target - 1) * 100).toFixed(1)}% diff)`)
// interaction: grid density toggle switches first section to 2 columns
await page.click('#polestar .view-btn:first-child')
const tracks = await page.$eval('#polestar .grid', (el) => getComputedStyle(el).gridTemplateColumns.split(' ').length)
check('view-toggle-interaction', tracks === 2, `after clicking 2-col toggle, grid has ${tracks} columns`)
await page.click('#polestar .view-btn:last-child')
// interaction: card hover feedback (opacity transition defined)
const hoverFx = await page.$eval('.cell', (el) => getComputedStyle(el).transition)
check('card-feedback', /opacity|transform/.test(hoverFx), `card transition: ${hoverFx}`)
// responsive snapshots
for (const [w, h, name] of [[900, 900, 'tablet-900'], [420, 900, 'mobile-420']]) {
await page.setViewportSize({ width: w, height: h })
await page.waitForTimeout(250)
await page.screenshot({ path: `screenshots/${name}.png`, fullPage: false })
const cols = await page.$eval('#polestar .grid', (el) => getComputedStyle(el).gridTemplateColumns.split(' ').length)
check(`responsive-${w}`, w <= 480 ? cols === 1 : cols === 2, `${w}px viewport renders ${cols} column(s)`)
}
check('no-console-errors', errors.length === 0, errors.length ? errors.join('; ').slice(0, 300) : 'no page/console errors')
await browser.close()
results.passed = results.checks.filter((c) => c.passed).length
results.failed = results.checks.filter((c) => !c.passed).length
results.status = results.failed === 0 ? 'passed' : 'failed'
writeFileSync('/workspace/artifacts/validation-results.json', JSON.stringify(results, null, 2))
console.log(`\n${results.status.toUpperCase()}: ${results.passed}/${results.checks.length} checks passed`)
if (results.failed > 0) process.exit(1)