Files
2026-09-29 00:56:28 +00:00

30 lines
1.6 KiB
TypeScript

import { expect, type Page } from '@playwright/test';
// Shared e2e helpers — kept out of the specs so both files can reuse them.
interface WorldSystemDto { id: number; name: string; x: number; y: number; discovered: boolean; current: boolean }
interface WorldState { systems: WorldSystemDto[]; edges: [number, number][]; turn: number }
// Jump to an adjacent system, preferring one that is still undiscovered. Every jump into an
// undiscovered system generates 4 fresh neighbours (lib/game/world.ts), so fresh territory is
// always available — picking the FIRST edge instead ping-pongs between exhausted systems, because
// edges come back in insertion order and the oldest route out of a system is usually the one that
// led here. Falls back to any non-current adjacent system if none is undiscovered.
export async function jumpToFreshSystem(page: Page): Promise<WorldState> {
const world = (await (await page.request.get('/api/world')).json()) as WorldState;
const currentId = world.systems.find((s) => s.current)!.id;
const adjacentIds = new Set<number>();
for (const [a, b] of world.edges) {
if (a === currentId) adjacentIds.add(b);
else if (b === currentId) adjacentIds.add(a);
}
const candidates = [...adjacentIds].map((id) => world.systems.find((s) => s.id === id)!);
const target = candidates.find((s) => !s.discovered) ?? candidates[0];
expect(target).toBeDefined(); // every system has at least one route out (ring generation)
const res = await page.request.post('/api/jump', { data: { systemId: target.id } });
expect(res.status()).toBe(200);
return (await res.json()) as WorldState;
}