'use strict'; /* * Shared plumbing for the smoke e2e: boots the real server in-process (real clock, temp SQLite file, high WORLD_SPEED so * builds finish in seconds), creates users, opens pages with console/page-error capture, and a tiny pass/fail recorder. * Self-contained: only depends on server/, public/js/rules.js and playwright-core. */ const fs = require('fs'); const os = require('os'); const path = require('path'); const { createServer } = require('../server'); const R = require('../public/js/rules.js'); const ROOT = path.join(__dirname, '..'); const PASSWORD = 'correct horse battery'; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); // ---------------------------------------------------------------- pass/fail recorder const results = []; let currentGroup = ''; function group(name) { currentGroup = name; console.log('\n== ' + name); } function record(name, ok, detail) { results.push({ group: currentGroup, name, ok: !!ok, detail }); console.log((ok ? ' PASS ' : ' FAIL ') + name + (!ok && detail ? '\n ' + String(detail).split('\n').join('\n ') : '')); return !!ok; } // Run one named check; an exception is a failure, not a crash of the whole run. async function check(name, fn) { const t0 = Date.now(); try { const r = await fn(); if (r === false) return record(name, false, 'returned false'); if (typeof r === 'string') return record(name, false, r); // a string is a failure message return record(name, true); } catch (e) { return record(name, false, (e && e.message || String(e)).split('\n').slice(0, 6).join('\n') + ' [' + (Date.now() - t0) + ' ms]'); } } function summary() { const failed = results.filter((r) => !r.ok); console.log('\n' + '='.repeat(60)); console.log(results.length - failed.length + ' passed, ' + failed.length + ' failed, ' + results.length + ' checks'); for (const f of failed) console.log(' FAIL [' + f.group + '] ' + f.name); return failed.length; } // ---------------------------------------------------------------- server async function bootServer(o) { o = o || {}; const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'upstream-e2e-')); const logs = []; const srv = await createServer({ port: 0, host: '127.0.0.1', dbPath: path.join(dir, 'game.db'), log: (m) => { logs.push(m); }, // one machine plays many users: lift the per-IP / per-token / auth rate limits (they are covered by server tests) ipRate: 1e6, ipBurst: 1e6, tokenRate: 1e6, tokenBurst: 1e6, authRate: 1e6, authBurst: 1e6, signupCode: o.signupCode || '', world: { speed: o.speed || 4000, mapRadius: o.mapRadius || 60, name: 'E2E' }, }); const world = srv.db.get('SELECT * FROM worlds LIMIT 1'); const base = 'http://127.0.0.1:' + srv.port; async function api(p, method, body, token) { const r = await fetch(base + '/api/v1' + p, { method: method || 'GET', headers: Object.assign({ 'content-type': 'application/json' }, token ? { authorization: 'Bearer ' + token } : {}), body: body === undefined ? undefined : JSON.stringify(body) }); const j = await r.json().catch(() => ({})); if (r.status >= 300) throw new Error((method || 'GET') + ' ' + p + ' -> ' + r.status + ' ' + JSON.stringify(j)); return j; } async function signup(name) { const r = await api('/auth/signup', 'POST', { username: name, password: PASSWORD, device: 'e2e' }); const op = r.operators[0]; return { name, token: r.token, userId: r.user.id, operatorId: op.id, townId: op.towns[0].id }; } async function close() { await srv.close(); try { fs.rmSync(dir, { recursive: true, force: true }); } catch (e) { /* ignore */ } } return { srv, db: srv.db, world, base, api, signup, close, logs, dir }; } // ---------------------------------------------------------------- direct DB seeding (what a long game would have built up) function landNear(S, x, y) { for (let i = 0; i < 400; i++) { const xx = x + i; if (R.tileAt(S.world.seed, S.world.map_radius, xx, y).type === 'land' && !S.db.get('SELECT 1 AS x FROM towns WHERE world_id = ? AND x = ? AND y = ?', S.world.id, xx, y)) return { x: xx, y }; } throw new Error('no land near ' + x + ',' + y); } function placeTown(S, townId, x, y) { const p = landNear(S, x, y); S.db.run('UPDATE towns SET x = ?, y = ? WHERE id = ?', p.x, p.y, townId); return p; } function setBuildings(S, townId, levels) { for (const type of Object.keys(levels)) { if (S.db.get('SELECT 1 AS x FROM buildings WHERE town_id = ? AND key = ?', townId, type)) S.db.run('UPDATE buildings SET level = ? WHERE town_id = ? AND key = ?', levels[type], townId, type); else S.db.run('INSERT INTO buildings (town_id, key, type, level) VALUES (?, ?, ?, ?)', townId, type, type, levels[type]); } } function giveUnits(S, townId, units) { for (const k of Object.keys(units)) S.db.run('INSERT INTO town_units (town_id, unit, count) VALUES (?, ?, ?) ON CONFLICT (town_id, unit) DO UPDATE SET count = count + excluded.count', townId, k, units[k]); } // ---------------------------------------------------------------- browser pages const PROFILES = { desktop: { viewport: { width: 1280, height: 800 } }, phone: { viewport: { width: 390, height: 844 }, hasTouch: true, isMobile: true, deviceScaleFactor: 2 }, landscape: { viewport: { width: 844, height: 390 }, hasTouch: true, isMobile: true, deviceScaleFactor: 2 }, }; // A page with console/page-error/5xx capture. `errors` holds everything that must stay empty. async function newPage(browser, ctxOpts, o) { o = o || {}; const ctx = await browser.newContext(ctxOpts); const page = await ctx.newPage(); page.setDefaultTimeout(8000); // a missing element fails in 8 s, not 30 s const errors = []; page.on('console', (m) => { if (m.type() === 'error' || m.type() === 'warning') errors.push(m.type() + ': ' + m.text() + (m.location() && m.location().url ? ' @ ' + m.location().url : '')); }); page.on('pageerror', (e) => errors.push('pageerror: ' + e.message)); page.on('requestfailed', (r) => { if (!/\/ws(\?|$)/.test(r.url()) && !(r.failure() && /ERR_ABORTED/.test(r.failure().errorText))) errors.push('requestfailed: ' + r.url() + ' ' + (r.failure() && r.failure().errorText)); }); page.on('response', (r) => { if (r.status() >= 400 && !(o.allowStatus || []).includes(r.status())) errors.push('HTTP ' + r.status() + ' ' + r.request().method() + ' ' + r.url()); }); page.on('dialog', (d) => { errors.push('dialog: ' + d.message()); d.dismiss(); }); if (!o.keepLang) await page.addInitScript(() => { try { localStorage.setItem('upstream.lang', 'en'); } catch (e) { /* ignore */ } }); if (o.init) await page.addInitScript(o.init); // runs before the app's scripts, on every navigation return { ctx, page, errors }; } // Sign in by token injection (fast path used by the layout matrix and the push test). async function openSignedIn(browser, S, user, ctxOpts, hash, o) { const p = await newPage(browser, ctxOpts, o); await p.page.addInitScript((tok) => { try { localStorage.setItem('upstream.token', tok); } catch (e) { /* ignore */ } }, user.token); await p.page.goto(S.base + '/' + (hash || '#/pop')); await p.page.waitForSelector('#nav:not([hidden])', { timeout: 15000 }); return p; } // Horizontal overflow, as a player sees it. The app sets overflow-x:hidden on html/body/#view, so scrollWidth stays equal to the // viewport even when content is cut off. Therefore: (1) the page itself must not scroll, and (2) no visible element may extend // past the viewport edges unless a component-level ancestor deliberately clips or scrolls it (carousels, ellipsis text, bars). // Returns a description of the problem or null. async function overflowReport(page) { return page.evaluate(() => { const de = document.documentElement, b = document.body, vw = de.clientWidth; const out = []; if (de.scrollWidth > vw + 1) out.push('documentElement.scrollWidth ' + de.scrollWidth + ' > ' + vw); if (b.scrollWidth > vw + 1) out.push('body.scrollWidth ' + b.scrollWidth + ' > ' + vw); const page = new Set([document.documentElement, document.body, document.getElementById('app'), document.getElementById('view')]); const clips = (el) => { const o = getComputedStyle(el).overflowX; return o === 'auto' || o === 'scroll' || o === 'hidden' || o === 'clip'; }; const name = (el) => el.tagName.toLowerCase() + (el.id ? '#' + el.id : '') + (typeof el.className === 'string' && el.className.trim() ? '.' + el.className.trim().split(/\s+/).join('.') : ''); const bad = []; for (const el of document.querySelectorAll('#app *')) { if (el.closest('[hidden]') || el.closest('canvas') || el.tagName === 'CANVAS' || el instanceof SVGElement) continue; const cs = getComputedStyle(el); if (cs.visibility === 'hidden' || cs.display === 'none' || cs.position === 'fixed') continue; const r = el.getBoundingClientRect(); if (r.width === 0 || r.height === 0) continue; if (r.right <= vw + 1 && r.left >= -1) continue; let clipped = false; for (let a = el.parentElement; a && !page.has(a); a = a.parentElement) { if (clips(a)) { clipped = true; break; } } if (!clipped && el !== document.getElementById('view')) bad.push(name(el) + ' [' + Math.round(r.left) + '..' + Math.round(r.right) + '] vs viewport ' + vw); } if (bad.length) out.push(bad.length + ' element(s) escape the viewport: ' + bad.slice(0, 4).join('; ')); return out.length ? out.join('; ') : null; }); } // ---------------------------------------------------------------- helpers shared by run.js and flows.js const noErrors = (p, label) => (p.errors.length ? (label || 'console') + ' errors:\n' + p.errors.join('\n') : true); // Tap/click that does the right thing for the profile (real touch events on phones, real mouse on desktop). const tapper = (page, touch) => (sel, opts) => (touch ? page.tap(sel, opts) : page.click(sel, opts)); // Open a "More" screen (Rankings, Alerts, Settings, Trade, World) the way a player does: nav -> More -> row. async function openFromMore(page, touch, id, label) { const tap = tapper(page, touch); await tap('#nav [data-screen="more"]'); await page.waitForSelector('.more-list .row-btn'); await page.locator('.more-list .row-btn', { hasText: label }).first().waitFor({ timeout: 3000 }); await tap('.more-list .row-btn:has-text("' + label + '")'); await page.waitForSelector('[data-screen="' + id + '"]:not([hidden])', { timeout: 5000 }); await sleep(500); // let its first fetch land (errors show up here) } // A flight that normally takes the world's real time (speed 1) is made to land in `inMs`: both the movement row and its event move, // then the scheduler re-arms. This is what a long wait would have done; the handlers still run as in production. function hurryMovement(S, movementId, inMs) { const at = Date.now() + (inMs === undefined ? 2500 : inMs); S.db.run('UPDATE movements SET arrive_at = ? WHERE id = ?', at, movementId); S.db.run("UPDATE events SET due_at = ? WHERE status = 'pending' AND type IN ('arrive', 'return') AND json_extract(payload, '$.movementId') = ?", at, Number(movementId)); S.srv.scheduler.arm(); return at; } // Same for any pending event of a type (a training batch, a build). function hurryEvents(S, type, townId, inMs) { const at = Date.now() + (inMs === undefined ? 2500 : inMs); S.db.run("UPDATE events SET due_at = ? WHERE status = 'pending' AND type = ? AND town_id = ?", at, type, townId); S.srv.scheduler.arm(); return at; } const latestMovement = (S, ownerOperatorId, type) => S.db.get("SELECT * FROM movements WHERE owner_id = ? AND status = 'out' " + (type ? 'AND type = ? ' : '') + 'ORDER BY id DESC LIMIT 1', ...(type ? [ownerOperatorId, type] : [ownerOperatorId])); module.exports = { noErrors, tapper, openFromMore, hurryMovement, hurryEvents, latestMovement, ROOT, PASSWORD, sleep, group, check, record, summary, results, bootServer, landNear, placeTown, setBuildings, giveUnits, PROFILES, newPage, openSignedIn, overflowReport, R };