@@ -46,9 +46,9 @@ defmodule CopiWeb.GameLive.ShowTest do
4646
4747 test "successfully starts game with 3+ players" , % { conn: conn , game: game } do
4848 # Add 3 players
49- { :ok , _player1 } = Cornucopia . create_player ( % { name: "Player 1" , game_id: game . id } )
50- { :ok , _player2 } = Cornucopia . create_player ( % { name: "Player 2" , game_id: game . id } )
51- { :ok , _player3 } = Cornucopia . create_player ( % { name: "Player 3" , game_id: game . id } )
49+ { :ok , player1 } = Cornucopia . create_player ( % { name: "Player 1" , game_id: game . id } )
50+ { :ok , player2 } = Cornucopia . create_player ( % { name: "Player 2" , game_id: game . id } )
51+ { :ok , player3 } = Cornucopia . create_player ( % { name: "Player 3" , game_id: game . id } )
5252
5353 { :ok , view , html } = live ( conn , "/games/#{ game . id } " )
5454
@@ -61,6 +61,20 @@ defmodule CopiWeb.GameLive.ShowTest do
6161 # Verify game was started
6262 { :ok , updated_game } = Cornucopia.Game . find ( game . id )
6363 assert updated_game . started_at != nil
64+
65+ # Verify cards were dealt to all players via transaction
66+ player1_cards = Cornucopia . list_player_cards ( player1 . id )
67+ player2_cards = Cornucopia . list_player_cards ( player2 . id )
68+ player3_cards = Cornucopia . list_player_cards ( player3 . id )
69+
70+ # Each player should have cards
71+ assert length ( player1_cards ) > 0
72+ assert length ( player2_cards ) > 0
73+ assert length ( player3_cards ) > 0
74+
75+ # Total cards should equal 52 (standard deck) distributed evenly
76+ total_cards = length ( player1_cards ) + length ( player2_cards ) + length ( player3_cards )
77+ assert total_cards == 52
6478 end
6579
6680 test "does not restart an already started game" , % { conn: conn , game: game } do
@@ -85,5 +99,68 @@ defmodule CopiWeb.GameLive.ShowTest do
8599 { :ok , updated_game } = Cornucopia.Game . find ( started_game . id )
86100 assert DateTime . compare ( updated_game . started_at , original_time ) == :eq
87101 end
102+
103+ test "transaction ensures atomic card dealing with 4 players" , % { conn: conn , game: game } do
104+ # Add 4 players to test round-robin distribution
105+ { :ok , player1 } = Cornucopia . create_player ( % { name: "Player 1" , game_id: game . id } )
106+ { :ok , player2 } = Cornucopia . create_player ( % { name: "Player 2" , game_id: game . id } )
107+ { :ok , player3 } = Cornucopia . create_player ( % { name: "Player 3" , game_id: game . id } )
108+ { :ok , player4 } = Cornucopia . create_player ( % { name: "Player 4" , game_id: game . id } )
109+
110+ { :ok , view , _html } = live ( conn , "/games/#{ game . id } " )
111+
112+ # Start game
113+ render_click ( view , "start_game" , % { } )
114+
115+ # Verify game started
116+ { :ok , updated_game } = Cornucopia.Game . find ( game . id )
117+ assert updated_game . started_at != nil
118+
119+ # Verify all 52 cards were dealt atomically
120+ player1_cards = Cornucopia . list_player_cards ( player1 . id )
121+ player2_cards = Cornucopia . list_player_cards ( player2 . id )
122+ player3_cards = Cornucopia . list_player_cards ( player3 . id )
123+ player4_cards = Cornucopia . list_player_cards ( player4 . id )
124+
125+ # Each player should get 13 cards (52 / 4)
126+ assert length ( player1_cards ) == 13
127+ assert length ( player2_cards ) == 13
128+ assert length ( player3_cards ) == 13
129+ assert length ( player4_cards ) == 13
130+
131+ # Verify game update broadcast happened (started_at is set)
132+ assert updated_game . started_at != nil
133+ end
134+
135+ test "transaction with 5 players distributes cards evenly" , % { conn: conn , game: game } do
136+ # Add 5 players
137+ { :ok , player1 } = Cornucopia . create_player ( % { name: "Player 1" , game_id: game . id } )
138+ { :ok , player2 } = Cornucopia . create_player ( % { name: "Player 2" , game_id: game . id } )
139+ { :ok , player3 } = Cornucopia . create_player ( % { name: "Player 3" , game_id: game . id } )
140+ { :ok , player4 } = Cornucopia . create_player ( % { name: "Player 4" , game_id: game . id } )
141+ { :ok , player5 } = Cornucopia . create_player ( % { name: "Player 5" , game_id: game . id } )
142+
143+ { :ok , view , _html } = live ( conn , "/games/#{ game . id } " )
144+ render_click ( view , "start_game" , % { } )
145+
146+ # Verify cards distributed via transaction
147+ player1_cards = Cornucopia . list_player_cards ( player1 . id )
148+ player2_cards = Cornucopia . list_player_cards ( player2 . id )
149+ player3_cards = Cornucopia . list_player_cards ( player3 . id )
150+ player4_cards = Cornucopia . list_player_cards ( player4 . id )
151+ player5_cards = Cornucopia . list_player_cards ( player5 . id )
152+
153+ # With 5 players, first 2 players get 11 cards, last 3 get 10 (52 cards total)
154+ total = length ( player1_cards ) + length ( player2_cards ) + length ( player3_cards ) +
155+ length ( player4_cards ) + length ( player5_cards )
156+ assert total == 52
157+
158+ # All players should have at least 10 cards
159+ assert length ( player1_cards ) >= 10
160+ assert length ( player2_cards ) >= 10
161+ assert length ( player3_cards ) >= 10
162+ assert length ( player4_cards ) >= 10
163+ assert length ( player5_cards ) >= 10
164+ end
88165 end
89166end
0 commit comments