| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 5 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "net/url" |
| 9 | "reflect" |
| 10 | "strconv" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/equipo" |
| 16 | "amadan.net/oficina/equipo/internal/taskplan" |
| 17 | ) |
| 18 | |
| 19 | func modelChoiceFixture(t *testing.T) (*fixture, taskplan.Plan) { |
| 20 | t.Helper() |
| 21 | f := newFixture(t) |
| 22 | p := proposed(t, f) |
| 23 | f.planConnections(t, p.ProjectID) |
| 24 | f.exec(t, `INSERT INTO connections(id,runner_id,person,local_id,revision,last_seen,reports_json,security_hash) SELECT 'claude-two',runner_id,person,'claude-two',revision,last_seen,reports_json,security_hash FROM connections WHERE id='claude'`) |
| 25 | f.exec(t, `UPDATE connection_choices SET pinned_id='',connection_ids_json='["claude","claude-two","codex"]'`) |
| 26 | return f, p |
| 27 | } |
| 28 | func pick(t *testing.T, f *fixture, p taskplan.Plan, key string) equipo.Selection { |
| 29 | t.Helper() |
| 30 | s, e := f.Store.SelectPlanStep(ctx, owner, p.TaskID, key, equipo.Mutation{RequestID: "pick-" + key, Revision: p.Revision}) |
| 31 | must(t, e) |
| 32 | return s |
| 33 | } |
| 34 | func TestPlanModelChoiceOwnAccounts(t *testing.T) { |
| 35 | f, p := modelChoiceFixture(t) |
| 36 | f.exec(t, `UPDATE connections SET person=? WHERE id='claude'`, peer.Address) |
| 37 | s := pick(t, f, p, "reproduce") |
| 38 | if s.ConnectionID != "claude-two" || s.Person != owner.Address { |
| 39 | t.Fatal("borrowed another person's account", s) |
| 40 | } |
| 41 | if _, e := f.Store.SelectPlanStep(ctx, peer, p.TaskID, "reproduce", equipo.Mutation{RequestID: "peer", Revision: p.Revision}); e == nil { |
| 42 | t.Fatal("peer selected owner's account") |
| 43 | } |
| 44 | f.exec(t, `UPDATE connection_choices SET connection_ids_json='["codex"]',revision=revision+1`) |
| 45 | if _, e := f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude-two", "fixture-model", equipo.Mutation{RequestID: "unticked", Revision: p.Revision}); e == nil { |
| 46 | t.Fatal("unticked account accepted") |
| 47 | } |
| 48 | f.exec(t, `UPDATE specialists SET allowed_models='[{"Provider":"codex","Family":"gpt","Model":"fixture-model"}]' WHERE key='engineering'`) |
| 49 | s = pick(t, f, p, "implement") |
| 50 | if s.Provider != "codex" || s.Family != "gpt" { |
| 51 | t.Fatal("allowed model intersection ignored", s) |
| 52 | } |
| 53 | } |
| 54 | func TestPlanModelChoiceModes(t *testing.T) { |
| 55 | for _, mode := range []string{"best_fit", "pinned", "ask"} { |
| 56 | t.Run(mode, func(t *testing.T) { |
| 57 | f, p := modelChoiceFixture(t) |
| 58 | must(t, f.Store.SaveConnectionChoice(ctx, owner, equipo.ConnectionChoice{ProjectID: p.ProjectID, Revision: 1, ConnectionIDs: []string{"claude", "claude-two", "codex"}, Mode: mode, PinnedID: map[string]string{"pinned": "claude-two"}[mode]})) |
| 59 | m := equipo.Mutation{RequestID: "select", Revision: p.Revision} |
| 60 | s, e := f.Store.SelectPlanStep(ctx, owner, p.TaskID, "reproduce", m) |
| 61 | if mode == "ask" { |
| 62 | if e == nil { |
| 63 | t.Fatal("ask selected without confirmation") |
| 64 | } |
| 65 | // A plan proposal is not consent to an ask-each-pick selection. |
| 66 | p.Steps[0].ConnectionID = "claude" |
| 67 | p, e = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "proposal-pick", Revision: p.Revision}) |
| 68 | must(t, e) |
| 69 | if _, e = f.Store.SelectPlanStep(ctx, owner, p.TaskID, "reproduce", equipo.Mutation{RequestID: "proposal-not-consent", Revision: p.Revision}); e == nil { |
| 70 | t.Fatal("plan edit confirmed ask") |
| 71 | } |
| 72 | m = equipo.Mutation{RequestID: "confirm", Revision: p.Revision} |
| 73 | s, e = f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude-two", "fixture-model", m) |
| 74 | must(t, e) |
| 75 | again, e := f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude-two", "fixture-model", m) |
| 76 | must(t, e) |
| 77 | if !reflect.DeepEqual(s, again) { |
| 78 | t.Fatal("confirmation replay changed") |
| 79 | } |
| 80 | if _, e = f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude", "fixture-model", m); !errors.Is(e, equipo.ErrConflict) { |
| 81 | t.Fatal("changed replay accepted", e) |
| 82 | } |
| 83 | if s.Reason != "connections.reason_confirmed" { |
| 84 | t.Fatal("missing confirmed reason", s) |
| 85 | } |
| 86 | } else { |
| 87 | must(t, e) |
| 88 | want := "claude" |
| 89 | if mode == "pinned" { |
| 90 | want = "claude-two" |
| 91 | } |
| 92 | if s.ConnectionID != want { |
| 93 | t.Fatal("mode ignored", s) |
| 94 | } |
| 95 | } |
| 96 | if s.AllowanceKnown || s.AllowanceRemaining != 0 || s.ChoiceRevision != 2 || s.SpecialistRevision == 0 || s.Revision == 0 { |
| 97 | t.Fatal("unknown or revision lost", s) |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |
| 102 | func TestPlanModelChoiceReviewFamily(t *testing.T) { |
| 103 | f, p := modelChoiceFixture(t) |
| 104 | pick(t, f, p, "implement") |
| 105 | if _, e := f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "review", "claude", "fixture-model", equipo.Mutation{RequestID: "same-family", Revision: p.Revision}); e == nil { |
| 106 | t.Fatal("same-family review accepted") |
| 107 | } |
| 108 | must(t, f.Store.SaveConnectionChoice(ctx, owner, equipo.ConnectionChoice{ProjectID: p.ProjectID, Revision: 1, ConnectionIDs: []string{"claude", "claude-two", "codex"}, Mode: "pinned", PinnedID: "claude"})) |
| 109 | s := pick(t, f, p, "review") |
| 110 | if s.Family != "gpt" { |
| 111 | t.Fatal("pin defeated independent review", s) |
| 112 | } |
| 113 | f.exec(t, `UPDATE specialists SET allowed_models='[{"Provider":"claude","Family":"claude","Model":"fixture-model"}]' WHERE key='review'`) |
| 114 | if _, e := f.Store.SelectPlanStep(ctx, owner, p.TaskID, "review", equipo.Mutation{RequestID: "family-restricted", Revision: p.Revision}); e == nil { |
| 115 | t.Fatal("allowed-family intersection bypassed") |
| 116 | } |
| 117 | } |
| 118 | func TestPlanModelChoiceImmutableStep(t *testing.T) { |
| 119 | f, p := modelChoiceFixture(t) |
| 120 | s := pick(t, f, p, "reproduce") |
| 121 | raw, e := json.Marshal(s) |
| 122 | must(t, e) |
| 123 | f.exec(t, `INSERT INTO runner_assignments(id,task_id,job_id,step_key,claim_request,selection,state) VALUES('actual',?,0,'reproduce','claimed',?,'claimed')`, p.TaskID, string(raw)) |
| 124 | must(t, f.Store.SaveConnectionChoice(ctx, owner, equipo.ConnectionChoice{ProjectID: p.ProjectID, Revision: 1, ConnectionIDs: []string{"claude-two", "codex"}, Mode: "pinned", PinnedID: "claude-two"})) |
| 125 | actual, e := f.Store.SelectPlanStep(ctx, owner, p.TaskID, "reproduce", equipo.Mutation{RequestID: "after-change", Revision: p.Revision}) |
| 126 | must(t, e) |
| 127 | if actual.ConnectionID != s.ConnectionID || actual.Model != s.Model || actual.Reason != s.Reason { |
| 128 | t.Fatal("actual selection rewritten", actual) |
| 129 | } |
| 130 | if _, e = f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude-two", "fixture-model", equipo.Mutation{RequestID: "switch", Revision: p.Revision}); e == nil { |
| 131 | t.Fatal("active work switched accounts") |
| 132 | } |
| 133 | } |
| 134 | func TestPlanModelChoiceUnavailable(t *testing.T) { |
| 135 | f, p := modelChoiceFixture(t) |
| 136 | s := pick(t, f, p, "implement") |
| 137 | if s.AllowanceKnown { |
| 138 | t.Fatal("unknown allowance became known") |
| 139 | } |
| 140 | f.exec(t, `DELETE FROM connections WHERE id='codex'`) |
| 141 | if _, e := f.Store.SelectPlanStep(ctx, owner, p.TaskID, "review", equipo.Mutation{RequestID: "missing-family", Revision: p.Revision}); e == nil { |
| 142 | t.Fatal("missing second family ignored") |
| 143 | } |
| 144 | f.exec(t, `DROP TABLE connection_choices`) |
| 145 | if _, e := f.Store.SelectPlanStep(ctx, owner, p.TaskID, "reproduce", equipo.Mutation{RequestID: "query-failure", Revision: p.Revision}); e == nil { |
| 146 | t.Fatal("query error treated as empty choices") |
| 147 | } |
| 148 | } |
| 149 | func TestPlanModelChoiceCoordinatorConnection(t *testing.T) { |
| 150 | for _, mode := range []string{"best_fit", "pinned", "ask", "excluded", "alone"} { |
| 151 | t.Run(mode, func(t *testing.T) { |
| 152 | f, p := modelChoiceFixture(t) |
| 153 | f.exec(t, `INSERT INTO coordinator_bindings(id,person,runner_id,connection_id,revision,generation) VALUES('binding',?,'fixture-runner','claude',1,1)`, owner.Address) |
| 154 | if mode == "pinned" || mode == "ask" { |
| 155 | must(t, f.Store.SaveConnectionChoice(ctx, owner, equipo.ConnectionChoice{ProjectID: p.ProjectID, Revision: 1, ConnectionIDs: []string{"claude", "claude-two", "codex"}, Mode: mode, PinnedID: "claude"})) |
| 156 | } |
| 157 | if mode == "excluded" { |
| 158 | f.exec(t, `UPDATE connection_choices SET connection_ids_json='["claude","codex"]'`) |
| 159 | f.exec(t, `UPDATE specialists SET allowed_models='[{"Provider":"claude","Family":"claude","Model":"fixture-model"}]' WHERE key='engineering'`) |
| 160 | } |
| 161 | if mode == "alone" { |
| 162 | f.exec(t, `UPDATE connection_choices SET connection_ids_json='["claude"]'`) |
| 163 | } |
| 164 | var s equipo.Selection |
| 165 | if mode == "ask" { |
| 166 | var e error |
| 167 | s, e = f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude", "fixture-model", equipo.Mutation{RequestID: "ask-shared", Revision: p.Revision}) |
| 168 | must(t, e) |
| 169 | } else { |
| 170 | s = pick(t, f, p, "reproduce") |
| 171 | } |
| 172 | want := "claude" |
| 173 | if mode == "best_fit" { |
| 174 | want = "claude-two" |
| 175 | } |
| 176 | if s.ConnectionID != want { |
| 177 | t.Fatal("coordinator preference overrode eligibility or consent", s) |
| 178 | } |
| 179 | }) |
| 180 | } |
| 181 | t.Run("waiting-and-release", TestCoordinatorConnectionWaiting) |
| 182 | } |
| 183 | |
| 184 | func TestPlanModelChoiceClaimAndTurn(t *testing.T) { |
| 185 | for _, boundary := range []string{"claim", "turn"} { |
| 186 | for _, change := range []string{"choice", "connection", "specialist", "confirmation"} { |
| 187 | t.Run(boundary+"/"+change, func(t *testing.T) { |
| 188 | f, p := modelChoiceFixture(t) |
| 189 | p.Kind = taskplan.Recommendation |
| 190 | p.Steps = []taskplan.Step{{Key: "research", Specialist: "product", Action: "recommend"}, {Key: "merge", Action: "person"}} |
| 191 | var err error |
| 192 | p, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "research", Revision: p.Revision}) |
| 193 | must(t, err) |
| 194 | must(t, f.Store.SaveConnectionChoice(ctx, owner, equipo.ConnectionChoice{ProjectID: p.ProjectID, Revision: 1, ConnectionIDs: []string{"claude", "claude-two", "codex"}, Mode: "ask"})) |
| 195 | var path string |
| 196 | must(t, f.DB.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path)) |
| 197 | x, err := f.Store.AttachExecution(ctx, path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)}) |
| 198 | must(t, err) |
| 199 | t.Cleanup(func() { must(t, x.Core.Close()) }) |
| 200 | _, err = x.Core.Resume(ctx) |
| 201 | must(t, err) |
| 202 | f.exec(t, "INSERT INTO runner_credentials(id,runner_id,instance_id,audience,person,machine_key_hash,token_hash,expires_at) VALUES('fixture-credential','fixture-runner',?,?,?,'','fixture-token',?)", f.TeamID, runnerproto.RunnerAudience, owner.Address, time.Now().Add(time.Hour).UTC()) |
| 203 | principal := equipo.RunnerPrincipal{RunnerID: "fixture-runner", Person: owner.Address, CredentialID: "fixture-credential"} |
| 204 | _, err = f.Store.ApprovePlan(ctx, owner, p.TaskID, equipo.Mutation{RequestID: "approve", Revision: p.Revision}) |
| 205 | must(t, err) |
| 206 | if err = f.Store.CheckPlanDispatch(ctx, owner, p.TaskID); err == nil { |
| 207 | t.Fatal("plan approval confirmed ask") |
| 208 | } |
| 209 | _, err = f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "research", "claude", "fixture-model", equipo.Mutation{RequestID: "confirm-research", Revision: p.Revision}) |
| 210 | must(t, err) |
| 211 | must(t, x.Sync(ctx)) |
| 212 | var id string |
| 213 | var revision int64 |
| 214 | must(t, f.DB.Writer().QueryRow("SELECT id,revision FROM runner_assignments WHERE task_id=? AND state='pending'", p.TaskID).Scan(&id, &revision)) |
| 215 | claim := equipo.ClaimRequest{Version: 2, AssignmentID: id, Revision: revision, RequestID: "claim"} |
| 216 | var q equipo.RunnerRequest |
| 217 | if boundary == "turn" { |
| 218 | c, e := x.Claim(ctx, principal, claim) |
| 219 | must(t, e) |
| 220 | q = equipo.RunnerRequest{Version: 2, AssignmentID: id, Scope: c.Scope, Handle: c.Handle, RequestID: "validate"} |
| 221 | _, e = x.Mutate(ctx, principal, "validate", q) |
| 222 | must(t, e) |
| 223 | } |
| 224 | switch change { |
| 225 | case "choice": |
| 226 | f.exec(t, "UPDATE connection_choices SET revision=revision+1") |
| 227 | case "connection": |
| 228 | f.exec(t, "UPDATE connections SET revision=revision+1 WHERE id='claude'") |
| 229 | case "specialist": |
| 230 | f.exec(t, "UPDATE specialists SET revision=revision+1 WHERE key='product'") |
| 231 | case "confirmation": |
| 232 | f.exec(t, "UPDATE plan_selections SET confirmed=false") |
| 233 | } |
| 234 | if boundary == "claim" { |
| 235 | _, err = x.Claim(ctx, principal, claim) |
| 236 | } else { |
| 237 | _, err = x.Mutate(ctx, principal, "validate", q) |
| 238 | } |
| 239 | if err == nil { |
| 240 | t.Fatal("stale selection reached " + boundary) |
| 241 | } |
| 242 | }) |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | func TestPlanModelChoiceHeadroom(t *testing.T) { |
| 248 | f, p := modelChoiceFixture(t) |
| 249 | f.exec(t, `UPDATE connections SET reports_json=json_set(reports_json,'$[0].AllowanceKnown',json('true'),'$[0].AllowanceRemaining',50) WHERE id='claude-two'`) |
| 250 | f.exec(t, `UPDATE connections SET reports_json=json_set(reports_json,'$[0].AllowanceRemaining',9999) WHERE id='claude'`) |
| 251 | s := pick(t, f, p, "reproduce") |
| 252 | if s.ConnectionID != "claude-two" || !s.AllowanceKnown || s.AllowanceRemaining != 50 { |
| 253 | t.Fatal("known headroom ignored or unknown treated as known", s) |
| 254 | } |
| 255 | f.exec(t, `INSERT INTO coordinator_bindings(id,person,runner_id,connection_id,revision,generation) VALUES('binding',?,'fixture-runner','claude-two',1,1)`, owner.Address) |
| 256 | s = pick(t, f, p, "implement") |
| 257 | if s.ConnectionID != "claude" || s.AllowanceKnown || s.AllowanceRemaining != 0 { |
| 258 | t.Fatal("headroom defeated coordinator preference or invented allowance", s) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func TestPlanModelChoiceForms(t *testing.T) { |
| 263 | f, p := modelChoiceFixture(t) |
| 264 | apiRouter(t, f, nil) |
| 265 | v := url.Values{"revision": {"1"}, "mode": {"ask"}, "connection_id": {"claude", "claude-two", "codex"}} |
| 266 | expectHTTP(t, formPost(t, f, owner, "/projects/"+strconv.FormatInt(p.ProjectID, 10)+"/connections", v), 303) |
| 267 | w := f.get(t, owner, "/connections") |
| 268 | expectHTTP(t, w, 200) |
| 269 | if !strings.Contains(w.Body.String(), `value="ask" selected`) || !strings.Contains(w.Body.String(), "Remaining allowance is unknown.") { |
| 270 | t.Fatal("mode or unknown allowance absent") |
| 271 | } |
| 272 | w = f.get(t, owner, "/tasks/"+p.TaskID+"/plan") |
| 273 | expectHTTP(t, w, 200) |
| 274 | if !strings.Contains(w.Body.String(), "/steps/reproduce/selection") { |
| 275 | t.Fatal("plan has no independent confirmation form") |
| 276 | } |
| 277 | v = actionForm("form-confirm", p.Revision) |
| 278 | v.Set("connection_id", "claude") |
| 279 | v.Set("model", "fixture-model") |
| 280 | expectHTTP(t, formPost(t, f, owner, "/tasks/"+p.TaskID+"/steps/reproduce/selection", v), 303) |
| 281 | expectHTTP(t, formPost(t, f, owner, "/tasks/"+p.TaskID+"/steps/reproduce/selection", v), 303) |
| 282 | w = f.get(t, owner, "/tasks/"+p.TaskID+"/plan") |
| 283 | expectHTTP(t, w, 200) |
| 284 | if !strings.Contains(w.Body.String(), "Selection reason") { |
| 285 | t.Fatal("recorded reason absent") |
| 286 | } |
| 287 | expectHTTP(t, formPost(t, f, peer, "/tasks/"+p.TaskID+"/steps/reproduce/selection", v), 403) |
| 288 | f.exec(t, "UPDATE connection_choices SET revision=revision+1") |
| 289 | w = f.get(t, owner, "/tasks/"+p.TaskID+"/plan") |
| 290 | expectHTTP(t, w, 200) |
| 291 | if !strings.Contains(w.Body.String(), "The selection changed. Confirm it again.") { |
| 292 | t.Fatal("stale pick looks confirmed") |
| 293 | } |
| 294 | |
| 295 | // Task and journey projections keep unknown values nullable and omit routing. |
| 296 | w = apiRequest(t, f, owner, "GET", "/api/v1/tasks/"+p.TaskID, "", "", "") |
| 297 | expectHTTP(t, w, 200) |
| 298 | body := w.Body.String() |
| 299 | if !strings.Contains(body, `"allowance_remaining":null`) || !strings.Contains(body, `"usage":null`) || strings.Contains(body, "CredentialHome") || strings.Contains(body, "ObservedAccount") { |
| 300 | t.Fatal("unsafe or invented person projection", body) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | func TestPlanModelChoiceAuthorityAndRollback(t *testing.T) { |
| 305 | f, p := modelChoiceFixture(t) |
| 306 | m := equipo.Mutation{RequestID: "atomic", Revision: p.Revision} |
| 307 | f.exec(t, `CREATE TRIGGER reject_selection_audit BEFORE INSERT ON audit_events WHEN NEW.kind='plan.confirm_selection' BEGIN SELECT RAISE(ABORT,'fixture audit failure'); END`) |
| 308 | if _, err := f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude", "fixture-model", m); err == nil { |
| 309 | t.Fatal("selection ignored failed audit") |
| 310 | } |
| 311 | if f.count(t, "SELECT count(*) FROM plan_selections") != 0 { |
| 312 | t.Fatal("failed audit left selection") |
| 313 | } |
| 314 | f.exec(t, "DROP TRIGGER reject_selection_audit") |
| 315 | _, err := f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude", "fixture-model", m) |
| 316 | must(t, err) |
| 317 | f.exec(t, "UPDATE members SET active=false WHERE address=?", owner.Address) |
| 318 | if _, err = f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude", "fixture-model", m); err == nil { |
| 319 | t.Fatal("replay ignored removed membership") |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | func TestPlanModelChoiceAskBuilder(t *testing.T) { |
| 324 | f, p := modelChoiceFixture(t) |
| 325 | must(t, f.Store.SaveConnectionChoice(ctx, owner, equipo.ConnectionChoice{ProjectID: p.ProjectID, Revision: 1, ConnectionIDs: []string{"claude", "claude-two", "codex"}, Mode: "ask"})) |
| 326 | _, err := f.Store.ConfirmPlanSelection(ctx, owner, p.TaskID, "reproduce", "claude", "fixture-model", equipo.Mutation{RequestID: "confirm-reproduce", Revision: p.Revision}) |
| 327 | must(t, err) |
| 328 | var path string |
| 329 | must(t, f.DB.Writer().QueryRow("SELECT file FROM pragma_database_list WHERE name='main'").Scan(&path)) |
| 330 | x, err := f.Store.AttachExecution(ctx, path, team.Options{Clock: team.RealClock{}, Authority: equipo.NewAuthority(f.Store, nil)}) |
| 331 | must(t, err) |
| 332 | t.Cleanup(func() { must(t, x.Core.Close()) }) |
| 333 | _, err = x.Core.Resume(ctx) |
| 334 | must(t, err) |
| 335 | f.exec(t, "INSERT INTO runner_credentials(id,runner_id,instance_id,audience,person,machine_key_hash,token_hash,expires_at) VALUES('fixture-credential','fixture-runner',?,?,?,'','fixture-token',?)", f.TeamID, runnerproto.RunnerAudience, owner.Address, time.Now().Add(time.Hour).UTC()) |
| 336 | principal := equipo.RunnerPrincipal{RunnerID: "fixture-runner", Person: owner.Address, CredentialID: "fixture-credential"} |
| 337 | must(t, x.Sync(ctx)) |
| 338 | var id string |
| 339 | var revision int64 |
| 340 | must(t, f.DB.Writer().QueryRow("SELECT id,revision FROM runner_assignments WHERE task_id=? AND state='pending'", p.TaskID).Scan(&id, &revision)) |
| 341 | _, err = x.Claim(ctx, principal, equipo.ClaimRequest{Version: 2, AssignmentID: id, Revision: revision, RequestID: "prepare-claim"}) |
| 342 | must(t, err) |
| 343 | } |
| 344 | |
| 345 | func TestPlanModelChoicePinAndCustomReview(t *testing.T) { |
| 346 | f, p := modelChoiceFixture(t) |
| 347 | bad := equipo.ConnectionChoice{ProjectID: p.ProjectID, Revision: 1, ConnectionIDs: []string{"claude", "claude-two", "codex"}, Mode: "pinned"} |
| 348 | if err := f.Store.SaveConnectionChoice(ctx, owner, bad); err == nil { |
| 349 | t.Fatal("pinned mode accepted without a pin") |
| 350 | } |
| 351 | bad.Mode = "invented" |
| 352 | if err := f.Store.SaveConnectionChoice(ctx, owner, bad); err == nil { |
| 353 | t.Fatal("unknown mode accepted") |
| 354 | } |
| 355 | bad.Mode = "pinned" |
| 356 | bad.PinnedID = "claude" |
| 357 | must(t, f.Store.SaveConnectionChoice(ctx, owner, bad)) |
| 358 | f.exec(t, `UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('false')) WHERE id='claude'`) |
| 359 | if _, err := f.Store.SelectPlanStep(ctx, owner, p.TaskID, "reproduce", equipo.Mutation{RequestID: "unavailable-pin", Revision: p.Revision}); err == nil { |
| 360 | t.Fatal("unavailable pin silently switched") |
| 361 | } |
| 362 | f.exec(t, `UPDATE connections SET reports_json=json_set(reports_json,'$[0].Available',json('true')) WHERE id='claude'`) |
| 363 | var sp equipo.Specialist |
| 364 | must(t, f.DB.G.First(&sp, "key='review'").Error) |
| 365 | sp.Key = "custom-review" |
| 366 | sp.Name = "Custom review" |
| 367 | sp.Required = false |
| 368 | sp.Role = "" |
| 369 | must(t, f.DB.G.Create(&sp).Error) |
| 370 | for i := range p.Steps { |
| 371 | if p.Steps[i].Action == "review" { |
| 372 | p.Steps[i].Specialist = sp.Key |
| 373 | } |
| 374 | } |
| 375 | var err error |
| 376 | if _, err = f.Store.EditPlan(ctx, owner, p, equipo.Mutation{RequestID: "custom-review", Revision: p.Revision}); err == nil { |
| 377 | t.Fatal("custom specialist replaced required independent Review") |
| 378 | } |
| 379 | } |
| 380 | |