package bootstrap

import (
	"os"
	"path/filepath"
	"testing"
)

func TestLoadEnvFile(t *testing.T) {
	dir := t.TempDir()
	path := filepath.Join(dir, ".env")
	content := `# comment
HAIXUN_JWT_ACCESS_SECRET=from-file
THREADS_APP_ID="1566509781118049"
`
	if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
		t.Fatal(err)
	}
	t.Setenv("HAIXUN_JWT_REFRESH_SECRET", "preset")
	unset := []string{"HAIXUN_JWT_ACCESS_SECRET", "THREADS_APP_ID"}
	for _, key := range unset {
		_ = os.Unsetenv(key)
	}
	if !loadEnvFile(path) {
		t.Fatal("loadEnvFile returned false")
	}
	if got := os.Getenv("HAIXUN_JWT_ACCESS_SECRET"); got != "from-file" {
		t.Fatalf("access secret = %q", got)
	}
	if got := os.Getenv("HAIXUN_JWT_REFRESH_SECRET"); got != "preset" {
		t.Fatalf("refresh secret should stay preset, got %q", got)
	}
	if got := os.Getenv("THREADS_APP_ID"); got != "1566509781118049" {
		t.Fatalf("threads app id = %q", got)
	}
}
