| 1 | package equipotest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/equipo/internal/equipo" |
| 15 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 16 | ) |
| 17 | |
| 18 | var connectionContext = context.Background() |
| 19 | |
| 20 | func connectionReport(local, provider, family, model string) equipo.ConnectionReport { |
| 21 | version := "2.1.312" |
| 22 | if provider == "codex" { |
| 23 | version = "0.165.0" |
| 24 | } |
| 25 | return equipo.ConnectionReport{LocalID: local, Provider: provider, ProviderVersion: version, LauncherVersion: "direct-v2", BwrapVersion: "0.11.0", PolicyVersion: "policy-v2", Family: family, Model: model, AccountLabel: "shared@example.com", Kind: "subscription", Available: true, ReadIsolation: true, IsolationConfigHash: strings.Repeat("a", 64), Routing: equipo.ConnectionRouting{BindingID: local, AccountID: "opaque-" + local, ObservedAccountID: "opaque-" + local, CredentialHomeHash: strings.Repeat("b", 64), AccountPinned: true, NoFailover: true, TimelineRecovery: true, ProcessOwnership: true}} |
| 26 | } |
| 27 | func connectionTuple(r equipo.ConnectionReport) team.IsolationTuple { |
| 28 | return team.IsolationTuple{Provider: r.Provider, ProviderVersion: r.ProviderVersion, LauncherVersion: r.LauncherVersion, BwrapVersion: r.BwrapVersion, PolicyVersion: r.PolicyVersion, IsolationConfigHash: r.IsolationConfigHash} |
| 29 | } |
| 30 | |
| 31 | type connectionFixture struct { |
| 32 | enrolFixture |
| 33 | principal equipo.RunnerPrincipal |
| 34 | project equipo.Project |
| 35 | reports []equipo.ConnectionReport |
| 36 | ids map[string]string |
| 37 | } |
| 38 | |
| 39 | func connectionStore(t *testing.T) connectionFixture { |
| 40 | t.Helper() |
| 41 | f := enrolStore(t) |
| 42 | reports := []equipo.ConnectionReport{connectionReport("claude", "claude", "claude", "sonnet"), connectionReport("codex", "codex", "gpt", "gpt-5")} |
| 43 | f.s = equipo.NewStore(f.d, equipo.WithRunnerClock(f.clock), equipo.WithVerifiedIsolationTuples([]team.IsolationTuple{connectionTuple(reports[0]), connectionTuple(reports[1])})) |
| 44 | setMember(t, f.s, "member@example.com", equipo.RoleMember, true) |
| 45 | a, err := f.s.SaveAccount(connectionContext, ownerActor, equipo.Account{Label: "Connections"}) |
| 46 | if err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | project := saveProject(t, f.s, projectInput(a), projectOwners(), "connection-project") |
| 50 | code, key := beginDevice(t, f.s) |
| 51 | approveDevice(t, f.s, code, ownerAddress) |
| 52 | credential := claimDevice(t, f.s, code, key) |
| 53 | p, err := f.s.AuthenticateRunner(connectionContext, credential.Token) |
| 54 | if err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | if err := f.s.ReportConnections(connectionContext, p, reports); err != nil { |
| 58 | t.Fatal(err) |
| 59 | } |
| 60 | rows, err := f.s.Connections(connectionContext, ownerActor) |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | ids := map[string]string{} |
| 65 | for _, row := range rows { |
| 66 | ids[row.LocalID] = row.ID |
| 67 | } |
| 68 | return connectionFixture{f, p, project, reports, ids} |
| 69 | } |
| 70 | func (f connectionFixture) choice(t *testing.T, pin string) { |
| 71 | t.Helper() |
| 72 | if err := f.s.SaveConnectionChoice(connectionContext, ownerActor, equipo.ConnectionChoice{ProjectID: f.project.ID, ConnectionIDs: []string{f.ids["claude"], f.ids["codex"]}, PinnedID: pin}); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | } |
| 76 | func (f connectionFixture) selectOK(t *testing.T, specialist, excluded string) equipo.Selection { |
| 77 | t.Helper() |
| 78 | result, err := f.s.SelectConnection(connectionContext, ownerAddress, f.project.ID, specialist, excluded) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | return result |
| 83 | } |
| 84 | func (f connectionFixture) denied(t *testing.T, specialist, excluded string) { |
| 85 | t.Helper() |
| 86 | r, err := f.s.SelectConnection(connectionContext, ownerAddress, f.project.ID, specialist, excluded) |
| 87 | if err == nil || r.ConnectionID != "" { |
| 88 | t.Fatalf("ineligible selection accepted: %+v %v", r, err) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestConnectionsBelongToPersonAndMachine(t *testing.T) { |
| 93 | f := connectionStore(t) |
| 94 | f.choice(t, "") |
| 95 | code, key := beginDevice(t, f.s) |
| 96 | approveDevice(t, f.s, code, "member@example.com") |
| 97 | c := claimDevice(t, f.s, code, key) |
| 98 | p, err := f.s.AuthenticateRunner(connectionContext, c.Token) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | if err = f.s.ReportConnections(connectionContext, p, f.reports); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | rows, err := f.s.Connections(connectionContext, equipo.Actor{Address: p.Person}) |
| 106 | if err != nil || len(rows) != 2 { |
| 107 | t.Fatalf("personal rows %v %v", rows, err) |
| 108 | } |
| 109 | for _, r := range rows { |
| 110 | if r.Person != p.Person || r.RunnerID != p.RunnerID || r.ID == f.ids[r.LocalID] { |
| 111 | t.Fatal("shared label or local ID shared identity") |
| 112 | } |
| 113 | } |
| 114 | if err = f.s.SaveConnectionChoice(connectionContext, equipo.Actor{Address: p.Person}, equipo.ConnectionChoice{ProjectID: f.project.ID, ConnectionIDs: []string{f.ids["claude"]}}); err == nil { |
| 115 | t.Fatal("other person's connection enabled") |
| 116 | } |
| 117 | if _, err = f.s.SelectConnection(connectionContext, p.Person, f.project.ID, "product", ""); err == nil { |
| 118 | t.Fatal("selection crossed person") |
| 119 | } |
| 120 | wrongPerson := f.principal |
| 121 | wrongPerson.Person = p.Person |
| 122 | if err := f.s.ReportConnections(connectionContext, wrongPerson, f.reports); err == nil { |
| 123 | t.Fatal("principal person not rechecked") |
| 124 | } |
| 125 | forged := p |
| 126 | forged.RunnerID = f.principal.RunnerID |
| 127 | if err = f.s.ReportConnections(connectionContext, forged, f.reports); err == nil { |
| 128 | t.Fatal("forged principal reported") |
| 129 | } |
| 130 | mustSQL(t, f.d, "UPDATE runner_credentials SET revoked_at = ? WHERE id = ?", f.clock.Now(), p.CredentialID) |
| 131 | if err = f.s.ReportConnections(connectionContext, p, f.reports); err == nil { |
| 132 | t.Fatal("cached revoked principal reported") |
| 133 | } |
| 134 | } |
| 135 | func TestConnectionSelection(t *testing.T) { |
| 136 | f := connectionStore(t) |
| 137 | f.denied(t, "engineering", "") |
| 138 | f.choice(t, "") |
| 139 | selected := f.selectOK(t, "engineering", "") |
| 140 | want := f.ids["claude"] |
| 141 | if f.ids["codex"] < want { |
| 142 | want = f.ids["codex"] |
| 143 | } |
| 144 | if selected.ConnectionID != want || selected.Reason != "first_allowed" || selected.AllowanceKnown || selected.MaxConcurrent != 1 { |
| 145 | t.Fatalf("fallback or unknown allowance: %+v", selected) |
| 146 | } |
| 147 | specs, _ := f.s.Specialists(connectionContext) |
| 148 | for _, sp := range specs { |
| 149 | if sp.Key == "engineering" { |
| 150 | sp.ModelSelection = "fixed" |
| 151 | sp.Provider = "claude" |
| 152 | sp.ModelFamily = "claude" |
| 153 | sp.Model = "sonnet" |
| 154 | if err := f.s.SaveSpecialist(connectionContext, ownerActor, sp); err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | if got := f.selectOK(t, "engineering", ""); got.Model != "sonnet" { |
| 160 | t.Fatal("fixed model ignored") |
| 161 | } |
| 162 | f.reports = append(f.reports, connectionReport("claude", "claude", "claude", "aaa")) |
| 163 | if err := f.s.ReportConnections(connectionContext, f.principal, f.reports); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if got := f.selectOK(t, "product", ""); got.ConnectionID == f.ids["claude"] && got.Model != "aaa" { |
| 167 | t.Fatal("unstable model order") |
| 168 | } |
| 169 | if got := f.selectOK(t, "engineering", ""); got.Model != "sonnet" { |
| 170 | t.Fatal("fixed model lost") |
| 171 | } |
| 172 | f.clock.Advance(90 * time.Second) |
| 173 | f.denied(t, "product", "") |
| 174 | if err := f.s.ReportConnections(connectionContext, f.principal, f.reports); err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | f.selectOK(t, "product", "") |
| 178 | } |
| 179 | func TestReviewWaitsForOtherFamily(t *testing.T) { |
| 180 | f := connectionStore(t) |
| 181 | f.choice(t, "") |
| 182 | f.selectOK(t, "engineering", "") |
| 183 | r := f.selectOK(t, "review", "claude") |
| 184 | if r.Family != "gpt" { |
| 185 | t.Fatal("review repeated family") |
| 186 | } |
| 187 | f.reports[1].Available = false |
| 188 | if err := f.s.ReportConnections(connectionContext, f.principal, f.reports); err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | f.denied(t, "review", "claude") |
| 192 | f.denied(t, "engineering", "") |
| 193 | code, key := beginDevice(t, f.s) |
| 194 | approveDevice(t, f.s, code, ownerAddress) |
| 195 | c := claimDevice(t, f.s, code, key) |
| 196 | p, _ := f.s.AuthenticateRunner(connectionContext, c.Token) |
| 197 | if err := f.s.ReportConnections(connectionContext, p, []equipo.ConnectionReport{connectionReport("remote", "codex", "gpt", "gpt-5")}); err != nil { |
| 198 | t.Fatal(err) |
| 199 | } |
| 200 | rows, _ := f.s.Connections(connectionContext, ownerActor) |
| 201 | var remote string |
| 202 | for _, row := range rows { |
| 203 | if row.LocalID == "remote" { |
| 204 | remote = row.ID |
| 205 | } |
| 206 | } |
| 207 | if err := f.s.SaveConnectionChoice(connectionContext, ownerActor, equipo.ConnectionChoice{ProjectID: f.project.ID, Revision: 1, ConnectionIDs: []string{f.ids["claude"], remote}}); err != nil { |
| 208 | t.Fatal(err) |
| 209 | } |
| 210 | f.denied(t, "engineering", "") |
| 211 | } |
| 212 | func TestAccountFailoverSelection(t *testing.T) { |
| 213 | for _, which := range []string{"observed", "binding", "family", "no_failover", "pin_home", "timeline", "process"} { |
| 214 | t.Run(which, func(t *testing.T) { |
| 215 | f := connectionStore(t) |
| 216 | f.choice(t, f.ids["claude"]) |
| 217 | r := f.reports[0] |
| 218 | switch which { |
| 219 | case "observed": |
| 220 | r.Routing.ObservedAccountID = "other" |
| 221 | case "binding": |
| 222 | r.Routing.AccountID = "other" |
| 223 | r.Routing.ObservedAccountID = "other" |
| 224 | case "family": |
| 225 | r.Family = "gpt" |
| 226 | case "no_failover": |
| 227 | r.Routing.NoFailover = false |
| 228 | case "pin_home": |
| 229 | r.Routing.AccountPinned = false |
| 230 | case "timeline": |
| 231 | r.Routing.TimelineRecovery = false |
| 232 | case "process": |
| 233 | r.Routing.ProcessOwnership = false |
| 234 | } |
| 235 | if err := f.s.ReportConnections(connectionContext, f.principal, []equipo.ConnectionReport{r}); err == nil { |
| 236 | t.Fatal("unsafe routing accepted") |
| 237 | } |
| 238 | f.denied(t, "product", "") |
| 239 | if scalar(t, f.d, "SELECT count(*) FROM connection_invalidations WHERE connection_id = ?", f.ids["claude"]) == 0 { |
| 240 | t.Fatal("unsafe report did not request containment") |
| 241 | } |
| 242 | }) |
| 243 | } |
| 244 | f := connectionStore(t) |
| 245 | f.choice(t, f.ids["claude"]) |
| 246 | if got := f.selectOK(t, "product", ""); got.ConnectionID != f.ids["claude"] || got.Reason != "pinned" { |
| 247 | t.Fatal("pin ignored") |
| 248 | } |
| 249 | f.reports[0].Available = false |
| 250 | if err := f.s.ReportConnections(connectionContext, f.principal, f.reports); err != nil { |
| 251 | t.Fatal(err) |
| 252 | } |
| 253 | f.denied(t, "product", "") |
| 254 | if err := f.s.ReportConnections(connectionContext, f.principal, f.reports[1:]); err != nil { |
| 255 | t.Fatal(err) |
| 256 | } |
| 257 | f.denied(t, "product", "") |
| 258 | } |
| 259 | func TestProjectAccountNoDeleteAndASCIILabels(t *testing.T) { |
| 260 | f := connectionStore(t) |
| 261 | if _, err := f.s.SaveAccount(connectionContext, ownerActor, equipo.Account{Label: " CONNECTIONS "}); !errors.Is(err, equipo.ErrConflict) { |
| 262 | t.Fatalf("ASCII duplicate accepted: %v", err) |
| 263 | } |
| 264 | for _, label := range []string{"Équipe", "équipe"} { |
| 265 | if _, err := f.s.SaveAccount(connectionContext, ownerActor, equipo.Account{Label: label}); err != nil { |
| 266 | t.Fatal("changed non-ASCII semantics", err) |
| 267 | } |
| 268 | } |
| 269 | h, err := equipo.App(f.d, testConfig(testOrigin), &capturingMailer{}, nil) |
| 270 | if err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | n := scalar(t, f.d, "SELECT count(*) FROM audit_events") |
| 274 | for _, path := range []string{"/accounts/1", "/projects/1"} { |
| 275 | req := httptest.NewRequest(http.MethodDelete, path, nil) |
| 276 | req.Header.Set("Origin", testOrigin) |
| 277 | w := httptest.NewRecorder() |
| 278 | h.ServeHTTP(w, req) |
| 279 | if w.Code < 400 && w.Code != 303 { |
| 280 | t.Fatal("DELETE accepted") |
| 281 | } |
| 282 | } |
| 283 | if scalar(t, f.d, "SELECT count(*) FROM audit_events") != n || scalar(t, f.d, "SELECT count(*) FROM projects") != 1 || scalar(t, f.d, "SELECT count(*) FROM accounts") != 3 { |
| 284 | t.Fatal("DELETE changed records") |
| 285 | } |
| 286 | } |
| 287 | func TestConnectionsHTTPAuthority(t *testing.T) { |
| 288 | t.Parallel() |
| 289 | f := connectionStore(t) |
| 290 | m := &capturingMailer{} |
| 291 | h, err := equipo.App(f.d, testConfig(testOrigin), m, nil) |
| 292 | if err != nil { |
| 293 | t.Fatal(err) |
| 294 | } |
| 295 | server := httptest.NewServer(h) |
| 296 | t.Cleanup(server.Close) |
| 297 | c := signinClient(t, server.URL) |
| 298 | signinEmail(t, c, server.URL, testOrigin, m, ownerAddress) |
| 299 | target := server.URL + "/connections/" + f.ids["codex"] + "/acknowledge" |
| 300 | form := url.Values{"revision": {"1"}, "acknowledge": {"unconfined"}, "owner": {"forged@example.com"}, "provider_version": {"forged"}} |
| 301 | res := clientPost(t, c, target, "http://wrong.invalid", form) |
| 302 | res.Body.Close() |
| 303 | if res.StatusCode != 403 { |
| 304 | t.Fatalf("CSRF accepted %d", res.StatusCode) |
| 305 | } |
| 306 | res = clientPost(t, c, target, testOrigin, form) |
| 307 | res.Body.Close() |
| 308 | if res.StatusCode != 404 { |
| 309 | t.Fatalf("retired consent route accepted: %d", res.StatusCode) |
| 310 | } |
| 311 | if scalar(t, f.d, "SELECT count(*) FROM isolation_acknowledgements") != 0 { |
| 312 | t.Fatal("retired consent created authority") |
| 313 | } |
| 314 | choiceURL := server.URL + "/projects/" + strconv.FormatInt(f.project.ID, 10) + "/connections" |
| 315 | res = clientPost(t, c, choiceURL, testOrigin, url.Values{"revision": {"0"}, "connection_id": {f.ids["codex"]}, "person": {"member@example.com"}}) |
| 316 | res.Body.Close() |
| 317 | if res.StatusCode != 303 { |
| 318 | t.Fatal("choice handler failed", res.StatusCode) |
| 319 | } |
| 320 | if scalar(t, f.d, "SELECT count(*) FROM connection_choices WHERE person = ?", ownerAddress) != 1 { |
| 321 | t.Fatal("form supplied choice person") |
| 322 | } |
| 323 | n := scalar(t, f.d, "SELECT count(*) FROM audit_events") |
| 324 | for _, path := range []string{"/accounts/1", "/projects/1"} { |
| 325 | req, _ := http.NewRequest(http.MethodDelete, server.URL+path, nil) |
| 326 | req.Header.Set("Origin", testOrigin) |
| 327 | res, err := c.Do(req) |
| 328 | if err != nil { |
| 329 | t.Fatal(err) |
| 330 | } |
| 331 | res.Body.Close() |
| 332 | if res.StatusCode < 400 { |
| 333 | t.Fatal("authenticated DELETE accepted") |
| 334 | } |
| 335 | } |
| 336 | if scalar(t, f.d, "SELECT count(*) FROM audit_events") != n { |
| 337 | t.Fatal("authenticated DELETE audited") |
| 338 | } |
| 339 | setMember(t, f.s, "second@example.com", equipo.RoleOwner, true) |
| 340 | member, _ := f.s.Member(connectionContext, ownerAddress) |
| 341 | if err := f.s.SetMember(connectionContext, equipo.Actor{Address: "second@example.com"}, ownerAddress, equipo.RoleMember, true, member.Revision); err != nil { |
| 342 | t.Fatal(err) |
| 343 | } |
| 344 | form.Set("owner", "second@example.com") |
| 345 | form.Set("actor", "second@example.com") |
| 346 | res = clientPost(t, c, target, testOrigin, form) |
| 347 | res.Body.Close() |
| 348 | if res.StatusCode != 404 { |
| 349 | t.Fatal("forged current-owner form accepted", res.StatusCode) |
| 350 | } |
| 351 | res = clientPost(t, c, server.URL+"/connections/"+f.ids["codex"]+"/revoke-acknowledgement", testOrigin, form) |
| 352 | res.Body.Close() |
| 353 | if res.StatusCode != 404 { |
| 354 | t.Fatal("member revoked acknowledgement") |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | func TestConnectionSelectionGuards(t *testing.T) { |
| 359 | for _, change := range []string{"person", "roster", "runner_revoked", "future_clock", "missing_pin", "fixed_model", "capacity", "capacity_error", "verified_list", "claude_exception"} { |
| 360 | t.Run(change, func(t *testing.T) { |
| 361 | f := connectionStore(t) |
| 362 | f.choice(t, f.ids["codex"]) |
| 363 | f.selectOK(t, "product", "") |
| 364 | switch change { |
| 365 | case "person": |
| 366 | mustSQL(t, f.d, "UPDATE connections SET person='other@example.com' WHERE id=?", f.ids["codex"]) |
| 367 | case "roster": |
| 368 | mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", ownerAddress) |
| 369 | case "runner_revoked": |
| 370 | if err := f.s.RevokeRunner(connectionContext, ownerActor, f.principal.RunnerID, 1); err != nil { |
| 371 | t.Fatal(err) |
| 372 | } |
| 373 | if scalar(t, f.d, "SELECT count(*) FROM connection_invalidations WHERE reason='runner_revoked'") != 2 { |
| 374 | t.Fatal("runner revocation did not request connection containment") |
| 375 | } |
| 376 | case "future_clock": |
| 377 | f.clock.Advance(-time.Second) |
| 378 | case "missing_pin": |
| 379 | mustSQL(t, f.d, "UPDATE connection_choices SET pinned_id='missing'") |
| 380 | case "fixed_model": |
| 381 | mustSQL(t, f.d, "UPDATE specialists SET model_selection='fixed', provider='codex', model_family='gpt', model='unavailable' WHERE key='product'") |
| 382 | case "capacity": |
| 383 | // Seed only the core rows used by this read-only capacity guard. No execution is opened. |
| 384 | mustSQL(t, f.d, `INSERT INTO jobs (id,account,parent,depth,requester,target,evidence,policy_version,policy,grant_ref,grant_version,grant_epoch,grant_observed_at,state,wait,revision,outcome,created_at,updated_at) VALUES(1,'team',0,0,'person','{}','{}',2,'{}','grant',1,1,'','active','{}',1,'','','')`) |
| 385 | mustSQL(t, f.d, `INSERT INTO steps(id,job_id,key,capability,version,depends_on,state,fence,holder,attempts) VALUES(1,1,'product','scope',1,'[]','running',1,1,1)`) |
| 386 | mustSQL(t, f.d, `INSERT INTO attempts(id,job_id,step_id,number,runner,workspace,fence,lease_deadline,state,containment,stop_requested,stop_acked,provider,model,effort,outcome,created_at,updated_at) VALUES(1,1,1,1,'runner','work',1,'','active','pending','','','codex','gpt-5','','','','')`) |
| 387 | case "capacity_error": |
| 388 | mustSQL(t, f.d, "ALTER TABLE attempts RENAME TO attempts_unavailable") |
| 389 | case "owner_role": |
| 390 | mustSQL(t, f.d, "UPDATE members SET role='member' WHERE address=?", ownerAddress) |
| 391 | case "owner_active": |
| 392 | setMember(t, f.s, "ack@example.com", equipo.RoleOwner, true) |
| 393 | mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address='ack@example.com'") |
| 394 | case "owner_revision": |
| 395 | mustSQL(t, f.d, "UPDATE members SET revision=revision+1 WHERE address=?", ownerAddress) |
| 396 | case "verified_list", "claude_exception": |
| 397 | if err := f.s.SaveConnectionChoice(connectionContext, ownerActor, equipo.ConnectionChoice{ProjectID: f.project.ID, Revision: 1, ConnectionIDs: []string{f.ids["claude"]}}); err != nil { |
| 398 | t.Fatal(err) |
| 399 | } |
| 400 | f.s = equipo.NewStore(f.d, equipo.WithRunnerClock(f.clock)) |
| 401 | |
| 402 | } |
| 403 | f.denied(t, "product", "") |
| 404 | }) |
| 405 | } |
| 406 | t.Run("immutable_tuples", func(t *testing.T) { |
| 407 | f := connectionStore(t) |
| 408 | tuples := []team.IsolationTuple{connectionTuple(f.reports[0])} |
| 409 | option := equipo.WithVerifiedIsolationTuples(tuples) |
| 410 | tuples[0] = connectionTuple(f.reports[1]) |
| 411 | f.s = equipo.NewStore(f.d, equipo.WithRunnerClock(f.clock), option) |
| 412 | f.choice(t, f.ids["codex"]) |
| 413 | f.denied(t, "product", "") |
| 414 | }) |
| 415 | } |
| 416 | |
| 417 | func TestConnectionsAtomicWrites(t *testing.T) { |
| 418 | for _, operation := range []string{"report", "choice"} { |
| 419 | t.Run(operation, func(t *testing.T) { |
| 420 | f := connectionStore(t) |
| 421 | n := scalar(t, f.d, "SELECT count(*) FROM audit_events") |
| 422 | requests := scalar(t, f.d, "SELECT count(*) FROM connection_invalidations") |
| 423 | mustSQL(t, f.d, "CREATE TRIGGER fail_connection_audit BEFORE INSERT ON audit_events BEGIN SELECT RAISE(ABORT, 'audit offline'); END") |
| 424 | var err error |
| 425 | switch operation { |
| 426 | case "report": |
| 427 | f.reports[1].IsolationConfigHash = strings.Repeat("c", 64) |
| 428 | err = f.s.ReportConnections(connectionContext, f.principal, f.reports) |
| 429 | case "choice": |
| 430 | err = f.s.SaveConnectionChoice(connectionContext, ownerActor, equipo.ConnectionChoice{ProjectID: f.project.ID, ConnectionIDs: []string{f.ids["codex"]}}) |
| 431 | } |
| 432 | if err == nil { |
| 433 | t.Fatal("write succeeded without audit") |
| 434 | } |
| 435 | if scalar(t, f.d, "SELECT count(*) FROM audit_events") != n || scalar(t, f.d, "SELECT count(*) FROM connection_invalidations") != requests || scalar(t, f.d, "SELECT revision FROM connections WHERE id=?", f.ids["codex"]) != 1 || scalar(t, f.d, "SELECT count(*) FROM isolation_acknowledgements") != 0 || scalar(t, f.d, "SELECT count(*) FROM connection_choices") != 0 { |
| 436 | t.Fatal("failed audit left mutation") |
| 437 | } |
| 438 | }) |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | func TestConnectionSelectionModelAndAllowance(t *testing.T) { |
| 443 | f := connectionStore(t) |
| 444 | f.choice(t, f.ids["claude"]) |
| 445 | f.reports = append(f.reports, connectionReport("claude", "claude", "claude", "aaa")) |
| 446 | f.reports[0].AllowanceKnown = true |
| 447 | f.reports[0].AllowanceRemaining = 15 |
| 448 | if err := f.s.ReportConnections(connectionContext, f.principal, f.reports); err != nil { |
| 449 | t.Fatal(err) |
| 450 | } |
| 451 | got := f.selectOK(t, "product", "") |
| 452 | if got.Model != "aaa" || got.AllowanceKnown { |
| 453 | t.Fatal("model order or unknown allowance") |
| 454 | } |
| 455 | mustSQL(t, f.d, "UPDATE specialists SET model_selection='fixed',provider='claude',model_family='claude',model='sonnet' WHERE key='product'") |
| 456 | got = f.selectOK(t, "product", "") |
| 457 | if !got.AllowanceKnown || got.AllowanceRemaining != 15 { |
| 458 | t.Fatal("known allowance lost") |
| 459 | } |
| 460 | rows, err := f.s.Connections(connectionContext, ownerActor) |
| 461 | if err != nil { |
| 462 | t.Fatal(err) |
| 463 | } |
| 464 | for _, r := range rows { |
| 465 | if r.Revision != 1 { |
| 466 | t.Fatal("models or allowance changed security revision") |
| 467 | } |
| 468 | } |
| 469 | if err := f.s.SaveConnectionChoice(connectionContext, ownerActor, equipo.ConnectionChoice{ProjectID: f.project.ID, Revision: 0, ConnectionIDs: []string{f.ids["claude"]}}); !errors.Is(err, equipo.ErrConflict) { |
| 470 | t.Fatal("stale choice accepted") |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | func TestConnectionSelectionOptIn(t *testing.T) { |
| 475 | f := connectionStore(t) |
| 476 | if err := f.s.SaveConnectionChoice(connectionContext, ownerActor, equipo.ConnectionChoice{ProjectID: f.project.ID, ConnectionIDs: []string{f.ids["claude"]}}); err != nil { |
| 477 | t.Fatal(err) |
| 478 | } |
| 479 | mustSQL(t, f.d, "UPDATE specialists SET model_selection='fixed',provider='codex',model_family='gpt',model='gpt-5' WHERE key='product'") |
| 480 | f.denied(t, "product", "") |
| 481 | } |
| 482 | |
| 483 | func setDirectAvailable(s *equipo.Store, p equipo.RunnerPrincipal, reports []equipo.ConnectionReport, available bool) error { |
| 484 | r := append([]equipo.ConnectionReport(nil), reports...) |
| 485 | r[1].Available = available |
| 486 | return s.ReportConnections(connectionContext, p, r) |
| 487 | } |
| 488 | |