feat: add structured economic events

This commit is contained in:
2026-07-05 18:04:11 +02:00
parent ebf70a711a
commit f9601a67d1
11 changed files with 358 additions and 17 deletions
+57
View File
@@ -110,6 +110,21 @@ func _run() -> void:
is_equal_approx(conserved_food, 9.0),
"Exactly one food unit should leave the world when eaten"
)
_check_economic_event_chain(manager)
var restored: Node = load("res://simulation/SimulationManager.gd").new()
restored.debug_logs = false
root.add_child(restored)
restored.set_process(false)
_check(
restored.restore_state_from_json(manager.serialize_state()),
"Economic events should restore with the simulation"
)
_check(
restored.economic_events.size() == manager.economic_events.size()
and restored.next_event_id == manager.next_event_id,
"Economic event history and its next ID should round-trip"
)
if failures.is_empty():
print("[TEST] Food storage loop passed")
@@ -122,3 +137,45 @@ func _run() -> void:
func _check(condition: bool, message: String) -> void:
if not condition:
failures.append(message)
func _check_economic_event_chain(manager: Node) -> void:
var expected_types: Array[StringName] = [
SimulationIds.EVENT_RESOURCE_EXTRACTED,
SimulationIds.EVENT_STORAGE_DEPOSITED,
SimulationIds.EVENT_STORAGE_WITHDRAWN,
SimulationIds.EVENT_ITEM_CONSUMED
]
_check(
manager.economic_events.size() == expected_types.size(),
"Food transfer loop should emit one structured event per transfer"
)
if manager.economic_events.size() != expected_types.size():
return
for index in expected_types.size():
var event: EconomicEventRecord = manager.economic_events[index]
_check(
int(event.data["event_id"]) == index
and StringName(event.data["event_type"]) == expected_types[index]
and StringName(event.data["item_id"])
== SimulationIds.RESOURCE_FOOD,
"Economic event order, identity, and item should be deterministic"
)
_check(
is_equal_approx(
float(manager.economic_events[0].data["amount"]),
2.0
)
and is_equal_approx(
float(manager.economic_events[1].data["amount"]),
2.0
)
and is_equal_approx(
float(manager.economic_events[2].data["amount"]),
1.0
)
and is_equal_approx(
float(manager.economic_events[3].data["amount"]),
1.0
),
"Economic events should record exact transferred quantities"
)