forked from johnny603/lux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rooms.py
More file actions
209 lines (171 loc) · 6.99 KB
/
Copy pathtest_rooms.py
File metadata and controls
209 lines (171 loc) · 6.99 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
import pytest
import cli
import rooms
import storage
from server import app
@pytest.fixture
def client(tmp_path, monkeypatch):
test_state_file = str(tmp_path / "test_lux_state.json")
monkeypatch.setenv(storage.DEFAULT_STATE_ENV, test_state_file)
app.config["TESTING"] = True
with app.test_client() as c:
yield c
def test_rooms_default_catalog():
all_rooms = rooms.get_all_rooms()
assert len(all_rooms) == 5
assert all_rooms[0]["id"] == "room-1"
assert all_rooms[0]["locked"] is False
assert len(all_rooms[0]["objects"]) >= 2
assert all_rooms[1]["locked"] is True
def test_room_objects_inspection():
objs = rooms.get_room_objects("room-1")
assert objs is not None
assert len(objs) >= 2
term = rooms.get_room_object("room-1", "obj-flickering-terminal")
assert term is not None
assert term["is_pickupable"] is False
assert "Flickering Terminal" in term["name"]
keycard = rooms.get_room_object("room-1", "obj-brass-keycard")
assert keycard is not None
assert keycard["is_pickupable"] is True
def test_interact_with_object():
res = rooms.interact_with_object("room-1", "obj-flickering-terminal", action="examine")
assert res is not None
assert res["success"] is True
assert "System Log" in res["message"]
assert res["state_effect"] == "terminal_inspected"
res_pickup = rooms.interact_with_object("room-1", "obj-brass-keycard", action="pickup")
assert res_pickup is not None
assert res_pickup["success"] is True
assert res_pickup["is_pickupable"] is True
assert "has_brass_keycard" == res_pickup["state_effect"]
def test_api_rooms_and_objects(client):
res = client.get("/api/v1/rooms")
assert res.status_code == 200
data = res.get_json()
assert len(data) == 5
assert data[0]["is_unlocked"] is True
assert "objects" in data[0]
res_objs = client.get("/api/v1/rooms/room-1/objects")
assert res_objs.status_code == 200
objs = res_objs.get_json()
assert len(objs) >= 2
# Interact endpoint
res_interact = client.post(
"/api/v1/rooms/room-1/objects/obj-flickering-terminal/interact",
json={"action": "examine"},
)
assert res_interact.status_code == 200
idata = res_interact.get_json()
assert idata["ok"] is True
assert "System Log" in idata["message"]
# 404 for non-existent room or object
res_404_room = client.get("/api/v1/rooms/nonexistent/objects")
assert res_404_room.status_code == 404
res_404_obj = client.post(
"/api/v1/rooms/room-1/objects/nonexistent/interact",
json={"action": "examine"},
)
assert res_404_obj.status_code == 404
def test_cli_examine_formatting():
objs = rooms.get_room_objects("room-1")
formatted = cli.format_room_objects(objs)
assert "Flickering Terminal" in formatted
assert "[static]" in formatted
assert "[pickupable]" in formatted
examined = cli.examine_object(objs[0])
assert "=== Flickering Terminal ===" in examined
assert "Actions:" in examined
def test_timed_escape_pressure(client):
from datetime import datetime, timedelta, timezone
# Check room-2 timer initial state
res = client.get("/api/v1/rooms/room-2")
assert res.status_code == 200
rdata = res.get_json()
assert rdata["time_limit_seconds"] == 300
# Room-1 escape unlocks room-2
state = storage.load_state()
storage.mark_room_escaped(state, "room-1")
storage.save_state(state)
# Unlock / Enter room-2 starts timer
res_unlock = client.post("/api/v1/rooms/room-2/unlock")
assert res_unlock.status_code == 200
udata = res_unlock.get_json()
assert udata["unlocked"] is True
assert udata["timer"] is not None
assert udata["timer"]["time_limit_seconds"] == 300
assert udata["timer"]["is_expired"] is False
# Simulate expired timer
state = storage.load_state()
past_iso = (datetime.now(timezone.utc) - timedelta(seconds=350)).isoformat()
state["game"]["room_timers"]["room-2"]["started_at"] = past_iso
storage.save_state(state)
# Room-2 should now report expired and be locked
res_expired = client.get("/api/v1/rooms/room-2")
assert res_expired.status_code == 200
ex_data = res_expired.get_json()
assert ex_data["status"] == "expired"
assert ex_data["is_unlocked"] is False
assert ex_data["is_expired"] is True
assert "Reset room" in ex_data["unlock_instruction"]
# Unlock attempt fails when expired
res_fail_unlock = client.post("/api/v1/rooms/room-2/unlock")
assert res_fail_unlock.status_code == 403
# Reset room-2 restores timer
res_reset = client.post("/api/v1/rooms/room-2/reset")
assert res_reset.status_code == 200
reset_data = res_reset.get_json()
assert reset_data["ok"] is True
assert reset_data["timer"]["is_expired"] is False
assert reset_data["timer"]["remaining_seconds"] > 290
# Format timer CLI check
timer_str = cli.format_room_timer(reset_data["timer"])
assert "remaining" in timer_str
assert "300s" in timer_str
def test_inventory_pickup_and_use(client):
# Check initial empty inventory
res_inv = client.get("/api/v1/inventory")
assert res_inv.status_code == 200
inv_data = res_inv.get_json()
assert inv_data["ok"] is True
assert inv_data["inventory"] == []
# Attempt to pickup static object fails
res_bad_pickup = client.post("/api/v1/rooms/room-1/objects/obj-flickering-terminal/pickup")
assert res_bad_pickup.status_code == 400
# Pickup pickupable object succeeds
res_pickup = client.post("/api/v1/rooms/room-1/objects/obj-brass-keycard/pickup")
assert res_pickup.status_code == 200
pdata = res_pickup.get_json()
assert pdata["ok"] is True
assert pdata["object_id"] == "obj-brass-keycard"
assert "obj-brass-keycard" in pdata["inventory"]
# Verify inventory endpoint reflects item
res_inv2 = client.get("/api/v1/inventory")
assert res_inv2.status_code == 200
inv2_data = res_inv2.get_json()
assert "obj-brass-keycard" in inv2_data["inventory"]
assert len(inv2_data["items"]) == 1
assert inv2_data["items"][0]["name"] == "Brass Keycard"
# Use item in valid target
res_use = client.post(
"/api/v1/rooms/room-1/objects/obj-brass-keycard/use",
json={"target_id": "obj-security-console"},
)
assert res_use.status_code == 200
udata = res_use.get_json()
assert udata["ok"] is True
assert udata["success"] is True
assert "swipe the Brass Keycard" in udata["message"]
# Use item not in inventory fails
res_use_missing = client.post(
"/api/v1/rooms/room-2/objects/obj-c-reference-manual/use"
)
assert res_use_missing.status_code == 400
assert "do not possess" in res_use_missing.get_json()["error"]
# Test CLI formatting
state = storage.load_state()
inv_ids = storage.get_inventory(state)
items = [rooms.find_object_across_rooms(i) for i in inv_ids]
formatted = cli.format_inventory(items)
assert "🎒 Player Inventory:" in formatted
assert "Brass Keycard" in formatted