World: one city among many under cities, no gameplay change

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rope 2026-11-04 15:02:19 +00:00
parent 7dc780352c
commit 11e4d98d49
3 changed files with 100 additions and 7 deletions

View file

@ -3552,6 +3552,48 @@ class TownsfolkTest(unittest.TestCase):
self.assertIn("the hearths", game.city_uses("kindling"))
class WorldShapeTest(unittest.TestCase):
def test_a_new_world_keeps_its_city_under_cities(self):
world = game.new_world(T0)
self.assertEqual(list(world.doc["cities"]), [game.HOME_CITY])
self.assertNotIn("city", world.doc)
self.assertIs(world["city"], world.doc["cities"][game.HOME_CITY])
self.assertIs(world["events"], world["city"]["events"])
self.assertNotIn("events", world.doc)
def test_a_flat_save_moves_under_cities_and_loses_nothing(self):
world = game.new_world(T0)
city = world.doc["cities"].pop(game.HOME_CITY)
moved = {key: city.pop(key) for key in game.CITY_KEYS}
moved["events"].append({"id": 7, "kind": "raid", "status": "done"})
flat = {**world.doc, **moved, "city": city, "version": 38}
del flat["cities"]
loaded = game.migrate(json.loads(json.dumps(flat)), T0)
self.assertEqual(loaded["version"], game.WORLD_VERSION)
self.assertEqual(loaded["events"], moved["events"])
self.assertEqual(loaded["city"]["population"], city["population"])
for key in (*game.CITY_KEYS, "city"):
self.assertNotIn(key, loaded.doc)
def test_a_saved_world_loads_back_the_same(self):
world = game.new_world(T0)
saved = json.dumps(world.doc)
self.assertEqual(json.dumps(game.migrate(json.loads(saved), T0).doc), saved)
def test_each_city_sees_its_own_state_and_shares_the_rest(self):
world = game.new_world(T0)
second = json.loads(json.dumps(world["city"]))
second["population"] = 5.0
world.doc["cities"]["coast"] = second
coast = world.at("coast")
self.assertEqual(coast["city"]["population"], 5.0)
self.assertEqual(world["city"]["population"], 20.0)
coast["next_event_at"] = 1.0
self.assertNotEqual(world["next_event_at"], 1.0)
coast["next_event_id"] = 99
self.assertEqual(world["next_event_id"], 99)
if __name__ == "__main__":
unittest.main()