Files
TheTree_mock/tests/mvp/test_runtime_operations_integration.gd
T
Takeshi b84bade045 Save The Tree implementation and acceptance work in progress
Preserve production code, regression fixtures, audio assets and provenance, test infrastructure, and the original-completion plan and progress ledger. Final game and Windows acceptance remain pending; excluded local artifacts and native experiments are not part of this snapshot.
2026-10-11 15:58:00 +09:00

551 lines
28 KiB
GDScript

extends SceneTree
var _checks := 0
var _failures := 0
var _clock: Variant
var _state: Variant
var _runtime: Variant
func _initialize() -> void:
call_deferred("_run")
func _run() -> void:
print("MVP: Runtime operations integration")
_reset_runtime()
_test_disabled_compatibility_and_enable()
_test_enabled_snapshot_round_trip()
_test_seed_424242_second_arrival_boundary()
_test_after_mutation_notifications_are_atomic()
_test_deferred_failure_backlog_and_exact_retry()
_test_upcoming_reserved_request_capture_and_retry()
_test_reserved_enqueue_rejection_is_atomic()
_test_external_capture_rejects_invalid_state()
_test_daily_payroll_financials_and_crosslink_tamper()
_test_legacy_financial_calendar()
_cleanup()
if _failures == 0:
print("PASS: %d checks" % _checks)
else:
push_error("FAIL: %d of %d checks failed" % [_failures, _checks])
quit(_failures)
func _reset_runtime(seed: int = 20260827) -> void:
_cleanup()
_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()
var report: Dictionary = _runtime.initialize(_clock, _state, seed)
_expect(bool(report.get(&"valid", false)), "Runtime initializes before Operations opt-in")
_clock.set_paused(true)
func _cleanup() -> void:
if _runtime != null:
_runtime.free()
if _clock != null:
_clock.free()
if _state != null:
_state.free()
_runtime = null
_clock = null
_state = null
func _test_disabled_compatibility_and_enable() -> void:
var disabled: Dictionary = _runtime.get_operations_report()
_expect(not disabled[&"enabled"], "Operations remain disabled for existing Runtime tests")
var disabled_snapshot: Dictionary = _runtime.create_snapshot()
var disabled_runtime: Dictionary = disabled_snapshot[&"systems"][&"runtime"]
_expect(int(disabled_runtime[&"runtime_schema_version"]) == 14, "Runtime snapshot advances to schema v14")
_expect(
disabled_runtime[&"operations"] == {
&"wrapper_version": 1,
&"enabled": false,
&"personal_action_points": 0,
&"runtime_failure_latched": false,
&"runtime_failure_report": {},
&"deferred_runtime_minute": -1,
&"coordinator": {},
},
"disabled Operations wrapper has one canonical shape"
)
var enabled: Dictionary = _runtime.enable_production_operations()
var enabled_ok := bool(enabled.get(&"ok", false))
_expect(enabled_ok, "Production Operations opt in succeeds only at Day 1 06:00")
if not enabled_ok:
print(" enable diagnostic: %s" % JSON.stringify(enabled, "", true, true))
return
_expect(
_runtime.operations_coordinator.current_minute == 360,
"enable processes the 06:00 boundary from minute 359 exactly once"
)
_expect(
int(_runtime.get_operations_report()[&"personal_action_points"]) == 2,
"first morning boundary restores two personal action points"
)
func _test_enabled_snapshot_round_trip() -> void:
if _runtime.operations_coordinator == null:
return
var snapshot: Dictionary = _runtime.create_snapshot()
var expected_digest := SimulationDigest.compute(snapshot)
_expect(_runtime.restore_snapshot(snapshot), "enabled Runtime v5 snapshot restores")
_expect(_runtime.latest_digest == expected_digest, "enabled Runtime v5 round trip is exact")
var operations: Dictionary = _runtime.get_operations_report()
_expect(
operations[&"enabled"] and _runtime.operations_coordinator.current_minute == _clock.absolute_minute,
"restored Coordinator clock cross-link remains exact"
)
func _test_seed_424242_second_arrival_boundary() -> void:
_reset_runtime(424242)
var enabled: Dictionary = _runtime.enable_production_operations()
_expect(bool(enabled.get(&"ok", false)), "seed 424242 enables production Operations")
if not bool(enabled.get(&"ok", false)):
return
var before_count: int = _runtime.guest_service.guest_count()
_clock.advance_minutes(30)
var report: Dictionary = _runtime.get_operations_report()
_expect(
_clock.absolute_minute == 390 \
and int(report.get(&"deferred_runtime_minute", -2)) == -1 \
and not bool(report.get(&"runtime_failure_latched", true)) \
and not _runtime.operations_coordinator.is_external_quarantined(),
"seed 424242 completes the minute 390 arrival boundary without quarantine"
)
_expect(
_runtime.operations_coordinator.get_population_count() \
== _runtime.guest_service.guest_count() \
and _runtime.guest_service.guest_count() >= before_count,
"second arrival keeps Coordinator and Guest population cross-linked"
)
func _test_after_mutation_notifications_are_atomic() -> void:
_reset_runtime(424242)
_expect(
bool(_runtime.enable_production_operations().get(&"ok", false)),
"after-mutation rollback fixture enables Operations"
)
_clock.advance_minutes(29)
var before := {
&"economy": _runtime.economy_service.create_snapshot(),
&"guests": _runtime.guest_service.create_snapshot(),
&"path": _runtime.path_service.create_snapshot(),
&"movement": _runtime._movement_credits.duplicate(true),
&"progression": _runtime.progression_service.create_snapshot(),
&"events": _runtime.domain_events.create_snapshot(),
&"ids": _runtime.id_generator.create_snapshot(),
}
var signals := {&"guest": 0, &"economy": 0, &"arrival": 0}
_runtime.guest_service.guest_spawned.connect(
func(_guest: GuestRuntime) -> void: signals[&"guest"] += 1
)
_runtime.economy_service.transaction_posted.connect(
func(_transaction_id: StringName, _entries: Array) -> void:
signals[&"economy"] += 1
)
_runtime.operations_coordinator.arrivals_committed.connect(
func(_report: Dictionary) -> void: signals[&"arrival"] += 1
)
_runtime.set_operations_test_boundary_hook(
&"ARRIVAL_PLAN",
func(_payload: Dictionary, phase: StringName, _token: Variant) -> Dictionary:
return {
&"ok": phase != _runtime.OPERATIONS_TEST_AFTER_FIRST_MUTATION_PHASE,
&"reason": &"FORCED_AFTER_MUTATION_FAILURE",
}
)
_clock.advance_minutes(1)
var after := {
&"economy": _runtime.economy_service.create_snapshot(),
&"guests": _runtime.guest_service.create_snapshot(),
&"path": _runtime.path_service.create_snapshot(),
&"movement": _runtime._movement_credits.duplicate(true),
&"progression": _runtime.progression_service.create_snapshot(),
&"events": _runtime.domain_events.create_snapshot(),
&"ids": _runtime.id_generator.create_snapshot(),
}
_expect(
_clock.absolute_minute == 390 and _clock.paused and before == after,
"failure after the first arrival mutation restores every external domain exactly"
)
_expect(
int(signals[&"guest"]) == 0 and int(signals[&"economy"]) == 0 \
and int(signals[&"arrival"]) == 0,
"rolled-back arrival leaks no Guest, Economy, or Coordinator success signal"
)
_runtime.set_operations_test_boundary_hook(&"ARRIVAL_PLAN", Callable())
var retried: Dictionary = _runtime.acknowledge_operations_pause()
_expect(
bool(retried.get(&"ok", false)) and int(signals[&"guest"]) > 0 \
and int(signals[&"economy"]) > 0 and int(signals[&"arrival"]) == 1,
"successful retry flushes each staged service notification after commit"
)
func _test_deferred_failure_backlog_and_exact_retry() -> void:
_reset_runtime(424242)
_expect(
bool(_runtime.enable_production_operations().get(&"ok", false)),
"deferred retry fixture enables Operations"
)
_clock.advance_minutes(29)
var scheduled: CommandResult = _runtime.submit_command(
&"spawn_guest",
{
&"guest_id": &"scheduled_boundary_guest",
&"guest_type_id": _runtime.get_mvp_guest_definition_ids()[0],
&"gold": 25,
&"options": {
&"current_cell": Vector3i.ZERO,
&"planned_exit_at": 2_000,
},
},
&"test", &"scheduled_boundary_guest", 100, 390,
&"operations.test.scheduled.spawn"
)
_expect(scheduled.is_success(), "minute 390 command is queued before the boundary")
var counts := {&"guest": 0, &"arrival": 0, &"hook": 0}
_runtime.guest_service.guest_spawned.connect(
func(_guest: GuestRuntime) -> void: counts[&"guest"] += 1
)
_runtime.operations_coordinator.arrivals_committed.connect(
func(_report: Dictionary) -> void: counts[&"arrival"] += 1
)
_runtime.set_operations_test_boundary_hook(
&"ARRIVAL_PLAN",
func(_payload: Dictionary, phase: StringName, _token: Variant) -> Dictionary:
counts[&"hook"] += 1
return {
&"ok": false,
&"reason": &"FORCED_PREPARE_FAILURE",
&"phase": phase,
}
)
_clock.set_speed(3)
var processed: int = _clock.process_real_delta(5.0)
var failed_report: Dictionary = _runtime.get_operations_report()
_expect(
processed == 1 and _clock.absolute_minute == 390 and _clock.paused,
"boundary failure stops an eight-tick backlog at the failed minute"
)
_expect(
int(failed_report[&"deferred_runtime_minute"]) == 390 \
and failed_report[&"runtime_failure_latched"] \
and _runtime.operations_coordinator.current_minute == 389,
"failed boundary persists one canonical deferred shape"
)
_expect(
_runtime.guest_service.has_guest(&"scheduled_boundary_guest") \
and int(counts[&"guest"]) == 1 \
and int(counts[&"arrival"]) == 0,
"pre-boundary command commits once while failed callback emits no arrival notification"
)
var interrupted: Dictionary = _runtime.create_snapshot()
_expect(
_runtime.restore_snapshot(interrupted),
"deferred v5 snapshot restores before acknowledgement"
)
_runtime.set_operations_test_boundary_hook(&"ARRIVAL_PLAN", Callable())
var retry_guest_signals := {&"count": 0}
_runtime.guest_service.guest_spawned.connect(
func(_guest: GuestRuntime) -> void: retry_guest_signals[&"count"] += 1
)
var acknowledged: Dictionary = _runtime.acknowledge_operations_pause()
_expect(
bool(acknowledged.get(&"ok", false)) \
and bool(acknowledged.get(&"suffix_replayed", false)) \
and _clock.absolute_minute == 390 \
and _runtime.operations_coordinator.current_minute == 390,
"acknowledgement retries the same boundary and suffix exactly once"
)
var completion_count := 0
for event: SimulationDomainEvent in _runtime.domain_events.recent_events():
if event.event_type == &"COMMAND_COMPLETED" \
and event.causation_command_id == scheduled.command_id:
completion_count += 1
_expect(
completion_count == 1 \
and _runtime.command_queue.processed_count() == 1 \
and _runtime.guest_service.current_minute == 390 \
and int(retry_guest_signals[&"count"]) \
== _runtime.operations_coordinator.get_arrival_reports()[-1][&"total_count"],
"retry never replays the command and advances the skipped Runtime suffix once"
)
var completed: Dictionary = _runtime.create_snapshot()
_expect(_runtime.restore_snapshot(completed), "completed deferred retry round-trips exactly")
func _test_daily_payroll_financials_and_crosslink_tamper() -> void:
_reset_runtime(811_004)
var hired: CommandResult = _runtime.submit_command(
&"hire_staff_candidate",
{&"candidate_id": &"staff_akane", &"current_cell": Vector3i.ZERO},
&"player", &"staff_akane", 100, -1, &"operations.payroll.hire.akane"
)
_expect(hired.is_success(), "daily payroll fixture hires one catalog Staff member")
_expect(
bool(_runtime.enable_production_operations().get(&"ok", false)),
"daily payroll fixture enables Operations after hiring"
)
_clock.advance_minutes(1_440)
var summary: Dictionary = _runtime.operations_coordinator.get_day_summary(1)
var settlement: Dictionary = summary.get(&"settlement", {})
_expect(
_clock.absolute_minute == 1_800 \
and int(summary.get(&"closed_at", -1)) == 1_440 \
and int(summary[&"category_totals"][&"wages"]) == 120,
"00:00 closes Day 1 once and accrues only the new daily wage"
)
_expect(
int(settlement.get(&"paid_total", -1)) == 120 \
and int(settlement.get(&"arrears_total", -1)) == 0 \
and _runtime.staff_service.get_staff(&"staff_akane").unpaid_wages == 0,
"sufficient Gold pays the full lexical payroll with no arrears"
)
var wage_transaction: StringName = _runtime.economy_service.get_transaction_id_for_idempotency_key(
&"operations.wage.day.00000001"
)
var wage_entries: Array[LedgerEntry] = _runtime.economy_service.get_transaction_entries(wage_transaction)
_expect(
wage_entries.size() == 1 \
and wage_entries[0].category == &"WAGE" \
and wage_entries[0].absolute_minute == 1_440 \
and wage_entries[0].amount == 120,
"paid payroll posts one canonical WAGE ledger line at the closing boundary"
)
var canonical: Dictionary = _runtime.create_snapshot()
_expect(_runtime.restore_snapshot(canonical), "Day 1 payroll/P&L v5 snapshot round-trips")
var signing_category: Dictionary = canonical.duplicate(true)
for raw_entry: Dictionary in signing_category[&"systems"][&"runtime"][&"economy"][&"entries"]:
if String(raw_entry[&"category"]) == "STAFF_SIGNING_COST":
raw_entry[&"category"] = "TRANSFER"
break
_expect(
_rejects_atomically(signing_category),
"closed non-WAGE ledger category tamper is rejected atomically"
)
var wage_category: Dictionary = canonical.duplicate(true)
for raw_entry: Dictionary in wage_category[&"systems"][&"runtime"][&"economy"][&"entries"]:
if String(raw_entry[&"category"]) == "WAGE":
raw_entry[&"category"] = "TRANSFER"
break
_expect(
_rejects_atomically(wage_category),
"Staff settlement cannot outlive its canonical WAGE ledger evidence"
)
var lifetime: Dictionary = canonical.duplicate(true)
lifetime[&"systems"][&"runtime"][&"progression"][&"metrics"][&"guest_lifetime"] += 1
_expect(
_rejects_atomically(lifetime),
"Progression lifetime Guest inflation is rejected against active plus persistent Guests"
)
var plan_class: Dictionary = canonical.duplicate(true)
var arrival_reports: Array = plan_class[&"systems"][&"runtime"][&"operations"][&"coordinator"][&"arrival_reports"]
var changed_plan := false
for report: Dictionary in arrival_reports:
if not (report[&"plans"] as Array).is_empty():
var plan: Dictionary = report[&"plans"][0]
plan[&"guest_class"] = "noble" if String(plan[&"guest_class"]) != "noble" else "laborer"
changed_plan = true
break
_expect(
changed_plan and _rejects_atomically(plan_class),
"arrival plan class cannot diverge from its persisted Guest definition"
)
var downgraded: Dictionary = canonical.duplicate(true)
downgraded[&"systems"][&"runtime"][&"runtime_schema_version"] = 4
downgraded[&"systems"][&"runtime"].erase(&"kitchen_production")
downgraded[&"systems"][&"runtime"].erase(&"elevators")
downgraded[&"systems"][&"runtime"].erase(&"legacy_stairs")
preload("res://tests/fixtures/pre_upkeep_runtime.gd").strip_empty_upkeep(downgraded[&"systems"][&"runtime"])
for infrastructure_key: StringName in [&"excavation", &"spatial_subslots", &"route_projection", &"facility_infrastructure"]:
downgraded[&"systems"][&"runtime"].erase(infrastructure_key)
downgraded[&"systems"][&"runtime"][&"path"][&"schema_version"] = 3
downgraded[&"systems"][&"runtime"][&"path"].erase(&"vertical_walk_blockers")
downgraded[&"systems"][&"runtime"][&"path"].erase(&"vertical_walk_requires_connector")
downgraded[&"systems"][&"runtime"][&"path"].erase(&"transit_actor_ids")
_expect(_runtime._legacy_infrastructure_history_is_absent(downgraded[&"systems"][&"runtime"]), "Operations downgrade fixture excludes unrelated later infrastructure owners")
_expect(
_rejects_atomically(downgraded),
"relabelled enabled v5 cannot discard Operations history through v4 migration"
)
func _test_legacy_financial_calendar() -> void:
_reset_runtime(811_005)
_expect(_runtime.submit_command(&"hire_staff_candidate", {&"candidate_id": &"staff_akane", &"current_cell": Vector3i.ZERO}).is_success(), "legacy Runtime fixture hires a real wage-bearing employee")
_expect(bool(_runtime.enable_production_operations().get(&"ok", false)), "legacy Runtime fixture enables operations")
# Reproduce the old calendar and its zero-arrears counter. Payroll and ledger
# entries stay untouched; only the new loss metadata is converted to v3 semantics.
_runtime.operations_coordinator._legacy_calendar_last_day = 1
_clock.advance_minutes(1440)
var snapshot: Dictionary = _runtime.create_snapshot()
var old: Dictionary = snapshot[&"systems"][&"runtime"][&"operations"][&"coordinator"]
old[&"schema_version"] = 3
old.erase(&"legacy_calendar_last_day")
old.erase(&"legacy_history_last_day")
old.erase(&"legacy_principal_profit_last_day")
old[&"consecutive_insolvency_count"] = 0
for row: Dictionary in old[&"day_summaries"]:
row[&"consecutive_insolvency_count"] = 0
old[&"alerts"] = (old[&"alerts"] as Array).filter(func(row: Dictionary) -> bool: return String(row.get(&"code", "")) != "LOSS_STREAK")
_expect(int(old[&"day_summaries"][0][&"settlement"][&"paid_total"]) == 120, "legacy fixture contains an actual paid payroll at 06:00")
_expect(_runtime.restore_snapshot(snapshot), "Runtime restores v3 06:00 payroll and ledger crosslinks")
_clock.advance_minutes(1440)
_expect(_runtime.operations_coordinator.get_day_summary(2)[&"closed_at"] == 3240, "legacy open day completes at 06:00 once")
var transition: Dictionary = _runtime.create_snapshot()
_expect(_runtime.restore_snapshot(transition), "Runtime restores the transition checkpoint")
_clock.advance_minutes(1080)
var summary: Dictionary = _runtime.operations_coordinator.get_day_summary(3)
_expect(summary[&"closed_at"] == 4320 and summary[&"period_start_minute"] == 3240, "Runtime transitions to midnight with a continuous 18-hour financial period")
_expect(_runtime.restore_snapshot(_runtime.create_snapshot()), "midnight transition preserves payroll and ledger crosslinks on load")
func _rejects_atomically(candidate: Dictionary) -> bool:
var before: Dictionary = _runtime.create_snapshot()
return not _runtime.restore_snapshot(candidate) \
and _runtime.create_snapshot() == before
func _expect(condition: bool, label: String) -> void:
_checks += 1
if condition:
print(" ✓ %s" % label)
return
_failures += 1
push_error(" ✗ %s" % label)
class RecordingInventory:
extends InventoryService
var successful_reservations: Array[Dictionary] = []
func reserve_item(owner_inventory_id: StringName, item_id: StringName, quantity: int, requester_id: StringName, purpose: StringName, created_at: int = -1, expires_at: int = -1, operation_id: StringName = &"") -> Dictionary:
var result := super.reserve_item(owner_inventory_id, item_id, quantity, requester_id, purpose, created_at, expires_at, operation_id)
if bool(result.get(&"ok", false)):
successful_reservations.append({&"result": result.duplicate(true), &"state": create_snapshot()})
return result
func _prepare_upcoming_inventory_fixture() -> FacilityInstance:
_reset_runtime(424242)
_expect(bool(_runtime.enable_production_operations().get(&"ok", false)), "upcoming inventory fixture enables Operations")
_clock.advance_minutes(29)
# Local integration seam uses the canonical definition and permitted Guest APIs.
# It does not establish construction or ordinary OS business acceptance.
_runtime._register_operational_facility({&"instance_id": &"upcoming_kitchen", &"definition_id": &"kitchen", &"completed_at": 389})
var facility: FacilityInstance = _runtime.facility_service.get_instance(&"upcoming_kitchen")
_expect(facility != null, "canonical kitchen registers for reservation integration")
if facility == null: return null
_expect(bool(_runtime.inventory_service.add_stack(facility.local_inventory_id, &"ingredient/raw_food", 8, 100, -1, 389).get(&"ok", false)), "private fixture holds canonical recipe input")
var spawned: CommandResult = _runtime.submit_command(&"spawn_guest", {&"guest_id": &"upcoming_payer", &"guest_type_id": _runtime.get_mvp_guest_definition_ids()[0], &"options": {&"current_cell": _runtime._operations_entrance_cell()}}, &"test")
_expect(spawned.is_success(), "dedicated reservation Guest spawns through Runtime command")
_expect(bool(_runtime.guest_service.transition_guest(&"upcoming_payer", GuestRuntime.State.DECIDING, 389, &"TEST_FIXTURE").get(&"ok", false)), "dedicated Guest uses permitted decision transition")
_expect(_runtime._validate_runtime_crosslinks(_runtime.facility_service, _runtime.guest_service, _runtime.economy_service, _runtime.inventory_service, _runtime.staff_service, 389, _runtime._staffing_projection_enabled), "reservation fixture starts with valid crosslinks")
return facility
func _test_upcoming_reserved_request_capture_and_retry() -> void:
for failure_phase: StringName in [&"PREPARE", &"AFTER_FIRST_MUTATION"]:
var facility := _prepare_upcoming_inventory_fixture()
if facility == null: continue
var observed := {&"token": {}, &"command_id": -1, &"completions": 0, &"guest": 0, &"economy": 0, &"arrival": 0, &"forced_failures": 0}
_runtime.command_completed.connect(func(result: CommandResult) -> void:
if result.command_id == observed[&"command_id"]: observed[&"completions"] += 1)
_runtime.guest_service.guest_spawned.connect(func(_guest: GuestRuntime) -> void:
if not observed[&"token"].is_empty(): observed[&"guest"] += 1)
_runtime.economy_service.transaction_posted.connect(func(_id: StringName, _entries: Array) -> void:
if not observed[&"token"].is_empty(): observed[&"economy"] += 1)
_runtime.operations_coordinator.arrivals_committed.connect(func(_report: Dictionary) -> void:
if not observed[&"token"].is_empty(): observed[&"arrival"] += 1)
_runtime.set_operations_test_boundary_hook(&"ARRIVAL_PLAN", func(_payload: Dictionary, phase: StringName, token: Variant) -> Dictionary:
if phase == &"PREPARE": observed[&"token"] = token.duplicate(true)
if phase == failure_phase: observed[&"forced_failures"] += 1
return {&"ok": phase != failure_phase, &"reason": &"UPCOMING_REQUEST_ROLLBACK"})
_clock.set_paused(false)
var queued: CommandResult = _runtime.submit_command(&"create_service_request", {&"request_id": &"upcoming_reserved", &"facility_id": facility.instance_id, &"guest_ids": [&"upcoming_payer"]}, &"test", facility.instance_id, 100, 390, &"upcoming.reserved")
observed[&"command_id"] = queued.command_id
_expect(queued.is_success(), "upcoming reserved request is queued for minute390")
_clock.advance_one_minute()
var token: Dictionary = observed[&"token"]
_expect(observed[&"forced_failures"] == 1, "specified arrival fault phase is reached exactly once")
_expect(not token.is_empty() and int(token.get(&"captured_minute", -1)) == 389 and int(token.get(&"captured_clock_minute", -1)) == 390, "upcoming queue preserves participant389 and captures clock390")
_expect(_clock.paused and not _runtime.operations_coordinator.is_external_quarantined() and bool(_runtime.get_operations_report().get(&"runtime_failure_latched", false)), "forced arrival failure rolls back without quarantine")
_expect(not token.is_empty() and var_to_bytes(token) == var_to_bytes(_runtime._build_operations_external_state()), "post-command external token restores literally including reservation queues IDs RNG and ledger")
_expect(observed[&"guest"] == 0 and observed[&"economy"] == 0 and observed[&"arrival"] == 0, "rolled-back arrival emits no success notification")
var request: ServiceRequest = _runtime.facility_service.get_request(&"upcoming_reserved")
_expect(request != null and request.created_at == 390 and request.enqueued_at == 390 and request.reserved_inventory_ids.size() == 1, "request effective time and one reserved input survive rollback")
if request != null and request.reserved_inventory_ids.size() == 1:
var reservation: InventoryReservation = _runtime.inventory_service.get_reservation(request.reserved_inventory_ids[0])
_expect(reservation != null and reservation.created_at == 390 and reservation.quantity == 4, "canonical raw-food reservation retains quantity and effective time")
_runtime.set_operations_test_boundary_hook(&"ARRIVAL_PLAN", Callable())
var retried: Dictionary = _runtime.acknowledge_operations_pause()
_expect(bool(retried.get(&"ok", false)) and observed[&"completions"] == 1 and observed[&"arrival"] == 1 and observed[&"guest"] > 0 and observed[&"economy"] > 0, "retry commits arrivals once without replaying reserved request")
_expect(_runtime._runtime_aggregate_committed_minute() == 390 and not bool(_runtime.get_operations_report().get(&"runtime_failure_latched", true)), "retry advances every participant suffix once")
func _request_participants() -> Dictionary:
return {&"inventory": _runtime.inventory_service.create_snapshot(), &"facilities": _runtime.facility_service.create_snapshot(), &"guests": _runtime.guest_service.create_snapshot(), &"economy": _runtime.economy_service.create_snapshot(), &"events": _runtime.domain_events.create_snapshot(), &"ids": _runtime.id_generator.create_snapshot()}
func _test_reserved_enqueue_rejection_is_atomic() -> void:
var facility := _prepare_upcoming_inventory_fixture()
if facility == null: return
# Zero queue space is an explicit rejection fixture, not a game value change.
facility.queue_capacity = 0
var recorded := RecordingInventory.new()
_expect(recorded.restore_snapshot(_runtime.inventory_service.create_snapshot()), "reservation recording seam starts from exact inventory")
_runtime.inventory_service = recorded
var observed := {&"calls": 0, &"before": {}, &"after": {}, &"result": {}, &"result_code": -1, &"guest_notifications": -1}
var notifications := {&"queue": 0, &"guest": 0}
_runtime.unregister_command_executor(&"create_service_request")
var installed: bool = _runtime.register_command_executor(&"create_service_request", func(command: SimulationCommand) -> CommandResult:
observed[&"calls"] += 1
observed[&"before"] = _request_participants()
var guest_notifications_before: int = notifications[&"guest"]
var result: CommandResult = _runtime._execute_create_service_request(command)
observed[&"after"] = _request_participants()
observed[&"result"] = result.to_dictionary()
observed[&"result_code"] = result.code
observed[&"guest_notifications"] = int(notifications[&"guest"]) - guest_notifications_before
return result)
_expect(installed, "request executor observation wrapper registers successfully")
_runtime.facility_service.request_enqueued.connect(func(_request: ServiceRequest) -> void: notifications[&"queue"] += 1)
_runtime.guest_service.guest_state_changed.connect(func(_id: StringName, _old: int, _new: int, _reason: StringName) -> void: notifications[&"guest"] += 1)
_clock.set_paused(false)
var queued: CommandResult = _runtime.submit_command(&"create_service_request", {&"facility_id": facility.instance_id, &"guest_ids": [&"upcoming_payer"]}, &"test", facility.instance_id, 100, 390, &"upcoming.rejected")
_expect(queued.is_success(), "reservation rejection command enters normal queue")
_clock.advance_one_minute()
print("REJECTION_EXECUTOR_OBSERVATION ", JSON.stringify({&"calls": observed[&"calls"], &"result_code": observed[&"result_code"], &"result": observed[&"result"], &"guest_notifications": observed[&"guest_notifications"]}, "", true, true))
_expect(observed[&"guest_notifications"] == 0, "failed executor emits no Guest state-change notification")
_expect(observed[&"calls"] == 1 and observed[&"result_code"] == CommandResult.Code.VALIDATION_FAILED, "normal executor rejects queue after attempting request once")
_expect(recorded.successful_reservations.size() == 1 and not recorded.successful_reservations[0][&"result"][&"reservation_ids"].is_empty(), "input reservation actually succeeds before enqueue rejection")
_expect(var_to_bytes(observed[&"before"]) == var_to_bytes(observed[&"after"]), "enqueue rejection restores inventory facility Guest economy events and IDs literally before completion event")
_expect(notifications[&"queue"] == 0, "failed enqueue emits no queue-success notification")
_expect(_runtime._runtime_aggregate_committed_minute() == 390 and not _runtime.operations_coordinator.is_external_quarantined(), "rejected request leaves subsequent arrival and suffix healthy")
func _test_external_capture_rejects_invalid_state() -> void:
_reset_runtime(424242)
_expect(bool(_runtime.enable_production_operations().get(&"ok", false)), "strict capture fixture enables Operations")
var before := var_to_bytes(_runtime.create_snapshot())
_expect(not _runtime._restore_operations_external_state({}) and not _runtime._restore_operations_external_state(null), "empty and invalid-type tokens are rejected")
var valid: Dictionary = _runtime._build_operations_external_state()
var corrupt: Dictionary = valid.duplicate(true)
corrupt.erase(&"economy")
_expect(not _runtime._restore_operations_external_state(corrupt), "missing required aggregate token is rejected")
_expect(before == var_to_bytes(_runtime.create_snapshot()), "invalid token rejection preserves entire live snapshot")
_runtime.facility_service.current_minute += 1
_expect(_runtime._capture_operations_external_state().is_empty(), "real participant clock mismatch yields empty capture instead of success")
_runtime.facility_service.current_minute -= 1
_expect(before == var_to_bytes(_runtime.create_snapshot()), "restored fixture minute preserves canonical state")