package env

import "testing"

func TestLoadEnvConfigDefaults(t *testing.T) {
	e := EnvSource{}
	loaded := LoadEnvConfig(e, "/tmp")

	if loaded.Host != "127.0.0.1" {
		t.Errorf("expected 127.0.0.1, got %s", loaded.Host)
	}
	if loaded.Port != 8765 {
		t.Errorf("expected 8765, got %d", loaded.Port)
	}
	if loaded.DefaultModel != "auto" {
		t.Errorf("expected auto, got %s", loaded.DefaultModel)
	}
	if loaded.AgentBin != "agent" {
		t.Errorf("expected agent, got %s", loaded.AgentBin)
	}
	if !loaded.StrictModel {
		t.Error("expected strictModel=true by default")
	}
}

func TestLoadEnvConfigOverride(t *testing.T) {
	e := EnvSource{
		"CURSOR_BRIDGE_HOST": "0.0.0.0",
		"CURSOR_BRIDGE_PORT": "9000",
		"CURSOR_BRIDGE_DEFAULT_MODEL": "gpt-4",
		"CURSOR_AGENT_BIN": "/usr/local/bin/agent",
	}
	loaded := LoadEnvConfig(e, "/tmp")

	if loaded.Host != "0.0.0.0" {
		t.Errorf("expected 0.0.0.0, got %s", loaded.Host)
	}
	if loaded.Port != 9000 {
		t.Errorf("expected 9000, got %d", loaded.Port)
	}
	if loaded.DefaultModel != "gpt-4" {
		t.Errorf("expected gpt-4, got %s", loaded.DefaultModel)
	}
	if loaded.AgentBin != "/usr/local/bin/agent" {
		t.Errorf("expected /usr/local/bin/agent, got %s", loaded.AgentBin)
	}
}

func TestNormalizeModelID(t *testing.T) {
	tests := []struct {
		input string
		want  string
	}{
		{"gpt-4", "gpt-4"},
		{"openai/gpt-4", "gpt-4"},
		{"", "auto"},
		{"  ", "auto"},
	}
	for _, tc := range tests {
		got := normalizeModelId(tc.input)
		if got != tc.want {
			t.Errorf("normalizeModelId(%q) = %q, want %q", tc.input, got, tc.want)
		}
	}
}
