extends SceneTree var checks := 0 var failures := 0 var runtime: Variant var clock: Variant var state: Variant func _initialize() -> void: call_deferred("_run") func expect(ok: bool, message: String) -> void: checks += 1 if not ok: failures += 1; push_error(message) else: print(" ✓ ",message) func _run() -> void: _test_paths() _test_incapacitated_shift() _test_task_compatibility() clock = load("res://autoload/game_clock.gd").new() state = load("res://autoload/game_state.gd").new() runtime = load("res://autoload/simulation_runtime.gd").new() clock.reset(1,6,0); state.reset() expect(runtime.initialize(clock,state,711)[&"valid"], "rescue runtime initializes") clock.set_paused(true) expect(runtime.submit_command(&"build_facility", {&"definition_id": &"infirmary", &"origin": Vector3i(-6,0,1)}, &"player").is_success(), "medical facility builds through public command") clock.advance_minutes(80) expect(runtime.facility_service.get_instances().size() == 1, "medical facility construction completes") var clinic: FacilityInstance = runtime.facility_service.get_instances()[0] for id in [&"patient", &"porter"]: var options := {&"current_cell": Vector3i(3,0,1) if id == &"patient" else Vector3i(5,0,1), &"health": 0 if id == &"patient" else 100, &"state": StaffRuntime.State.AVAILABLE, &"role_tags": [&"medic"], &"permitted_task_tags": [&"rescue"]} expect(runtime.submit_command(&"register_staff", {&"staff_id": id, &"role_id": &"porter", &"options": options}, &"player").is_success(), "rescue actor registers: " + String(id)) var patient: StaffRuntime = runtime.staff_service.get_staff(&"patient") expect(patient.state == StaffRuntime.State.INCAPACITATED, "zero health means incapacitated") runtime.facility_service.set_operational_status(clinic.instance_id, {&"operator_open": false}, clock.absolute_minute) clock.advance_minutes(3) expect(patient.current_cell == Vector3i(3,0,1) and runtime.staff_service.get_tasks().is_empty(), "closed clinic leaves patient in place without a rescue task") runtime.facility_service.set_operational_status(clinic.instance_id, {&"operator_open": true}, clock.absolute_minute) var carrying := false for minute in range(30): clock.advance_minutes(1) for task: StaffTask in runtime.staff_service.get_tasks(): if task.task_type == &"rescue_transport" and task.state == StaffTask.State.MOVING: carrying = true if carrying: break expect(carrying, "medic reaches patient before transport begins") var carrier: StaffRuntime = runtime.staff_service.get_staff(&"porter") expect(patient.current_cell == carrier.current_cell and patient.health == 0, "pickup does not heal or teleport patient to clinic") expect(runtime.staff_service.get_tasks().size() == 1, "one patient has one active rescue") var midpoint: Dictionary = runtime.create_snapshot() expect(runtime.save_to_path("user://saves/rescue_regression")[&"ok"], "mid-transport state writes to an isolated save package") clock.advance_minutes(5) expect(runtime.load_from_path("user://saves/rescue_regression")[&"ok"], "mid-transport disk save loads") expect(runtime.latest_digest == SimulationDigest.compute(midpoint), "disk load restores the exact persisted rescue digest") SaveService.delete_save("user://saves/rescue_regression") # Manual save/load publish lifecycle events; compare replay from the loaded state. midpoint = runtime.create_snapshot() patient = runtime.staff_service.get_staff(&"patient") clock.advance_minutes(70) expect(patient.health == 100 and patient.state == StaffRuntime.State.OFF_DUTY, "transport and treatment restore patient at medical facility") expect(patient.current_cell.x < 0 and patient.current_cell.z == 1, "patient actually travelled through the backyard") var finished: Dictionary = runtime.create_snapshot() expect(runtime.restore_snapshot(midpoint), "mid-transport memory snapshot restores") clock.advance_minutes(70) expect(runtime.create_snapshot() == finished, "snapshot replay reproduces exact typed state and events") expect(runtime.restore_snapshot(midpoint), "carrier failure fixture restores") patient = runtime.staff_service.get_staff(&"patient") carrier = runtime.staff_service.get_staff(&"porter") var stranded_at := patient.current_cell carrier.health = 0 clock.advance_minutes(4) expect(patient.health == 0 and patient.current_cell == stranded_at, "collapsed medic cannot move or heal their patient") expect(runtime.staff_service.validate_invariants()[&"ok"], "carrier collapse preserves task ownership") expect(runtime.restore_snapshot(midpoint), "interruption fixture restores") patient = runtime.staff_service.get_staff(&"patient") var stopped_at := patient.current_cell runtime.facility_service.set_operational_status(clinic.instance_id, {&"operator_open": false}, clock.absolute_minute) clock.advance_minutes(4) expect(patient.health == 0 and patient.current_cell == stopped_at, "clinic outage cancels transport without moving or healing patient") expect(runtime.staff_service.validate_invariants()[&"ok"], "interruption preserves staff/task invariants") runtime.facility_service.set_operational_status(clinic.instance_id, {&"operator_open": true}, clock.absolute_minute) clock.advance_minutes(80) expect(patient.health == 100, "clinic reopening allows a new rescue from the last position") runtime.facility_service.set_operational_status(clinic.instance_id, {&"operator_open": false}, clock.absolute_minute) runtime.progression_service.update_metrics({&"reputation": 10}) expect(runtime.submit_command(&"register_staff", {&"staff_id": &"visible_patient", &"role_id": &"porter", &"options": {&"health": 0, &"current_cell": Vector3i.ZERO}}, &"player").is_success(), "front-layer collapse fixture registers") expect(runtime.submit_command(&"spawn_guest", {&"guest_id": &"witness", &"guest_type_id": &"noble_human", &"gold": 10}, &"player").is_success(), "witness enters the same front cell") clock.advance_minutes(1) expect(runtime.progression_service.metrics[&"reputation"] == 9, "visible collapse decreases reputation") clock.advance_minutes(3) expect(runtime.progression_service.metrics[&"reputation"] == 9, "one collapse cannot repeatedly reduce reputation") expect(not runtime._authority_line_of_sight(Vector3i(0,0,1), Vector3i.ZERO, {}), "back-layer guest cannot witness a front-layer collapse") expect(runtime.get_staff_rescue_report()[&"waiting"] == 1, "rescue report exposes the patient waiting for an operational clinic") runtime.free();clock.free();state.free() _test_work_collapse() print("%s: %d checks" % ["PASS" if failures == 0 else "FAIL", checks]);quit(failures) func _test_paths() -> void: var path := PathService.new() path.rebuild_graph([Vector3i(0,0,0),Vector3i(1,0,0),Vector3i(0,0,1),Vector3i(1,0,1)]) expect(path.find_backyard_path(Vector3i(0,0,0),Vector3i(1,0,0)).is_empty(), "front corridor alone cannot be used for rescue") path.configure_connector(&"door_a",Vector3i(0,0,0),Vector3i(0,0,1),100,true,&"staff") path.configure_connector(&"door_b",Vector3i(1,0,1),Vector3i(1,0,0),100,true,&"staff") var route := path.find_backyard_path(Vector3i(0,0,0),Vector3i(1,0,0)) expect(route.size() == 4 and route[1].z == 1 and route[2].z == 1, "rescue uses real doors and backyard even with a shorter front path") var lift := PathService.new() lift.rebuild_graph([Vector3i(0,0,1), Vector3i(0,1,1)]) lift.set_vertical_walk_requires_connector(true) lift.configure_elevator(&"infra.connector:rescue",Vector3i(0,0,1),Vector3i(0,1,1),{&"cabin_capacity": 1}) expect(lift.find_backyard_path(Vector3i(0,0,1),Vector3i(0,1,1)).is_empty(), "one-person lift cannot transport a rescuer and patient") lift.configure_elevator(&"infra.connector:rescue",Vector3i(0,0,1),Vector3i(0,1,1),{&"cabin_capacity": 2}) expect(lift.find_backyard_path(Vector3i(0,0,1),Vector3i(0,1,1)).is_empty(), "paired rescue does not use a lift without atomic group boarding") lift.configure_elevator(&"transport.pair",Vector3i(0,0,1),Vector3i(0,1,1),{&"cabin_capacity": 1}) expect(lift.find_backyard_path(Vector3i(0,0,1),Vector3i(0,1,1),true).is_empty(), "managed one-seat cabin cannot serve rescue") lift.configure_elevator(&"transport.pair",Vector3i(0,0,1),Vector3i(0,1,1),{&"cabin_capacity": 2}) expect(lift.find_backyard_path(Vector3i(0,0,1),Vector3i(0,1,1),true).size() == 2, "managed two-seat BACK cabin serves rescue") var front := PathService.new() front.rebuild_graph([Vector3i(0,0,0),Vector3i(0,1,0),Vector3i(0,1,1)]) front.set_vertical_walk_requires_connector(true) front.configure_elevator(&"transport.front",Vector3i(0,0,0),Vector3i(0,1,0),{&"cabin_capacity": 4}) front.configure_connector(&"upper_door",Vector3i(0,1,0),Vector3i(0,1,1),100,true,&"staff") expect(front.find_backyard_path(Vector3i(0,0,0),Vector3i(0,1,1),true).is_empty(), "rescue cannot substitute a FRONT passenger lift for the BACK route") lift.configure_connector(&"rescue_stairs",Vector3i(0,0,1),Vector3i(0,1,1),100,true,&"staff") expect(lift.find_backyard_path(Vector3i(0,0,1),Vector3i(0,1,1)).size() == 2, "real backyard stairs carry the rescue pair") path.set_connector_enabled(&"door_a",false) path.set_connector_enabled(&"door_b",false) expect(path.find_backyard_path(Vector3i(0,0,0),Vector3i(1,0,0)).is_empty(), "blocked backyard entry cannot be bypassed") func _test_incapacitated_shift() -> void: var service := StaffService.new() var member := StaffRuntime.new().configure(&"worker", &"porter", {&"state": StaffRuntime.State.AVAILABLE}) service.register_staff(member) service.create_flex_shift(&"worker",0,0,1) member.health = 0 member.transition_to(StaffRuntime.State.INCAPACITATED,&"STAFF_COLLAPSED",0) service.advance_one_minute(2) expect(member.state == StaffRuntime.State.INCAPACITATED, "shift ending cannot wake an incapacitated staff member") func _test_work_collapse() -> void: clock = load("res://autoload/game_clock.gd").new() state = load("res://autoload/game_state.gd").new() runtime = load("res://autoload/simulation_runtime.gd").new() clock.reset(1,6,0);state.reset();runtime.initialize(clock,state,812);clock.set_paused(true) expect(runtime.enable_production_operations()[&"ok"], "work-health fixture enables production") expect(runtime.submit_command(&"register_staff", {&"staff_id": &"tired", &"role_id": &"porter", &"options": {&"health": 1, &"state": StaffRuntime.State.AVAILABLE, &"current_cell": Vector3i(0,0,1)}}, &"player").is_success(), "working staff starts with one health") runtime.staff_service.create_task(&"work",50,360,{&"target_cell": Vector3i(0,0,1), &"estimated_minutes": 90}) clock.advance_minutes(30) var tired: StaffRuntime = runtime.staff_service.get_staff(&"tired") expect(tired.health == 0 and tired.state == StaffRuntime.State.INCAPACITATED and tired.current_task_id.is_empty(), "real work drains health and cancels the collapsed worker's task") expect(runtime.restore_snapshot(runtime.create_snapshot()), "work-collapse state and cancelled task save together") runtime.free();clock.free();state.free() func _test_task_compatibility() -> void: var task := StaffTask.new().configure(&"legacy", &"work", 1, 0, {}) var saved := task.create_snapshot() saved.erase("rescue_facility_id") expect(StaffTask.from_snapshot(saved) != null, "legacy task without rescue metadata loads") saved["rescue_facility_id"] = 12 expect(StaffTask.from_snapshot(saved) == null, "malformed rescue metadata is rejected")