From d62359b0d37c82c06e2f7c898e559623b00ef57e Mon Sep 17 00:00:00 2001 From: Denny Date: Tue, 19 Nov 2024 21:32:26 +0530 Subject: [PATCH] feat: add test for changes to room --- lib/viral_spiral/entity/room.ex | 27 ++++++------ test/viral_spiral/entity/room_test.exs | 44 +++++++++++++++++++ test/viral_spiral/room/room_test.exs | 60 -------------------------- 3 files changed, 57 insertions(+), 74 deletions(-) create mode 100644 test/viral_spiral/entity/room_test.exs delete mode 100644 test/viral_spiral/room/room_test.exs diff --git a/lib/viral_spiral/entity/room.ex b/lib/viral_spiral/entity/room.ex index 699aa6d..28aa5a1 100644 --- a/lib/viral_spiral/entity/room.ex +++ b/lib/viral_spiral/entity/room.ex @@ -33,6 +33,19 @@ defmodule ViralSpiral.Entity.Room do volatality: EngineConfig.volatility() } + def new() do + engine_config = %EngineConfig{} + + %Room{ + id: UXID.generate!(prefix: "room", size: :small), + name: name(), + state: :uninitialized, + chaos_counter: engine_config.chaos_counter, + chaos: engine_config.chaos_counter, + volatality: engine_config.volatility + } + end + @doc """ Reserve a room. @@ -80,19 +93,6 @@ defmodule ViralSpiral.Entity.Room do } end - def new() do - engine_config = %EngineConfig{} - - %Room{ - id: UXID.generate!(prefix: "room", size: :small), - name: name(), - state: :uninitialized, - chaos_counter: engine_config.chaos_counter, - chaos: engine_config.chaos_counter, - volatality: engine_config.volatility - } - end - def set_state(%Room{} = room, state) when state in @all_states do %{room | state: state} end @@ -162,7 +162,6 @@ defmodule ViralSpiral.Entity.Room do end defimpl ViralSpiral.Entity.Change, for: ViralSpiral.Entity.Room do - alias ViralSpiral.Game.State alias ViralSpiral.Entity.Room @doc """ diff --git a/test/viral_spiral/entity/room_test.exs b/test/viral_spiral/entity/room_test.exs new file mode 100644 index 0000000..854c6ae --- /dev/null +++ b/test/viral_spiral/entity/room_test.exs @@ -0,0 +1,44 @@ +defmodule ViralSpiral.Entity.RoomTest do + alias ViralSpiral.Room.EngineConfig + alias ViralSpiral.Room.ChangeDescriptions + alias ViralSpiral.Entity.Change + alias ViralSpiral.Entity.Room + use ExUnit.Case + + describe "deterministic room configs" do + test "communities - yellow, red; affinities - sock, houseboat" do + :rand.seed(:exsss, {1, 8, 12}) + room = Room.reserve("hello") |> Room.start(4) + + assert room.name == "hello" + assert room.affinities == [:houseboat, :skub] + assert room.communities == [:red, :yellow, :blue] + assert room.chaos == 0 + assert room.chaos_counter == 10 + end + end + + describe "changes" do + setup do + room = %Room{chaos: 4} + %{room: room} + end + + test "change chaos countdown", %{room: room} do + new_room = Change.apply_change(room, ChangeDescriptions.change_chaos(5)) + assert new_room.chaos == 9 + end + + test "pass invalid offset in change description", %{room: room} do + assert_raise ArithmeticError, fn -> + Change.apply_change(room, ChangeDescriptions.change_chaos("hi")) + end + end + + test "pass opts without required fields", %{room: room} do + assert_raise ArgumentError, fn -> + Change.apply_change(room, invalid: "random") + end + end + end +end diff --git a/test/viral_spiral/room/room_test.exs b/test/viral_spiral/room/room_test.exs deleted file mode 100644 index 2530297..0000000 --- a/test/viral_spiral/room/room_test.exs +++ /dev/null @@ -1,60 +0,0 @@ -defmodule ViralSpiral.Room.RoomTest do - alias ViralSpiral.Entity.Change - alias ViralSpiral.Entity.Room - use ExUnit.Case - - describe "deterministic room configs" do - test "communities - yellow, red; affinities - sock, houseboat" do - :rand.seed(:exsss, {1, 8, 12}) - room = Room.new(3) - - assert room == %Room{ - affinities: [:houseboat, :skub], - communities: [:yellow, :red], - chaos_counter: 10, - volatality: :medium - } - end - - test "communities - a,b; affinities - x,y" do - :rand.seed(:exsss, {1, 2, 12}) - room = Room.new(3) - - assert room = %Room{ - affinities: [:highfive, :skub], - communities: [:yellow, :blue], - chaos_counter: 10, - volatality: :medium - } - end - end - - describe "Room Functions" do - test "player one creates a room and collects its link" do - room = Room.name() |> IO.inspect() - end - end - - describe "changes" do - setup do - room = %Room{chaos_counter: 4} - %{room: room} - end - - test "change chaos countdown", %{room: room} do - new_room = Change.apply_change(room, offset: 5) - assert new_room.chaos_counter == 9 - end - - test "pass invalid offset in change description", %{room: room} do - new_room = Change.apply_change(room, offset: "hi") - assert new_room.chaos_counter == 4 - end - - test "pass opts without required fields", %{room: room} do - assert_raise ArgumentError, fn -> - Change.apply_change(room, invalid: "random") - end - end - end -end