Files
TheTree_mock/tests/s0/test_stable_id_generator.gd
T

36 lines
1.4 KiB
GDScript

extends SceneTree
var _checks := 0
var _failures := 0
func _initialize() -> void:
call_deferred("_run")
func _run() -> void:
print("S0: stable ID generator")
var generator := StableIdGenerator.new()
_expect(generator.next_id(&"guest") == &"guest.000000000001", "first ID uses a stable padded sequence")
_expect(generator.next_id(&"guest") == &"guest.000000000002", "same prefix increments monotonically")
_expect(generator.next_id(&"staff") == &"staff.000000000001", "prefixes have independent counters")
var snapshot := generator.create_snapshot()
var restored := StableIdGenerator.new()
_expect(restored.restore_snapshot(snapshot), "valid ID counters restore")
_expect(restored.next_id(&"guest") == &"guest.000000000003", "restored counter never reuses an ID")
_expect(restored.next_id(&"staff") == &"staff.000000000002", "all prefix counters restore")
var before_invalid := restored.create_snapshot()
_expect(not restored.restore_snapshot({"version": 1, "counters": {"Bad Prefix": 2}}), "invalid prefix snapshot is rejected")
_expect(restored.create_snapshot() == before_invalid, "rejected restore leaves counters unchanged")
if _failures == 0:
print("PASS: %d checks" % _checks)
else:
push_error("FAIL: %d of %d checks failed" % [_failures, _checks])
quit(_failures)
func _expect(condition: bool, label: String) -> void:
_checks += 1
if condition:
print(" ✓ %s" % label)
return
_failures += 1
push_error(" ✗ %s" % label)