powergrid/viewer/scripts/premove-smoke.mjs

235 lines
11 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdir } from 'node:fs/promises';
import { chromium } from 'playwright';
import { premovePreviewServer } from './premove-preview.mjs';
// Real viewer + real wrapper. No component mocks or synthetic game state patches in the browser.
const server = premovePreviewServer();
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
const url = `http://127.0.0.1:${server.address().port}`;
const browser = await chromium.launch({
headless: true,
...(process.env.CHROMIUM_PATH ? { executablePath: process.env.CHROMIUM_PATH } : {}),
});
const screenshots = process.env.SCREENSHOT_DIR;
if (screenshots) await mkdir(screenshots, { recursive: true });
const errors = [];
try {
const page = await browser.newPage({ viewport: { width: 1440, height: 1080 }, reducedMotion: 'reduce' });
page.on('pageerror', (error) => errors.push(String(error)));
let sent = 0;
page.on('request', (request) => {
if (new URL(request.url()).pathname === '/move') sent++;
});
const button = (name) =>
page.getByRole('button', {
name:
name === 'Plan'
? 'Plan cities and powering for this round'
: name === 'Simulate'
? 'Simulate the rest of this round with the game controls'
: name,
exact: true,
});
const queuedPlan = (count) =>
page
.locator('[data-board-control="plan"][aria-pressed="false"]')
.filter({ has: page.locator('text', { hasText: new RegExp(`^${count}$`) }) });
const city = (name) =>
page.locator('circle.canClick[pointer-events="all"]').filter({
has: page.locator('title').filter({ hasText: new RegExp(`^${name} — build`) }),
});
const card = (area, number) =>
page
.locator(`[data-tutorial="${area}"] g.canClick`)
.filter({
has: page.locator('text').filter({ hasText: new RegExp(`^${number}$`) }),
})
.first();
const done = () => page.locator('[data-tutorial="turn"] g.enabled').filter({ hasText: 'Done' }).first();
const undo = () => page.locator('[data-tutorial="turn"] g.enabled').filter({ hasText: 'Undo' }).first();
const ownBoard = () => page.locator('.player-board').filter({ hasText: 'You' }).first();
const state = async (scenario = 'after-resources', seat = 0) =>
(await (await fetch(`${url}/state?scenario=${scenario}&seat=${seat}`)).json()).state;
async function phase(text) {
await page.waitForFunction(
(text) => document.querySelector('.status-message')?.textContent.includes(text),
text
);
}
async function screenshot(name) {
if (screenshots) {
await page.evaluate(() => scrollTo(0, 0));
await page.screenshot({ path: `${screenshots}/${name}.png`, fullPage: true });
}
}
await page.goto(url);
await button('Plan').waitFor();
assert.equal(
await page.locator('.statusBar [role=button], .statusBar button').count(),
0,
'status text has the full toolbar width'
);
assert.equal(await page.locator('#scene [data-board-control="plan"]').count(), 1, 'planning is on the board');
assert.equal(await page.locator('[data-board-control="log"], .modal-log').count(), 0, 'no duplicate journal modal');
const color = page.locator('[data-board-control="color-blind"]');
await color.focus();
await page.keyboard.press('Enter');
assert.equal(await color.getAttribute('aria-pressed'), 'true');
await page.keyboard.press('Space');
assert.equal(await color.getAttribute('aria-pressed'), 'false');
assert.equal(await page.locator('.round-planner').count(), 0, 'no empty planning toolbar');
const initial = await state();
const deckLabel = () =>
page
.locator('title')
.filter({ hasText: /cards left/ })
.textContent();
const initialDeck = await deckLabel();
await screenshot('toolbar');
await button('Plan').click();
await phase('Building');
assert.equal(await button('Start over').count(), 0, 'use the board Undo control');
await city('Osnabrück').click();
await city('Münster').click();
assert.match(await ownBoard().textContent(), /\$43/);
assert.equal(await deckLabel(), initialDeck, 'public deck display survives local city builds');
await done().click();
await phase('Powering');
await card('players', 4).click();
await card('players', 13).click();
await done().click();
await phase('Simulation complete');
assert.match(await ownBoard().textContent(), /\$76/);
assert.equal(sent, 0, 'planning never submits real moves');
assert.deepEqual(await state(), initial, 'live state unchanged by simulation');
await screenshot('planning');
await button('Validate').click();
await queuedPlan(4).waitFor();
assert.equal(await page.locator('.round-planner').count(), 0, 'saved queue adds no separate bar');
assert.equal(sent, 1);
assert.equal((await state()).automation.plans[0].phases.length, 2);
assert.deepEqual((await state('after-resources', 1)).automation.plans, {}, 'queue is private');
await page.reload();
await queuedPlan(4).waitFor();
assert.equal(await button('View on board').count(), 0, 'Plan is the single entry to the saved queue');
assert.equal(await button('Cancel powering').count(), 0);
assert.equal(await button('Cancel from here').count(), 0, 'no duplicate Cancel premoves control');
await screenshot('queued');
await queuedPlan(4).click();
await phase('Simulation complete');
assert.equal(await button('Validate').count(), 0, 'viewing an unchanged queue needs no validation');
await button('Cancel premoves').click();
await page.waitForFunction(
() => !document.querySelector('.round-planner')?.textContent.includes('Saving premoves')
);
await phase('Simulation complete');
assert.equal((await state()).automation.plans[0].phases.length, 0);
assert.match(await ownBoard().textContent(), /\$76/, 'cancelling keeps local simulation');
await button('Validate').click();
await queuedPlan(4).waitFor();
for (let i = 0; i < 2; i++) {
const response = page.waitForResponse((r) => new URL(r.url()).pathname === '/opponent');
await button('Finish next opponent phase').click();
await response;
}
await queuedPlan(2).waitFor();
let live = await state();
assert.equal(live.players[0].cities.length, 2);
assert.equal(live.players[0].money, 43);
assert.deepEqual(
live.automation.plans[0].phases.map((p) => p.phase),
['Bureaucracy']
);
for (let i = 0; i < 2; i++) {
const response = page.waitForResponse((r) => new URL(r.url()).pathname === '/opponent');
await button('Finish next opponent phase').click();
await response;
}
live = await state();
assert.equal(live.players[0].money, 76);
assert.equal(live.players[0].coalLeft, 0);
assert.equal(live.automation.increments[0], 2);
assert.equal(live.automation.plans[0].phases.length, 0);
console.log('Off-turn plan, budget, private queue, reload, cancel, and execution passed.');
await page.goto(`${url}/?scenario=auction`);
await button('Simulate').click();
const sentBeforeAuction = sent;
await card('plants', 3).click();
for (let i = 0; i < 12; i++) await page.locator('.calculator > g').nth(0).click();
await page.locator('.calculator g').filter({ hasText: 'OK' }).click();
await phase('Resources');
assert.match(await ownBoard().textContent(), /\$55/);
const oil = () =>
page
.locator('[data-tutorial="resources"] g.canClick')
.filter({ has: page.locator('title').filter({ hasText: /^Oil$/ }) })
.first();
await oil().click();
await oil().click();
await done().click();
await phase('Building');
await city('Osnabrück').click();
assert.match(await ownBoard().textContent(), /\$39/);
assert.equal(await button('Validate').count(), 0, 'assumed purchases cannot be queued');
await screenshot('auction');
for (let i = 0; i < 6; i++) await undo().click();
await phase('Auction');
assert.equal(await undo().count(), 0, 'Undo rewinds through purchases and phase changes to the start');
await done().click();
await page.locator('.modal.visible').getByRole('button', { name: 'OK', exact: true }).click();
await phase('Resources');
assert.match(await ownBoard().textContent(), /\$70/);
assert.equal(sent, sentBeforeAuction);
assert.equal((await state('auction')).players[0].money, 70);
console.log('Assumed plant price, fuel purchases, no purchase, and simulation-only restriction passed.');
await page.setViewportSize({ width: 390, height: 844 });
await page.goto(`${url}/?scenario=powering`);
await button('Plan').click();
await phase('Powering');
await card('players', 4).click();
await card('players', 13).click();
await done().click();
await phase('Simulation complete');
assert.match(await ownBoard().textContent(), /\$75/);
assert.ok(
await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1),
'no mobile horizontal overflow'
);
await screenshot('mobile');
await button('Validate').click();
await queuedPlan(2).waitFor();
assert.equal(await page.locator('.round-planner').count(), 0, 'no saved-queue bar on mobile');
assert.deepEqual(
(await state('powering')).automation.plans[0].phases.map((p) => p.phase),
['Bureaucracy']
);
await queuedPlan(2).click();
await phase('Simulation complete');
const cancelled = page.waitForResponse((r) => new URL(r.url()).pathname === '/move');
await button('Cancel premoves').click();
await cancelled;
await button('Validate').waitFor();
// Even a phase with no cities/plants queues a pass and needs a visible counter.
for (let i = 0; i < 3; i++) await undo().click();
await phase('Powering');
await done().click();
await page.locator('.modal.visible').getByRole('button', { name: 'OK', exact: true }).click();
await button('Validate').click();
await queuedPlan(1).waitFor();
await queuedPlan(1).click();
await phase('Simulation complete');
await button('Cancel premoves').click();
await button('Validate').waitFor();
await button('Return to live game').click();
await button('Plan').waitFor();
await page.locator('.round-planner').waitFor({ state: 'detached' });
assert.equal(await page.locator('.round-planner').count(), 0, 'empty saved queue has no leftover strip');
console.log('Mobile powering-only plan passed.');
assert.deepEqual(errors, [], 'no browser runtime errors');
} finally {
await browser.close();
await new Promise((resolve) => server.close(resolve));
}