extends SceneTree

## Scene-free contract, admission and LRU regression for terrain revisit meshes.

const CACHE_SCRIPT := preload("res://src/render/terrain/terrain_quality_mesh_cache.gd")
const LOADER_PATH := "res://src/scenes/streaming/streaming_world_loader.gd"


func _initialize() -> void:
	var failures: Array[String] = []
	_verify_cache_contract(failures)
	_verify_loader_boundary(failures)
	if not failures.is_empty():
		for failure in failures:
			push_error("TERRAIN_QUALITY_MESH_CACHE: %s" % failure)
		quit(1)
		return
	print("TERRAIN_QUALITY_MESH_CACHE PASS contract_cases=15 loader_calls=3")
	quit(0)


func _verify_cache_contract(failures: Array[String]) -> void:
	var cache: RefCounted = CACHE_SCRIPT.new()
	var mesh_a := ArrayMesh.new()
	var mesh_b := ArrayMesh.new()
	var mesh_c := ArrayMesh.new()
	_expect_true(not bool(cache.call("store", "", mesh_a, "baked_full", 2)), "empty key rejected", failures)
	_expect_true(not bool(cache.call("store", "a", null, "baked_full", 2)), "null mesh rejected", failures)
	for excluded_source in ["control_splat_cache", "splat_cache", "splat"]:
		_expect_true(not bool(cache.call("store", "a", mesh_a, excluded_source, 2)), "%s rejected" % excluded_source, failures)
	_expect_true(bool(cache.call("store", "a", mesh_a, "baked_full", 2)), "first store", failures)
	_expect_true(bool(cache.call("store", "b", mesh_b, "baked_full", 2)), "second store", failures)
	var restored_a: Dictionary = cache.call("restore", "a")
	_expect_true(restored_a.get("mesh") == mesh_a, "restore retains mesh reference", failures)
	_expect_true(String(restored_a.get("source", "")) == "baked_full", "restore retains source", failures)
	_expect_true(bool(cache.call("store", "c", mesh_c, "baked_full", 2)), "third store", failures)
	_expect_true((cache.call("restore", "b") as Dictionary).is_empty(), "oldest evicted after touch", failures)
	_expect_true(not (cache.call("restore", "a") as Dictionary).is_empty(), "touched entry retained", failures)
	var diagnostics: Dictionary = cache.call("diagnostic_snapshot")
	_expect_true(int(diagnostics.get("entry_count", 0)) == 2, "bounded count", failures)
	(diagnostics.get("entries_oldest_to_newest") as Array).clear()
	_expect_true(int((cache.call("diagnostic_snapshot") as Dictionary).get("entry_count", 0)) == 2, "diagnostics detached", failures)
	_expect_true(not bool(cache.call("store", "zero", ArrayMesh.new(), "baked_full", -1)), "negative capacity clamps to zero", failures)
	cache.call("clear")
	_expect_true(int((cache.call("diagnostic_snapshot") as Dictionary).get("entry_count", -1)) == 0, "clear releases entries", failures)


func _verify_loader_boundary(failures: Array[String]) -> void:
	var source := _read_text(LOADER_PATH, failures)
	_expect_true(source.contains("TERRAIN_QUALITY_MESH_CACHE_SCRIPT.new()"), "loader composes cache", failures)
	_expect_true(source.contains('_terrain_quality_mesh_cache.call("restore", tile_key)'), "loader delegates restore", failures)
	_expect_true(source.contains('_terrain_quality_mesh_cache.call("clear")'), "loader delegates clear", failures)
	for removed_symbol in ["_terrain_quality_mesh_cache_order", "func _touch_quality_terrain_mesh_cache", "func _trim_quality_terrain_mesh_cache"]:
		_expect_true(not source.contains(removed_symbol), "loader omits %s" % removed_symbol, failures)


func _read_text(path: String, failures: Array[String]) -> String:
	var file := FileAccess.open(path, FileAccess.READ)
	if file == null:
		failures.append("cannot open %s" % path)
		return ""
	return file.get_as_text()


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