// Headless Chromium smoke test: loads dist/index.html at mobile and desktop sizes, plays with // real touch (CDP) and keyboard input, checks controls work and the console is clean. // Usage: npm test (env: CHROME_PATH, SHOTS=, URL=) import { chromium } from 'playwright-core' import { pathToFileURL } from 'node:url' import { resolve } from 'node:path' import { mkdirSync } from 'node:fs' const SHOTS = process.env.SHOTS || 'dist/shots' mkdirSync(SHOTS, { recursive: true }) const url = process.env.URL || pathToFileURL(resolve('dist/index.html')).href const errors = [] const fail = (m) => { console.error('FAIL', m); process.exitCode = 1 } const ok = (m) => console.log('ok ', m) const browser = await chromium.launch({ executablePath: process.env.CHROME_PATH || '/usr/bin/google-chrome' }) const snap = (page) => page.evaluate(() => window.__starsnake.snap) async function touch(cdp, type, pts) { await cdp.send('Input.dispatchTouchEvent', { type, touchPoints: pts.map((p, i) => ({ x: p[0], y: p[1], id: p[2] ?? i })) }) } // ---------- mobile, portrait ---------- { const ctx = await browser.newContext({ viewport: { width: 390, height: 844 }, deviceScaleFactor: 3, isMobile: true, hasTouch: true }) const page = await ctx.newPage() page.on('console', (m) => { if (m.type() === 'error' || m.type() === 'warning') errors.push(m.text()) }) page.on('pageerror', (e) => errors.push(String(e))) await page.goto(url + (url.includes('?') ? '&' : '?') + 'seed=7') await page.waitForTimeout(400) await page.screenshot({ path: `${SHOTS}/mobile-title.png` }) if ((await snap(page)).state !== 'title') fail('not on title') await page.tap('#howBtn'); await page.waitForTimeout(150) await page.screenshot({ path: `${SHOTS}/mobile-howto.png` }) await page.tap('#howBack'); await page.waitForTimeout(100) await page.tap("#playBtn"); await page.waitForTimeout(300) let s = await snap(page) s.state === 'play' ? ok('PLAY tap starts game') : fail('PLAY did not start: ' + s.state) const cdp = await ctx.newCDPSession(page) // Steer: drag from lower-left toward the upper right and hold (boost). const a0 = s.ship.a await touch(cdp, 'touchStart', [[120, 600]]) for (let i = 1; i <= 8; i++) { await touch(cdp, 'touchMove', [[120 + i * 8, 600 - i * 8]]); await page.waitForTimeout(40) } await page.waitForTimeout(700) s = await snap(page) const turned = Math.abs(Math.atan2(Math.sin(s.ship.a - a0), Math.cos(s.ship.a - a0))) turned > 0.4 ? ok(`touch joystick turns the ship (${turned.toFixed(2)} rad)`) : fail('ship did not turn: ' + JSON.stringify(s)) await page.screenshot({ path: `${SHOTS}/mobile-steering.png` }) await touch(cdp, 'touchEnd', []) // Build a tower with a second tap while not steering. await page.evaluate(() => window.__starsnake.give(20)) const before = (await snap(page)).towers await page.tap('#buildBar [data-act=pulse]'); await page.waitForTimeout(100) let after = await snap(page) after.towers === before + 1 ? ok('tower button drops a tower') : fail(`tower not built (${before} -> ${after.towers}) ` + JSON.stringify(after)) // Two thumbs: hold the stick and tap a build button at the same time. await touch(cdp, 'touchStart', [[100, 650, 1]]) await touch(cdp, 'touchMove', [[100, 600, 1]]) await page.waitForTimeout(500) await touch(cdp, 'touchStart', [[100, 600, 1], [255, 800, 2]]); await touch(cdp, 'touchEnd', [[100, 600, 1]]) await page.waitForTimeout(100) await touch(cdp, 'touchEnd', []) // Let waves run; assert no scroll/zoom. await page.evaluate(() => window.__starsnake.scene()) await page.waitForTimeout(1200) await page.screenshot({ path: `${SHOTS}/mobile-play.png` }) const noScroll = await page.evaluate(() => ({ sy: scrollY, sx: scrollX, scale: visualViewport.scale, over: getComputedStyle(document.body).overflow })) noScroll.sy === 0 && noScroll.scale === 1 ? ok('no scroll or zoom') : fail('scroll/zoom: ' + JSON.stringify(noScroll)) // pause on visibility change await page.evaluate(() => { Object.defineProperty(document, 'hidden', { value: true, configurable: true }); document.dispatchEvent(new Event('visibilitychange')) }) s = await snap(page) s.state === 'paused' ? ok('pauses when the page is hidden') : fail('did not pause: ' + s.state) await page.screenshot({ path: `${SHOTS}/mobile-paused.png` }) await page.evaluate(() => { Object.defineProperty(document, 'hidden', { value: false, configurable: true }) }) await page.tap('#resumeBtn') // Die by core destruction, check game over + high score persisted. await page.evaluate(() => { const s = window.__starsnake; s.setTail(0) }) await page.waitForTimeout(100) await ctx.close() } // ---------- mobile, landscape ---------- { const ctx = await browser.newContext({ viewport: { width: 844, height: 390 }, deviceScaleFactor: 2, isMobile: true, hasTouch: true }) const page = await ctx.newPage() page.on('console', (m) => { if (m.type() === 'error' || m.type() === 'warning') errors.push(m.text()) }) page.on('pageerror', (e) => errors.push(String(e))) await page.goto(url + (url.includes('?') ? '&' : '?') + 'seed=7') await page.waitForTimeout(300) await page.screenshot({ path: `${SHOTS}/mobile-landscape-title.png` }) await page.tap('#playBtn'); await page.waitForTimeout(200) await page.evaluate(() => window.__starsnake.scene()) await page.waitForTimeout(1200) await page.screenshot({ path: `${SHOTS}/mobile-landscape-play.png` }) ok('landscape renders') await ctx.close() } // ---------- desktop, keyboard ---------- { const ctx = await browser.newContext({ viewport: { width: 1280, height: 800 } }) const page = await ctx.newPage() page.on('console', (m) => { if (m.type() === 'error' || m.type() === 'warning') errors.push(m.text()) }) page.on('pageerror', (e) => errors.push(String(e))) await page.goto(url + (url.includes('?') ? '&' : '?') + 'seed=11') await page.waitForTimeout(400) await page.screenshot({ path: `${SHOTS}/desktop-title.png` }) await page.keyboard.press('Space'); await page.waitForTimeout(200) let s = await snap(page) s.state === 'play' ? ok('Space starts the game') : fail('Space did not start ' + s.state) const a0 = s.ship.a await page.keyboard.down('ArrowRight'); await page.keyboard.down('ArrowUp'); await page.waitForTimeout(500) await page.keyboard.up('ArrowRight'); await page.keyboard.up('ArrowUp') s = await snap(page) Math.abs(s.ship.a - a0) > 0.8 ? ok('keyboard turns the ship') : fail('keyboard did not turn ' + JSON.stringify(s)) await page.evaluate(() => window.__starsnake.give(60)) await page.keyboard.press('Digit2') s = await snap(page) s.towers === 1 ? ok('key 2 builds a cannon') : fail('no tower on key 2 ' + JSON.stringify(s)) // The tail is a weapon: a rock parked on a tail bead must be destroyed by the tail alone. for (const size of [2, 3]) { await page.evaluate(() => window.__starsnake.setTail(12)) await page.waitForTimeout(600) const t0 = await page.evaluate((n) => window.__starsnake.tailTest(n), size) const k0 = (await snap(page)).tailKills let s2 = await snap(page), sawHurt = false // Knockback flings a hit rock off the tail, so keep putting it back on a bead until it dies. for (let i = 0; i < 60 && s2.tailKills === k0; i++) { await page.waitForTimeout(100); s2 = await snap(page) if (s2.rockHp > 0 && s2.rockHp < t0.rock.hp) { sawHurt = true; await page.evaluate(() => window.__starsnake.rockToTail()) } } const ex = size === 3 ? sawHurt : true s2.tailKills > k0 && s2.tail >= t0.tail && ex ? ok(`tail destroys a size-${size} rock${size === 3 ? ' (survives the first hit, dies on the second)' : ' in one hit'} (tail kills ${k0} -> ${s2.tailKills}, beads ${t0.tail} -> ${s2.tail})`) : fail(`tail did not destroy size-${size} rock as designed: ` + JSON.stringify(s2)) } await page.keyboard.press('Digit4'); await page.keyboard.press('Digit5') await page.evaluate(() => window.__starsnake.scene()) await page.waitForTimeout(1500) await page.screenshot({ path: `${SHOTS}/desktop-play.png` }) // Steering in a tight circle with a long tail must end the run: you hit your own tail. await page.evaluate(() => { const g = window.__starsnake; g.setTail(30) }) await page.keyboard.down('ArrowRight') for (let i = 0; i < 80 && (await snap(page)).state === 'play'; i++) await page.waitForTimeout(100) await page.keyboard.up('ArrowRight') await page.waitForTimeout(1500) s = await snap(page) s.state === 'over' && s.over === 'tail' ? ok('hitting your own tail ends the game') : fail('no tail game over: ' + JSON.stringify(s)) await page.screenshot({ path: `${SHOTS}/desktop-gameover.png` }) const hi = await page.evaluate(() => localStorage.getItem('starsnake-hi')) Number(hi) > 0 ? ok(`high score saved (${hi})`) : fail('high score not saved') await page.click('#againBtn'); await page.waitForTimeout(200) ;(await snap(page)).state === 'play' ? ok('PLAY AGAIN restarts') : fail('restart failed') await page.reload(); await page.waitForTimeout(300) ;(await page.textContent('#hiTitle')) === hi ? ok('high score shown on title after reload') : fail('hi not shown after reload') await ctx.close() } await browser.close() // file:// pages cannot load router-served images, so only then ignore resource misses. const bad = url.startsWith('file:') ? errors.filter((e) => !e.includes('ERR_FILE_NOT_FOUND') && !e.includes('Failed to load resource')) : errors if (bad.length) { console.error('Console errors:', bad); process.exit(1) } if (process.exitCode) process.exit(process.exitCode) console.log('OK: no console errors')