Upstream/e2e/lib.js
jvanvik cc0524de5e Smoke e2e, docs consistency pass, status summary
Built: e2e/run.js + lib.js (playwright-core, in-process server, temp DB): desktop flow, phone touch portrait/landscape, layout matrix on 5 viewports x pop/map/trade/world (overflow + console errors), WebSocket push incoming-banner without reload. 61/61 checks, ~54 s, identical over 4 runs; the push and overflow checks were shown to fail when the behaviour is removed.
Docs: fixed stale numbers/claims in DESIGN, DECISIONS, BALANCE, API (undocumented routes, rate limits, notification types), DEPLOY, NEXT (status-at-a-glance on top). Deploy path exercised in a scratch dir: migrations, healthz, signup/build, admin CLI, SIGKILL catch-up, backup restore.

Tested: npm test 207/207, npm run e2e 61/61, bot run 50x14d INVARIANTS OK.
NOT tested: Chromium only; no systemd/nginx Proxy Manager/Node 22.13 run; e2e does not cover units, alliances, messages, trade or end-game flows beyond empty-state rendering; no nb, PWA or polling-fallback e2e; docs/workflows/ still empty.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q2MJaQ5LXAHzGnTDgceT14
2026-10-11 20:34:35 +02:00

158 lines
9.3 KiB
JavaScript

'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();
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(); });
await page.addInitScript(() => { try { localStorage.setItem('upstream.lang', 'en'); } catch (e) { /* ignore */ } });
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;
});
}
module.exports = { ROOT, PASSWORD, sleep, group, check, record, summary, results, bootServer, landNear, placeTown, setBuildings, giveUnits, PROFILES, newPage, openSignedIn, overflowReport, R };