Announce presses

This commit is contained in:
2026-10-21 12:26:51 -04:00
parent 65bf6dbc98
commit f4c10c9d7d
5 changed files with 66 additions and 6 deletions
+35 -2
View File
@@ -235,7 +235,7 @@ class PersistenceTests(unittest.TestCase):
self.assertEqual(status, 201)
self.assertEqual(posted["name"], self.game.state["players"]["alice"]["name"])
self.assertEqual(posted["message"], "<b>Hello</b> '); --")
self.assertEqual(set(posted), {"id", "name", "at", "message"})
self.assertEqual(set(posted), {"id", "name", "at", "message", "kind"})
self.assertEqual(request("POST", {"message": "Too soon"}, True)[0], 429)
# Snapshot writes delete/reinsert players; deferred chat FKs must survive.
self.game.save_state(self.game.state)
@@ -250,6 +250,26 @@ class PersistenceTests(unittest.TestCase):
with self.game.database() as connection:
self.assertEqual(connection.execute("PRAGMA foreign_key_check").fetchall(), [])
def test_existing_chat_schema_upgrades_without_losing_messages(self):
self.game.load_state()
self.game.state = self.sample_state()
self.game.save_state(self.game.state)
with self.game.database() as connection:
connection.execute("DROP TABLE chat_messages")
connection.execute("""CREATE TABLE chat_messages (
id INTEGER PRIMARY KEY,
player_id TEXT NOT NULL REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED,
name TEXT NOT NULL, at INTEGER NOT NULL, message TEXT NOT NULL
)""")
connection.execute(
"INSERT INTO chat_messages (player_id, name, at, message) VALUES (?, ?, ?, ?)",
("alice", "Alice", 123, "Existing message"),
)
self.game.load_state()
self.assertEqual(self.game.chat_messages(), [
{"id": 1, "name": "Alice", "at": 123, "message": "Existing message", "kind": "message"},
])
def test_chat_returns_latest_hundred_in_chronological_order(self):
self.game.load_state()
self.game.state = self.sample_state()
@@ -290,15 +310,28 @@ class PersistenceTests(unittest.TestCase):
status, registered = request("/api/register", {"name": "Alice", "password": "test-password"})
self.assertEqual(status, 201)
token = registered["token"]
self.assertEqual(request("/api/press", {}, token)[0], 200)
self.assertEqual(request("/api/press", {})[0], 401)
self.assertEqual(self.game.chat_messages(), [])
t = self.game.now_ms()
self.game.state["deadline"] = t + 1234
with patch.object(self.game, "now_ms", return_value=t):
self.assertEqual(request("/api/press", {}, token)[0], 200)
announcements = self.game.chat_messages()
self.assertEqual(len(announcements), 1)
self.assertEqual(announcements[0]["kind"], "press")
self.assertEqual(announcements[0]["name"], "The Button")
self.assertEqual(announcements[0]["message"], "Alice pushed the button with 00:00:01.234 remaining and received 32 points.")
self.assertEqual(request("/api/chat", {"message": "That was close!"}, token)[0], 201)
self.assertEqual(request("/api/subscribe", {"first_name": "Alice", "email": "alice@example.com"}, token)[0], 200)
self.game.state = self.game.load_state()
self.assertEqual(self.game.chat_messages()[0], announcements[0])
self.game.token_index = {th: pid for pid, p in self.game.state["players"].items() for th in p["token_hashes"]}
self.assertEqual(request("/api/press", {}, token)[0], 200)
status, logged_in = request("/api/login", {"name": "Alice", "password": "test-password"})
self.assertEqual(status, 200)
self.assertEqual(request("/api/logout", {}, token)[0], 200)
self.assertEqual(request("/api/press", {}, token)[0], 401)
self.assertEqual(len([m for m in self.game.chat_messages() if m["kind"] == "press"]), 2)
loaded = self.game.load_state()
player = next(iter(loaded["players"].values()))
self.assertTrue(player["subscribed"])