batched round: queue actions/diplomacy, jump forward commits one turn (v0.2.0)
CI / build + unit + e2e (pull_request) Successful in 1m51s
CI / build + unit + e2e (pull_request) Successful in 1m51s
This commit is contained in:
+70
-35
@@ -2,19 +2,20 @@ import { test, expect, type Page } from '@playwright/test';
|
||||
|
||||
/**
|
||||
* Fully deterministic end-to-end test. The Playwright webServer boots the real
|
||||
* stack (Vite :5173 → Express :4321) with LLM_STUB=1, so the backend uses the
|
||||
* deterministic turn simulator in server/stub.ts: the player nation's `military`
|
||||
* stat is set to (current + 5, clamped) each turn and exactly one event is
|
||||
* emitted (action → military, chat → diplomatic, jump → political). There is no
|
||||
* live LLM and no flakiness — every assertion below is an exact, predicted value.
|
||||
* stack (Vite → Express) with LLM_STUB=1, so the backend uses the deterministic
|
||||
* turn simulator in server/stub.ts.
|
||||
*
|
||||
* Beyond the core simulation loop it also asserts the polished command-center
|
||||
* UI: the dossier's animated stat bars, the map legend (generated from the
|
||||
* region map), and the new event cards (type icon + label + colored outcome
|
||||
* badge). The map itself is a canvas, so we assert on its DOM legend, not pixels.
|
||||
* Batched-round model: actions and diplomatic messages are queued in
|
||||
* `game.pending` (no turn advance, no LLM call); a single "Jump forward"
|
||||
* commits the whole batch in one LLM turn. The stub emits one event per
|
||||
* queued item (action → military/success, diplomacy → diplomatic/partial) or
|
||||
* one political "The world moves on" event when the round is empty. It always
|
||||
* sets the player's military to (current + 5, clamped) and allies the player
|
||||
* with the first other nation, advancing the date by 2 days.
|
||||
*/
|
||||
test.describe('History — deterministic simulation (LLM_STUB=1)', () => {
|
||||
test('scenario → nation → action → jump, with predicted stat changes', async ({ page }) => {
|
||||
test('scenario → nation → queue action + diplomacy → jump, with predicted stat changes', async ({ page }) => {
|
||||
test.setTimeout(120000);
|
||||
await page.goto('/');
|
||||
|
||||
// Home: the scenario grid loads with all 5 presets.
|
||||
@@ -44,9 +45,11 @@ test.describe('History — deterministic simulation (LLM_STUB=1)', () => {
|
||||
await expect(page.getByTestId('stat-stability')).toBeVisible();
|
||||
await expect(page.getByTestId('stat-population')).toBeVisible();
|
||||
|
||||
// The player (UK) starts at military 80, with no events yet.
|
||||
// The player (UK) starts at military 80, date 1939-09-01, no events yet.
|
||||
const military = page.getByTestId('stat-military');
|
||||
const date = page.getByTestId('game-date');
|
||||
await expect(military).toHaveText('80');
|
||||
await expect(date).toHaveText('1939-09-01');
|
||||
await expect(page.getByTestId('event-item')).toHaveCount(0);
|
||||
|
||||
// The map legend is auto-generated from the region map: all 7 nations of the
|
||||
@@ -54,34 +57,63 @@ test.describe('History — deterministic simulation (LLM_STUB=1)', () => {
|
||||
await expect(page.getByTestId('map-legend-item')).toHaveCount(7, { timeout: 15000 });
|
||||
await expect(page.getByTestId('map-legend-item').filter({ hasText: 'United Kingdom' })).toBeVisible();
|
||||
|
||||
// Take an action: the stub bumps UK military to 85 and emits a military event.
|
||||
// --- Queue an action (no turn advance, no LLM call) ---
|
||||
await page.getByTestId('action-textarea').fill('Mobilize the home guard and fortify Dover');
|
||||
await page.getByTestId('action-submit').click();
|
||||
await page.getByTestId('add-action').click();
|
||||
|
||||
// The pending list shows one queued action; turn and date are unchanged.
|
||||
await expect(page.getByTestId('pending-item')).toHaveCount(1);
|
||||
await expect(page.getByTestId('pending-item').first()).toHaveAttribute('data-kind', 'action');
|
||||
await expect(page.getByTestId('pending-item').first()).toContainText('Mobilize the home guard and fortify Dover');
|
||||
await expect(page.getByTestId('turn-count')).toHaveText('1');
|
||||
await expect(date).toHaveText('1939-09-01');
|
||||
await expect(page.getByTestId('event-item')).toHaveCount(0);
|
||||
// The "Your move" window's title badge shows the pending count.
|
||||
await expect(page.getByTestId('title-queued')).toHaveText('1 queued');
|
||||
|
||||
// --- Queue a diplomatic message (no turn advance, no LLM call) ---
|
||||
await page.getByTestId('tab-diplo').click();
|
||||
await page.getByTestId('chat-target').click();
|
||||
await page.getByRole('option', { name: 'United States' }).click();
|
||||
await page.getByTestId('chat-message').fill('We propose a mutual defense pact');
|
||||
await page.getByTestId('add-diplomacy').click();
|
||||
|
||||
// The pending list now shows two items (action + diplomacy); turn still 1.
|
||||
await expect(page.getByTestId('pending-item')).toHaveCount(2);
|
||||
await expect(page.getByTestId('pending-item').first()).toHaveAttribute('data-kind', 'action');
|
||||
await expect(page.getByTestId('pending-item').nth(1)).toHaveAttribute('data-kind', 'diplomacy');
|
||||
await expect(page.getByTestId('pending-item').nth(1)).toContainText('→ United States');
|
||||
await expect(page.getByTestId('pending-item').nth(1)).toContainText('We propose a mutual defense pact');
|
||||
await expect(page.getByTestId('turn-count')).toHaveText('1');
|
||||
await expect(date).toHaveText('1939-09-01');
|
||||
await expect(page.getByTestId('event-item')).toHaveCount(0);
|
||||
await expect(page.getByTestId('title-queued')).toHaveText('2 queued');
|
||||
|
||||
// --- Jump forward: commit the whole pending round in a single LLM turn ---
|
||||
// The button sits at the bottom of the scrollable "Your move" window body
|
||||
// (max-h-[72vh] overflow-y-auto) and may be outside the page viewport; a
|
||||
// native DOM click bypasses Playwright's viewport actionability check.
|
||||
await page
|
||||
.getByTestId('jump-forward')
|
||||
.evaluate((el) => (el as HTMLElement).click());
|
||||
|
||||
// Turn advances to 2, date to 1939-09-03 (+2 days), military 80 → 85.
|
||||
await expect(page.getByTestId('turn-count')).toHaveText('2', { timeout: 20000 });
|
||||
await expect(military).toHaveText('85', { timeout: 20000 });
|
||||
await expect(page.getByTestId('event-item')).toHaveCount(1);
|
||||
await expect(
|
||||
page.getByTestId('event-feed').getByText('Mobilize the home guard and fortify Dover').first(),
|
||||
).toBeVisible();
|
||||
await expect(date).toHaveText('1939-09-03', { timeout: 20000 });
|
||||
|
||||
// The new event card carries its type (icon + label) and a colored outcome badge.
|
||||
await expect(page.getByTestId('event-item').first().getByTestId('event-type-icon')).toBeVisible();
|
||||
await expect(page.getByTestId('event-item').first().getByTestId('event-type')).toHaveText('military');
|
||||
await expect(page.getByTestId('event-item').first().getByTestId('event-outcome')).toHaveText('success');
|
||||
|
||||
// Jump forward (no action): a "world moves on" political event, military → 90.
|
||||
await page.getByTestId('jump-forward').click();
|
||||
|
||||
await expect(page.getByTestId('turn-count')).toHaveText('3', { timeout: 20000 });
|
||||
await expect(military).toHaveText('90', { timeout: 20000 });
|
||||
// Two events were emitted (one per queued item): one military, one diplomatic.
|
||||
await expect(page.getByTestId('event-item')).toHaveCount(2);
|
||||
await expect(page.getByTestId('event-feed').getByText('The world moves on').first()).toBeVisible();
|
||||
|
||||
// Newest first: the jump event (political / neutral) sits on top, and the
|
||||
// action event (military / success) is second — both with their outcome badges.
|
||||
await expect(page.getByTestId('event-item').first().getByTestId('event-type')).toHaveText('political');
|
||||
await expect(page.getByTestId('event-item').first().getByTestId('event-outcome')).toHaveText('neutral');
|
||||
// The pending queue is now empty — the round was committed and cleared.
|
||||
await expect(page.getByTestId('pending-empty')).toBeVisible();
|
||||
await expect(page.getByTestId('title-queued')).not.toBeVisible();
|
||||
|
||||
// Newest first: the diplomatic event (2nd queued item) sits on top, the
|
||||
// military event (1st queued item) is second.
|
||||
await expect(page.getByTestId('event-item').first().getByTestId('event-type')).toHaveText('diplomatic');
|
||||
await expect(page.getByTestId('event-item').first().getByTestId('event-outcome')).toHaveText('partial');
|
||||
await expect(page.getByTestId('event-item').nth(1).getByTestId('event-type')).toHaveText('military');
|
||||
await expect(page.getByTestId('event-item').nth(1).getByTestId('event-outcome')).toHaveText('success');
|
||||
});
|
||||
});
|
||||
@@ -91,7 +123,7 @@ test.describe('History — deterministic simulation (LLM_STUB=1)', () => {
|
||||
* localStorage, so a reload (or the home screen's "Resume last game" button)
|
||||
* must drop the user straight back into the same game — same turn, same stats,
|
||||
* same map — rather than resetting to the home screen. Deterministic under the
|
||||
* stub (UK military starts at 80 and +5 per turn).
|
||||
* stub (UK military starts at 80 and +5 per committed turn).
|
||||
*/
|
||||
test.describe('History — save / resume across a page refresh', () => {
|
||||
async function createUkGameAtTurn2(page: Page): Promise<void> {
|
||||
@@ -108,9 +140,12 @@ test.describe('History — save / resume across a page refresh', () => {
|
||||
await expect(page.getByTestId('turn-count')).toHaveText('1');
|
||||
await expect(page.getByTestId('stat-military')).toHaveText('80');
|
||||
|
||||
// One action: the stub bumps UK military to 85 and advances to turn 2.
|
||||
// Queue one action onto the pending round, then jump forward: the stub
|
||||
// bumps UK military to 85 and advances to turn 2.
|
||||
await page.getByTestId('action-textarea').fill('Reinforce the Atlantic convoys');
|
||||
await page.getByTestId('action-submit').click();
|
||||
await page.getByTestId('add-action').click();
|
||||
await expect(page.getByTestId('pending-item')).toHaveCount(1);
|
||||
await page.getByTestId('jump-forward').click();
|
||||
await expect(page.getByTestId('turn-count')).toHaveText('2', { timeout: 20000 });
|
||||
await expect(page.getByTestId('stat-military')).toHaveText('85', { timeout: 20000 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user