extends SceneTree const CommandType = preload("res://scripts/core/command.gd") const CommandResultType = preload("res://scripts/core/command_result.gd") const CommandQueueType = preload("res://scripts/core/command_queue.gd") const TransactionType = preload("res://scripts/core/state_transaction.gd") const OWNERSHIP_FIXED := true const ROLLBACK_ALIAS_FIXED := true var _checks := 0 var _failures := 0 func _initialize() -> void: call_deferred("_run") func _run() -> void: print("S0: Command Queue and State Transaction") _test_deterministic_queue_order() _test_idempotency_and_result_retention() _test_snapshot_preserves_accepted_idempotency_history() _test_legacy_snapshot_migrates_accepted_metadata() _test_transaction_commit() _test_executor_ownership() _test_validation_is_atomic() _test_apply_failure_rolls_back() _test_rollback_alias_snapshot() if _failures == 0: print("PASS: %d checks" % _checks) else: push_error("FAIL: %d of %d checks failed" % [_failures, _checks]) quit(_failures) func _test_deterministic_queue_order() -> void: var queue = CommandQueueType.new() _expect(queue.enqueue(_command(3, 11, 100, &"three")).is_success(), "command 3 accepted") _expect(queue.enqueue(_command(2, 10, 10, &"two")).is_success(), "command 2 accepted") _expect(queue.enqueue(_command(1, 10, 10, &"one")).is_success(), "command 1 accepted") _expect(queue.enqueue(_command(4, 10, 90, &"four")).is_success(), "command 4 accepted") var ready: Array = queue.drain_ready(10) var ids: Array[int] = [] for command in ready: ids.append(command.command_id) _expect(ids == [4, 1, 2], "ready order is tick, descending priority, command id") _expect(queue.next_execute_tick() == 11, "future command remains queued") func _test_idempotency_and_result_retention() -> void: var queue = CommandQueueType.new() var original = _command(10, 1, 0, &"same-operation") _expect(queue.enqueue(original).is_success(), "idempotent command accepted once") var duplicate = _command(11, 1, 0, &"same-operation") var duplicate_result = queue.enqueue(duplicate) _expect( duplicate_result.code == CommandResultType.Code.DUPLICATE_IDEMPOTENCY_KEY, "duplicate idempotency key rejected" ) queue.drain_ready(1) queue.complete(10, CommandResultType.succeeded(10, {"value": 42})) var retained = queue.get_result_by_idempotency_key(&"same-operation") _expect(retained != null and retained.details["value"] == 42, "processed result retained by key") _expect(retained.is_replay, "retained lookup is marked as replay") func _test_snapshot_preserves_accepted_idempotency_history() -> void: var queue = CommandQueueType.new() _expect(queue.enqueue(_command(1, 0, 10, &"processed-key")).is_success(), "snapshot fixture accepts keyed command") _expect(queue.enqueue(_command(2, 0, 5, &"")).is_success(), "snapshot fixture accepts keyless command") _expect(queue.enqueue(_command(3, 5, 1, &"pending-key")).is_success(), "snapshot fixture accepts pending keyed command") var completed: Array = queue.execute_ready(0, func(command: SimulationCommand) -> CommandResult: return CommandResultType.succeeded(command.command_id, {"completed": true}) ) _expect(completed.size() == 2, "snapshot fixture completes its ready commands") var snapshot: Dictionary = queue.create_snapshot() _expect(int(snapshot["version"]) == CommandQueueType.SNAPSHOT_VERSION, "Command Queue writes the current snapshot version") _expect(snapshot["accepted"] == [ {"command_id": 1, "idempotency_key": "processed-key"}, {"command_id": 2, "idempotency_key": ""}, {"command_id": 3, "idempotency_key": "pending-key"}, ], "snapshot retains accepted metadata after Command inputs leave the queue") var restored = CommandQueueType.new() _expect(restored.restore_snapshot(snapshot), "strict current Command Queue snapshot restores") _expect(restored.create_snapshot() == snapshot, "current Command Queue snapshot round trip is exact") var metadata: Array[Dictionary] = restored.get_accepted_command_metadata() metadata[0]["idempotency_key"] = "changed" metadata.clear() _expect(restored.get_accepted_command_metadata() == snapshot["accepted"], "accepted metadata query is a defensive deep copy") var live_before: Dictionary = restored.create_snapshot() var missing_map: Dictionary = snapshot.duplicate(true) missing_map["idempotency"].erase("processed-key") _expect(not restored.restore_snapshot(missing_map), "processed idempotency mapping deletion is rejected") _expect(restored.create_snapshot() == live_before, "mapping deletion rejection is restore-atomic") var changed_metadata: Dictionary = snapshot.duplicate(true) changed_metadata["accepted"][0]["idempotency_key"] = "" _expect(not restored.restore_snapshot(changed_metadata), "accepted metadata cannot disagree with the reverse map") _expect(restored.create_snapshot() == live_before, "metadata mismatch rejection is restore-atomic") var missing_metadata: Dictionary = snapshot.duplicate(true) missing_metadata["accepted"].remove_at(0) _expect(not restored.restore_snapshot(missing_metadata), "every known Command requires one accepted metadata row") var unordered_metadata: Dictionary = snapshot.duplicate(true) var first: Dictionary = unordered_metadata["accepted"][0] unordered_metadata["accepted"][0] = unordered_metadata["accepted"][1] unordered_metadata["accepted"][1] = first _expect(not restored.restore_snapshot(unordered_metadata), "accepted metadata must be ordered by Command ID") var unknown_field: Dictionary = snapshot.duplicate(true) unknown_field["extra"] = true _expect(not restored.restore_snapshot(unknown_field), "current snapshot rejects unknown root fields") var string_version: Dictionary = snapshot.duplicate(true) string_version["version"] = "2" _expect(not restored.restore_snapshot(string_version), "current snapshot rejects a coerced version") var float_result_id: Dictionary = snapshot.duplicate(true) float_result_id["processed"][0]["command_id"] = 1.0 _expect(not restored.restore_snapshot(float_result_id), "current snapshot rejects a coerced processed Command ID") _expect(restored.create_snapshot() == live_before, "all malformed current snapshots preserve live queue state") func _test_legacy_snapshot_migrates_accepted_metadata() -> void: var source = CommandQueueType.new() source.enqueue(_command(7, 0, 1, &"legacy-processed")) source.drain_ready(0) source.complete(7, CommandResultType.succeeded(7)) source.enqueue(_command(9, 20, 1, &"legacy-pending")) var legacy: Dictionary = source.create_snapshot() legacy.erase("version") legacy.erase("accepted") var restored = CommandQueueType.new() _expect(restored.restore_snapshot(legacy), "legacy unversioned Command Queue snapshot restores") var migrated: Dictionary = restored.create_snapshot() _expect(int(migrated["version"]) == CommandQueueType.SNAPSHOT_VERSION, "legacy restore writes the current version next time") _expect(migrated["accepted"] == [ {"command_id": 7, "idempotency_key": "legacy-processed"}, {"command_id": 9, "idempotency_key": "legacy-pending"}, ], "legacy reverse map reconstructs processed accepted idempotency metadata") _expect(restored.get_result_by_idempotency_key(&"legacy-processed") != null, "legacy processed key remains exactly-once after migration") func _test_transaction_commit() -> void: var state := {"gold": 100, "stock": 3} var transaction = TransactionType.new() transaction.stage_dictionary_increment(state, "gold", -40, 0) transaction.stage_dictionary_increment(state, "stock", -1, 0) _expect(state == {"gold": 100, "stock": 3}, "staging does not mutate state") var result = transaction.commit() _expect(result.is_success(), "valid transaction commits") _expect(state == {"gold": 60, "stock": 2}, "all staged changes commit together") func _test_validation_is_atomic() -> void: var state := {"gold": 100, "stock": 3} var transaction = TransactionType.new() transaction.stage_dictionary_set(state, "gold", 60) transaction.stage_dictionary_set(state, "stock", 2) transaction.add_validator(func() -> bool: return false) var result = transaction.commit() _expect(result.code == CommandResultType.Code.VALIDATION_FAILED, "validation failure reported") _expect(state == {"gold": 100, "stock": 3}, "validation failure applies no changes") func _test_apply_failure_rolls_back() -> void: var state := {"gold": 100, "stock": 3} var transaction = TransactionType.new() transaction.stage_dictionary_set(state, "gold", 60) transaction.stage_custom( "failing stock update", func() -> bool: return true, func() -> bool: state["stock"] = 2 return false, func() -> void: state["stock"] = 3 ) var result = transaction.commit() _expect( result.code == CommandResultType.Code.TRANSACTION_APPLY_FAILED, "apply failure reported" ) _expect(state == {"gold": 100, "stock": 3}, "applied changes roll back in reverse order") func _command( command_id: int, execute_tick: int, priority: int, idempotency_key: StringName ): return CommandType.new( command_id, 0, execute_tick, priority, &"Test", &"actor", &"target", {}, idempotency_key ) func _expect(condition: bool, label: String) -> void: _checks += 1 if condition: print(" ✓ %s" % label) return _failures += 1 push_error(" ✗ %s" % label) func _test_executor_ownership() -> void: var queue = CommandQueueType.new() var input = _command(91, 0, 1, &"ownership-91") input.payload = {&"nested": {&"amount": 40}} _expect(queue.enqueue(input).is_success(), "ownership fixture accepted") var ready: Array = queue.drain_ready(0) var before: Dictionary = queue.create_snapshot() ready[0].command_id = 900 ready[0].idempotency_key = &"changed" ready[0].payload[&"nested"][&"amount"] = 99 var after: Dictionary = queue.create_snapshot() _expect((after == before) == OWNERSHIP_FIXED, "drained caller identity and nested payload isolation") var restored = CommandQueueType.new() _expect(restored.restore_snapshot(after) == OWNERSHIP_FIXED, "caller mutation does not corrupt restorable accepted metadata") _expect(queue.complete(91, CommandResultType.succeeded(91)).is_success(), "complete original accepted identity") _expect(queue.in_flight_count() == 0 and queue.processed_count() == 1, "original identity completes exactly once") var executor_queue = CommandQueueType.new() _expect(executor_queue.enqueue(_command(92, 0, 1, &"ownership-92")).is_success(), "executor mutation fixture accepted") var results: Array = executor_queue.execute_ready(0, func(command: SimulationCommand) -> CommandResult: command.command_id = 901 return CommandResultType.succeeded(901, {"completed": true}) ) _expect(results.size() == 1 and (results[0].command_id == 92) == OWNERSHIP_FIXED, "executor cannot replace accepted result identity") _expect((executor_queue.in_flight_count() == 0) == OWNERSHIP_FIXED, "executor mutation does not strand accepted in-flight command") _expect((executor_queue.get_result_by_idempotency_key(&"ownership-92") != null) == OWNERSHIP_FIXED, "executor mutation preserves replay result") var mismatch_queue = CommandQueueType.new() mismatch_queue.enqueue(_command(93, 0, 1, &"ownership-93")) var mismatched: Array = mismatch_queue.execute_ready(0, func(_command_value: SimulationCommand) -> CommandResult: return CommandResultType.succeeded(999) ) _expect((mismatched[0].command_id == 93) == OWNERSHIP_FIXED, "returned result uses the same identity as stored replay result") _expect(mismatch_queue.get_result_by_idempotency_key(&"ownership-93").command_id == 93, "stored result keeps accepted identity") print("COMMAND_OWNERSHIP_FIXED=" + str(OWNERSHIP_FIXED)) func _test_rollback_alias_snapshot() -> void: for erase: bool in [false, true]: var original := {"nested": {"balance": 100}, "entries": [1, 2]} var target := {"value": original} var before := target.duplicate(true) var transaction = TransactionType.new() var mode := "erase" if erase else "set" _expect(transaction.stage_dictionary_erase(target, "value") if erase else transaction.stage_dictionary_set(target, "value", {"replacement": true}), mode + " alias fixture staged") _expect(target == before, mode + " staging preserves original nested value") transaction.stage_custom( "alias mutation followed by failure", func() -> bool: return true, func() -> bool: original["nested"]["balance"] = 0 original["entries"].append(3) return false, func() -> void: pass ) var result = transaction.commit() _expect(result.code == CommandResultType.Code.TRANSACTION_APPLY_FAILED and transaction.status == TransactionType.Status.ROLLED_BACK, mode + " failed apply rolls back") _expect((target == before) == ROLLBACK_ALIAS_FIXED, mode + " rollback restores staged original values instead of changed external alias") original["nested"]["balance"] = -1 original["entries"].clear() _expect((target == before) == ROLLBACK_ALIAS_FIXED, mode + " later alias mutation cannot change rolled-back target") _expect(transaction.commit().code == CommandResultType.Code.TRANSACTION_CLOSED, mode + " rolled-back transaction cannot apply twice") print("ROLLBACK_ALIAS_FIXED=" + str(ROLLBACK_ALIAS_FIXED))