package config

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

func TestLoadNormalizesFirebaseProjects(t *testing.T) {
	// 多 App Firebase 配置必须在加载阶段归一化，避免线上 YAML 的空格或大小写让 app 路由失效。
	path := filepath.Join(t.TempDir(), "config.yaml")
	if err := os.WriteFile(path, []byte(`
third_party:
  allowed_providers:
    - " firebase "
  firebase:
    project_id: " lalu-party "
    allowed_sign_in_providers:
      - " google.com "
    certs_url: " https://certs.example.test "
    projects:
      " Huwaa ":
        project_id: " huwaa-6f3aa "
      lalu:
        project_id: " lalu-party "
`), 0o600); err != nil {
		t.Fatalf("write config failed: %v", err)
	}

	cfg, err := Load(path)
	if err != nil {
		t.Fatalf("Load failed: %v", err)
	}
	huwaa, ok := cfg.ThirdParty.Firebase.Projects["huwaa"]
	if !ok {
		t.Fatalf("huwaa Firebase project must be keyed by normalized app_code: %+v", cfg.ThirdParty.Firebase.Projects)
	}
	if huwaa.ProjectID != "huwaa-6f3aa" {
		t.Fatalf("huwaa project_id mismatch: %+v", huwaa)
	}
	if len(huwaa.AllowedSignInProviders) != 1 || huwaa.AllowedSignInProviders[0] != "google.com" {
		t.Fatalf("huwaa sign-in providers should inherit normalized global allowlist: %+v", huwaa.AllowedSignInProviders)
	}
	if huwaa.CertsURL != "https://certs.example.test" {
		t.Fatalf("huwaa certs_url should inherit global certs_url: %q", huwaa.CertsURL)
	}
}
