| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | ) |
| 12 | |
| 13 | func TestAccountHomeConfiguredDiscovery(t *testing.T) { |
| 14 | c := configFixture(t) |
| 15 | bs := []AccountBinding{ |
| 16 | {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"}, |
| 17 | } |
| 18 | second := bs[0] |
| 19 | second.Model = "first" |
| 20 | bs = append(bs, second) |
| 21 | save := func() { |
| 22 | raw, _ := json.Marshal(bs) |
| 23 | if e := os.WriteFile(c.AccountBindingsPath, raw, 0600); e != nil { |
| 24 | t.Fatal(e) |
| 25 | } |
| 26 | } |
| 27 | save() |
| 28 | calls := 0 |
| 29 | reports, e := discoverDirect(context.Background(), c, func(_ context.Context, b provider.Binding) (provider.Identity, error) { |
| 30 | calls++ |
| 31 | if b.DedicatedHome || b.VerifiedDedicatedHome() { |
| 32 | t.Fatal("configuration granted egress authority") |
| 33 | } |
| 34 | return b.Expected, nil |
| 35 | }) |
| 36 | if e != nil || calls != 2 || len(reports) != 2 { |
| 37 | t.Fatal("configured discovery failed", e, calls) |
| 38 | } |
| 39 | if reports[0].Binding.Model != "second" || reports[1].Binding.Model != "first" || reports[0].Binding.PersonID != "person" || reports[0].Binding.LocalID != "person-local" { |
| 40 | t.Fatal("association or order lost") |
| 41 | } |
| 42 | bs = append(bs, bs[0]) |
| 43 | save() |
| 44 | if _, e = loadDirectBindings(c.AccountBindingsPath); e == nil { |
| 45 | t.Fatal("second matching binding accepted") |
| 46 | } |
| 47 | bs = bs[:1] |
| 48 | bs[0].ExpectedIdentity = provider.Identity{} |
| 49 | save() |
| 50 | if _, e = loadDirectBindings(c.AccountBindingsPath); e == nil { |
| 51 | t.Fatal("missing identity accepted") |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestAccountHomeConfiguredDiscoveryAliases(t *testing.T) { |
| 56 | for _, kind := range []string{"home", "home-id", "account"} { |
| 57 | t.Run(kind, func(t *testing.T) { |
| 58 | c := configFixture(t) |
| 59 | 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"} |
| 60 | second := first |
| 61 | second.ID = "second" |
| 62 | second.LocalID = "second" |
| 63 | second.PersonID = "other-person" |
| 64 | if kind != "home" { |
| 65 | second.ProviderHome = filepath.Join(t.TempDir(), "other") |
| 66 | } |
| 67 | if kind != "home-id" { |
| 68 | second.ExpectedIdentity.HomeID = "other" |
| 69 | } |
| 70 | if kind != "account" { |
| 71 | second.AccountID = "other" |
| 72 | second.ExpectedIdentity.AccountID = "other" |
| 73 | } |
| 74 | raw, _ := json.Marshal([]AccountBinding{first, second}) |
| 75 | if e := os.WriteFile(c.AccountBindingsPath, raw, 0600); e != nil { |
| 76 | t.Fatal(e) |
| 77 | } |
| 78 | if _, e := loadDirectBindings(c.AccountBindingsPath); e == nil { |
| 79 | t.Fatal("shared binding authority accepted") |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |