refactor(game): split game.js into sfx, pages, online and workshop modules (P4-5)

Modules are classic-script factories RC.Game<Part>(G); G gives live access to the shared state.
Guarded by new browser tests for every moved page and workshop tool (tests/e2e/screens.spec.js).
This commit is contained in:
Benjamin Oezcan
2026-11-19 09:58:53 +01:00
parent fade816169
commit 4535ac7d8a
11 changed files with 975 additions and 711 deletions

View File

@@ -26,6 +26,12 @@ async function toLobby(page) {
await expect(page.locator('body')).toHaveAttribute('data-mode', 'lobby');
}
const credits = async page => Number((await page.locator('#hMoney').textContent()).replace(/[^\d]/g, ''));
// the header shows big amounts short like Kit.fmt does ("10.8K", "1.20M", "3B")
const UNIT = { K: 1e3, M: 1e6, B: 1e9 };
const parseCredits = text => {
const m = String(text).replace(/[,\s]/g, '').match(/([\d.]+)([KMB]?)/i);
return m ? Math.round(parseFloat(m[1]) * (UNIT[(m[2] || '').toUpperCase()] || 1)) : NaN;
};
const credits = async page => parseCredits(await page.locator('#hMoney').textContent());
module.exports = { mode, login, chooseCountry, toLobby, credits };
module.exports = { mode, login, chooseCountry, toLobby, credits, parseCredits };

View File

@@ -3,6 +3,7 @@
const { test, expect } = require('@playwright/test');
const path = require('path');
const { pathToFileURL } = require('url');
const { credits } = require('./helpers');
const file = pathToFileURL(path.join(__dirname, '..', '..', 'game', 'index.html')).href;
@@ -19,7 +20,7 @@ test('campaign runs from file:// : training fight with the AI pilot, layout chec
test('campaign workshop: buying a part works offline', async ({ page }) => {
await page.goto(file + '?play');
await expect(page.locator('body')).toHaveAttribute('data-mode', 'garage');
const money = async () => Number((await page.locator('#hMoney').textContent()).replace(/[^\d]/g, ''));
const money = () => credits(page);
const before = await money();
await page.locator('.tab[data-slot="armour"]').click();
await page.locator('#cards .item:not(:has(.badge)):not(:has(.pr.no))').first().click();

89
tests/e2e/screens.spec.js Normal file
View File

@@ -0,0 +1,89 @@
// Every menu page and workshop tool once, in the real browser, offline (file://) - guards the game.js split:
// a missing reference between the modules shows up here as a JavaScript error.
'use strict';
const { test, expect } = require('@playwright/test');
const path = require('path');
const { pathToFileURL } = require('url');
const file = pathToFileURL(path.join(__dirname, '..', '..', 'game', 'index.html')).href;
const { credits: money } = require('./helpers');
test.beforeEach(async ({ page }) => {
page.errors = [];
page.on('pageerror', e => page.errors.push(e.message));
await page.goto(file + '?play');
await expect(page.locator('body')).toHaveAttribute('data-mode', 'garage');
});
test.afterEach(async ({ page }) => { expect(page.errors, 'no JavaScript errors').toEqual([]); });
test('menu pages: settings, controls, legal, achievements, missions', async ({ page }) => {
await page.locator('#bSettings').click();
await expect(page.locator('#sKeys')).toBeVisible();
await page.locator('#sKeys').click(); // controls page from settings
await page.locator('#bOk').click();
await expect(page.locator('#sLegal')).toBeVisible(); // back in settings
await page.locator('#sLegal').click();
await page.locator('#bOk').click();
await page.locator('#bBack').click();
await expect(page.locator('body')).toHaveAttribute('data-mode', 'garage');
await page.locator('#chipFame').click(); // achievements
await expect(page.locator('.achlist')).toBeVisible();
await page.locator('#bBack').click();
await page.locator('#bMissions').click(); // campaign missions
await expect(page.locator('.misslist').first()).toBeVisible();
await page.locator('#bBack').click();
});
test('guest via the title: country, tutorial, then dev coins go through the economy', async ({ page }) => {
await page.goto(file + '?title');
await page.locator('#bGuest').click();
await page.locator('#cfGo').click(); // country card
await page.locator('#tSkip').click(); // tutorial cards
await expect(page.locator('body')).toHaveAttribute('data-mode', 'garage');
await page.locator('#bSettings').click();
await page.locator('[data-coin="10000"]').click();
await page.locator('#bBack').click();
expect(await money(page)).toBeGreaterThanOrEqual(10800);
});
test('weapon editor: buy, place, sell a weapon', async ({ page }) => {
const start = await money(page);
await page.locator('.tab[data-slot="weapon"]').click();
await expect(page.locator('#wplace')).toBeVisible();
const before = await page.locator('.wphead').first().textContent();
await page.locator('#cards .item:not(:has(.pr.no))').first().click();
await page.locator('#detail button[data-wact="add"]').click();
expect(await money(page)).toBeLessThan(start);
await expect(page.locator('.wphead').first()).not.toHaveText(before); // weapon count went up
await page.locator('[data-side="left"]').click();
await expect(page.locator('[data-side="left"]')).toHaveClass(/on/);
await page.locator('#detail button[data-wact="rem"]').click();
});
test('paint tab: colour, scheme, paint slots', async ({ page }) => {
await page.locator('#shopTabs [data-tab="paint"]').click();
await expect(page.locator('#paintHdr')).toBeVisible();
await page.locator('#paintBody .sw:not(.locked)').nth(2).click();
await expect(page.locator('#paintBody .sw.on')).toHaveCount(1);
await page.locator('#paintSchemes .sw').nth(1).click();
await page.locator('[data-save="1"]').click(); // store into slot 2
await page.locator('[data-load="0"]').click(); // load slot 1
await expect(page.locator('.pslot.on')).toHaveCount(1);
await page.locator('#shopTabs [data-tab="build"]').click();
await expect(page.locator('#paintHdr')).toHaveCount(0);
});
test('hangar: new robot, switch, delete', async ({ page }) => {
await page.locator('#shopTabs [data-tab="hangar"]').click();
await expect(page.locator('.hmrow')).toHaveCount(1);
await page.locator('#hAdd').click();
await page.locator('#shopTabs [data-tab="hangar"]').click();
await expect(page.locator('.hmrow')).toHaveCount(2);
await page.locator('[data-use="0"]').click();
await page.locator('#shopTabs [data-tab="hangar"]').click();
await expect(page.locator('.hmrow.on')).toHaveAttribute('data-i', '0');
await page.locator('[data-del="1"]').click();
await expect(page.locator('.hmrow')).toHaveCount(1);
});