package runtimeapi

import (
	"context"
	"testing"

	"github.com/nexuscrm/kernel/pkg/perftiming"
)

func TestRuntimeFlowProfilingRequiresExplicitValidOptIn(t *testing.T) {
	t.Setenv(runtimeAPIProfilingEnabledEnv, "")
	t.Setenv(runtimeFlowPerfTimingEnabledEnv, "")
	if perftiming.Enabled(withRuntimeFlowProfiling(context.Background())) {
		t.Fatal("profiling enabled without operator opt-in")
	}

	t.Setenv(runtimeFlowPerfTimingEnabledEnv, "not-a-boolean")
	if perftiming.Enabled(withRuntimeFlowProfiling(context.Background())) {
		t.Fatal("profiling enabled for malformed operator input")
	}

	t.Setenv(runtimeFlowPerfTimingEnabledEnv, "true")
	if perftiming.Enabled(withRuntimeFlowProfiling(context.Background())) {
		t.Fatal("verbose timing enabled while process profiling is disabled")
	}

	t.Setenv(runtimeAPIProfilingEnabledEnv, "not-a-boolean")
	if perftiming.Enabled(withRuntimeFlowProfiling(context.Background())) {
		t.Fatal("verbose timing enabled for malformed process profiling input")
	}

	t.Setenv(runtimeAPIProfilingEnabledEnv, "true")
	if !perftiming.Enabled(withRuntimeFlowProfiling(context.Background())) {
		t.Fatal("profiling was not enabled when both diagnostic gates opted in")
	}
}
