extends SceneTree

## Cold-start contract for immutable renderer environment input.

const SNAPSHOT_SCRIPT := preload("res://src/render/environment/world_environment_snapshot.gd")


func _initialize() -> void:
	var failures: Array[String] = []
	var afternoon: RefCounted = SNAPSHOT_SCRIPT.at_time_of_day(13.5)
	_expect_true(bool(afternoon.get("is_valid")), "finite snapshot", failures)
	_expect_near(float(afternoon.get("time_of_day_hours")), 13.5, "finite time", failures)

	var wrapped: RefCounted = SNAPSHOT_SCRIPT.at_time_of_day(25.25)
	_expect_near(float(wrapped.get("time_of_day_hours")), 1.25, "positive day wrap", failures)
	var negative: RefCounted = SNAPSHOT_SCRIPT.at_time_of_day(-1.0)
	_expect_near(float(negative.get("time_of_day_hours")), 23.0, "negative day wrap", failures)

	var invalid: RefCounted = SNAPSHOT_SCRIPT.at_time_of_day(INF)
	_expect_true(not bool(invalid.get("is_valid")), "non-finite invalid", failures)
	_expect_true(
		StringName(invalid.get("failure_code")) == &"environment_time_not_finite",
		"non-finite failure code",
		failures
	)

	if not failures.is_empty():
		for failure in failures:
			push_error("WORLD_ENVIRONMENT_SNAPSHOT: %s" % failure)
		quit(1)
		return
	print("WORLD_ENVIRONMENT_SNAPSHOT PASS contract=5")
	quit(0)


func _expect_near(actual_value: float, expected_value: float, label: String, failures: Array[String]) -> void:
	if not is_equal_approx(actual_value, expected_value):
		failures.append("%s expected %.3f, got %.3f" % [label, expected_value, actual_value])


func _expect_true(actual_value: bool, label: String, failures: Array[String]) -> void:
	if not actual_value:
		failures.append("%s expected true" % label)
