Files
TheTree_mock/tests/s1/test_facility_infrastructure_projector.gd
T
Takeshi 5dde0c5ec2 Record original-alignment implementation and traceability checkpoint
Capture current source, content data, tests, asset licenses, and original-specification traceability. Original implementation remains incomplete. The accepted matching UI generation passed 154 suites and 12,253 checks, with eight rendered staff-contract screens reviewed. Latest native Windows and full-original acceptance remain outstanding.
2026-09-30 23:52:24 +09:00

602 lines
38 KiB
GDScript

extends SceneTree
const ProjectorType = preload("res://scripts/world/facility_infrastructure_projector.gd")
const AdapterType = preload("res://scripts/world/route_subslot_path_adapter.gd")
const SpatialType = preload("res://scripts/world/spatial_subslot_service.gd")
const PathType = preload("res://scripts/world/path_service.gd")
const WorldType = preload("res://scripts/world/world_service.gd")
const FacilityDefinitionType = preload("res://scripts/data/facility_definition.gd")
const CommandResultType = preload("res://scripts/core/command_result.gd")
const GUEST_CORRIDOR = preload("res://data/facilities/guest_corridor.tres")
const SERVICE_CORRIDOR = preload("res://data/facilities/service_corridor.tres")
const STAFF_DOOR = preload("res://data/facilities/staff_door.tres")
const GUEST_ELEVATOR = preload("res://data/facilities/guest_elevator.tres")
const SERVICE_ELEVATOR = preload("res://data/facilities/service_elevator.tres")
class ControlledPathService:
extends PathService
var fail_next_rebuild_after_mutation := false
var fail_next_connector_after_mutation := false
var fail_next_remove_after_mutation := false
var reject_restore := false
var reenter_next_connector := false
var tamper_projector_next_connector := false
var projector: Variant = null
var reentry_result: Variant = null
func rebuild_graph(passable_cells: Variant) -> bool:
var should_fail := fail_next_rebuild_after_mutation
fail_next_rebuild_after_mutation = false
var result := super.rebuild_graph(passable_cells)
if should_fail:
last_error = "Injected graph rebuild failure after mutation."
return false
return result
func configure_connector(
connector_id: StringName,
from_cell: Vector3i,
to_cell: Vector3i,
cost := DEFAULT_CONNECTOR_COST,
bidirectional := true,
access_id: StringName = &"",
enabled := true,
options: Dictionary = {}
) -> bool:
var should_fail := fail_next_connector_after_mutation
fail_next_connector_after_mutation = false
var should_reenter := reenter_next_connector
reenter_next_connector = false
var should_tamper := tamper_projector_next_connector
tamper_projector_next_connector = false
var result := super.configure_connector(
connector_id, from_cell, to_cell, cost, bidirectional, access_id, enabled, options
)
if should_reenter and projector != null:
reentry_result = projector.unproject_facility(&"nested_unproject", &"facility_nested")
if should_tamper and projector != null:
projector.projection_revision += 100
if should_fail:
last_error = "Injected connector failure after mutation."
return false
return result
func remove_connector(connector_id: StringName) -> bool:
var should_fail := fail_next_remove_after_mutation
fail_next_remove_after_mutation = false
var result := super.remove_connector(connector_id)
if should_fail:
last_error = "Injected connector removal failure after mutation."
return false
return result
func restore_snapshot(snapshot: Dictionary) -> bool:
if reject_restore:
last_error = "Injected Path snapshot restore rejection."
return false
return super.restore_snapshot(snapshot)
var _checks := 0
var _failures := 0
func _initialize() -> void:
call_deferred("_run")
func _run() -> void:
print("S1: completed-facility infrastructure projection")
_test_corridors_and_primary_world_coexistence()
_test_staff_door_access_and_stable_ids()
_test_project_unproject_replay_and_defensive_queries()
_test_vertical_topology_projects_without_walk_bypass()
_test_definition_and_instance_crosslink_validation()
_test_project_and_unproject_failures_roll_back_exactly()
_test_callback_reentry_tamper_and_quarantine_recovery()
_test_strict_snapshot_round_trip_and_atomic_rejection()
_test_configuration_and_stale_dependency_boundaries()
_test_validation_cache_detects_changes()
if _failures == 0:
print("PASS: %d checks" % _checks)
else:
push_error("FAIL: %d of %d checks failed" % [_failures, _checks])
quit(_failures)
func _test_corridors_and_primary_world_coexistence() -> void:
var front_start := Vector3i(0, 0, PathType.FRONT)
var front_mid := Vector3i(1, 0, PathType.FRONT)
var front_goal := Vector3i(2, 0, PathType.FRONT)
var back_start := Vector3i(0, 2, PathType.BACK)
var back_mid := Vector3i(1, 2, PathType.BACK)
var back_goal := Vector3i(2, 2, PathType.BACK)
var context := _context([front_start, front_goal, back_start, back_goal])
var world := WorldType.new()
world.reset({&"min_x": -2, &"max_x": 4, &"min_y": -2, &"max_y": 4})
_expect(world.occupy(&"facility_00000001", [front_mid], &"facility"), "completed facility occupies the primary World slot")
var guest_instance := _instance(GUEST_CORRIDOR, &"facility_00000001", front_mid, 10, &"construction_00000001")
var guest_result = context.projector.project_facility(&"project_guest", GUEST_CORRIDOR, guest_instance)
_expect(guest_result.is_success(), "guest corridor projects through the atomic boundary")
_expect(context.path.find_path(front_start, front_goal) == [front_start, front_mid, front_goal], "guest corridor makes the real FRONT Path reachable")
_expect(world.get_occupant(front_mid) == &"facility_00000001", "ROUTE subslot projection preserves primary World occupancy")
var guest_projection: Dictionary = context.projector.get_projection(&"facility_00000001")
_expect(guest_projection[&"layer"] == PathType.FRONT and not guest_projection[&"staff_only"], "guest corridor metadata preserves FRONT guest meaning")
var service_instance := _instance(SERVICE_CORRIDOR, &"facility_00000002", back_mid, 11, &"construction_00000002")
var service_result = context.projector.project_facility(&"project_service", SERVICE_CORRIDOR, service_instance)
_expect(service_result.is_success(), "service corridor projects through the same boundary")
_expect(context.path.find_path(back_start, back_goal) == [back_start, back_mid, back_goal], "service corridor makes the real BACK Path reachable")
_expect(context.path.find_path(back_start, back_goal, [PathType.FRONT]).is_empty(), "FRONT-only guest profile cannot use the staff-only BACK corridor")
var service_projection: Dictionary = context.projector.get_projection(&"facility_00000002")
_expect(service_projection[&"layer"] == PathType.BACK and service_projection[&"staff_only"], "service corridor retains explicit staff-only semantics")
_expect(context.path.find_path(front_start, back_goal, [PathType.FRONT, PathType.BACK]).is_empty(), "FRONT and BACK remain separated without a staff door")
_expect(context.projector.projection_revision == 2, "corridor projections advance one deterministic revision each")
_expect(context.projector.validate_invariants()[&"ok"], "corridor route/entity/cell crosslinks are valid")
func _test_staff_door_access_and_stable_ids() -> void:
var front := Vector3i(5, 0, PathType.FRONT)
var back := Vector3i(5, 0, PathType.BACK)
var context := _context([front])
var instance := _instance(STAFF_DOOR, &"facility_00000003", back, 20, &"construction_00000003")
var result = context.projector.project_facility(&"project_staff_door", STAFF_DOOR, instance)
_expect(result.is_success(), "staff door projects its BACK endpoint and connector atomically")
var projection: Dictionary = context.projector.get_projection(&"facility_00000003")
_expect(projection[&"route_entity_id"] == &"infra.route:facility_00000003", "staff door route entity ID is stable")
_expect(projection[&"connector_id"] == &"infra.connector:facility_00000003", "staff door connector ID is stable")
_expect(context.source.get_entity_cells(projection[&"route_entity_id"]) == [back], "staff door owns exactly its BACK ROUTE endpoint")
var connector: Dictionary = context.path.get_connector(projection[&"connector_id"])
_expect(connector[&"from"] == front and connector[&"to"] == back, "connector joins the same coordinate across FRONT/BACK")
_expect(connector[&"cost"] == 120 and connector[&"bidirectional"] and connector[&"access_id"] == &"staff", "connector is bidirectional, cost 120, and staff-gated")
_expect(context.path.find_path(front, back, [PathType.FRONT, PathType.BACK], []).is_empty(), "guest access cannot traverse the staff door")
_expect(context.path.last_no_route_reason != context.path.ROUTE_OK, "guest denial reports a real no-route reason rather than fake reachability")
_expect(context.path.find_path(front, back, [PathType.FRONT, PathType.BACK], [&"staff"]) == [front, back], "staff connector access traverses the real door")
_expect(context.path.last_path_cost == 120, "staff door route report uses the exact specification cost")
_expect(context.path.calculate_path_cost([front, back], [&"staff"]) == 120, "explicit Path cost calculation preserves staff-door cost 120")
_expect(context.projector.validate_invariants()[&"ok"], "staff-door connector and route crosslinks are exact")
func _test_project_unproject_replay_and_defensive_queries() -> void:
var cell := Vector3i(1, 4, PathType.FRONT)
var context := _context([])
var instance := _instance(GUEST_CORRIDOR, &"facility_00000004", cell, 30, &"construction_00000004")
var first = context.projector.project_facility(&"idem_project", GUEST_CORRIDOR, instance)
var after_projector: Dictionary = context.projector.create_snapshot()
var after_source: Dictionary = context.source.create_snapshot()
var after_adapter: Dictionary = context.adapter.create_snapshot()
var after_path: Dictionary = context.path.create_snapshot()
var replay = context.projector.project_facility(&"idem_project", GUEST_CORRIDOR, instance)
_expect(first.is_success() and replay.is_success() and replay.is_replay, "same project request and fingerprint replays")
_expect(replay.details == first.details, "project replay preserves exact result details")
_expect(context.projector.create_snapshot() == after_projector, "project replay does not advance projector state")
_expect(context.source.create_snapshot() == after_source and context.adapter.create_snapshot() == after_adapter and context.path.create_snapshot() == after_path, "project replay leaves all three dependencies exact")
var changed_instance := instance.duplicate(true)
changed_instance[&"completed_at"] = 31
var conflict = context.projector.project_facility(&"idem_project", GUEST_CORRIDOR, changed_instance)
_expect(conflict.code == CommandResultType.Code.DUPLICATE_IDEMPOTENCY_KEY, "same request ID with changed completed input conflicts")
var duplicate = context.projector.project_facility(&"another_project", GUEST_CORRIDOR, instance)
_expect(duplicate.code == CommandResultType.Code.DUPLICATE_COMMAND_ID, "another request cannot double-project one facility")
var returned: Dictionary = context.projector.get_projection(&"facility_00000004")
returned[&"cells"].clear()
var ids: Array[StringName] = context.projector.get_projection_ids()
ids.clear()
var status: Dictionary = context.projector.get_status()
status[&"projection_count"] = 99
_expect(context.projector.get_projection(&"facility_00000004")[&"cells"] == [cell], "projection query is a defensive deep copy")
_expect(context.projector.get_projection_ids() == [&"facility_00000004"], "projection ID query is defensive and lexical")
_expect(context.projector.get_status()[&"projection_count"] == 1, "status query is defensive")
var removed = context.projector.unproject_facility(&"idem_remove", &"facility_00000004")
_expect(removed.is_success(), "facility infrastructure unprojects atomically")
_expect(not context.source.has_entity(&"infra.route:facility_00000004") and not context.path.has_cell(cell), "unproject removes the route from both source and real Path")
var removed_snapshot: Dictionary = context.projector.create_snapshot()
var remove_replay = context.projector.unproject_facility(&"idem_remove", &"facility_00000004")
_expect(remove_replay.is_success() and remove_replay.is_replay, "same unproject request replays after the entity is gone")
_expect(context.projector.create_snapshot() == removed_snapshot, "unproject replay is mutation-free")
var remove_conflict = context.projector.unproject_facility(&"idem_remove", &"facility_other")
_expect(remove_conflict.code == CommandResultType.Code.DUPLICATE_IDEMPOTENCY_KEY, "unproject request conflict is rejected")
_expect(context.projector.projection_revision == 2, "project and unproject each advance exactly once")
func _test_vertical_topology_projects_without_walk_bypass() -> void:
var context := _context([Vector3i(0, 0, PathType.FRONT)])
var guest_instance := _instance(GUEST_ELEVATOR, &"facility_00000005", Vector3i(4, 4, PathType.FRONT), 40, &"construction_00000005")
var guest = context.projector.project_facility(&"guest_elevator", GUEST_ELEVATOR, guest_instance)
_expect(guest.is_success(), "guest elevator projects completed shaft topology")
var service_instance := _instance(SERVICE_ELEVATOR, &"facility_00000006", Vector3i(8, 4, PathType.BACK), 41, &"construction_00000006")
var service = context.projector.project_facility(&"service_elevator", SERVICE_ELEVATOR, service_instance)
_expect(service.is_success(), "service elevator projects completed shaft topology")
_expect(context.path.find_path(Vector3i(4, 4, 0), Vector3i(4, 5, 0)).is_empty(), "guest shaft cannot be climbed without its separately owned cabin connector")
_expect(context.path.find_path(Vector3i(8, 4, 1), Vector3i(8, 5, 1)).is_empty(), "service shaft cannot be climbed by ordinary walk edges")
_expect(context.path.get_vertical_walk_blockers().size() == 2 and context.projector.validate_invariants()[&"ok"], "both completed elevator footprints retain authoritative owned shaft restrictions")
func _test_definition_and_instance_crosslink_validation() -> void:
var context := _context([])
var base_instance := _instance(GUEST_CORRIDOR, &"facility_00000007", Vector3i(0, 6, PathType.FRONT), 50, &"construction_00000007")
var before := _all_snapshots(context)
var wrong_layer: FacilityDefinition = GUEST_CORRIDOR.duplicate(true)
wrong_layer.layer = FacilityDefinitionType.ViewLayer.BACK
_expect(not context.projector.project_facility(&"bad_definition_layer", wrong_layer, base_instance).is_success(), "definition layer mismatch is rejected")
var wrong_tags: FacilityDefinition = GUEST_CORRIDOR.duplicate(true)
wrong_tags.tags = [&"corridor", &"movement", &"route_subslot", &"schema_facility_proxy"]
_expect(not context.projector.project_facility(&"bad_definition_tags", wrong_tags, base_instance).is_success(), "definition tag mismatch is rejected")
var wrong_pass: FacilityDefinition = GUEST_CORRIDOR.duplicate(true)
wrong_pass.pass_through = false
_expect(not context.projector.project_facility(&"bad_definition_pass", wrong_pass, base_instance).is_success(), "definition pass-through mismatch is rejected")
var unknown: FacilityDefinition = GUEST_CORRIDOR.duplicate(true)
unknown.id = &"unknown_proxy"
_expect(not context.projector.project_facility(&"unknown_definition", unknown, base_instance).is_success(), "unknown proxy definition is rejected")
var extra := base_instance.duplicate(true)
extra[&"extra"] = true
_expect(not context.projector.project_facility(&"instance_extra", GUEST_CORRIDOR, extra).is_success(), "completed instance with an extra field is rejected")
var wrong_definition := base_instance.duplicate(true)
wrong_definition[&"definition_id"] = &"service_corridor"
_expect(not context.projector.project_facility(&"instance_definition", GUEST_CORRIDOR, wrong_definition).is_success(), "instance-to-definition ID mismatch is rejected")
var wrong_state := base_instance.duplicate(true)
wrong_state[&"state"] = &"PLANNED"
_expect(not context.projector.project_facility(&"instance_state", GUEST_CORRIDOR, wrong_state).is_success(), "non-ACTIVE completed instance is rejected")
var wrong_layer_instance := base_instance.duplicate(true)
wrong_layer_instance[&"layer"] = PathType.BACK
_expect(not context.projector.project_facility(&"instance_layer", GUEST_CORRIDOR, wrong_layer_instance).is_success(), "instance layer crosslink mismatch is rejected")
var wrong_cells := base_instance.duplicate(true)
wrong_cells[&"occupied_cells"] = [Vector3i(1, 6, PathType.FRONT)]
_expect(not context.projector.project_facility(&"instance_cells", GUEST_CORRIDOR, wrong_cells).is_success(), "instance cells outside its definition footprint are rejected")
var duplicate_cells := base_instance.duplicate(true)
duplicate_cells[&"occupied_cells"] = [base_instance[&"origin"], base_instance[&"origin"]]
_expect(not context.projector.project_facility(&"instance_duplicate_cells", GUEST_CORRIDOR, duplicate_cells).is_success(), "duplicate instance cells are rejected")
var float_cost := base_instance.duplicate(true)
float_cost[&"construction_cost"] = {&"gold": 20.0}
_expect(not context.projector.project_facility(&"instance_cost_type", GUEST_CORRIDOR, float_cost).is_success(), "non-integer construction cost is rejected")
var negative_cost := base_instance.duplicate(true)
negative_cost[&"construction_cost"] = {&"gold": -1}
_expect(not context.projector.project_facility(&"instance_cost_negative", GUEST_CORRIDOR, negative_cost).is_success(), "negative construction cost is rejected")
var bad_request = context.projector.project_facility(4, GUEST_CORRIDOR, base_instance)
_expect(bad_request.code == CommandResultType.Code.INVALID_PAYLOAD, "numeric request ID is rejected rather than coerced")
_expect(_all_snapshots(context) == before, "all definition/instance validation failures preserve every service exactly")
func _test_project_and_unproject_failures_roll_back_exactly() -> void:
var path := ControlledPathService.new()
var context := _context([Vector3i(0, 8, PathType.FRONT)], path)
path.projector = context.projector
var guest := _instance(GUEST_CORRIDOR, &"facility_00000008", Vector3i(1, 8, PathType.FRONT), 60, &"construction_00000008")
var before := _all_snapshots(context)
path.fail_next_rebuild_after_mutation = true
var route_failure = context.projector.project_facility(&"retry_route_failure", GUEST_CORRIDOR, guest)
_assert_exact_rollback(context, before, route_failure, "route adapter Path rebuild failure")
_expect(context.projector.project_facility(&"retry_route_failure", GUEST_CORRIDOR, guest).is_success(), "failed project request can retry after exact rollback")
var door := _instance(STAFF_DOOR, &"facility_00000009", Vector3i(3, 8, PathType.BACK), 61, &"construction_00000009")
before = _all_snapshots(context)
path.fail_next_connector_after_mutation = true
var connector_failure = context.projector.project_facility(&"retry_connector_failure", STAFF_DOOR, door)
_assert_exact_rollback(context, before, connector_failure, "staff connector apply failure")
_expect(not context.source.has_entity(&"infra.route:facility_00000009") and not context.path.has_connector(&"infra.connector:facility_00000009"), "failed staff-door projection leaves neither route nor connector")
_expect(context.projector.project_facility(&"retry_connector_failure", STAFF_DOOR, door).is_success(), "failed staff-door request retries after exact rollback")
before = _all_snapshots(context)
path.fail_next_remove_after_mutation = true
var remove_failure = context.projector.unproject_facility(&"retry_remove_failure", &"facility_00000009")
_assert_exact_rollback(context, before, remove_failure, "staff connector removal failure")
_expect(context.source.has_entity(&"infra.route:facility_00000009") and context.path.has_connector(&"infra.connector:facility_00000009"), "failed unproject restores route and connector")
_expect(context.projector.unproject_facility(&"retry_remove_failure", &"facility_00000009").is_success(), "failed unproject request retries after exact rollback")
path.projector = null
func _test_callback_reentry_tamper_and_quarantine_recovery() -> void:
var path := ControlledPathService.new()
var front := Vector3i(5, 10, PathType.FRONT)
var context := _context([front], path)
path.projector = context.projector
var first_door := _instance(STAFF_DOOR, &"facility_00000010", Vector3i(5, 10, PathType.BACK), 70, &"construction_00000010")
path.reenter_next_connector = true
var outer = context.projector.project_facility(&"outer_reentry", STAFF_DOOR, first_door)
_expect(outer.is_success(), "outer staff-door transaction commits when callback only attempts reentry")
_expect(path.reentry_result != null and path.reentry_result.code == CommandResultType.Code.TRANSACTION_CLOSED, "Path callback reentry is rejected")
_expect(context.projector.projection_revision == 1, "reentry cannot advance a nested projector revision")
var second_door := _instance(STAFF_DOOR, &"facility_00000011", Vector3i(7, 10, PathType.BACK), 71, &"construction_00000011")
var before := _all_snapshots(context)
path.tamper_projector_next_connector = true
var tampered = context.projector.project_facility(&"callback_tamper", STAFF_DOOR, second_door)
_assert_exact_rollback(context, before, tampered, "Path callback projector tamper")
_expect(context.projector.projection_revision == 1 and not context.projector.has_projection(&"facility_00000011"), "callback tamper cannot leak owned metadata")
var recovery := _all_snapshots(context)
path.fail_next_connector_after_mutation = true
path.reject_restore = true
var uncertain_door := _instance(STAFF_DOOR, &"facility_00000012", Vector3i(9, 10, PathType.BACK), 72, &"construction_00000012")
var uncertain = context.projector.project_facility(&"uncertain_project", STAFF_DOOR, uncertain_door)
_expect(not uncertain.is_success() and not uncertain.details[&"rollback_ok"], "uncertain dependency rollback reports its uncertainty")
_expect(context.projector.is_quarantined() and context.projector.get_quarantine_reason() == context.projector.QUARANTINE_TRANSACTION_ROLLBACK, "uncertain rollback quarantines the projector")
_expect(context.projector.project_facility(&"blocked_quarantine", GUEST_CORRIDOR, _instance(GUEST_CORRIDOR, &"facility_00000013", Vector3i(11, 10, 0), 73, &"construction_00000013")).code == CommandResultType.Code.TRANSACTION_CLOSED, "quarantine blocks further projection")
path.reject_restore = false
_expect(context.source.restore_snapshot(recovery.source), "quarantine recovery restores the Spatial snapshot")
_restore_path_for_test(context.path, recovery.path_cells, recovery.path)
_expect(context.adapter.restore_snapshot(recovery.adapter), "quarantine recovery restores the Route adapter snapshot")
_expect(context.projector.restore_snapshot(recovery.projector), "matching projector snapshot clears quarantine")
_expect(not context.projector.is_quarantined() and context.projector.validate_invariants()[&"ok"], "recovered projector returns to a valid available state")
path.projector = null
func _test_strict_snapshot_round_trip_and_atomic_rejection() -> void:
var base := [
Vector3i(0, 12, PathType.FRONT),
Vector3i(4, 12, PathType.FRONT),
]
var context := _context(base)
var guest := _instance(GUEST_CORRIDOR, &"facility_00000020", Vector3i(1, 12, 0), 80, &"construction_00000020")
var door := _instance(STAFF_DOOR, &"facility_00000021", Vector3i(3, 12, 1), 81, &"construction_00000021")
context.projector.project_facility(&"z_project", GUEST_CORRIDOR, guest)
context.projector.project_facility(&"a_project", STAFF_DOOR, door)
context.projector.unproject_facility(&"m_unproject", &"facility_00000020")
var snapshot: Dictionary = context.projector.create_snapshot()
_expect(snapshot[&"projections"].map(func(row: Dictionary) -> String: return row[&"instance_id"]) == ["facility_00000021"], "snapshot stores only current projections in lexical order")
_expect(snapshot[&"processed_requests"].map(func(row: Dictionary) -> String: return row[&"request_id"]) == ["a_project", "m_unproject", "z_project"], "snapshot history is lexical independently of revision order")
_expect(snapshot[&"projection_revision"] == 3, "snapshot preserves contiguous successful revision history")
var restored_source := SpatialType.new()
var restored_path := PathType.new()
var restored_adapter := AdapterType.new()
_expect(restored_adapter.configure(restored_source, restored_path, base), "round-trip target adapter configures empty dependencies")
var restored_projector := ProjectorType.new()
_expect(restored_projector.configure(restored_adapter, restored_source, restored_path), "round-trip target projector configures before dependency restore")
_expect(restored_source.restore_snapshot(context.source.create_snapshot()), "round-trip target restores Spatial state")
_restore_path_for_test(restored_path, _path_cells(context.path), context.path.create_snapshot())
_expect(restored_adapter.restore_snapshot(context.adapter.create_snapshot()), "round-trip target restores Route adapter state")
_expect(restored_projector.restore_snapshot(snapshot), "strict current projector snapshot restores against matching dependencies")
_expect(restored_projector.create_snapshot() == snapshot, "projector snapshot round trip is exact")
_expect(restored_projector.validate_invariants()[&"ok"], "restored history/projection/dependency crosslinks validate")
var replay_source: Dictionary = restored_source.create_snapshot()
var replay_adapter: Dictionary = restored_adapter.create_snapshot()
var replay_path: Dictionary = restored_path.create_snapshot()
var restored_replay = restored_projector.project_facility(&"a_project", STAFF_DOOR, door)
_expect(restored_replay.is_success() and restored_replay.is_replay, "restored project history replays with the same canonical input")
_expect(restored_projector.create_snapshot() == snapshot and restored_source.create_snapshot() == replay_source and restored_adapter.create_snapshot() == replay_adapter and restored_path.create_snapshot() == replay_path, "restored replay leaves metadata and dependencies unchanged")
var restored_context := {
&"source": restored_source,
&"path": restored_path,
&"adapter": restored_adapter,
&"projector": restored_projector,
}
var before := _all_snapshots(restored_context)
var wrong_version: Dictionary = snapshot.duplicate(true)
wrong_version[&"schema_version"] = 2
_atomic_snapshot_reject(restored_context, wrong_version, before, "unknown snapshot version")
var version_type: Dictionary = snapshot.duplicate(true)
version_type[&"schema_version"] = "1"
_atomic_snapshot_reject(restored_context, version_type, before, "string snapshot version")
var unknown_field: Dictionary = snapshot.duplicate(true)
unknown_field[&"extra"] = true
_atomic_snapshot_reject(restored_context, unknown_field, before, "unknown root field")
var revision_type: Dictionary = snapshot.duplicate(true)
revision_type[&"projection_revision"] = 3.0
_atomic_snapshot_reject(restored_context, revision_type, before, "floating projection revision")
var projection_order: Dictionary = snapshot.duplicate(true)
projection_order[&"projections"].append(projection_order[&"projections"][0].duplicate(true))
_atomic_snapshot_reject(restored_context, projection_order, before, "duplicate projection order")
var request_order: Dictionary = snapshot.duplicate(true)
request_order[&"processed_requests"].reverse()
_atomic_snapshot_reject(restored_context, request_order, before, "unordered request history")
var fingerprint: Dictionary = snapshot.duplicate(true)
fingerprint[&"processed_requests"][0][&"fingerprint"] = "0".repeat(64)
_atomic_snapshot_reject(restored_context, fingerprint, before, "tampered request fingerprint")
var result_projection: Dictionary = snapshot.duplicate(true)
result_projection[&"processed_requests"][0][&"details"][&"projection"][&"staff_only"] = false
_atomic_snapshot_reject(restored_context, result_projection, before, "tampered result projection semantics")
var history_revision: Dictionary = snapshot.duplicate(true)
history_revision[&"processed_requests"][0][&"details"][&"projection_revision"] = 3
_atomic_snapshot_reject(restored_context, history_revision, before, "duplicate history revision")
var final_crosslink: Dictionary = snapshot.duplicate(true)
final_crosslink[&"projections"].clear()
_atomic_snapshot_reject(restored_context, final_crosslink, before, "history-to-current projection mismatch")
var record_fingerprint: Dictionary = snapshot.duplicate(true)
record_fingerprint[&"projections"][0][&"record_fingerprint"] = "bad"
_atomic_snapshot_reject(restored_context, record_fingerprint, before, "tampered projection record fingerprint")
var extra_projection_field: Dictionary = snapshot.duplicate(true)
extra_projection_field[&"projections"][0][&"extra"] = true
_atomic_snapshot_reject(restored_context, extra_projection_field, before, "unknown projection field")
var deep: Dictionary = snapshot.duplicate(true)
var deep_value: Variant = []
for _index: int in range(restored_projector.MAX_SNAPSHOT_DEPTH + 2):
deep_value = [deep_value]
deep[&"processed_requests"] = deep_value
_atomic_snapshot_reject(restored_context, deep, before, "excessive snapshot depth")
var connector_id := &"infra.connector:facility_00000021"
var connector_before := restored_path.get_connector(connector_id)
_expect(restored_path.configure_connector(connector_id, connector_before[&"from"], connector_before[&"to"], 2, true, &"staff"), "test can introduce a live connector crosslink mismatch")
var stale_owned_before := restored_projector.create_snapshot()
var dependencies_before_restore := {
&"source": restored_source.create_snapshot(),
&"adapter": restored_adapter.create_snapshot(),
&"path": restored_path.create_snapshot(),
}
_expect(not restored_projector.restore_snapshot(snapshot), "valid metadata snapshot rejects a mismatched live staff connector")
_expect(restored_projector.create_snapshot() == stale_owned_before, "live-crosslink rejection is projector-atomic")
_expect(restored_source.create_snapshot() == dependencies_before_restore.source and restored_adapter.create_snapshot() == dependencies_before_restore.adapter and restored_path.create_snapshot() == dependencies_before_restore.path, "live-crosslink rejection preserves all dependency snapshots")
func _test_configuration_and_stale_dependency_boundaries() -> void:
var source_a := SpatialType.new()
var source_b := SpatialType.new()
var path_a := PathType.new()
var path_b := PathType.new()
var adapter := AdapterType.new()
adapter.configure(source_a, path_a, [])
var projector := ProjectorType.new()
_expect(not projector.configure(adapter, source_b, path_a), "configuration rejects a different Spatial instance with equal state")
_expect(not projector.configure(adapter, source_a, path_b), "configuration rejects a different Path instance with equal state")
_expect(projector.configure(adapter, source_a, path_a), "configuration accepts the exact adapter binding")
_expect(not projector.configure(adapter, source_a, path_a), "configured projector rejects reconfiguration")
var context := _context([])
var instance := _instance(GUEST_CORRIDOR, &"facility_00000030", Vector3i(1, 14, 0), 90, &"construction_00000030")
context.projector.project_facility(&"stale_project", GUEST_CORRIDOR, instance)
var before := _all_snapshots(context)
_expect(context.source.place(&"external_route", context.source.CATEGORY_ROUTE, [Vector3i(2, 14, 0)]).is_success(), "external ROUTE mutation can stale the lower adapter")
var source_after_external: Dictionary = context.source.create_snapshot()
var blocked = context.projector.unproject_facility(&"blocked_stale", &"facility_00000030")
_expect(blocked.code == CommandResultType.Code.TRANSACTION_CLOSED, "stale dependencies block projector mutation")
_expect(context.projector.create_snapshot() == before.projector and context.adapter.create_snapshot() == before.adapter and context.path.create_snapshot() == before.path, "stale rejection preserves projector, adapter, and Path state")
_expect(context.source.create_snapshot() == source_after_external, "stale rejection does not undo an external source mutation")
_expect(context.projector.is_projection_stale(), "projector exposes stale dependency status")
var collision_source := SpatialType.new()
var collision_path := PathType.new()
var collision_adapter := AdapterType.new()
collision_adapter.configure(collision_source, collision_path, [])
collision_adapter.place_route(&"preexisting_managed", &"infra.route:foreign", [Vector3i(4, 14, 0)])
var collision_projector := ProjectorType.new()
_expect(not collision_projector.configure(collision_adapter, collision_source, collision_path), "configuration rejects an untracked managed-ID namespace collision")
func _test_validation_cache_detects_changes() -> void:
var start := Vector3i(0, 0, PathType.FRONT)
var middle := Vector3i(1, 0, PathType.FRONT)
var goal := Vector3i(2, 0, PathType.FRONT)
var context := _context([start, goal])
var instance := _instance(GUEST_CORRIDOR, &"facility_cache", middle, 10, &"construction_cache")
_expect(context.projector.project_facility(&"project_cache", GUEST_CORRIDOR, instance).is_success(), "cache fixture projects corridor")
_expect(context.projector.validate_invariants()[&"ok"], "initial complete validation succeeds")
_expect(context.projector.validate_invariants()[&"ok"], "unchanged validation succeeds")
var before: Dictionary = context.projector.create_snapshot()
var exported: Dictionary = context.projector.create_snapshot()
exported[&"projections"][0][&"cells"].clear()
_expect(context.projector.validate_invariants()[&"ok"] and context.projector.create_snapshot() == before, "exported containers cannot alter validation cache or live state")
var records: Dictionary = context.projector.get("_projections")
var fingerprint: String = records[&"facility_cache"][&"record_fingerprint"]
records[&"facility_cache"][&"record_fingerprint"] = "invalid"
_expect(not context.projector.validate_invariants()[&"ok"], "warm cache detects changed projection content at unchanged revision")
records[&"facility_cache"][&"record_fingerprint"] = fingerprint
_expect(context.projector.validate_invariants()[&"ok"], "repaired owned state validates")
var requests: Dictionary = context.projector.get("_processed_requests")
var request_fingerprint: String = requests[&"project_cache"][&"fingerprint"]
requests[&"project_cache"][&"fingerprint"] = "invalid"
_expect(not context.projector.validate_invariants()[&"ok"], "warm cache detects changed request history")
requests[&"project_cache"][&"fingerprint"] = request_fingerprint
var path_snapshot: Dictionary = context.path.create_snapshot()
_expect(context.path.rebuild_graph([start, goal]), "inject live path change without projector mutation")
_expect(not context.projector.validate_invariants()[&"ok"], "unchanged owned snapshot still validates live dependencies")
_expect(context.path.rebuild_graph([start, middle, goal]) and context.path.restore_snapshot(path_snapshot), "restore valid live graph and traffic snapshot")
_expect(context.projector.validate_invariants()[&"ok"], "repaired dependencies validate against warm cache")
_expect(context.projector.unproject_facility(&"unproject_cache", &"facility_cache").is_success(), "new history after cache warm can unproject")
_expect(context.projector.validate_invariants()[&"ok"], "new history receives full validation")
func _context(base_cells: Array, custom_path: Variant = null) -> Dictionary:
var source := SpatialType.new()
var path: PathService = custom_path if custom_path != null else PathType.new()
var adapter := AdapterType.new()
_expect(adapter.configure(source, path, base_cells), "test context configures Route adapter")
var projector := ProjectorType.new()
_expect(projector.configure(adapter, source, path), "test context configures Facility projector")
return {
&"source": source,
&"path": path,
&"adapter": adapter,
&"projector": projector,
}
func _instance(
definition: FacilityDefinition,
instance_id: StringName,
origin: Vector3i,
completed_at: int,
source_job_id: StringName,
rotation: int = 0
) -> Dictionary:
var dimensions := definition.footprint
if rotation in [90, 270]:
dimensions = Vector2i(definition.footprint.y, definition.footprint.x)
var cells: Array[Vector3i] = []
for y_offset: int in range(dimensions.y):
for x_offset: int in range(dimensions.x):
cells.append(origin + Vector3i(x_offset, y_offset, 0))
return {
&"instance_id": instance_id,
&"definition_id": definition.id,
&"origin": origin,
&"rotation": rotation,
&"layer": definition.layer,
&"occupied_cells": cells,
&"construction_cost": definition.construction_cost.duplicate(true),
&"source_job_id": source_job_id,
&"state": &"ACTIVE",
&"completed_at": completed_at,
}
func _all_snapshots(context: Dictionary) -> Dictionary:
return {
&"projector": context.projector.create_snapshot(),
&"source": context.source.create_snapshot(),
&"adapter": context.adapter.create_snapshot(),
&"path": context.path.create_snapshot(),
&"path_cells": _path_cells(context.path),
}
func _path_cells(path: PathService) -> Array[Vector3i]:
var result: Array[Vector3i] = []
for row: Variant in path.create_snapshot().get(&"cell_traffic", []):
if row is Dictionary and row.get(&"cell", null) is Vector3i:
result.append(row[&"cell"])
result.sort_custom(func(left: Vector3i, right: Vector3i) -> bool:
if left.y != right.y:
return left.y < right.y
if left.x != right.x:
return left.x < right.x
return left.z < right.z
)
return result
func _restore_path_for_test(path: PathService, cells: Array, snapshot: Dictionary) -> void:
path.rebuild_graph(cells)
path.restore_snapshot(snapshot)
path.rebuild_graph(cells)
func _assert_exact_rollback(
context: Dictionary,
before: Dictionary,
result: CommandResult,
label: String
) -> void:
_expect(not result.is_success() and bool(result.details.get(&"rollback_ok", false)), "%s reports exact rollback" % label)
_expect(context.projector.create_snapshot() == before.projector, "%s restores exact projector snapshot" % label)
_expect(context.source.create_snapshot() == before.source, "%s restores exact Spatial snapshot" % label)
_expect(context.adapter.create_snapshot() == before.adapter, "%s restores exact Route adapter snapshot" % label)
_expect(context.path.create_snapshot() == before.path, "%s restores exact Path snapshot" % label)
func _atomic_snapshot_reject(
context: Dictionary,
candidate: Dictionary,
before: Dictionary,
label: String
) -> void:
_expect(not context.projector.restore_snapshot(candidate), "%s is rejected" % label)
_expect(context.projector.create_snapshot() == before.projector, "%s rejection is projector-atomic" % label)
_expect(context.source.create_snapshot() == before.source and context.adapter.create_snapshot() == before.adapter and context.path.create_snapshot() == before.path, "%s rejection leaves every dependency untouched" % label)
func _expect(condition: bool, label: String) -> void:
_checks += 1
if condition:
print(" ✓ %s" % label)
return
_failures += 1
push_error(" ✗ %s" % label)