package backend_test import ( "errors" "reflect" "testing" "gitea.maximumdirect.net/eric/promptkit/internal/backend" "gitea.maximumdirect.net/eric/promptkit/internal/domain" ) func TestEffectiveRegistryOwnsDefinitionsAndCapacityPolicies(t *testing.T) { params := map[string]any{"nested": map[string]any{"enabled": true}} defaultProfile := "catalog-profile" reachabilityEndpoint := "https://probe.example/ready" authenticationKind := "openrouter_key" authenticationEndpoint := "https://probe.example/key" smokeTests := &domain.BackendSmokeTestConfig{ DefaultProfile: &defaultProfile, Reachability: &domain.ReachabilityProbeConfig{Endpoint: &reachabilityEndpoint}, Authentication: &domain.AuthenticationProbeConfig{ Kind: &authenticationKind, Endpoint: &authenticationEndpoint, }, } registry, err := backend.NewEffectiveRegistry(map[string]domain.Backend{ " local.v1 ": { Endpoint: "https://backend.example/v1", ExtraParams: params, ConcurrencyLimit: 2, QueueCapacity: 3, QueueCapacitySet: true, SmokeTests: smokeTests, }, }) if err != nil { t.Fatal(err) } params["nested"].(map[string]any)["enabled"] = false defaultProfile = "mutated-profile" reachabilityEndpoint = "https://mutated.example/ready" authenticationKind = "none" authenticationEndpoint = "https://mutated.example/key" selected, err := registry.GetBackend(" local.v1 ") if err != nil || selected.ID != " local.v1 " || selected.ExtraParams["nested"].(map[string]any)["enabled"] != true { t.Fatalf("effective backend was not captured under its literal ID: %+v, %v", selected, err) } if selected.SmokeTests == nil || selected.SmokeTests.DefaultProfile == nil || *selected.SmokeTests.DefaultProfile != "catalog-profile" || selected.SmokeTests.Reachability == nil || selected.SmokeTests.Reachability.Endpoint == nil || *selected.SmokeTests.Reachability.Endpoint != "https://probe.example/ready" || selected.SmokeTests.Authentication == nil || selected.SmokeTests.Authentication.Kind == nil || *selected.SmokeTests.Authentication.Kind != "openrouter_key" || selected.SmokeTests.Authentication.Endpoint == nil || *selected.SmokeTests.Authentication.Endpoint != "https://probe.example/key" { t.Fatalf("registry did not capture nested diagnostic metadata: %+v", selected.SmokeTests) } selected.ExtraParams["nested"].(map[string]any)["enabled"] = false *selected.SmokeTests.DefaultProfile = "mutated-return" *selected.SmokeTests.Reachability.Endpoint = "https://mutated-return.example/ready" *selected.SmokeTests.Authentication.Kind = "none" *selected.SmokeTests.Authentication.Endpoint = "https://mutated-return.example/key" again, err := registry.GetBackend(" local.v1 ") if err != nil || again.ExtraParams["nested"].(map[string]any)["enabled"] != true { t.Fatalf("lookup exposed registry values: %+v, %v", again, err) } if again.SmokeTests == nil || again.SmokeTests.DefaultProfile == nil || *again.SmokeTests.DefaultProfile != "catalog-profile" || again.SmokeTests.Reachability == nil || again.SmokeTests.Reachability.Endpoint == nil || *again.SmokeTests.Reachability.Endpoint != "https://probe.example/ready" || again.SmokeTests.Authentication == nil || again.SmokeTests.Authentication.Kind == nil || *again.SmokeTests.Authentication.Kind != "openrouter_key" || again.SmokeTests.Authentication.Endpoint == nil || *again.SmokeTests.Authentication.Endpoint != "https://probe.example/key" { t.Fatalf("lookup exposed nested diagnostic metadata: %+v", again.SmokeTests) } policies := registry.CapacityPolicies() if !reflect.DeepEqual(policies, map[string]domain.BackendCapacityPolicy{ " local.v1 ": {ConcurrencyLimit: 2, QueueCapacity: 3}, }) { t.Fatalf("capacity policy = %+v", policies) } delete(policies, " local.v1 ") if len(registry.CapacityPolicies()) != 1 { t.Fatal("capacity policy map exposed registry state") } } func TestEffectiveRegistryMissingBackend(t *testing.T) { registry, err := backend.NewEffectiveRegistry(nil) if err != nil { t.Fatal(err) } if _, err := registry.GetBackend("missing"); !errors.Is(err, backend.ErrBackendNotFound) { t.Fatalf("missing backend error = %v", err) } if _, err := registry.GetBackend("local.v1"); !errors.Is(err, backend.ErrBackendNotFound) { t.Fatalf("trimmed lookup unexpectedly matched: %v", err) } }