Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

game-state.test.ts

246 lines8.3 KB

1import { beforeEach, describe, expect, it } from "vitest"
2import { GameState } from "../../../shared/game-state"
3import { HORIZ_COUNT, W, getBoxEdges } from "../../../shared/edge-math"
4​
5describe(`GameState`, () => {
6 let state: GameState
7​
8 beforeEach(() => {
9 state = new GameState()
10 })
11​
12 describe(`initial state`, () => {
13 it(`starts with no edges taken`, () => {
14 expect(state.isEdgeTaken(0)).toBe(false)
15 expect(state.isEdgeTaken(HORIZ_COUNT)).toBe(false)
16 expect(state.getEdgesPlacedCount()).toBe(0)
17 })
18​
19 it(`starts with all boxes unclaimed`, () => {
20 expect(state.getBoxOwner(0)).toBe(0)
21 expect(state.getBoxOwner(500000)).toBe(0)
22 })
23​
24 it(`starts with all scores at zero`, () => {
25 expect(state.getScores()).toEqual([0, 0, 0, 0])
26 expect(state.getScore(0)).toBe(0)
27 expect(state.getScore(1)).toBe(0)
28 expect(state.getScore(2)).toBe(0)
29 expect(state.getScore(3)).toBe(0)
30 })
31​
32 it(`is not complete`, () => {
33 expect(state.isComplete()).toBe(false)
34 })
35​
36 it(`has no winner`, () => {
37 expect(state.getWinner()).toBe(null)
38 })
39 })
40​
41 describe(`isEdgeTaken`, () => {
42 it(`returns false for untaken edges`, () => {
43 expect(state.isEdgeTaken(12345)).toBe(false)
44 })
45​
46 it(`returns true after edge is placed`, () => {
47 state.applyEvent({ edgeId: 12345, teamId: 0 })
48 expect(state.isEdgeTaken(12345)).toBe(true)
49 })
50 })
51​
52 describe(`applyEvent`, () => {
53 it(`marks edge as taken`, () => {
54 state.applyEvent({ edgeId: 0, teamId: 0 })
55 expect(state.isEdgeTaken(0)).toBe(true)
56 expect(state.getEdgesPlacedCount()).toBe(1)
57 })
58​
59 it(`ignores duplicate edge placement`, () => {
60 state.applyEvent({ edgeId: 0, teamId: 0 })
61 state.applyEvent({ edgeId: 0, teamId: 1 }) // Should be ignored
62 expect(state.getEdgesPlacedCount()).toBe(1)
63 })
64​
65 it(`returns empty boxesClaimed for non-completing edge`, () => {
66 const result = state.applyEvent({ edgeId: 0, teamId: 0 })
67 expect(result.boxesClaimed).toEqual([])
68 })
69​
70 it(`claims box when fourth edge is placed`, () => {
71 // Box at (0,0) needs edges:
72 // top = h(0,0) = 0
73 // bottom = h(0,1) = W = 1000
74 // left = v(0,0) = HORIZ_COUNT
75 // right = v(1,0) = HORIZ_COUNT + 1
76 const [top, bottom, left, right] = getBoxEdges(0, 0)
77​
78 expect(top).toBe(0)
79 expect(bottom).toBe(W)
80 expect(left).toBe(HORIZ_COUNT)
81 expect(right).toBe(HORIZ_COUNT + 1)
82​
83 state.applyEvent({ edgeId: top, teamId: 0 })
84 state.applyEvent({ edgeId: bottom, teamId: 1 })
85 state.applyEvent({ edgeId: left, teamId: 2 })
86​
87 expect(state.getBoxOwner(0)).toBe(0) // Still unclaimed
88​
89 const result = state.applyEvent({ edgeId: right, teamId: 3 })
90​
91 expect(state.getBoxOwner(0)).toBe(4) // Claimed by team 3 (teamId + 1)
92 expect(state.getScore(3)).toBe(1)
93 expect(result.boxesClaimed).toEqual([0])
94 })
95​
96 it(`can claim two boxes with one edge`, () => {
97 // Edge at h(0, 1) borders box (0,0) and box (0,1)
98 // Complete both boxes except for the shared edge
99​
100 // Box (0,0): top, left, right
101 state.applyEvent({ edgeId: 0, teamId: 0 }) // top = h(0,0)
102 state.applyEvent({ edgeId: HORIZ_COUNT, teamId: 0 }) // left = v(0,0)
103 state.applyEvent({ edgeId: HORIZ_COUNT + 1, teamId: 0 }) // right = v(1,0)
104​
105 // Box (0,1): bottom, left, right
106 state.applyEvent({ edgeId: 2 * W, teamId: 1 }) // bottom = h(0,2)
107 state.applyEvent({ edgeId: HORIZ_COUNT + (W + 1), teamId: 1 }) // left = v(0,1)
108 state.applyEvent({ edgeId: HORIZ_COUNT + (W + 1) + 1, teamId: 1 }) // right = v(1,1)
109​
110 // Now place the shared edge - should claim both boxes
111 const result = state.applyEvent({ edgeId: W, teamId: 2 }) // h(0,1)
112​
113 expect(result.boxesClaimed).toHaveLength(2)
114 expect(result.boxesClaimed).toContain(0) // box (0,0)
115 expect(result.boxesClaimed).toContain(W) // box (0,1) = boxId 1000
116 expect(state.getScore(2)).toBe(2)
117 })
118 })
119​
120 describe(`getScores`, () => {
121 it(`returns copy of scores array`, () => {
122 const scores = state.getScores()
123 scores[0] = 999 // Mutate the copy
124 expect(state.getScore(0)).toBe(0) // Original unchanged
125 })
126 })
127​
128 describe(`isComplete`, () => {
129 it(`returns false when not all edges placed`, () => {
130 state.applyEvent({ edgeId: 0, teamId: 0 })
131 expect(state.isComplete()).toBe(false)
132 })
133​
134 // Note: Testing full completion would require placing 2M+ edges
135 // We test the logic indirectly through getEdgesPlacedCount
136 })
137​
138 describe(`getWinner`, () => {
139 it(`returns null for tie`, () => {
140 expect(state.getWinner()).toBe(null)
141 })
142​
143 it(`returns null when game is not complete`, () => {
144 // Complete a box for team 2
145 const [top, bottom, left, right] = getBoxEdges(0, 0)
146 state.applyEvent({ edgeId: top, teamId: 0 })
147 state.applyEvent({ edgeId: bottom, teamId: 1 })
148 state.applyEvent({ edgeId: left, teamId: 1 })
149 state.applyEvent({ edgeId: right, teamId: 2 })
150​
151 expect(state.getScores()).toEqual([0, 0, 1, 0])
152 expect(state.getWinner()).toBe(null)
153 })
154​
155 it(`returns null when multiple teams tied for highest`, () => {
156 // Complete box (0,0) for team 0
157 const box0edges = getBoxEdges(0, 0)
158 state.applyEvent({ edgeId: box0edges[0], teamId: 1 })
159 state.applyEvent({ edgeId: box0edges[1], teamId: 1 })
160 state.applyEvent({ edgeId: box0edges[2], teamId: 1 })
161 state.applyEvent({ edgeId: box0edges[3], teamId: 0 }) // Team 0 claims
162​
163 // Complete box (2,0) for team 1
164 const box1edges = getBoxEdges(2, 0)
165 state.applyEvent({ edgeId: box1edges[0], teamId: 0 })
166 state.applyEvent({ edgeId: box1edges[1], teamId: 0 })
167 state.applyEvent({ edgeId: box1edges[2], teamId: 0 })
168 state.applyEvent({ edgeId: box1edges[3], teamId: 1 }) // Team 1 claims
169​
170 expect(state.getScores()).toEqual([1, 1, 0, 0])
171 expect(state.getWinner()).toBe(null) // Tie
172 })
173 })
174​
175 describe(`export / import`, () => {
176 it(`exports current state`, () => {
177 state.applyEvent({ edgeId: 0, teamId: 0 })
178 state.applyEvent({ edgeId: 1, teamId: 1 })
179​
180 const exported = state.export()
181​
182 expect(exported.edgesPlaced).toBe(2)
183 expect(exported.scores).toEqual([0, 0, 0, 0])
184 expect(exported.edgeTaken).toBeInstanceOf(Uint8Array)
185 expect(exported.edgeOwner).toBeInstanceOf(Int8Array)
186 expect(exported.boxOwner).toBeInstanceOf(Uint8Array)
187 })
188​
189 it(`imports state correctly`, () => {
190 state.applyEvent({ edgeId: 0, teamId: 0 })
191 state.applyEvent({ edgeId: 1, teamId: 1 })
192​
193 const exported = state.export()
194 const imported = GameState.import(exported)
195​
196 expect(imported.isEdgeTaken(0)).toBe(true)
197 expect(imported.isEdgeTaken(1)).toBe(true)
198 expect(imported.isEdgeTaken(2)).toBe(false)
199 expect(imported.getEdgeOwner(0)).toBe(0)
200 expect(imported.getEdgeOwner(1)).toBe(1)
201 expect(imported.getEdgesPlacedCount()).toBe(2)
202 expect(imported.getScores()).toEqual([0, 0, 0, 0])
203 })
204​
205 it(`export returns a copy, not a reference`, () => {
206 state.applyEvent({ edgeId: 0, teamId: 0 })
207 const exported = state.export()
208​
209 // Mutate exported data
210 exported.edgeTaken[0] = 0
211​
212 // Original state should be unchanged
213 expect(state.isEdgeTaken(0)).toBe(true)
214 })
215 })
216​
217 describe(`complete box scenarios`, () => {
218 it(`completing box in center of grid`, () => {
219 const [top, bottom, left, right] = getBoxEdges(500, 500)
220​
221 state.applyEvent({ edgeId: top, teamId: 0 })
222 state.applyEvent({ edgeId: bottom, teamId: 1 })
223 state.applyEvent({ edgeId: left, teamId: 2 })
224​
225 expect(state.getBoxOwner(500 * W + 500)).toBe(0) // Unclaimed
226​
227 state.applyEvent({ edgeId: right, teamId: 3 })
228​
229 const boxId = 500 * W + 500
230 expect(state.getBoxOwner(boxId)).toBe(4) // Team 3
231 expect(state.getScore(3)).toBe(1)
232 })
233​
234 it(`completing corner box (999, 999)`, () => {
235 const [top, bottom, left, right] = getBoxEdges(999, 999)
236​
237 state.applyEvent({ edgeId: top, teamId: 0 })
238 state.applyEvent({ edgeId: bottom, teamId: 0 })
239 state.applyEvent({ edgeId: left, teamId: 0 })
240 state.applyEvent({ edgeId: right, teamId: 1 })
241​
242 const boxId = 999 * W + 999
243 expect(state.getBoxOwner(boxId)).toBe(2) // Team 1
244 })
245 })
246})