extends SceneTree

## Invalid/missing/native-boundary/source/performance contract for raw M2 I/O.

const REPOSITORY_SCRIPT := preload("res://src/render/m2/m2_raw_model_repository.gd")
const REPOSITORY_PATH := "res://src/render/m2/m2_raw_model_repository.gd"
const NATIVE_OBSERVER_PATH := (
	"res://src/render/m2/m2_native_animation_resource_observer.gd"
)
const MESH_FINALIZER_PATH := "res://src/render/m2/m2_mesh_resource_finalizer.gd"
const LOADER_PATH := "res://src/scenes/streaming/streaming_world_loader.gd"


func _initialize() -> void:
	var failures: Array[String] = []
	_verify_rejected_inputs(failures)
	_verify_ownership_boundaries(failures)
	var elapsed_milliseconds := _verify_bounded_timing(failures)
	if not failures.is_empty():
		for failure in failures:
			push_error("M2_RAW_MODEL_REPOSITORY: %s" % failure)
		quit(1)
		return
	print(
		"M2_RAW_MODEL_REPOSITORY PASS cases=10 iterations=10000 elapsed_ms=%.3f"
		% elapsed_milliseconds
	)
	quit(0)


func _verify_rejected_inputs(failures: Array[String]) -> void:
	var repository: RefCounted = REPOSITORY_SCRIPT.new()
	_expect_empty(
		repository.call("load_static_model_data", "res://data/extracted", ""),
		"empty static path",
		failures
	)
	_expect_empty(
		repository.call("load_animated_model_data", "res://data/extracted", ""),
		"empty animated path",
		failures
	)
	var missing_path := "world/__openwc_missing__/repository_fixture.m2"
	_expect_empty(
		repository.call("load_static_model_data", "res://data/extracted", missing_path),
		"missing static file",
		failures
	)
	_expect_empty(
		repository.call("load_animated_model_data", "res://data/extracted", missing_path),
		"missing animated file",
		failures
	)
	_expect_empty(
		repository.call("load_static_model_data", "res://data/extracted", missing_path),
		"missing failure is not retained",
		failures
	)


func _verify_ownership_boundaries(failures: Array[String]) -> void:
	var repository_source := FileAccess.get_file_as_string(REPOSITORY_PATH)
	var native_observer_source := FileAccess.get_file_as_string(NATIVE_OBSERVER_PATH)
	var mesh_finalizer_source := FileAccess.get_file_as_string(MESH_FINALIZER_PATH)
	var loader_source := FileAccess.get_file_as_string(LOADER_PATH)
	_expect_true(
		loader_source.contains("M2_RAW_MODEL_REPOSITORY_SCRIPT.new()"),
		"loader composes raw model repository",
		failures
	)
	_expect_equal(
		loader_source.count("_m2_raw_model_repository.load_static_model_data(")
		+ mesh_finalizer_source.count("\"load_static_model_data\""),
		2,
		"two static callers delegate",
		failures
	)
	_expect_equal(
		(
			loader_source.count("_m2_raw_model_repository.load_animated_model_data(")
			+ native_observer_source.count("\"load_animated_model_data\"")
		),
		1,
		"animated caller delegates",
		failures
	)
	for removed_loader_rule in [
		"func _load_m2_raw_data_for_refresh(",
		"ClassDB.class_exists(\"M2Loader\")",
		"ClassDB.instantiate(\"M2Loader\")",
		"loader.call(\"load_m2\"",
		"loader.call(\"load_m2_animated\"",
	]:
		_expect_false(loader_source.contains(removed_loader_rule), "loader omits %s" % removed_loader_rule, failures)
	for required_repository_rule in [
		"ClassDB.class_exists(NATIVE_LOADER_CLASS_NAME)",
		"ProjectSettings.globalize_path(",
		"extracted_directory.path_join(normalized_relative_path)",
		"FileAccess.file_exists(absolute_path)",
		"ClassDB.instantiate(NATIVE_LOADER_CLASS_NAME)",
		"native_loader.has_method(native_method_name)",
		"\"load_m2\"",
		"\"load_m2_animated\"",
	]:
		_expect_true(repository_source.contains(required_repository_rule), "repository owns %s" % required_repository_rule, failures)
	for forbidden_dependency in [
		"ResourceLoader.",
		"M2_BUILDER",
		"_m2_prototype_cache_state",
		"Mesh",
		"Node",
		"WorkerThreadPool",
	]:
		_expect_false(repository_source.contains(forbidden_dependency), "repository omits %s" % forbidden_dependency, failures)


func _verify_bounded_timing(failures: Array[String]) -> float:
	var repository: RefCounted = REPOSITORY_SCRIPT.new()
	var started_microseconds := Time.get_ticks_usec()
	for _iteration in range(10000):
		repository.call("load_static_model_data", "res://data/extracted", "")
	var elapsed_milliseconds := float(Time.get_ticks_usec() - started_microseconds) / 1000.0
	_expect_true(elapsed_milliseconds < 1000.0, "10000 empty-path rejects under 1 second", failures)
	return elapsed_milliseconds


func _expect_empty(actual: Variant, label: String, failures: Array[String]) -> void:
	if not (actual is Dictionary) or not (actual as Dictionary).is_empty():
		failures.append(label)


func _expect_true(condition: bool, label: String, failures: Array[String]) -> void:
	if not condition:
		failures.append(label)


func _expect_false(condition: bool, label: String, failures: Array[String]) -> void:
	_expect_true(not condition, label, failures)


func _expect_equal(actual: int, expected: int, label: String, failures: Array[String]) -> void:
	if actual != expected:
		failures.append("%s expected=%d actual=%d" % [label, expected, actual])
