extends SceneTree

## Cold-start contract for the renderer-owned ground query result value.

const SAMPLE_SCRIPT := preload("res://src/render/terrain/rendered_ground_sample.gd")


func _initialize() -> void:
	var failures: Array[String] = []
	var available_sample: RefCounted = SAMPLE_SCRIPT.available(42.25)
	_expect_true(bool(available_sample.get("is_available")), "available sample", failures)
	_expect_near(float(available_sample.get("height_units")), 42.25, "available height", failures)

	var non_finite_sample: RefCounted = SAMPLE_SCRIPT.available(INF)
	_expect_true(not bool(non_finite_sample.get("is_available")), "non-finite unavailable", failures)
	_expect_true(
		StringName(non_finite_sample.get("failure_code")) == &"render_terrain_height_not_finite",
		"non-finite failure code",
		failures
	)

	var unavailable_sample: RefCounted = SAMPLE_SCRIPT.unavailable(&"")
	_expect_true(
		StringName(unavailable_sample.get("failure_code")) == &"render_terrain_unavailable",
		"empty failure code normalization",
		failures
	)

	if not failures.is_empty():
		for failure in failures:
			push_error("RENDERED_GROUND_SAMPLE: %s" % failure)
		quit(1)
		return
	print("RENDERED_GROUND_SAMPLE PASS contract=3")
	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)
