|
| 1 | +defmodule CopiWeb.PlayerLive.ShowTest do |
| 2 | + use CopiWeb.ConnCase, async: false |
| 3 | + |
| 4 | + import Phoenix.LiveViewTest |
| 5 | + |
| 6 | + alias Copi.Cornucopia |
| 7 | + |
| 8 | + @game_attrs %{name: "show test game"} |
| 9 | + |
| 10 | + defp create_player(_) do |
| 11 | + {:ok, game} = Cornucopia.create_game(@game_attrs) |
| 12 | + {:ok, player} = Cornucopia.create_player(%{name: "Player 1", game_id: game.id}) |
| 13 | + %{player: player} |
| 14 | + end |
| 15 | + |
| 16 | + describe "Show - additional coverage" do |
| 17 | + setup [:create_player] |
| 18 | + |
| 19 | + test "handle_info :proceed_to_next_round advances rounds_played", %{conn: conn, player: player} do |
| 20 | + game_id = player.game_id |
| 21 | + {:ok, game} = Cornucopia.Game.find(game_id) |
| 22 | + |
| 23 | + Copi.Repo.update!( |
| 24 | + Ecto.Changeset.change(game, started_at: DateTime.truncate(DateTime.utc_now(), :second)) |
| 25 | + ) |
| 26 | + |
| 27 | + {:ok, show_live, _html} = live(conn, "/games/#{game_id}/players/#{player.id}") |
| 28 | + |
| 29 | + send(show_live.pid, :proceed_to_next_round) |
| 30 | + :timer.sleep(100) |
| 31 | + |
| 32 | + {:ok, updated_game} = Cornucopia.Game.find(game_id) |
| 33 | + assert updated_game.rounds_played == 1 |
| 34 | + end |
| 35 | + |
| 36 | + test "handle_info :proceed_to_next_round sets finished_at on last round", %{conn: conn, player: player} do |
| 37 | + game_id = player.game_id |
| 38 | + {:ok, game} = Cornucopia.Game.find(game_id) |
| 39 | + |
| 40 | + Copi.Repo.update!( |
| 41 | + Ecto.Changeset.change(game, started_at: DateTime.truncate(DateTime.utc_now(), :second)) |
| 42 | + ) |
| 43 | + |
| 44 | + # A played card with no nil-round cards remaining → last_round? returns true |
| 45 | + {:ok, card} = |
| 46 | + Cornucopia.create_card(%{ |
| 47 | + category: "C", value: "V", description: "D", edition: "webapp", |
| 48 | + version: "2.2", external_id: "ST1", language: "en", misc: "misc", |
| 49 | + owasp_scp: [], owasp_devguide: [], owasp_asvs: [], owasp_appsensor: [], |
| 50 | + capec: [], safecode: [], owasp_mastg: [], owasp_masvs: [] |
| 51 | + }) |
| 52 | + |
| 53 | + Copi.Repo.insert!(%Copi.Cornucopia.DealtCard{ |
| 54 | + player_id: player.id, card_id: card.id, played_in_round: 1 |
| 55 | + }) |
| 56 | + |
| 57 | + {:ok, show_live, _html} = live(conn, "/games/#{game_id}/players/#{player.id}") |
| 58 | + |
| 59 | + send(show_live.pid, :proceed_to_next_round) |
| 60 | + :timer.sleep(100) |
| 61 | + |
| 62 | + {:ok, updated_game} = Cornucopia.Game.find(game_id) |
| 63 | + assert updated_game.finished_at != nil |
| 64 | + end |
| 65 | + |
| 66 | + test "next_round is no-op when round is open and cannot continue", %{conn: conn, player: player} do |
| 67 | + game_id = player.game_id |
| 68 | + {:ok, game} = Cornucopia.Game.find(game_id) |
| 69 | + |
| 70 | + # Start game but add no dealt cards and no continue votes → round_open? true, can_continue? false |
| 71 | + Copi.Repo.update!( |
| 72 | + Ecto.Changeset.change(game, started_at: DateTime.truncate(DateTime.utc_now(), :second)) |
| 73 | + ) |
| 74 | + |
| 75 | + {:ok, show_live, _html} = live(conn, "/games/#{game_id}/players/#{player.id}") |
| 76 | + html = render_click(show_live, "next_round", %{}) |
| 77 | + assert is_binary(html) |
| 78 | + |
| 79 | + {:ok, unchanged_game} = Cornucopia.Game.find(game_id) |
| 80 | + assert unchanged_game.rounds_played == 0 |
| 81 | + end |
| 82 | + |
| 83 | + test "next_round proceeds when majority continue votes reached", %{conn: conn, player: player} do |
| 84 | + game_id = player.game_id |
| 85 | + {:ok, game} = Cornucopia.Game.find(game_id) |
| 86 | + |
| 87 | + Copi.Repo.update!( |
| 88 | + Ecto.Changeset.change(game, started_at: DateTime.truncate(DateTime.utc_now(), :second)) |
| 89 | + ) |
| 90 | + |
| 91 | + # 1 player + 1 continue vote → majority reached (1 > div(1,2) = 0), round_open? true |
| 92 | + Copi.Repo.insert!(%Copi.Cornucopia.ContinueVote{player_id: player.id, game_id: game_id}) |
| 93 | + |
| 94 | + {:ok, show_live, _html} = live(conn, "/games/#{game_id}/players/#{player.id}") |
| 95 | + render_click(show_live, "next_round", %{}) |
| 96 | + |
| 97 | + # Wait for the async :proceed_to_next_round message (100ms delay + buffer) |
| 98 | + :timer.sleep(300) |
| 99 | + |
| 100 | + {:ok, updated_game} = Cornucopia.Game.find(game_id) |
| 101 | + assert updated_game.rounds_played == 1 |
| 102 | + end |
| 103 | + |
| 104 | + test "helper functions return expected values", %{conn: _conn, player: _player} do |
| 105 | + alias CopiWeb.PlayerLive.Show |
| 106 | + |
| 107 | + assert Show.ordered_cards([]) == [] |
| 108 | + assert Show.unplayed_cards([]) == [] |
| 109 | + assert Show.played_cards([], 1) == [] |
| 110 | + assert Show.card_played_in_round([], 1) == nil |
| 111 | + # With no players, no one is still to play → round_open? is false → round_closed? is true |
| 112 | + assert Show.round_closed?(%{players: [], rounds_played: 0}) == true |
| 113 | + |
| 114 | + assert Show.display_game_session("webapp") == "Cornucopia Web Session:" |
| 115 | + assert Show.display_game_session("ecommerce") == "Cornucopia Web Session:" |
| 116 | + assert Show.display_game_session("mobileapp") == "Cornucopia Mobile Session:" |
| 117 | + assert Show.display_game_session("masvs") == "Cornucopia Mobile Session:" |
| 118 | + assert Show.display_game_session("cumulus") == "OWASP Cumulus Session:" |
| 119 | + assert Show.display_game_session("mlsec") == "Elevation of MLSec Session:" |
| 120 | + assert Show.display_game_session("eop") == "EoP Session:" |
| 121 | + end |
| 122 | + end |
| 123 | +end |
0 commit comments