Fix save/resume after refresh + map basemap

Map (basemap was rendering black):
- Inline MapLibre style (dark-navy background + subtle OpenFreeMap ne2sr
  raster) instead of the remote "dark" style, whose vector tiles are empty at
  world zoom and gated the choropleth on a remote styledata fetch.
- Explicit prod worker URL (maplibre-gl-worker via ?url) so GeoJSON sources
  process in the production bundle.
- Hardened the choropleth against geo load-order races (empty sources up
  front, ownership stamped once data lands) and bumped fill opacity so the
  vivid per-nation colors are the star on the non-black background.

Save/resume (a refresh used to drop users to the home screen):
- Persist the active game id in localStorage (history.activeGameId) on create
  and resume; clear it when explicitly starting a new game.
- Auto-resume the last game on a page refresh (GET /api/games/:id + /map),
  re-attaching the SSE stream; fall back to home gracefully if it's gone.
- Home screen: prominent "Resume last game" button + a "Saved games" section
  (newest first). New GET /api/games list endpoint.

e2e: two new deterministic tests — auto-resume-on-reload and explicit resume
from the home screen (both under LLM_STUB=1).

Quality gates: typecheck, lint, 21 unit tests, 3 e2e tests — all pass.
This commit is contained in:
2026-10-11 07:18:11 +00:00
parent 913d094e11
commit 2ed1b15318
6 changed files with 380 additions and 32 deletions
+61 -1
View File
@@ -1,4 +1,4 @@
import { test, expect } from '@playwright/test';
import { test, expect, type Page } from '@playwright/test';
/**
* Fully deterministic end-to-end test. The Playwright webServer boots the real
@@ -85,3 +85,63 @@ test.describe('History — deterministic simulation (LLM_STUB=1)', () => {
await expect(page.getByTestId('event-item').nth(1).getByTestId('event-outcome')).toHaveText('success');
});
});
/**
* Save / resume across a page refresh. The active game's id is persisted in
* 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).
*/
test.describe('History — save / resume across a page refresh', () => {
async function createUkGameAtTurn2(page: Page): Promise<void> {
await page.goto('/');
await page.getByTestId('preset-card').first().waitFor({ timeout: 15000 });
await page.getByTestId('preset-card').first().getByRole('button', { name: 'Play' }).click();
await page
.getByTestId('nation-card-uk')
.getByRole('button', { name: 'Play as United Kingdom' })
.click();
// Game screen mounts at turn 1 (UK military starts at 80).
await expect(page.getByTestId('world-map')).toBeVisible();
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.
await page.getByTestId('action-textarea').fill('Reinforce the Atlantic convoys');
await page.getByTestId('action-submit').click();
await expect(page.getByTestId('turn-count')).toHaveText('2', { timeout: 20000 });
await expect(page.getByTestId('stat-military')).toHaveText('85', { timeout: 20000 });
}
test('auto-resumes into the same game after a page reload', async ({ page }) => {
await createUkGameAtTurn2(page);
// Reload: the persisted active game id must restore the game — not home.
await page.reload();
await expect(page.getByTestId('world-map')).toBeVisible();
await expect(page.getByTestId('turn-count')).toHaveText('2', { timeout: 20000 });
await expect(page.getByTestId('stat-military')).toHaveText('85', { timeout: 20000 });
// The player's identity is restored in the header…
await expect(page.getByTestId('player-nation')).toHaveText('United Kingdom');
// …and the map legend is regenerated from the persisted region map.
await expect(page.getByTestId('map-legend-item').first()).toBeVisible({ timeout: 15000 });
});
test('explicitly resumes a saved game from the home screen', async ({ page }) => {
await createUkGameAtTurn2(page);
// Back to the home screen: the "Resume last game" affordance is present.
await page.getByTestId('back-to-scenarios').click();
await expect(page.getByTestId('resume-last')).toBeVisible();
// Resuming drops back into the same game, at the saved turn.
await page.getByTestId('resume-last').click();
await expect(page.getByTestId('world-map')).toBeVisible();
await expect(page.getByTestId('turn-count')).toHaveText('2', { timeout: 20000 });
await expect(page.getByTestId('stat-military')).toHaveText('85', { timeout: 20000 });
await expect(page.getByTestId('player-nation')).toHaveText('United Kingdom');
});
});