Skip to content

scripts/smoke.mjs

27 lines · 1.8 KB · JavaScript
Raw
import { chromium } from 'playwright-core'
import { pathToFileURL } from 'node:url'
import { resolve } from 'node:path'
const errors = []
const browser = await chromium.launch({ executablePath: process.env.CHROME_PATH || '/usr/bin/google-chrome' })
for (const vp of [{ width: 1280, height: 800 }, { width: 390, height: 844, isMobile: true, hasTouch: true }]) {
	const ctx = await browser.newContext({ viewport: { width: vp.width, height: vp.height }, isMobile: vp.isMobile, hasTouch: vp.hasTouch })
	const page = await ctx.newPage()
	// file:// cannot load the router-served icons/manifest; ignore those resource misses.
	page.on('console', (m) => m.type() === 'error' && !m.text().includes('ERR_FILE_NOT_FOUND') && errors.push(m.text()))
	page.on('pageerror', (e) => errors.push(String(e)))
	await page.goto(pathToFileURL(resolve('dist/index.html')).href)
	await page.waitForTimeout(500)
	if ((await page.evaluate(() => window.__asteroids.state)) !== 'title') throw new Error('not on title')
	await page.keyboard.press('Space')
	if ((await page.evaluate(() => window.__asteroids.state)) !== 'play') throw new Error('did not start')
	await page.keyboard.down('ArrowUp'); await page.keyboard.down('Space'); await page.keyboard.down('KeyA')
	await page.waitForTimeout(2000)
	await page.keyboard.up('ArrowUp'); await page.keyboard.up('Space'); await page.keyboard.up('KeyA')
	const score = await page.locator('#score').textContent()
	await page.screenshot({ path: `dist/shot-${vp.width}.png` })
	console.log(vp.width, 'state', await page.evaluate(() => window.__asteroids.state), 'score', score, 'rocks', await page.evaluate(() => window.__asteroids.rocks))
	await ctx.close()
}
await browser.close()
if (errors.length) { console.error('Console errors:', errors); process.exit(1) }
console.log('OK: no console errors')