|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { createGame } from '../../shared/engine/game-engine'; |
| 4 | +import { |
| 5 | + buildSolarSystemMap, |
| 6 | + findBaseHex, |
| 7 | + SCENARIOS, |
| 8 | +} from '../../shared/map-data'; |
| 9 | +import type { GameState } from '../../shared/types'; |
| 10 | +import { |
| 11 | + type ApplyClientGameStateDeps, |
| 12 | + applyClientGameState, |
| 13 | +} from './game-state-store'; |
| 14 | + |
| 15 | +const createState = (overrides: Partial<GameState> = {}): GameState => ({ |
| 16 | + ...createGame(SCENARIOS.duel, buildSolarSystemMap(), 'STORE1', findBaseHex), |
| 17 | + ...overrides, |
| 18 | +}); |
| 19 | + |
| 20 | +const createDeps = ( |
| 21 | + selectedShipId: string | null = null, |
| 22 | +): ApplyClientGameStateDeps & { |
| 23 | + rendererCalls: GameState[]; |
| 24 | +} => { |
| 25 | + const rendererCalls: GameState[] = []; |
| 26 | + |
| 27 | + return { |
| 28 | + ctx: { |
| 29 | + gameState: null, |
| 30 | + planningState: { |
| 31 | + selectedShipId, |
| 32 | + }, |
| 33 | + }, |
| 34 | + renderer: { |
| 35 | + setGameState: (state) => { |
| 36 | + rendererCalls.push(state); |
| 37 | + }, |
| 38 | + }, |
| 39 | + rendererCalls, |
| 40 | + }; |
| 41 | +}; |
| 42 | + |
| 43 | +describe('applyClientGameState', () => { |
| 44 | + it('stores the game state and updates the renderer', () => { |
| 45 | + const state = createState(); |
| 46 | + const deps = createDeps(); |
| 47 | + |
| 48 | + applyClientGameState(deps, state); |
| 49 | + |
| 50 | + expect(deps.ctx.gameState).toBe(state); |
| 51 | + expect(deps.rendererCalls).toEqual([state]); |
| 52 | + }); |
| 53 | + |
| 54 | + it('keeps the selected ship when it still exists and is alive', () => { |
| 55 | + const state = createState(); |
| 56 | + const selectedShipId = state.ships[0]?.id ?? null; |
| 57 | + const deps = createDeps(selectedShipId); |
| 58 | + |
| 59 | + applyClientGameState(deps, state); |
| 60 | + |
| 61 | + expect(deps.ctx.planningState.selectedShipId).toBe(selectedShipId); |
| 62 | + }); |
| 63 | + |
| 64 | + it('clears the selected ship when it no longer exists', () => { |
| 65 | + const state = createState(); |
| 66 | + const deps = createDeps('missing-ship'); |
| 67 | + |
| 68 | + applyClientGameState(deps, state); |
| 69 | + |
| 70 | + expect(deps.ctx.planningState.selectedShipId).toBeNull(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('clears the selected ship when it was destroyed', () => { |
| 74 | + const state = createState({ |
| 75 | + ships: createState().ships.map((ship, index) => |
| 76 | + index === 0 ? { ...ship, destroyed: true } : ship, |
| 77 | + ), |
| 78 | + }); |
| 79 | + const destroyedShipId = state.ships[0]?.id ?? null; |
| 80 | + const deps = createDeps(destroyedShipId); |
| 81 | + |
| 82 | + applyClientGameState(deps, state); |
| 83 | + |
| 84 | + expect(deps.ctx.planningState.selectedShipId).toBeNull(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments