package router

import (
	"os"
	"testing"
)

// TestONNXRuntimeCanBeReacquired pins the process-global lifecycle itself.
// The model gates exercise live sessions; this smaller check proves that the
// last lease unloads a managed environment and that a later gate can acquire
// it again. It is opt-in for the same reason as every model measurement.
func TestONNXRuntimeCanBeReacquired(t *testing.T) {
	lib := os.Getenv("MAVEN_ONNX_LIB")
	if lib == "" {
		t.Skip("MAVEN_ONNX_LIB unset — see AGENTS.md § Embedder model for intent routing")
	}
	if _, err := os.Stat(lib); err != nil {
		t.Skipf("missing %s: %v", lib, err)
	}

	first, err := AcquireONNXRuntime(lib)
	if err != nil {
		t.Fatalf("first acquire: %v", err)
	}
	if err := first.Close(); err != nil {
		t.Fatalf("first close: %v", err)
	}
	if err := first.Close(); err != nil {
		t.Fatalf("idempotent close: %v", err)
	}

	second, err := AcquireONNXRuntime(lib)
	if err != nil {
		t.Fatalf("reacquire after last close: %v", err)
	}
	if err := second.Close(); err != nil {
		t.Fatalf("second close: %v", err)
	}
}
