package config

import (
	"reflect"
	"testing"
)

func TestLoadDefaultsIncludeLocalWebSocketOrigins(t *testing.T) {
	t.Setenv("ALT_API_HOST", "")
	t.Setenv("ALT_API_PORT", "")
	t.Setenv("ALT_API_SOCKET_PATH", "")
	t.Setenv("ALT_SOCKET_HEARTBEAT_INTERVAL_SEC", "")
	t.Setenv("ALT_SOCKET_HEARTBEAT_WAIT_SEC", "")
	t.Setenv("ALT_API_WS_ORIGIN_PATTERNS", "")

	cfg := Load()

	want := []string{"localhost:*", "127.0.0.1:*"}
	if !reflect.DeepEqual(cfg.WSOriginPatterns, want) {
		t.Fatalf("WSOriginPatterns = %v, want %v", cfg.WSOriginPatterns, want)
	}
}

func TestLoadParsesWebSocketOriginPatterns(t *testing.T) {
	t.Setenv("ALT_API_WS_ORIGIN_PATTERNS", "localhost:*, 127.0.0.1:* , toki-labs.com:*")

	cfg := Load()

	want := []string{"localhost:*", "127.0.0.1:*", "toki-labs.com:*"}
	if !reflect.DeepEqual(cfg.WSOriginPatterns, want) {
		t.Fatalf("WSOriginPatterns = %v, want %v", cfg.WSOriginPatterns, want)
	}
}

func TestLoadWorkerSocketURL(t *testing.T) {
	t.Setenv("ALT_WORKER_SOCKET_URL", "ws://127.0.0.1:9090/custom")
	cfg := Load()
	if cfg.WorkerSocketURL != "ws://127.0.0.1:9090/custom" {
		t.Errorf("expected WorkerSocketURL to be ws://127.0.0.1:9090/custom, got %q", cfg.WorkerSocketURL)
	}

	t.Setenv("ALT_WORKER_SOCKET_URL", "")
	cfg = Load()
	if cfg.WorkerSocketURL != "ws://127.0.0.1:8081/socket" {
		t.Errorf("expected default WorkerSocketURL to be ws://127.0.0.1:8081/socket, got %q", cfg.WorkerSocketURL)
	}
}
