extends GdUnitTestSuite

## Covers [code]cooldown_state.gd[/code] snapshot math (NEO-32).
## Paired with [code]main.gd[/code] wiring when that file changes (pre-commit).
const CooldownStateScript := preload("res://scripts/cooldown_state.gd")


func test_remaining_is_zero_when_end_before_server_time() -> void:
	# Arrange — server anchor after end → slot reads as ready.
	var state: RefCounted = CooldownStateScript.new()
	var snap := {
		"serverTimeUtc": "2026-01-01T12:01:00.000Z",
		"slots": [{"slotIndex": 0, "cooldownEndsAtUtc": "2026-01-01T12:00:05.000Z"}],
	}

	# Act
	state.call("apply_snapshot", snap)
	var rem: float = state.call("remaining_for_slot", 0) as float

	# Assert
	assert_that(rem).is_equal(0.0)


func test_remaining_positive_when_end_after_server_time() -> void:
	# Arrange
	var state: RefCounted = CooldownStateScript.new()
	var snap := {
		"serverTimeUtc": "2026-01-01T12:00:00.000Z",
		"slots": [{"slotIndex": 0, "cooldownEndsAtUtc": "2026-01-01T12:00:10.000Z"}],
	}

	# Act
	state.call("apply_snapshot", snap)
	var rem: float = state.call("remaining_for_slot", 0) as float

	# Assert
	assert_that(rem).is_greater(9.0)
	assert_that(rem).is_less(11.0)


func test_invalid_server_time_string_does_not_lock_slot_ready() -> void:
	# Arrange: bogus ISO parses as 0; anchor must fall back so slot 0 is not stuck cooling.
	var state: RefCounted = CooldownStateScript.new()
	var snap := {"serverTimeUtc": "__not_iso__", "slots": [{"slotIndex": 0}]}

	# Act
	state.call("apply_snapshot", snap)
	var cooling: bool = bool(state.call("is_slot_cooling", 0))

	# Assert
	assert_that(cooling).is_false()
