refactor: move resource authority into simulation
This commit is contained in:
@@ -8,6 +8,7 @@ func _initialize() -> void:
|
||||
func _run() -> void:
|
||||
await _test_deterministic_continuation()
|
||||
await _test_resource_state_round_trip()
|
||||
_test_legacy_resource_migration()
|
||||
_test_schema_rejection()
|
||||
|
||||
if failures.is_empty():
|
||||
@@ -64,33 +65,65 @@ func _test_resource_state_round_trip() -> void:
|
||||
"res://world/resource_nodes/ResourceNode.tscn"
|
||||
).instantiate()
|
||||
resource.node_id = &"SerializationTestBerry"
|
||||
resource.amount_remaining = 9.0
|
||||
resource.initial_amount = 9.0
|
||||
resource.yield_per_action = 2.0
|
||||
root.add_child(resource)
|
||||
await process_frame
|
||||
|
||||
_check(resource.reserve(42), "Resource should accept the test reservation")
|
||||
resource.extract()
|
||||
|
||||
var manager := _create_manager(77)
|
||||
_check(
|
||||
manager.register_resource_node(resource),
|
||||
"Resource presentation should bind to simulation authority"
|
||||
)
|
||||
var resource_state: ResourceStateRecord = manager.get_resource_state(
|
||||
resource.node_id
|
||||
)
|
||||
_check(resource_state.reserve(42), "Resource should accept the test reservation")
|
||||
resource_state.extract()
|
||||
var saved_json: String = manager.serialize_state()
|
||||
resource.extract(5.0)
|
||||
resource.release(42)
|
||||
resource.enabled = false
|
||||
resource.free()
|
||||
|
||||
_check(
|
||||
is_equal_approx(resource_state.get_amount_remaining(), 7.0),
|
||||
"Resource state should survive without its presentation node"
|
||||
)
|
||||
resource_state.extract(5.0)
|
||||
resource_state.release(42)
|
||||
resource_state.set_enabled(false)
|
||||
|
||||
_check(
|
||||
manager.restore_state_from_json(saved_json),
|
||||
"Resource state should restore with the simulation record"
|
||||
"Unloaded resource state should restore with the simulation record"
|
||||
)
|
||||
resource_state = manager.get_resource_state(&"SerializationTestBerry")
|
||||
_check(
|
||||
is_equal_approx(resource.amount_remaining, 7.0),
|
||||
is_equal_approx(resource_state.get_amount_remaining(), 7.0),
|
||||
"Resource amount should round-trip"
|
||||
)
|
||||
_check(resource.reserved_by == 42, "Resource reservation should round-trip")
|
||||
_check(resource.enabled, "Resource enabled state should round-trip")
|
||||
_check(
|
||||
resource_state.get_reserved_by() == 42,
|
||||
"Resource reservation should round-trip"
|
||||
)
|
||||
_check(resource_state.is_enabled(), "Resource enabled state should round-trip")
|
||||
|
||||
var rebound: ResourceNode = load(
|
||||
"res://world/resource_nodes/ResourceNode.tscn"
|
||||
).instantiate()
|
||||
rebound.node_id = &"SerializationTestBerry"
|
||||
rebound.initial_amount = 999.0
|
||||
root.add_child(rebound)
|
||||
await process_frame
|
||||
_check(
|
||||
rebound.state == resource_state,
|
||||
"Reloaded presentation should bind to the existing authoritative record"
|
||||
)
|
||||
_check(
|
||||
is_equal_approx(rebound.get_amount_remaining(), 7.0),
|
||||
"Reloaded presentation must not overwrite authoritative amount"
|
||||
)
|
||||
|
||||
manager.free()
|
||||
resource.free()
|
||||
rebound.free()
|
||||
|
||||
func _test_schema_rejection() -> void:
|
||||
var unsupported := JSON.stringify({
|
||||
@@ -106,6 +139,27 @@ func _test_schema_rejection() -> void:
|
||||
"Non-record JSON should be rejected"
|
||||
)
|
||||
|
||||
func _test_legacy_resource_migration() -> void:
|
||||
var migrated := ResourceStateRecord.from_dictionary({
|
||||
"schema_version": 1,
|
||||
"node_id": "legacy_tree",
|
||||
"amount_remaining": 6.0,
|
||||
"reserved_by": 12,
|
||||
"enabled": true
|
||||
})
|
||||
_check(migrated != null, "ResourceStateRecord v1 should migrate explicitly")
|
||||
if migrated == null:
|
||||
return
|
||||
_check(
|
||||
migrated.data["schema_version"] == ResourceStateRecord.SCHEMA_VERSION,
|
||||
"Migrated resource state should use the current nested schema"
|
||||
)
|
||||
_check(
|
||||
is_equal_approx(migrated.get_amount_remaining(), 6.0)
|
||||
and migrated.get_reserved_by() == 12,
|
||||
"Resource migration should preserve mutable authority"
|
||||
)
|
||||
|
||||
func _create_manager(seed_value: int) -> Node:
|
||||
var manager: Node = load("res://simulation/SimulationManager.gd").new()
|
||||
manager.simulation_seed = seed_value
|
||||
|
||||
Reference in New Issue
Block a user