forked from AztecProtocol/aztec-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpod_racing.nr
More file actions
385 lines (316 loc) · 14.8 KB
/
Copy pathpod_racing.nr
File metadata and controls
385 lines (316 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use crate::test::{utils, helpers};
use ::aztec::protocol::storage::map::derive_storage_slot_in_map;
use crate::PodRacing;
// ============================================================================
// INITIALIZATION TESTS
// ============================================================================
// Test: Contract initialization sets admin correctly
#[test]
unconstrained fn test_initializer() {
let (mut env, contract_address, admin) = utils::setup();
env.public_context_at(contract_address, |context| {
let current_admin = context.storage_read(PodRacing::storage_layout().admin.slot);
assert_eq(current_admin, admin);
});
}
// ============================================================================
// GAME CREATION TESTS
// ============================================================================
// Test: Creating a new game initializes Race struct correctly
#[test]
unconstrained fn test_create_game() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_1;
// Player 1 creates a game
env.call_public(player1, PodRacing::at(contract_address).create_game(game_id));
// Verify game was created with player1 set
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
// The race struct starts with player1 address as first field
let stored_player1 = context.storage_read(race_slot);
assert_eq(stored_player1, player1);
});
}
// Test: Cannot create a game with duplicate game_id
#[test(should_fail)]
unconstrained fn test_fail_create_duplicate_game() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_1;
// Create game once
env.call_public(player1, PodRacing::at(contract_address).create_game(game_id));
// Try to create the same game_id again (should fail)
env.call_public(player1, PodRacing::at(contract_address).create_game(game_id));
}
// ============================================================================
// GAME JOINING TESTS
// ============================================================================
// Test: Second player can join an existing game
#[test]
unconstrained fn test_join_game() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_2;
// Player 1 creates game
env.call_public(player1, PodRacing::at(contract_address).create_game(game_id));
// Player 2 joins
env.call_public(player2, PodRacing::at(contract_address).join_game(game_id));
// Verify both players are set
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
let stored_player1 = context.storage_read(race_slot);
// player2 is stored at offset 1 in the Race struct
let stored_player2 = context.storage_read(race_slot + 1);
assert_eq(stored_player1, player1);
assert_eq(stored_player2, player2);
});
}
// Test: Cannot join a game that doesn't exist
#[test(should_fail)]
unconstrained fn test_fail_join_nonexistent_game() {
let (mut env, contract_address, _) = utils::setup();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_2;
// Try to join without game being created (should fail)
env.call_public(player2, PodRacing::at(contract_address).join_game(game_id));
}
// Test: Cannot have the same player join as player1 and player2
#[test(should_fail)]
unconstrained fn test_fail_same_player_joins_twice() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_2;
env.call_public(player1, PodRacing::at(contract_address).create_game(game_id));
// Try to join own game (should fail - player2 must be different)
env.call_public(player1, PodRacing::at(contract_address).join_game(game_id));
}
// ============================================================================
// ROUND PLAYING TESTS
// ============================================================================
// Test: Playing a round creates private notes and updates round counter
#[test]
unconstrained fn test_play_round() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_3;
// Setup game
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Player 1 plays round 1 with balanced allocation
let allocation = helpers::balanced_allocation();
helpers::play_round_with_allocation(&mut env, contract_address, player1, game_id, 1, allocation);
// Verify player1's round counter was incremented
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
// player1_round is at offset 3 in Race struct
let player1_round = context.storage_read(race_slot + 3);
assert_eq(player1_round, 1);
});
}
// Test: Cannot allocate more than 9 points in a round
#[test(should_fail)]
unconstrained fn test_fail_play_round_too_many_points() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_4;
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Try to allocate 10 points (2+2+2+2+2) - should fail
env.call_private(
player1,
PodRacing::at(contract_address).play_round(game_id, 1, 2, 2, 2, 2, 2)
);
}
// Test: Allocating exactly 9 points is allowed
#[test]
unconstrained fn test_play_round_with_max_points() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_4;
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Allocate exactly 9 points (3+3+3+0+0) - should succeed
let allocation = helpers::aggressive_allocation();
helpers::play_round_with_allocation(&mut env, contract_address, player1, game_id, 1, allocation);
// Verify round was played
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
let player1_round = context.storage_read(race_slot + 3);
assert_eq(player1_round, 1);
});
}
// Test: Allocating 0 points is allowed
#[test]
unconstrained fn test_play_round_with_zero_points() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_4;
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Allocate 0 points total - should succeed
env.call_private(
player1,
PodRacing::at(contract_address).play_round(game_id, 1, 0, 0, 0, 0, 0)
);
// Verify round was played
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
let player1_round = context.storage_read(race_slot + 3);
assert_eq(player1_round, 1);
});
}
// Test: Must play rounds sequentially
#[test(should_fail)]
unconstrained fn test_fail_skip_round() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_5;
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Try to play round 2 without playing round 1 first (should fail)
let allocation = helpers::balanced_allocation();
helpers::play_round_with_allocation(&mut env, contract_address, player1, game_id, 2, allocation);
}
// Test: Both players can play rounds independently
#[test]
unconstrained fn test_both_players_play_rounds() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_5;
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Player 1 plays round 1
helpers::play_round_with_allocation(
&mut env, contract_address, player1, game_id, 1, helpers::aggressive_allocation()
);
// Player 2 plays round 1 (with different strategy)
helpers::play_round_with_allocation(
&mut env, contract_address, player2, game_id, 1, helpers::defensive_allocation()
);
// Verify both players' round counters
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
let player1_round = context.storage_read(race_slot + 3);
let player2_round = context.storage_read(race_slot + 4);
assert_eq(player1_round, 1);
assert_eq(player2_round, 1);
});
}
// ============================================================================
// FULL GAME FLOW TESTS
// ============================================================================
// Test: Full game flow - create, join, play all rounds, finish
#[test]
unconstrained fn test_full_game_flow() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_6;
// Create and join game
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Player 1 plays all 3 rounds - focusing on first 3 tracks
let p1_strategy = [
(3, 2, 2, 1, 1), // Round 1
(2, 3, 2, 1, 1), // Round 2
(2, 2, 3, 1, 1), // Round 3
];
helpers::play_all_rounds_with_strategy(&mut env, contract_address, player1, game_id, p1_strategy);
// Player 2 plays all 3 rounds - focusing on last 2 tracks
let p2_strategy = [
(1, 1, 2, 2, 3), // Round 1
(1, 1, 2, 3, 2), // Round 2
(1, 1, 2, 2, 3), // Round 3
];
helpers::play_all_rounds_with_strategy(&mut env, contract_address, player2, game_id, p2_strategy);
// Both players finish to reveal their scores
env.call_private(player1, PodRacing::at(contract_address).finish_game(game_id));
env.call_private(player2, PodRacing::at(contract_address).finish_game(game_id));
// Verify the scores were stored correctly
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
// Player 1 expected totals: (7, 7, 7, 3, 3)
let p1_track1 = context.storage_read(race_slot + 5);
let p1_track2 = context.storage_read(race_slot + 6);
let p1_track3 = context.storage_read(race_slot + 7);
let p1_track4 = context.storage_read(race_slot + 8);
let p1_track5 = context.storage_read(race_slot + 9);
assert_eq(p1_track1, 7);
assert_eq(p1_track2, 7);
assert_eq(p1_track3, 7);
assert_eq(p1_track4, 3);
assert_eq(p1_track5, 3);
// Player 2 expected totals: (3, 3, 6, 7, 8)
let p2_track1 = context.storage_read(race_slot + 10);
let p2_track2 = context.storage_read(race_slot + 11);
let p2_track3 = context.storage_read(race_slot + 12);
let p2_track4 = context.storage_read(race_slot + 13);
let p2_track5 = context.storage_read(race_slot + 14);
assert_eq(p2_track1, 3);
assert_eq(p2_track2, 3);
assert_eq(p2_track3, 6);
assert_eq(p2_track4, 7);
assert_eq(p2_track5, 8);
});
}
// Test: All points on one track is allowed
#[test]
unconstrained fn test_all_points_on_one_track() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_7;
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Put all 9 points on track 1
env.call_private(
player1,
PodRacing::at(contract_address).play_round(game_id, 1, 9, 0, 0, 0, 0)
);
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
let player1_round = context.storage_read(race_slot + 3);
assert_eq(player1_round, 1);
});
}
// Test: Verify round counting for both players works correctly after bug fix
#[test]
unconstrained fn test_player2_sequential_rounds() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_8;
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Player 2 plays all rounds sequentially
for i in 0..3 {
let round = (i + 1) as u8;
helpers::play_round_with_allocation(
&mut env, contract_address, player2, game_id, round, helpers::balanced_allocation()
);
}
// Verify player2 completed all 3 rounds
env.public_context_at(contract_address, |context| {
let race_slot = derive_storage_slot_in_map(PodRacing::storage_layout().races.slot, game_id);
let player2_round = context.storage_read(race_slot + 4);
assert_eq(player2_round, 3);
});
}
// ============================================================================
// EDGE CASE TESTS
// ============================================================================
// Test: Cannot double-reveal scores
#[test(should_fail)]
unconstrained fn test_fail_double_reveal() {
let (mut env, contract_address, _) = utils::setup();
let player1 = env.create_light_account();
let player2 = env.create_light_account();
let game_id = helpers::TEST_GAME_ID_7;
helpers::setup_two_player_game(&mut env, contract_address, player1, player2, game_id);
// Play all rounds
let strategy = [(3, 2, 2, 1, 1), (2, 3, 2, 1, 1), (2, 2, 3, 1, 1)];
helpers::play_all_rounds_with_strategy(&mut env, contract_address, player1, game_id, strategy);
// Finish once
env.call_private(player1, PodRacing::at(contract_address).finish_game(game_id));
// Try to finish again (should fail)
env.call_private(player1, PodRacing::at(contract_address).finish_game(game_id));
}