This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
room_test.exs
208 lines (155 loc) · 6.83 KB
/
room_test.exs
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
defmodule Web.RoomTest do
use Data.ModelCase
alias Web.Room
alias Web.Zone
setup do
{:ok, zone} = Zone.create(zone_attributes(%{name: "The Forest"}))
%{zone: zone}
end
test "creating a new room adds a child to the room supervision tree", %{zone: zone} do
params = %{
name: "Forest Path",
description: "A small forest path",
currency: "10",
x: 1,
y: 1,
map_layer: 1,
}
{:ok, room} = Room.create(zone, params)
assert room.name == "Forest Path"
state = Game.Zone._get_state(zone.id)
children = state.room_supervisor_pid |> Supervisor.which_children()
# room + event bus
assert children |> length() == 2
assert Game.Room._get_state(room.id)
end
test "updating a room updates the room state in the supervision tree", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{name: "Forest Path"}))
{:ok, room} = Room.update(room.id, %{name: "Pathway"})
assert room.name == "Pathway"
# Check the supervision tree to make sure casts have gone through
state = Game.Zone._get_state(zone.id)
children = state.room_supervisor_pid |> Supervisor.which_children()
# room + event bus
assert children |> length() == 2
state = Game.Room._get_state(room.id)
assert state.room.name == "Pathway"
end
test "adding an item to a room", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{name: "Forest Path"}))
item = create_item()
start_and_clear_items()
insert_item(item)
# Check the supervision tree to make sure casts have gone through
state = Game.Zone._get_state(zone.id)
children = state.room_supervisor_pid |> Supervisor.which_children()
# room + event bus
assert children |> length() == 2
{:ok, room} = Room.add_item(room, item.id)
state = Game.Room._get_state(room.id)
assert state.room.items |> length() == 1
end
test "create a room item", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{name: "Forest Path"}))
item = create_item()
{:ok, room} = Room.update(room.id, %{name: "Pathway"})
{:ok, _room_item} = Room.create_item(room, %{item_id: item.id, spawn_interval: 15})
state = Game.Room._get_state(room.id)
assert state.room.room_items |> length() == 1
end
test "delete room item", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{name: "Forest Path"}))
item = create_item()
room_item = create_room_item(room, item, %{spawn_interval: 15})
{:ok, room} = Room.update(room.id, %{name: "Pathway"})
{:ok, _room_item} = Room.delete_item(room_item.id)
state = Game.Room._get_state(room.id)
assert state.room.room_items |> length() == 0
end
test "create an exit", %{zone: zone} do
{:ok, room1} = Room.create(zone, room_attributes(%{name: "Forest Path"}))
{:ok, room2} = Room.create(zone, room_attributes(%{name: "Forest Path", y: 2}))
{:ok, _room_exit} = Room.create_exit(%{"start_room_id" => room1.id, "finish_room_id" => room2.id, "direction" => "south"})
state = Game.Room._get_state(room1.id)
assert state.room.exits |> length() == 1
state = Game.Room._get_state(room2.id)
assert state.room.exits |> length() == 1
end
test "delete a room exit", %{zone: zone} do
{:ok, room1} = Room.create(zone, room_attributes(%{name: "Forest Path"}))
{:ok, room2} = Room.create(zone, room_attributes(%{name: "Forest Path", y: 2}))
{:ok, room_exit} = Room.create_exit(%{"start_room_id" => room1.id, "finish_room_id" => room2.id, "direction" => "south"})
{:ok, _room_exit} = Room.delete_exit(room_exit.id)
state = Game.Room._get_state(room1.id)
assert state.room.exits |> length() == 0
state = Game.Room._get_state(room2.id)
assert state.room.exits |> length() == 0
end
describe "room features" do
test "add a new room feature", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{}))
{:ok, _feature} = Room.add_feature(room, %{"key" => "log", "short_description" => "short", "description" => "Long"})
state = Game.Room._get_state(room.id)
assert state.room.features |> length() == 1
end
test "edit a room feature", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{}))
{:ok, _feature} = Room.add_feature(room, %{"key" => "log", "short_description" => "short", "description" => "Long"})
state = Game.Room._get_state(room.id)
[feature] = state.room.features
{:ok, _room} = Room.edit_feature(room, feature.id, %{"key" => "log", "short_description" => "short", "description" => "longer"})
state = Game.Room._get_state(room.id)
assert [%{description: "longer"}] = state.room.features
end
test "remove a room feature", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{}))
{:ok, feature} = Room.add_feature(room, %{"key" => "log", "short_description" => "short", "description" => "Long"})
{:ok, _feature} = Room.delete_feature(room, feature.id)
state = Game.Room._get_state(room.id)
assert state.room.features |> length() == 0
end
end
describe "global features" do
test "add a feature", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{}))
{:ok, feature} = create_feature()
{:ok, _feature} = Room.add_global_feature(room, Integer.to_string(feature.id))
state = Game.Room._get_state(room.id)
assert state.room.feature_ids |> length() == 1
end
test "remove a feature", %{zone: zone} do
{:ok, room} = Room.create(zone, room_attributes(%{}))
{:ok, feature} = create_feature()
{:ok, _room} = Room.add_global_feature(room, Integer.to_string(feature.id))
{:ok, _room} = Room.remove_global_feature(room, Integer.to_string(feature.id))
state = Game.Room._get_state(room.id)
assert state.room.features |> length() == 0
end
end
describe "deleting a room" do
setup %{zone: zone} do
params = %{
name: "Forest Path",
description: "A small forest path",
currency: "10",
x: 1,
y: 1,
map_layer: 1,
}
{:ok, room} = Room.create(zone, params)
%{room: room}
end
test "does not delete a zone graveyard - is_graveyard", %{room: room} do
{:ok, room} = Room.update(room.id, %{is_graveyard: true})
assert {:error, :graveyard, _room} = Room.delete(room.id)
end
test "does not delete a zone graveyard - is_graveyard: false, but still attached", %{zone: zone, room: room} do
{:ok, _zone} = Zone.update(zone.id, %{graveyard_id: room.id})
assert {:error, :graveyard, _room} = Room.delete(room.id)
end
test "does not delete the starting room", %{room: room} do
create_config(:starting_save, %{room_id: room.id} |> Poison.encode!())
assert {:error, :starting_room, _room} = Room.delete(room.id)
end
end
end