| 1 | package equipotest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "net/http/httptest" |
| 10 | "net/url" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/equipo" |
| 16 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 17 | "gorm.io/gorm" |
| 18 | ) |
| 19 | |
| 20 | func bugFixture(t *testing.T) connectionFixture { |
| 21 | t.Helper() |
| 22 | f := connectionStore(t) |
| 23 | f.choice(t, f.ids["claude"]) |
| 24 | l := team.PeriodLimits{Calls: 24, InputTokens: 200000, OutputTokens: 40000, Seconds: 10000} |
| 25 | executionMust(t, f.s.SaveBudgets(connectionContext, ownerActor, equipo.BudgetConfig{Team: l, Projects: map[team.ProjectID]team.PeriodLimits{team.ProjectID(f.project.ID): l}})) |
| 26 | return f |
| 27 | } |
| 28 | func bugTask(t *testing.T, f connectionFixture) equipo.Task { |
| 29 | t.Helper() |
| 30 | task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "the total is wrong", "request-1") |
| 31 | executionMust(t, err) |
| 32 | return task |
| 33 | } |
| 34 | func bugPrepared(f connectionFixture) equipo.PreparedBug { |
| 35 | b := []byte("trusted check definition") |
| 36 | return equipo.PreparedBug{Repository: f.project.Repository, DefaultBranch: f.project.DefaultBranch, BaseCommit: strings.Repeat("a", 40), CheckDefinition: b, CheckDefinitionHash: fmt.Sprintf("%x", sha256.Sum256(b)), BaselineTestSetHash: strings.Repeat("b", 64)} |
| 37 | } |
| 38 | func TestBugSubmissionRetry(t *testing.T) { |
| 39 | f := bugFixture(t) |
| 40 | task := bugTask(t, f) |
| 41 | again := bugTask(t, f) |
| 42 | if again.ID != task.ID || task.ReviewRef != "equipo/"+task.ID || task.Requester != ownerAddress { |
| 43 | t.Fatal("submission identity changed") |
| 44 | } |
| 45 | if _, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "changed", "request-1"); !errors.Is(err, equipo.ErrConflict) { |
| 46 | t.Fatalf("changed payload accepted: %v", err) |
| 47 | } |
| 48 | if scalar(t, f.d, "SELECT count(*) FROM tasks") != 1 || scalar(t, f.d, "SELECT count(*) FROM task_outboxes") != 1 || scalar(t, f.d, "SELECT count(*) FROM audit_events WHERE kind='task.create'") != 1 { |
| 49 | t.Fatal("duplicate submission writes") |
| 50 | } |
| 51 | for _, s := range []string{"", strings.Repeat("a", 16385), string([]byte{0xff})} { |
| 52 | if _, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, s, "bad"); !errors.Is(err, equipo.ErrInvalid) { |
| 53 | t.Fatal("description accepted", err) |
| 54 | } |
| 55 | } |
| 56 | if _, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, strings.Repeat("é", 8192), "max"); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | setMember(t, f.s, "reader@example.com", equipo.RoleMember, true) |
| 60 | if _, err := f.s.CreateBug(connectionContext, equipo.Actor{Address: "reader@example.com"}, f.project.ID, "bug", "reader"); !errors.Is(err, equipo.ErrForbidden) { |
| 61 | t.Fatal("reader started task", err) |
| 62 | } |
| 63 | if _, err := f.s.Task(connectionContext, equipo.Actor{Address: "reader@example.com"}, task.ID); err != nil { |
| 64 | t.Fatal("member cannot read", err) |
| 65 | } |
| 66 | mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", ownerAddress) |
| 67 | if _, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, task.Description, task.RequestID); !errors.Is(err, equipo.ErrForbidden) { |
| 68 | t.Fatal("removed requester retry", err) |
| 69 | } |
| 70 | } |
| 71 | func TestBugFormCannotChooseAuthority(t *testing.T) { |
| 72 | f := bugFixture(t) |
| 73 | m := &capturingMailer{} |
| 74 | h, err := equipo.App(f.d, testConfig(testOrigin), m, nil) |
| 75 | executionMust(t, err) |
| 76 | server := httptest.NewServer(h) |
| 77 | t.Cleanup(server.Close) |
| 78 | c := signinClient(t, server.URL) |
| 79 | signinEmail(t, c, server.URL, testOrigin, m, ownerAddress) |
| 80 | form := url.Values{"project_id": {strconv.FormatInt(f.project.ID, 10)}, "description": {"bug"}, "request_id": {"form"}} |
| 81 | for _, key := range []string{"requester", "repository", "grant", "budget", "ref", "kind", "actor", "connection_id", "runner_id", "policy", "steps"} { |
| 82 | form.Set(key, "forged") |
| 83 | res := clientPost(t, c, server.URL+"/tasks", testOrigin, form) |
| 84 | res.Body.Close() |
| 85 | if res.StatusCode != 422 { |
| 86 | t.Fatalf("forged %s accepted: %d", key, res.StatusCode) |
| 87 | } |
| 88 | form.Del(key) |
| 89 | } |
| 90 | if scalar(t, f.d, "SELECT count(*) FROM tasks") != 0 { |
| 91 | t.Fatal("forged forms wrote tasks") |
| 92 | } |
| 93 | res := clientPost(t, c, server.URL+"/tasks", "http://wrong.invalid", form) |
| 94 | res.Body.Close() |
| 95 | if res.StatusCode != 403 { |
| 96 | t.Fatal("CSRF accepted") |
| 97 | } |
| 98 | } |
| 99 | func TestFixedBugPlan(t *testing.T) { |
| 100 | f := bugFixture(t) |
| 101 | task := bugTask(t, f) |
| 102 | task, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f)) |
| 103 | executionMust(t, err) |
| 104 | policy, err := team.RunnerPolicy("subscription", team.Limits{Calls: 24, InputTokens: 200000, OutputTokens: 40000}, team.Limits{Calls: 1, InputTokens: 32000, OutputTokens: 4000}, team.Rates{}) |
| 105 | executionMust(t, err) |
| 106 | spec, err := equipo.BugJobSpec(task, f.project, policy, task.Description) |
| 107 | executionMust(t, err) |
| 108 | caps := []team.Capability{team.CapReproduce, team.CapImplement, team.CapReview, team.CapVerify, team.CapPublish} |
| 109 | if len(spec.Steps) != 5 || spec.RequestKey != task.ID || spec.Project != team.ProjectID(task.ProjectID) || spec.GrantRef != equipo.GrantRef(task.ProjectID, task.Requester) || !spec.Evidence.RequireBugFreeze { |
| 110 | t.Fatal("wrong fixed plan", spec) |
| 111 | } |
| 112 | for i, cap := range caps { |
| 113 | st := spec.Steps[i] |
| 114 | if st.Capability != cap || i > 0 && (len(st.DependsOn) != 1 || st.DependsOn[0] != spec.Steps[i-1].Key) { |
| 115 | t.Fatal("wrong dependency", st) |
| 116 | } |
| 117 | } |
| 118 | plan := equipo.BugPlan(task) |
| 119 | if len(plan) != 6 || plan[5].Key != "merge" || plan[5].Person == "" || !plan[4].RunnerOnly { |
| 120 | t.Fatal("human/runner plan", plan) |
| 121 | } |
| 122 | wrong := f.project |
| 123 | wrong.ID++ |
| 124 | if _, err := equipo.BugJobSpec(task, wrong, policy, task.Description); err == nil { |
| 125 | t.Fatal("project/grant split") |
| 126 | } |
| 127 | } |
| 128 | func TestBugPreparationCrash(t *testing.T) { |
| 129 | for _, stage := range []string{"prepare", "link"} { |
| 130 | t.Run(stage, func(t *testing.T) { |
| 131 | f := bugFixture(t) |
| 132 | task := bugTask(t, f) |
| 133 | prep := bugPrepared(f) |
| 134 | var seq int |
| 135 | var name, path string |
| 136 | executionMust(t, f.d.Writer().QueryRow("PRAGMA database_list").Scan(&seq, &name, &path)) |
| 137 | x, err := f.s.AttachExecution(connectionContext, path, team.Options{Clock: f.clock, Authority: equipo.NewAuthority(f.s, f.clock)}) |
| 138 | executionMust(t, err) |
| 139 | t.Cleanup(func() { x.Core.Close() }) |
| 140 | _, err = x.Core.Resume(connectionContext) |
| 141 | executionMust(t, err) |
| 142 | reopen := func() { |
| 143 | executionMust(t, x.Core.Close()) |
| 144 | f.s = equipo.NewStore(f.d, equipo.WithRunnerClock(f.clock), equipo.WithVerifiedIsolationTuples([]team.IsolationTuple{connectionTuple(f.reports[0]), connectionTuple(f.reports[1])})) |
| 145 | x, err = f.s.AttachExecution(connectionContext, path, team.Options{Clock: f.clock, Authority: equipo.NewAuthority(f.s, f.clock)}) |
| 146 | executionMust(t, err) |
| 147 | _, err = x.Core.Resume(connectionContext) |
| 148 | executionMust(t, err) |
| 149 | } |
| 150 | fail := func(kind string) { |
| 151 | executionMust(t, f.d.G.Callback().Create().Before("gorm:create").Register("task7_crash", func(tx *gorm.DB) { |
| 152 | if tx.Statement.Table == "audit_events" { |
| 153 | if ev, ok := tx.Statement.Dest.(*equipo.AuditEvent); ok && ev.Kind == kind { |
| 154 | tx.AddError(errors.New("crash before acknowledgement")) |
| 155 | } |
| 156 | } |
| 157 | })) |
| 158 | } |
| 159 | if stage == "prepare" { |
| 160 | fail("task.prepare") |
| 161 | if _, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, prep); err == nil { |
| 162 | t.Fatal("prepare crash not injected") |
| 163 | } |
| 164 | executionMust(t, f.d.G.Callback().Create().Remove("task7_crash")) |
| 165 | if scalar(t, f.d, "SELECT revision FROM tasks WHERE id=?", task.ID) != 1 { |
| 166 | t.Fatal("partial preparation") |
| 167 | } |
| 168 | reopen() |
| 169 | } |
| 170 | task, err = f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, prep) |
| 171 | executionMust(t, err) |
| 172 | reopen() |
| 173 | if stage == "link" { |
| 174 | fail("task.link") |
| 175 | if _, err := f.s.StartBug(connectionContext, f.principal, task.ID, task.Revision); err == nil { |
| 176 | t.Fatal("link crash not injected") |
| 177 | } |
| 178 | executionMust(t, f.d.G.Callback().Create().Remove("task7_crash")) |
| 179 | if scalar(t, f.d, "SELECT count(*) FROM jobs") != 0 || scalar(t, f.d, "SELECT job_id FROM tasks WHERE id=?", task.ID) != 0 { |
| 180 | t.Fatal("job creation escaped the failed task-link transaction") |
| 181 | } |
| 182 | reopen() |
| 183 | } |
| 184 | linked, err := f.s.StartBug(connectionContext, f.principal, task.ID, task.Revision) |
| 185 | executionMust(t, err) |
| 186 | reopen() |
| 187 | again, err := f.s.StartBug(connectionContext, f.principal, task.ID, task.Revision) |
| 188 | executionMust(t, err) |
| 189 | if linked.JobID == 0 || linked.JobID != again.JobID || scalar(t, f.d, "SELECT count(*) FROM jobs") != 1 || scalar(t, f.d, "SELECT count(*) FROM task_outboxes WHERE state='done'") != 1 { |
| 190 | t.Fatal("outbox recovery duplicated job") |
| 191 | } |
| 192 | }) |
| 193 | } |
| 194 | } |
| 195 | func TestBugPreparationGuards(t *testing.T) { |
| 196 | for _, guard := range []string{"project", "connection", "review", "choice", "credential", "person", "revision", "repository", "default", "base", "definition", "baseline", "budget", "budget_revision", "review_snapshot", "roster"} { |
| 197 | t.Run(guard, func(t *testing.T) { |
| 198 | f := bugFixture(t) |
| 199 | task := bugTask(t, f) |
| 200 | prep := bugPrepared(f) |
| 201 | principal := f.principal |
| 202 | rev := task.Revision |
| 203 | switch guard { |
| 204 | case "project": |
| 205 | mustSQL(t, f.d, "UPDATE projects SET revision=revision+1") |
| 206 | case "connection": |
| 207 | mustSQL(t, f.d, "UPDATE connections SET revision=revision+1 WHERE id=?", f.ids["claude"]) |
| 208 | case "review": |
| 209 | mustSQL(t, f.d, "UPDATE connections SET revision=revision+1 WHERE id=?", f.ids["codex"]) |
| 210 | case "choice": |
| 211 | mustSQL(t, f.d, "UPDATE connection_choices SET revision=revision+1") |
| 212 | case "credential": |
| 213 | mustSQL(t, f.d, "UPDATE runner_credentials SET revoked_at=?", f.clock.Now()) |
| 214 | case "person": |
| 215 | principal.Person = "member@example.com" |
| 216 | case "revision": |
| 217 | rev++ |
| 218 | case "repository": |
| 219 | prep.Repository = "amadan.net/other/repo" |
| 220 | case "default": |
| 221 | prep.DefaultBranch = "other" |
| 222 | case "base": |
| 223 | prep.BaseCommit = "mutable-ref" |
| 224 | case "definition": |
| 225 | prep.CheckDefinitionHash = strings.Repeat("c", 64) |
| 226 | case "baseline": |
| 227 | prep.BaselineTestSetHash = "" |
| 228 | case "review_snapshot": |
| 229 | mustSQL(t, f.d, "UPDATE task_outboxes SET review=json_set(review,'$.Revision',99)") |
| 230 | case "budget_revision": |
| 231 | config, err := f.s.Budgets(connectionContext) |
| 232 | executionMust(t, err) |
| 233 | executionMust(t, f.s.SaveBudgets(connectionContext, ownerActor, config)) |
| 234 | case "budget": |
| 235 | mustSQL(t, f.d, "DELETE FROM budget_records") |
| 236 | case "roster": |
| 237 | mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", ownerAddress) |
| 238 | } |
| 239 | if _, err := f.s.PrepareBug(context.Background(), principal, task.ID, rev, prep); err == nil { |
| 240 | t.Fatal("preparation guard bypassed", guard) |
| 241 | } |
| 242 | if scalar(t, f.d, "SELECT revision FROM tasks WHERE id=?", task.ID) != 1 { |
| 243 | t.Fatal("failed preparation mutated task") |
| 244 | } |
| 245 | }) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | func TestBugSubmissionAtomicAndConcurrent(t *testing.T) { |
| 250 | f := bugFixture(t) |
| 251 | executionMust(t, f.d.G.Callback().Create().Before("gorm:create").Register("task7_create_fail", func(tx *gorm.DB) { |
| 252 | if tx.Statement.Table == "audit_events" { |
| 253 | tx.AddError(errors.New("audit unavailable")) |
| 254 | } |
| 255 | })) |
| 256 | if _, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "bug", "atomic"); err == nil { |
| 257 | t.Fatal("audit failure accepted") |
| 258 | } |
| 259 | executionMust(t, f.d.G.Callback().Create().Remove("task7_create_fail")) |
| 260 | if scalar(t, f.d, "SELECT count(*) FROM tasks") != 0 || scalar(t, f.d, "SELECT count(*) FROM task_outboxes") != 0 { |
| 261 | t.Fatal("partial creation") |
| 262 | } |
| 263 | results := make(chan equipo.Task, 6) |
| 264 | errs := make(chan error, 6) |
| 265 | for i := 0; i < 6; i++ { |
| 266 | go func() { |
| 267 | task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "bug", "concurrent") |
| 268 | results <- task |
| 269 | errs <- err |
| 270 | }() |
| 271 | } |
| 272 | var id string |
| 273 | for i := 0; i < 6; i++ { |
| 274 | task := <-results |
| 275 | executionMust(t, <-errs) |
| 276 | if id != "" && id != task.ID { |
| 277 | t.Fatal("concurrent duplicate") |
| 278 | } |
| 279 | id = task.ID |
| 280 | } |
| 281 | if scalar(t, f.d, "SELECT count(*) FROM tasks") != 1 { |
| 282 | t.Fatal("concurrent duplicate rows") |
| 283 | } |
| 284 | } |
| 285 | func TestBugWorkOwner(t *testing.T) { |
| 286 | f := bugFixture(t) |
| 287 | setMember(t, f.s, "ack@example.com", equipo.RoleOwner, true) |
| 288 | member, err := f.s.Member(connectionContext, ownerAddress) |
| 289 | executionMust(t, err) |
| 290 | executionMust(t, f.s.SetMember(connectionContext, equipo.Actor{Address: "ack@example.com"}, ownerAddress, equipo.RoleMember, true, member.Revision)) |
| 291 | task := bugTask(t, f) |
| 292 | if task.Requester != ownerAddress { |
| 293 | t.Fatal("work owner attribution") |
| 294 | } |
| 295 | mustSQL(t, f.d, "UPDATE decision_owners SET address=? WHERE decision='work'", "member@example.com") |
| 296 | if _, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, task.Description, task.RequestID); !errors.Is(err, equipo.ErrForbidden) { |
| 297 | t.Fatal("changed work owner retried", err) |
| 298 | } |
| 299 | } |
| 300 | func TestBugPreparedRetryIsImmutable(t *testing.T) { |
| 301 | f := bugFixture(t) |
| 302 | task := bugTask(t, f) |
| 303 | p := bugPrepared(f) |
| 304 | prepared, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, p) |
| 305 | executionMust(t, err) |
| 306 | again, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, p) |
| 307 | executionMust(t, err) |
| 308 | if prepared.Revision != again.Revision || scalar(t, f.d, "SELECT count(*) FROM audit_events WHERE kind='task.prepare'") != 1 { |
| 309 | t.Fatal("prepare retry changed state") |
| 310 | } |
| 311 | p.BaseCommit = strings.Repeat("c", 40) |
| 312 | if _, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, prepared.Revision, p); !errors.Is(err, equipo.ErrConflict) { |
| 313 | t.Fatal("base mutated", err) |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | func TestBugPreparationLookupFailure(t *testing.T) { |
| 318 | for _, table := range []string{"members", "projects", "budget_records", "connection_choices", "connections", "runner_credentials", "task_outboxes"} { |
| 319 | t.Run(table, func(t *testing.T) { |
| 320 | f := bugFixture(t) |
| 321 | task := bugTask(t, f) |
| 322 | executionMust(t, f.d.G.Callback().Query().Before("gorm:query").Register("task7_lookup", func(tx *gorm.DB) { |
| 323 | if tx.Statement.Table == table { |
| 324 | tx.AddError(errors.New("lookup unavailable")) |
| 325 | } |
| 326 | })) |
| 327 | _, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f)) |
| 328 | executionMust(t, f.d.G.Callback().Query().Remove("task7_lookup")) |
| 329 | if err == nil { |
| 330 | t.Fatal("lookup failure granted preparation", table) |
| 331 | } |
| 332 | if scalar(t, f.d, "SELECT revision FROM tasks WHERE id=?", task.ID) != 1 { |
| 333 | t.Fatal("lookup failure mutated task") |
| 334 | } |
| 335 | }) |
| 336 | } |
| 337 | } |
| 338 | func TestBugPreparationOtherMachine(t *testing.T) { |
| 339 | f := bugFixture(t) |
| 340 | task := bugTask(t, f) |
| 341 | code, key := beginDevice(t, f.s) |
| 342 | approveDevice(t, f.s, code, ownerAddress) |
| 343 | credential := claimDevice(t, f.s, code, key) |
| 344 | p, err := f.s.AuthenticateRunner(connectionContext, credential.Token) |
| 345 | executionMust(t, err) |
| 346 | if _, err := f.s.PrepareBug(connectionContext, p, task.ID, task.Revision, bugPrepared(f)); !errors.Is(err, equipo.ErrForbidden) { |
| 347 | t.Fatal("other machine prepared", err) |
| 348 | } |
| 349 | } |
| 350 | func TestBugSubmissionWaitsForConnectionsAndBudget(t *testing.T) { |
| 351 | for _, guard := range []string{"budget", "engineering", "review", "api"} { |
| 352 | t.Run(guard, func(t *testing.T) { |
| 353 | f := bugFixture(t) |
| 354 | switch guard { |
| 355 | case "budget": |
| 356 | mustSQL(t, f.d, "DELETE FROM budget_records") |
| 357 | case "engineering": |
| 358 | mustSQL(t, f.d, "UPDATE connection_choices SET pinned_id='missing'") |
| 359 | case "review": |
| 360 | executionMust(t, setDirectAvailable(f.s, f.principal, f.reports, false)) |
| 361 | case "api": |
| 362 | f.reports[0].Kind = "api" |
| 363 | f.reports[0].LocalID = "api-claude" |
| 364 | f.reports[0].Routing.BindingID = "api-claude" |
| 365 | executionMust(t, f.s.ReportConnections(connectionContext, f.principal, f.reports)) |
| 366 | connections, err := f.s.Connections(connectionContext, ownerActor) |
| 367 | executionMust(t, err) |
| 368 | for _, c := range connections { |
| 369 | if c.LocalID == "api-claude" { |
| 370 | f.ids["claude"] = c.ID |
| 371 | } |
| 372 | } |
| 373 | executionMust(t, f.s.SaveConnectionChoice(connectionContext, ownerActor, equipo.ConnectionChoice{ProjectID: f.project.ID, Revision: 1, ConnectionIDs: []string{f.ids["claude"], f.ids["codex"]}, PinnedID: f.ids["claude"]})) |
| 374 | } |
| 375 | task, err := f.s.CreateBug(connectionContext, ownerActor, f.project.ID, "bug", "refused") |
| 376 | if guard == "budget" { |
| 377 | if err == nil || scalar(t, f.d, "SELECT count(*) FROM tasks") != 0 { |
| 378 | t.Fatal("unfunded submission accepted") |
| 379 | } |
| 380 | } else { |
| 381 | executionMust(t, err) |
| 382 | if task.WaitCode != "task.wait.setup" || task.JobID != 0 { |
| 383 | t.Fatal("unavailable connection did not wait", task) |
| 384 | } |
| 385 | } |
| 386 | }) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | type fakeBugPreparer struct { |
| 391 | t *testing.T |
| 392 | f connectionFixture |
| 393 | } |
| 394 | |
| 395 | func (p fakeBugPreparer) Prepare(_ context.Context, target equipo.TargetBinding) (equipo.PreparedBug, error) { |
| 396 | if scalar(p.t, p.f.d, "SELECT count(*) FROM tasks") != 1 || scalar(p.t, p.f.d, "SELECT count(*) FROM task_outboxes WHERE state='prepare'") != 1 || scalar(p.t, p.f.d, "SELECT count(*) FROM audit_events WHERE kind='task.create'") != 1 { |
| 397 | p.t.Fatal("preparation before durable request") |
| 398 | } |
| 399 | if target.Repository != p.f.project.Repository || target.DefaultRef != p.f.project.DefaultBranch { |
| 400 | p.t.Fatal("wrong preparation target") |
| 401 | } |
| 402 | return bugPrepared(p.f), nil |
| 403 | } |
| 404 | func TestBugPreparationFake(t *testing.T) { |
| 405 | f := bugFixture(t) |
| 406 | task := bugTask(t, f) |
| 407 | var preparer equipo.BugPreparer = fakeBugPreparer{t, f} |
| 408 | observed, err := preparer.Prepare(connectionContext, equipo.TargetBinding{Kind: team.TargetRepository, Repository: f.project.Repository, DefaultRef: f.project.DefaultBranch, Ref: task.ReviewRef}) |
| 409 | executionMust(t, err) |
| 410 | _, err = f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, observed) |
| 411 | executionMust(t, err) |
| 412 | } |
| 413 | |
| 414 | func attachBugExecution(t *testing.T, f connectionFixture) *equipo.Execution { |
| 415 | t.Helper() |
| 416 | var seq int |
| 417 | var name, path string |
| 418 | executionMust(t, f.d.Writer().QueryRow("PRAGMA database_list").Scan(&seq, &name, &path)) |
| 419 | x, err := f.s.AttachExecution(connectionContext, path, team.Options{Clock: f.clock, Authority: equipo.NewAuthority(f.s, f.clock)}) |
| 420 | executionMust(t, err) |
| 421 | t.Cleanup(func() { x.Core.Close() }) |
| 422 | _, err = x.Core.Resume(connectionContext) |
| 423 | executionMust(t, err) |
| 424 | return x |
| 425 | } |
| 426 | func TestBugStartGuards(t *testing.T) { |
| 427 | for _, guard := range []string{"project", "choice", "review", "requester", "credential", "revision", "budget"} { |
| 428 | t.Run(guard, func(t *testing.T) { |
| 429 | f := bugFixture(t) |
| 430 | task := bugTask(t, f) |
| 431 | task, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f)) |
| 432 | executionMust(t, err) |
| 433 | attachBugExecution(t, f) |
| 434 | switch guard { |
| 435 | case "project": |
| 436 | mustSQL(t, f.d, "UPDATE projects SET revision=revision+1") |
| 437 | case "choice": |
| 438 | mustSQL(t, f.d, "UPDATE connection_choices SET revision=revision+1") |
| 439 | case "review": |
| 440 | mustSQL(t, f.d, "UPDATE task_outboxes SET review=json_set(review,'$.Revision',99)") |
| 441 | case "requester": |
| 442 | mustSQL(t, f.d, "UPDATE members SET active=0 WHERE address=?", ownerAddress) |
| 443 | case "credential": |
| 444 | mustSQL(t, f.d, "UPDATE runner_credentials SET revoked_at=?", f.clock.Now()) |
| 445 | case "revision": |
| 446 | task.Revision++ |
| 447 | case "budget": |
| 448 | mustSQL(t, f.d, "DELETE FROM budget_records") |
| 449 | } |
| 450 | if _, err := f.s.StartBug(connectionContext, f.principal, task.ID, task.Revision); err == nil { |
| 451 | t.Fatal("stale preparation started", guard) |
| 452 | } |
| 453 | if scalar(t, f.d, "SELECT count(*) FROM jobs") != 0 { |
| 454 | t.Fatal("job created before current checks") |
| 455 | } |
| 456 | }) |
| 457 | } |
| 458 | } |
| 459 | func TestJobRequestKeySharedSchema(t *testing.T) { |
| 460 | for _, missing := range []string{"index", "column"} { |
| 461 | t.Run(missing, func(t *testing.T) { |
| 462 | d, path := executionDB(t) |
| 463 | mustSQL(t, d, "DROP INDEX jobs_request") |
| 464 | if missing == "column" { |
| 465 | mustSQL(t, d, "ALTER TABLE jobs DROP COLUMN request_hash") |
| 466 | mustSQL(t, d, "CREATE UNIQUE INDEX jobs_request ON jobs(account,requester,request_key) WHERE request_key <> ''") |
| 467 | } |
| 468 | x, err := equipo.NewStore(d).AttachExecution(connectionContext, path, executionOptions()) |
| 469 | if err == nil { |
| 470 | x.Core.Close() |
| 471 | t.Fatal("incomplete v6 schema opened", missing) |
| 472 | } |
| 473 | }) |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | func TestBugReviewUsesSelectedMachine(t *testing.T) { |
| 478 | f := bugFixture(t) |
| 479 | code, key := beginDevice(t, f.s) |
| 480 | approveDevice(t, f.s, code, ownerAddress) |
| 481 | credential := claimDevice(t, f.s, code, key) |
| 482 | p, err := f.s.AuthenticateRunner(connectionContext, credential.Token) |
| 483 | executionMust(t, err) |
| 484 | executionMust(t, f.s.ReportConnections(connectionContext, p, []equipo.ConnectionReport{f.reports[1]})) |
| 485 | // Make the other machine's eligible reviewer sort first, independent of random IDs. |
| 486 | mustSQL(t, f.d, "UPDATE connections SET id='000-other-reviewer' WHERE runner_id=?", p.RunnerID) |
| 487 | executionMust(t, f.s.SaveConnectionChoice(connectionContext, ownerActor, equipo.ConnectionChoice{ProjectID: f.project.ID, Revision: 1, ConnectionIDs: []string{f.ids["claude"], f.ids["codex"], "000-other-reviewer"}, PinnedID: f.ids["claude"]})) |
| 488 | task := bugTask(t, f) |
| 489 | var review string |
| 490 | executionMust(t, f.d.Writer().QueryRow("SELECT review FROM task_outboxes WHERE task_id=?", task.ID).Scan(&review)) |
| 491 | var selected equipo.Selection |
| 492 | executionMust(t, json.Unmarshal([]byte(review), &selected)) |
| 493 | if selected.RunnerID != f.principal.RunnerID || selected.ConnectionID != f.ids["codex"] { |
| 494 | t.Fatal("review crossed selected machine", selected) |
| 495 | } |
| 496 | _, err = f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f)) |
| 497 | executionMust(t, err) |
| 498 | } |
| 499 | |
| 500 | func TestFixedBugPlanRejectsZeroProject(t *testing.T) { |
| 501 | f := bugFixture(t) |
| 502 | task := bugTask(t, f) |
| 503 | task, err := f.s.PrepareBug(connectionContext, f.principal, task.ID, task.Revision, bugPrepared(f)) |
| 504 | executionMust(t, err) |
| 505 | policy, err := team.RunnerPolicy("subscription", team.Limits{Calls: 24, InputTokens: 200000, OutputTokens: 40000}, team.Limits{Calls: 1, InputTokens: 32000, OutputTokens: 4000}, team.Rates{}) |
| 506 | executionMust(t, err) |
| 507 | task.ProjectID = 0 |
| 508 | f.project.ID = 0 |
| 509 | if _, err := equipo.BugJobSpec(task, f.project, policy, task.Description); err == nil { |
| 510 | t.Fatal("zero project produced an empty grant") |
| 511 | } |
| 512 | } |
| 513 | |