package runner import ( "context" "encoding/json" "os" "path/filepath" "testing" "amadan.net/oficina/equipo/internal/runner/provider" ) func TestAccountHomeConfiguredDiscovery(t *testing.T) { c := configFixture(t) bs := []AccountBinding{ {ID: "binding", LocalID: "person-local", PersonID: "person", Provider: "codex", Family: "gpt", Model: "second", AccountID: "account", ProviderHome: filepath.Join(t.TempDir(), "dedicated"), ExpectedIdentity: provider.Identity{AccountID: "account", HomeID: "home", ProviderVersion: "0.165.0"}, Kind: "subscription"}, } second := bs[0] second.Model = "first" bs = append(bs, second) save := func() { raw, _ := json.Marshal(bs) if e := os.WriteFile(c.AccountBindingsPath, raw, 0600); e != nil { t.Fatal(e) } } save() calls := 0 reports, e := discoverDirect(context.Background(), c, func(_ context.Context, b provider.Binding) (provider.Identity, error) { calls++ if b.DedicatedHome || b.VerifiedDedicatedHome() { t.Fatal("configuration granted egress authority") } return b.Expected, nil }) if e != nil || calls != 2 || len(reports) != 2 { t.Fatal("configured discovery failed", e, calls) } if reports[0].Binding.Model != "second" || reports[1].Binding.Model != "first" || reports[0].Binding.PersonID != "person" || reports[0].Binding.LocalID != "person-local" { t.Fatal("association or order lost") } bs = append(bs, bs[0]) save() if _, e = loadDirectBindings(c.AccountBindingsPath); e == nil { t.Fatal("second matching binding accepted") } bs = bs[:1] bs[0].ExpectedIdentity = provider.Identity{} save() if _, e = loadDirectBindings(c.AccountBindingsPath); e == nil { t.Fatal("missing identity accepted") } } func TestAccountHomeConfiguredDiscoveryAliases(t *testing.T) { for _, kind := range []string{"home", "home-id", "account"} { t.Run(kind, func(t *testing.T) { c := configFixture(t) first := AccountBinding{ID: "first", LocalID: "first", PersonID: "person", Provider: "codex", Family: "gpt", Model: "model", AccountID: "account", ProviderHome: filepath.Join(t.TempDir(), "dedicated"), ExpectedIdentity: provider.Identity{AccountID: "account", HomeID: "home", ProviderVersion: "0.165.0"}, Kind: "subscription"} second := first second.ID = "second" second.LocalID = "second" second.PersonID = "other-person" if kind != "home" { second.ProviderHome = filepath.Join(t.TempDir(), "other") } if kind != "home-id" { second.ExpectedIdentity.HomeID = "other" } if kind != "account" { second.AccountID = "other" second.ExpectedIdentity.AccountID = "other" } raw, _ := json.Marshal([]AccountBinding{first, second}) if e := os.WriteFile(c.AccountBindingsPath, raw, 0600); e != nil { t.Fatal(e) } if _, e := loadDirectBindings(c.AccountBindingsPath); e == nil { t.Fatal("shared binding authority accepted") } }) } }