extends SceneTree var _checks := 0 var _failures := 0 const ORIGIN := Vector3i.ZERO const DESTINATION := Vector3i(0, 1, 0) func _initialize() -> void: call_deferred("_run") func _run() -> void: _test_occupancy_lifecycle() _test_projection_and_despawn() _test_save_and_migration() print("Path transit density: %d checks, %d failures" % [_checks, _failures]) quit(_failures) func _path() -> PathService: var path := PathService.new() path.rebuild_graph([ORIGIN, DESTINATION, Vector3i(1, 0, 0)]) path.configure_elevator(&"lift", ORIGIN, DESTINATION, {&"cabin_capacity": 4}) path.set_cell_base_capacity(ORIGIN, 1) return path func _queue(path: PathService, actor: StringName, id: StringName) -> void: _expect(path.enqueue_elevator_request(&"lift", {&"request_id": id, &"unit_id": actor, &"floor_from": 0, &"floor_to": 1, &"enqueued_at": 10})[&"ok"], "elevator request registers: %s" % actor) func _populate(path: PathService) -> void: for id: StringName in [&"guest:a", &"guest:b", &"staff:c"]: path.project_actor_position(id, ORIGIN, 10) func _test_occupancy_lifecycle() -> void: var path := _path() _populate(path) _expect(path.get_current_density(ORIGIN) == 3 and is_equal_approx(path.get_speed_multiplier(ORIGIN), 0.5), "all three floor actors contribute to the specified half-speed congestion") _queue(path, &"guest:a", &"ride.a") _expect(path.get_current_density(ORIGIN) == 3 and path.get_transit_actor_ids().is_empty(), "queued passengers still occupy the floor") var before := path.create_snapshot() var graph_before := path.graph_revision var boarded := path.dequeue_elevator_boarding(&"lift", 0, 11, 1) _expect(boarded.size() == 1 and path.get_transit_actor_ids() == [&"guest:a"], "boarding derives transit occupancy from the existing Path owner") _expect(path.get_current_density(ORIGIN) == 2 and is_equal_approx(path.get_speed_multiplier(ORIGIN), 0.5), "boarding removes the passenger from density; the two remaining actors still move at half speed") _expect(path.get_actor_projected_cell(&"guest:a") == ORIGIN and path.create_snapshot()[&"actor_progress"] == before[&"actor_progress"], "boarding retains logical origin and progress records") _expect(path.graph_revision == graph_before and path.traffic_revision > before[&"traffic_revision"], "boarding changes traffic costs without changing structural topology") var stable := path.create_snapshot() _expect(path.dequeue_elevator_boarding(&"lift", 0, 11, 1).is_empty() and path.get_current_density(ORIGIN) == 2 \ and path.get_transit_actor_ids() == [&"guest:a"] and path.traffic_revision == stable[&"traffic_revision"], "repeated empty boarding does not change occupancy or traffic revision") _expect(path.complete_elevator_request(&"lift", &"ride.a"), "completion releases the existing boarded owner") _expect(path.get_current_density(ORIGIN) == 3 and not path.is_actor_in_transit(&"guest:a"), "completed trip restores the anchor's floor contribution before Runtime relocates it") _expect(path.project_actor_position(&"guest:a", DESTINATION, 12), "Runtime arrival projects the actor at the real destination") _expect(path.get_current_density(ORIGIN) == 2 and path.get_current_density(DESTINATION) == 1, "arrival moves exactly one floor occupant from origin to destination") stable = path.create_snapshot() _expect(not path.complete_elevator_request(&"lift", &"ride.a") and path.create_snapshot() == stable, "duplicate completion cannot release density twice") _queue(path, &"guest:b", &"ride.b") _expect(path.cancel_elevator_request(&"lift", &"ride.b") and path.get_current_density(ORIGIN) == 2, "cancelling a queued ride keeps its waiting actor on the floor") func _test_projection_and_despawn() -> void: var path := _path() _populate(path) _queue(path, &"staff:c", &"ride.c") path.dequeue_elevator_boarding(&"lift", 0, 11, 1) _expect(path.project_occupancy([{&"actor_id": &"guest:a", &"cell": ORIGIN}, {&"actor_id": &"staff:c", &"cell": ORIGIN}]), "batch projection retains existing boarded ownership") _expect(path.get_current_density(ORIGIN) == 1 and path.get_transit_actor_ids() == [&"staff:c"], "batch occupancy excludes the passenger exactly once") _expect(path.rebuild_graph([ORIGIN, DESTINATION, Vector3i(1, 0, 0)]) and path.get_current_density(ORIGIN) == 1, "topology refresh does not reintroduce cabin passengers into density") var revision := path.traffic_revision _expect(path.project_actor_position(&"staff:c", DESTINATION, 12), "transit projection can update its logical anchor without materializing an occupant") _expect(path.get_current_density(ORIGIN) == 1 and path.get_current_density(DESTINATION) == 0 and path.traffic_revision == revision, "moving an excluded anchor changes no floor density or route costs") _expect(path.despawn_actor(&"staff:c") and path.get_current_density(DESTINATION) == 0 and path.get_transit_actor_ids().is_empty(), "despawning a passenger does not decrement a zero-density floor") _expect(path.complete_elevator_request(&"lift", &"ride.c") and path.get_current_density(DESTINATION) == 0, "later owner cleanup cannot resurrect a despawned passenger") _queue(path, &"unprojected_unit", &"ride.unit") path.dequeue_elevator_boarding(&"lift", 0, 13, 1) _expect(path.get_transit_actor_ids().is_empty() and path.get_current_density(ORIGIN) == 1, "standalone elevator units without actor projections do not affect floor density") _expect(path.project_actor_position(&"unprojected_unit", ORIGIN, 14) and path.is_actor_in_transit(&"unprojected_unit") and path.get_current_density(ORIGIN) == 1, "a projection added after boarding inherits its existing transit ownership") _expect(path.actor_leave_cell(&"unprojected_unit", false) and not path.is_actor_in_transit(&"unprojected_unit"), "leaving the actor graph clears transit membership even when retaining progress") func _test_save_and_migration() -> void: var path := _path() _populate(path) _queue(path, &"guest:a", &"ride.a") _queue(path, &"staff:c", &"ride.c") path.dequeue_elevator_boarding(&"lift", 0, 11, 2) var saved := path.create_snapshot() _expect(saved[&"transit_actor_ids"] == [&"guest:a", &"staff:c"] and path.get_current_density(ORIGIN) == 1, "v5 saves canonical excluded actors and physical floor density") var restored := _path() _expect(restored.restore_snapshot(saved) and restored.create_snapshot() == saved, "boarded Path v5 snapshot restores exactly") for raw: Variant in [null, [], ["missing"], ["guest:a", "guest:a"], ["staff:c", "guest:a"], [17, "staff:c"]]: var bad := saved.duplicate(true) bad[&"transit_actor_ids"] = raw _expect(not restored.restore_snapshot(bad) and restored.create_snapshot() == saved, "invalid transit ownership rejects atomically: %s" % str(raw)) var wrong_density := saved.duplicate(true) for row: Dictionary in wrong_density[&"cell_traffic"]: if row[&"cell"] == ORIGIN: row[&"current_density"] += 1 _expect(not restored.restore_snapshot(wrong_density) and restored.create_snapshot() == saved, "v5 cannot count an excluded passenger back into origin density") var legacy := saved.duplicate(true) legacy[&"schema_version"] = 4 legacy.erase(&"transit_actor_ids") legacy.erase(&"vertical_walk_requires_connector") _expect(not restored.restore_snapshot(legacy) and restored.create_snapshot() == saved, "legacy snapshots must first satisfy their original full-occupancy density rule") for row: Dictionary in legacy[&"cell_traffic"]: if row[&"cell"] == ORIGIN: row[&"current_density"] = 3 _expect(restored.restore_snapshot(legacy), "genuine v4 boarded save migrates") var migrated := restored.create_snapshot() _expect(restored.get_current_density(ORIGIN) == 1 and migrated[&"transit_actor_ids"] == saved[&"transit_actor_ids"], "legacy migration removes only projected boarded actors from density") _expect(migrated[&"actor_cells"] == legacy[&"actor_cells"] and migrated[&"actor_progress"] == legacy[&"actor_progress"], "migration preserves every logical actor location and progress timestamp") _expect(migrated[&"traffic_revision"] == legacy[&"traffic_revision"] + 1 and migrated[&"graph_revision"] == legacy[&"graph_revision"], "migration advances traffic revision once without rewriting topology") _expect(restored.restore_snapshot(migrated) and restored.create_snapshot() == migrated, "canonical reload does not repeat legacy density subtraction") var replay := _path() _expect(replay.restore_snapshot(migrated), "a second runtime starts from the same boarded snapshot") for service: PathService in [restored, replay]: service.complete_elevator_request(&"lift", &"ride.a") service.project_actor_position(&"guest:a", DESTINATION, 12) service.complete_elevator_request(&"lift", &"ride.c") service.project_actor_position(&"staff:c", DESTINATION, 12) _expect(restored.create_snapshot() == replay.create_snapshot() and restored.get_current_density(DESTINATION) == 2, "arrival replay preserves exact ownership, density and timestamps after migration") func _expect(ok: bool, label: String) -> void: _checks += 1 if not ok: _failures += 1 push_error(label)