Files
TheTree_mock/tests/environment/test_runtime_cleanliness.gd

76 lines
6.0 KiB
GDScript

extends SceneTree
var checks := 0
var failures := 0
func expect(ok: bool, label: String) -> void:
checks += 1
if not ok: failures += 1; push_error(label)
func _initialize() -> void: call_deferred("run")
func run() -> void:
var runtime: Variant = load("res://autoload/simulation_runtime.gd").new()
var clock: Variant = load("res://autoload/game_clock.gd").new()
var state: Variant = load("res://autoload/game_state.gd").new()
clock.reset(1,6,0); state.reset()
expect(runtime.initialize(clock,state,42630)[&"valid"], "initialize with validated approved policy")
clock.set_paused(true)
expect(runtime.submit_command(&"build_facility", {&"definition_id":&"reception",&"origin":Vector3i(-6,0,0)}).is_success(), "normal paid construction begins")
expect(runtime.facility_cleanliness.create_snapshot()["facilities"].is_empty(), "unfinished building has no cleanliness record")
clock.advance_minutes(120)
var facility: FacilityInstance = runtime.facility_service.get_instances()[0]
var id := facility.instance_id
expect(runtime.facility_cleanliness.get_facility(id)["current"] == 100, "construction completion starts clean")
expect(runtime.submit_command(&"set_facility_conditions",{&"facility_id":id,&"conditions":{&"active_staff_ids":[&"reception_fixture"]}}).is_success(), "controlled staffing for transaction test")
expect(runtime.submit_command(&"register_guest_wallet",{&"guest_id":&"payer",&"gold":100}).is_success(), "payer wallet created")
expect(runtime.submit_command(&"create_service_request",{&"request_id":&"visit1",&"facility_id":id,&"guest_ids":[&"payer"],&"service_type":&"check_in"}).is_success(), "normal service request")
clock.advance_one_minute()
var active: Array = runtime.facility_service.get_active_services(id)
expect(active.size()==1, "service actually starts")
if active.is_empty(): runtime.free(); clock.free(); state.free(); quit(failures); return
var service_id: StringName = active[0].service_id
expect(runtime.facility_cleanliness.get_facility(id)["current"]==100, "starting a visit does not apply completion dirt")
var midpoint: Dictionary = runtime.create_snapshot()
expect(runtime.restore_snapshot(midpoint), "in-progress service with cleanliness restores")
clock.advance_minutes(21)
expect(runtime.facility_service.get_service(service_id).state==ServiceInstance.State.COMPLETED and runtime.facility_cleanliness.get_facility(id)["current"]==99, "completed reception visit applies defined dirt once")
var complete: Dictionary = runtime.create_snapshot()
expect(runtime.restore_snapshot(complete) and runtime.create_snapshot()==complete, "completed dirt and visit roundtrip exactly")
clock.advance_one_minute()
expect(runtime.facility_cleanliness.get_facility(id)["current"]==99, "saved completion does not repeat dirt")
expect(runtime.restore_snapshot(midpoint), "rewind visit")
clock.advance_minutes(21)
expect(runtime.create_snapshot()==complete, "save continuation reproduces full Runtime exactly")
var intact: Dictionary=runtime.create_snapshot()
var bad: Dictionary=intact.duplicate(true)
bad[&"systems"][&"runtime"].erase(&"facility_cleanliness")
expect(not runtime.restore_snapshot(bad) and runtime.create_snapshot()==intact, "v12 missing cleanliness rejects atomically")
bad=intact.duplicate(true)
bad[&"systems"][&"runtime"][&"facility_cleanliness"]["receipts"][0]["source_id"]="invented"
expect(not runtime.restore_snapshot(bad) and runtime.create_snapshot()==intact, "receipt without completed visit rejects atomically")
bad=intact.duplicate(true)
bad[&"systems"][&"runtime"][&"facility_cleanliness"]["facilities"]=[]
bad[&"systems"][&"runtime"][&"facility_cleanliness"]["receipts"]=[]
expect(not runtime.restore_snapshot(bad) and runtime.create_snapshot()==intact, "missing live facility cleanliness rejects")
var legacy: Dictionary=intact.duplicate(true)
legacy[&"systems"][&"runtime"][&"runtime_schema_version"]=11
preload("res://tests/fixtures/pre_auto_purchase_runtime.gd").strip_empty(legacy[&"systems"][&"runtime"])
legacy[&"systems"][&"runtime"].erase(&"facility_cleanliness")
expect(runtime.restore_snapshot(legacy) and runtime.facility_cleanliness.get_facility(id)["current"]==100, "v11 initializes cleanliness without guessing past dirt")
expect(runtime.create_snapshot()[&"systems"][&"runtime"][&"weather"]==intact[&"systems"][&"runtime"][&"weather"], "v11 weather survives migration unchanged")
expect(runtime.restore_snapshot(intact), "restore current dirt before rollback checks")
var rollback: Dictionary=runtime.call("_capture_infrastructure_demolition_state")
runtime.facility_cleanliness.unregister_facility(id)
expect(runtime.call("_restore_infrastructure_demolition_state",rollback) and runtime.create_snapshot()==intact, "infrastructure rollback includes cleanliness")
# Force the final dirt boundary to fail after payment/output staging; no partial settlement.
expect(runtime.restore_snapshot(midpoint), "rewind for failed cleanliness commit")
runtime.facility_cleanliness.unregister_facility(id)
clock.advance_minutes(21)
expect(runtime.facility_service.get_service(service_id).state!=ServiceInstance.State.COMPLETED, "missing dirt participant prevents partial service completion")
expect(runtime.economy_service.get_balance(&"GUEST_WALLET:payer")==100, "failed cleanliness transaction leaves wallet unchanged")
runtime.facility_cleanliness.register_facility(id,100,100)
clock.advance_one_minute()
expect(runtime.facility_service.get_service(service_id).state==ServiceInstance.State.COMPLETED and runtime.facility_cleanliness.get_facility(id)["current"]==99, "failed boundary retries once after recovery")
expect(runtime.submit_command(&"demolish_facility",{&"instance_id":id}).is_success(), "normal demolition command")
expect(runtime.facility_cleanliness.get_facility(id).is_empty() and runtime.facility_cleanliness.create_snapshot()["receipts"].is_empty(), "demolition removes state and dangling receipts")
expect(runtime.restore_snapshot(runtime.create_snapshot()), "post-demolition Runtime remains restorable")
runtime.free();clock.free();state.free()
print("Runtime cleanliness: %d checks, %d failures" % [checks,failures]);quit(failures)