|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { buildSolarSystemMap, findBaseHex, SCENARIOS } from '../map-data'; |
| 3 | +import type { EngineEvent } from './engine-events'; |
| 4 | +import { |
| 5 | + createGame, |
| 6 | + processAstrogation, |
| 7 | + processOrdnance, |
| 8 | + skipCombat, |
| 9 | +} from './game-engine'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Verify that all non-deterministic game outcomes are |
| 13 | + * captured as explicit facts in the emitted EngineEvents, |
| 14 | + * so event-sourced replay does not depend on re-running |
| 15 | + * the same Math.random() sequence. |
| 16 | + */ |
| 17 | + |
| 18 | +const map = buildSolarSystemMap(); |
| 19 | + |
| 20 | +const collectDiceEvents = (events: EngineEvent[]) => ({ |
| 21 | + combatRolls: events |
| 22 | + .filter((e) => e.type === 'combatAttack') |
| 23 | + .map((e) => (e as { roll: number; modifiedRoll: number }).roll), |
| 24 | + rammingRolls: events |
| 25 | + .filter((e) => e.type === 'ramming') |
| 26 | + .map((e) => (e as { roll: number }).roll), |
| 27 | + ordnanceRolls: events |
| 28 | + .filter((e) => e.type === 'ordnanceDetonated') |
| 29 | + .map((e) => (e as { roll: number }).roll), |
| 30 | +}); |
| 31 | + |
| 32 | +describe('RNG outcome capture in EngineEvents', () => { |
| 33 | + it('combat events capture die roll and modified roll', () => { |
| 34 | + const state = createGame(SCENARIOS.duel, map, 'RNG1', findBaseHex); |
| 35 | + |
| 36 | + // Place ships adjacent with zero velocity for combat |
| 37 | + state.phase = 'combat'; |
| 38 | + state.activePlayer = 0; |
| 39 | + state.ships[0].position = { q: 10, r: 10 }; |
| 40 | + state.ships[1].position = { q: 10, r: 11 }; |
| 41 | + state.ships[0].velocity = { dq: 0, dr: 0 }; |
| 42 | + state.ships[1].velocity = { dq: 0, dr: 0 }; |
| 43 | + |
| 44 | + // skipCombat auto-resolves any combats in range |
| 45 | + const result = skipCombat(state, 0, map, () => 0.5); |
| 46 | + |
| 47 | + if ('error' in result) return; |
| 48 | + |
| 49 | + const { combatRolls } = collectDiceEvents(result.engineEvents); |
| 50 | + |
| 51 | + // If combat occurred, rolls should be captured |
| 52 | + for (const roll of combatRolls) { |
| 53 | + expect(roll).toBeGreaterThanOrEqual(1); |
| 54 | + expect(roll).toBeLessThanOrEqual(6); |
| 55 | + } |
| 56 | + }); |
| 57 | + |
| 58 | + it('ramming events capture die roll', () => { |
| 59 | + const state = createGame(SCENARIOS.biplanetary, map, 'RNG2', findBaseHex); |
| 60 | + |
| 61 | + // Place two ships on the same hex to trigger ramming |
| 62 | + // during movement resolution |
| 63 | + state.phase = 'astrogation'; |
| 64 | + state.activePlayer = 0; |
| 65 | + |
| 66 | + // Position enemy ship where our ship will land |
| 67 | + const targetHex = { q: 5, r: 10 }; |
| 68 | + state.ships[0].position = { q: 4, r: 10 }; |
| 69 | + state.ships[0].velocity = { dq: 1, dr: 0 }; |
| 70 | + state.ships[1].position = targetHex; |
| 71 | + state.ships[1].velocity = { dq: 0, dr: 0 }; |
| 72 | + |
| 73 | + const result = processAstrogation( |
| 74 | + state, |
| 75 | + 0, |
| 76 | + [{ shipId: state.ships[0].id, burn: null }], |
| 77 | + map, |
| 78 | + () => 0.5, |
| 79 | + ); |
| 80 | + |
| 81 | + if ('error' in result) return; |
| 82 | + |
| 83 | + const { rammingRolls } = collectDiceEvents(result.engineEvents); |
| 84 | + |
| 85 | + // If ramming occurred, the roll should be captured |
| 86 | + if (rammingRolls.length > 0) { |
| 87 | + for (const roll of rammingRolls) { |
| 88 | + expect(roll).toBeGreaterThanOrEqual(1); |
| 89 | + expect(roll).toBeLessThanOrEqual(6); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('ordnance detonation events capture die roll', () => { |
| 95 | + const state = createGame(SCENARIOS.duel, map, 'RNG3', findBaseHex); |
| 96 | + |
| 97 | + // Place a mine and move enemy through it |
| 98 | + state.phase = 'ordnance'; |
| 99 | + state.activePlayer = 0; |
| 100 | + |
| 101 | + const result = processOrdnance( |
| 102 | + state, |
| 103 | + 0, |
| 104 | + [ |
| 105 | + { |
| 106 | + shipId: state.ships[0].id, |
| 107 | + ordnanceType: 'mine', |
| 108 | + }, |
| 109 | + ], |
| 110 | + map, |
| 111 | + () => 0.5, |
| 112 | + ); |
| 113 | + |
| 114 | + if ('error' in result) return; |
| 115 | + |
| 116 | + // Mine was launched — check the events |
| 117 | + const launchEvents = result.engineEvents.filter( |
| 118 | + (e) => e.type === 'ordnanceLaunched', |
| 119 | + ); |
| 120 | + |
| 121 | + // Mine launch should be captured |
| 122 | + if (launchEvents.length > 0) { |
| 123 | + expect(launchEvents[0].type).toBe('ordnanceLaunched'); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + it('all combat attack events have roll and modifiedRoll fields', () => { |
| 128 | + const state = createGame(SCENARIOS.duel, map, 'RNG4', findBaseHex); |
| 129 | + |
| 130 | + state.phase = 'combat'; |
| 131 | + state.activePlayer = 0; |
| 132 | + state.ships[0].position = { q: 10, r: 10 }; |
| 133 | + state.ships[1].position = { q: 11, r: 10 }; |
| 134 | + state.ships[0].velocity = { dq: 0, dr: 0 }; |
| 135 | + state.ships[1].velocity = { dq: 0, dr: 0 }; |
| 136 | + |
| 137 | + const result = skipCombat(state, 0, map, () => 0.5); |
| 138 | + |
| 139 | + if ('error' in result) return; |
| 140 | + |
| 141 | + const combatEvents = result.engineEvents.filter( |
| 142 | + (e) => e.type === 'combatAttack', |
| 143 | + ); |
| 144 | + |
| 145 | + for (const event of combatEvents) { |
| 146 | + const ce = event as { |
| 147 | + roll: number; |
| 148 | + modifiedRoll: number; |
| 149 | + }; |
| 150 | + expect(typeof ce.roll).toBe('number'); |
| 151 | + expect(typeof ce.modifiedRoll).toBe('number'); |
| 152 | + expect(ce.roll).toBeGreaterThanOrEqual(1); |
| 153 | + expect(ce.roll).toBeLessThanOrEqual(6); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + it('fugitiveDesignated event type is defined', () => { |
| 158 | + // Verify the event type compiles |
| 159 | + const event: EngineEvent = { |
| 160 | + type: 'fugitiveDesignated', |
| 161 | + shipId: 'p0s0', |
| 162 | + playerId: 0, |
| 163 | + }; |
| 164 | + |
| 165 | + expect(event.type).toBe('fugitiveDesignated'); |
| 166 | + }); |
| 167 | +}); |
0 commit comments