| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "gorm.io/gorm" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/specialist" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Task 16 (dynamic specialists slice 1): grants for every specialist |
| 15 | // (grantFor), the step's full binding at dispatch, the binding checked |
| 16 | // again by every privileged effect, and revocation by specialist. |
| 17 | |
| 18 | // grantRig is designRig whose runner leases are current and whose |
| 19 | // instance audit log is kept. |
| 20 | func grantRig(t *testing.T) (*hkRig, Lease, *[]string) { |
| 21 | r, l := designRig(t) |
| 22 | r.s.authority.CurrentRunner = func(*gorm.DB, RunnerPrincipal) error { return nil } |
| 23 | r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RolePrimary, nil } |
| 24 | audit := &[]string{} |
| 25 | r.s.authority.Audit = func(_ *gorm.DB, actor, kind, record string) error { |
| 26 | *audit = append(*audit, actor+" "+kind+" "+record) |
| 27 | return nil |
| 28 | } |
| 29 | return r, l, audit |
| 30 | } |
| 31 | |
| 32 | // designStarted adds Design for project (Patrix or Suite) through its |
| 33 | // card, and its continuation records an exploration there and starts it |
| 34 | // with Design: the specialist as it is now, the task and its step. |
| 35 | func (r *hkRig) designStarted(l Lease, project string) (specialistRecord, string, stepRecord) { |
| 36 | r.t.Helper() |
| 37 | var sp specialistRecord |
| 38 | var next turnRecord |
| 39 | if project == "Patrix" { |
| 40 | sp, next = r.added(l, askWords) |
| 41 | } else { |
| 42 | sp, next = r.added2(l) |
| 43 | } |
| 44 | res := r.call(l, next.ID, wire.ToolRecordTask, map[string]any{"repo": project, "title": "Calmer " + project + " design", "brief": "Options.", "kind": wire.KindExplore}) |
| 45 | task := madeTask2(r.t, res) |
| 46 | if res := r.call(l, next.ID, wire.ToolStartTask, map[string]any{"task": task, "specialist": sp.ID}); res.IsError || res.Code != "started" { |
| 47 | r.t.Fatalf("start %s with Design: %+v", project, res) |
| 48 | } |
| 49 | r.must(r.s.db.G.Where("id = ?", sp.ID).First(&sp).Error) |
| 50 | return sp, task, r.makerStep(task) |
| 51 | } |
| 52 | |
| 53 | // makerStep is the task's newest Engineering step. |
| 54 | func (r *hkRig) makerStep(task string) stepRecord { |
| 55 | r.t.Helper() |
| 56 | var s stepRecord |
| 57 | r.must(r.s.db.G.Where("task_id = ? AND specialist = ?", task, wire.SpecialistEngineering).Order("idx DESC").First(&s).Error) |
| 58 | return s |
| 59 | } |
| 60 | |
| 61 | func (r *hkRig) authorize(l Lease, s stepRecord, purpose string) string { |
| 62 | r.t.Helper() |
| 63 | refused, err := r.s.Authorize(r.ctx, l, s.ID, s.Attempt, purpose, "", "") |
| 64 | r.must(err) |
| 65 | return refused |
| 66 | } |
| 67 | |
| 68 | func (r *hkRig) revoke(id string) { |
| 69 | r.t.Helper() |
| 70 | var sp specialistRecord |
| 71 | r.must(r.s.db.G.Where("id = ?", id).First(&sp).Error) |
| 72 | if _, err := r.s.RevokeSpecialist(r.ctx, paul, id, wire.RevokeSpecialist{Revision: sp.Revision}); err != nil { |
| 73 | r.t.Fatal(err) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // dispatched is the newest dispatch command for the step. |
| 78 | func (r *hkRig) dispatched(stepID string) (commandRecord, wire.DispatchCommand) { |
| 79 | r.t.Helper() |
| 80 | var c commandRecord |
| 81 | r.must(r.s.db.G.Where("kind = ? AND body LIKE ?", wire.FrameDispatch, `%"step_id":"`+stepID+`"%`).Order("seq DESC").First(&c).Error) |
| 82 | var d wire.DispatchCommand |
| 83 | r.must(json.Unmarshal([]byte(c.Body), &d)) |
| 84 | return c, d |
| 85 | } |
| 86 | |
| 87 | // cancels counts the cancel_step commands for the step. |
| 88 | func (r *hkRig) cancels(stepID string) int64 { |
| 89 | return r.count("SELECT count(*) FROM runner_commands WHERE kind = ? AND body LIKE ?", wire.FrameCancelStep, `%"step_id":"`+stepID+`"%`) |
| 90 | } |
| 91 | |
| 92 | // ackDispatches marks the step's dispatches delivered to its runner. |
| 93 | func (r *hkRig) ackDispatches(stepID string) { |
| 94 | r.exec("UPDATE runner_commands SET acked_at = ? WHERE kind = ? AND body LIKE ?", r.now, wire.FrameDispatch, `%"step_id":"`+stepID+`"%`) |
| 95 | } |
| 96 | |
| 97 | func (r *hkRig) stepNow(id string) stepRecord { |
| 98 | r.t.Helper() |
| 99 | var s stepRecord |
| 100 | r.must(r.s.db.G.Where("id = ?", id).First(&s).Error) |
| 101 | return s |
| 102 | } |
| 103 | |
| 104 | var designCaps = `["branch.edit","page.render"]` |
| 105 | |
| 106 | // TestGrantForChecksTheGrant: grantFor finds the project's row, else the |
| 107 | // team's, and grants it only while it holds exactly its profile's |
| 108 | // capabilities, unrevoked, at its revision, given by its approver, who |
| 109 | // is an owner now; and only for a profile on the menu. A project row |
| 110 | // that fails any of these is no grant: the team's row does not stand in. |
| 111 | func TestGrantForChecksTheGrant(t *testing.T) { |
| 112 | r, l, _ := grantRig(t) |
| 113 | sp := r.propose(l, "Patrix", "p_patrix") |
| 114 | r.must(r.answer(paul, sp, wire.ChoiceAdd, 1)) |
| 115 | team := r.propose(l, "", "") |
| 116 | r.must(r.answer(paul, team, wire.ChoiceAdd, 1)) |
| 117 | r.must(r.s.db.G.Where("id = ?", sp.ID).First(&sp).Error) |
| 118 | get := func(project string) string { |
| 119 | t.Helper() |
| 120 | var found *specialistRecord |
| 121 | r.must(r.tx(func(tx *txn) error { |
| 122 | var err error |
| 123 | found, err = tx.grantFor(specialist.ProfileDesign, project) |
| 124 | return err |
| 125 | })) |
| 126 | if found == nil { |
| 127 | return "" |
| 128 | } |
| 129 | return found.ID |
| 130 | } |
| 131 | if got := get("p_patrix"); got != sp.ID { |
| 132 | t.Fatalf("project row over the team's: %q", got) |
| 133 | } |
| 134 | if got := get("p_suite"); got != team.ID { |
| 135 | t.Fatalf("another project, the team's row: %q", got) |
| 136 | } |
| 137 | denied := func(what string) { |
| 138 | t.Helper() |
| 139 | if got := get("p_patrix"); got != "" { |
| 140 | t.Fatalf("%s: granted %q", what, got) |
| 141 | } |
| 142 | if got := get("p_suite"); got != team.ID { |
| 143 | t.Fatalf("%s: the team's row for Suite %q", what, got) |
| 144 | } |
| 145 | } |
| 146 | usable := func(what string) { |
| 147 | t.Helper() |
| 148 | if got := get("p_patrix"); got != sp.ID { |
| 149 | t.Fatalf("%s: %q", what, got) |
| 150 | } |
| 151 | } |
| 152 | // The approver (Paul approved both) demoted: neither holds. |
| 153 | r.exec("UPDATE members SET role = 'member' WHERE address = ?", paul) |
| 154 | if a, b := get("p_patrix"), get("p_suite"); a != "" || b != "" { |
| 155 | t.Fatalf("approver demoted: %q %q", a, b) |
| 156 | } |
| 157 | r.exec("UPDATE members SET role = 'owner' WHERE address = ?", paul) |
| 158 | usable("approver an owner again") |
| 159 | r.exec("UPDATE chat_specialists SET approver = ? WHERE id = ?", hkOwner, sp.ID) |
| 160 | denied("approver not the granter") |
| 161 | r.exec("UPDATE chat_specialists SET approver = ? WHERE id = ?", paul, sp.ID) |
| 162 | // The grants: one revoked, one missing, one extra, one at another |
| 163 | // revision, one by someone else, one twice. |
| 164 | var grants []grantRecord |
| 165 | r.must(r.s.db.G.Where("specialist_id = ?", sp.ID).Order("capability").Find(&grants).Error) |
| 166 | if len(grants) != 2 { |
| 167 | t.Fatalf("grants %+v", grants) |
| 168 | } |
| 169 | g := grants[1].ID |
| 170 | r.exec("UPDATE chat_capability_grants SET revoked_at = ?, revoked_by = ? WHERE id = ?", r.now, paul, g) |
| 171 | denied("a grant revoked") |
| 172 | r.exec("UPDATE chat_capability_grants SET revoked_at = NULL, revoked_by = '' WHERE id = ?", g) |
| 173 | usable("unrevoked") |
| 174 | r.exec("UPDATE chat_capability_grants SET capability = ? WHERE id = ?", specialist.CapabilityWebResearch, g) |
| 175 | denied("another capability in its place") |
| 176 | r.exec("UPDATE chat_capability_grants SET capability = ? WHERE id = ?", grants[0].Capability, g) |
| 177 | denied("one capability twice") |
| 178 | r.exec("UPDATE chat_capability_grants SET capability = ? WHERE id = ?", grants[1].Capability, g) |
| 179 | r.exec("INSERT INTO chat_capability_grants(id, specialist_id, capability, revision, granted_by, granted_at) VALUES ('g_extra', ?, ?, ?, ?, ?)", sp.ID, specialist.CapabilityWebResearch, sp.Revision, paul, r.now) |
| 180 | denied("a capability beside the profile's") |
| 181 | r.exec("DELETE FROM chat_capability_grants WHERE id = 'g_extra'") |
| 182 | r.exec("UPDATE chat_capability_grants SET revoked_at = ? WHERE id = ?", r.now, g) |
| 183 | r.exec("INSERT INTO chat_capability_grants(id, specialist_id, capability, revision, granted_by, granted_at) VALUES ('g_extra', ?, ?, ?, ?, ?)", sp.ID, grants[0].Capability, sp.Revision, paul, r.now) |
| 184 | denied("one missing, the other twice") |
| 185 | r.exec("DELETE FROM chat_capability_grants WHERE id = 'g_extra'") |
| 186 | denied("one missing") |
| 187 | r.exec("UPDATE chat_capability_grants SET revoked_at = NULL WHERE id = ?", g) |
| 188 | r.exec("UPDATE chat_capability_grants SET revision = revision - 1 WHERE id = ?", g) |
| 189 | denied("a grant at another revision") |
| 190 | r.exec("UPDATE chat_capability_grants SET revision = revision + 1 WHERE id = ?", g) |
| 191 | r.exec("UPDATE chat_capability_grants SET granted_by = ? WHERE id = ?", hkOwner, g) |
| 192 | denied("granted by someone else") |
| 193 | r.exec("UPDATE chat_capability_grants SET granted_by = ? WHERE id = ?", paul, g) |
| 194 | r.exec("UPDATE chat_specialists SET revision = revision + 1 WHERE id = ?", sp.ID) |
| 195 | denied("the specialist moved on from its grants") |
| 196 | r.exec("UPDATE chat_specialists SET revision = revision - 1 WHERE id = ?", sp.ID) |
| 197 | r.exec("UPDATE chat_specialists SET approver = '' WHERE id = ?", sp.ID) |
| 198 | r.exec("UPDATE chat_capability_grants SET granted_by = '' WHERE specialist_id = ?", sp.ID) |
| 199 | r.exec("INSERT INTO members(address, active, role) VALUES ('', 1, 'owner')") |
| 200 | denied("no approver") |
| 201 | r.exec("DELETE FROM members WHERE address = ''") |
| 202 | r.exec("UPDATE chat_specialists SET approver = ? WHERE id = ?", paul, sp.ID) |
| 203 | r.exec("UPDATE chat_capability_grants SET granted_by = ? WHERE specialist_id = ?", paul, sp.ID) |
| 204 | usable("restored") |
| 205 | // A profile missing from the menu: no grant, for any row of it. |
| 206 | r.exec("UPDATE chat_specialists SET profile = 'design-v0' WHERE profile = ?", specialist.ProfileDesign) |
| 207 | var found *specialistRecord |
| 208 | r.must(r.tx(func(tx *txn) error { |
| 209 | var err error |
| 210 | found, err = tx.grantFor("design-v0", "p_patrix") |
| 211 | return err |
| 212 | })) |
| 213 | if found != nil { |
| 214 | t.Fatalf("a profile off the menu: %+v", found) |
| 215 | } |
| 216 | // Even one with no grants to compare (an empty profile would match). |
| 217 | r.exec("UPDATE chat_capability_grants SET revoked_at = ? WHERE specialist_id = ?", r.now, sp.ID) |
| 218 | r.must(r.tx(func(tx *txn) error { |
| 219 | var err error |
| 220 | found, err = tx.grantFor("design-v0", "p_patrix") |
| 221 | return err |
| 222 | })) |
| 223 | if found != nil { |
| 224 | t.Fatalf("a profile off the menu with no grants: %+v", found) |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | // TestDesignDispatchBinds: the Design step's dispatch records its full |
| 229 | // binding on the step and in the command (id, revision, project, |
| 230 | // profile, capabilities) and appends a specialist.used audit row in the |
| 231 | // dispatch's transaction, naming no credential. A step bound to the |
| 232 | // team's row records the team's scope. |
| 233 | func TestDesignDispatchBinds(t *testing.T) { |
| 234 | r, l, audit := grantRig(t) |
| 235 | sp, task, s := r.designStarted(l, "Patrix") |
| 236 | if s.SpecialistID != sp.ID || s.SpecialistRevision != sp.Revision || s.SpecialistProject != "p_patrix" || s.SpecialistCaps != designCaps { |
| 237 | t.Fatalf("binding %q %d %q %q", s.SpecialistID, s.SpecialistRevision, s.SpecialistProject, s.SpecialistCaps) |
| 238 | } |
| 239 | _, d := r.dispatched(s.ID) |
| 240 | if d.SpecialistID != sp.ID || d.SpecialistRevision != sp.Revision || d.SpecialistProject != "p_patrix" || d.Profile != specialist.ProfileDesign || strings.Join(d.Capabilities, ",") != "branch.edit,page.render" { |
| 241 | t.Fatalf("dispatch binding %+v", d) |
| 242 | } |
| 243 | if !wire.ValidSpecialistBinding(&d) { |
| 244 | t.Fatalf("the wire refuses the binding %+v", d) |
| 245 | } |
| 246 | var used []specialistAuditRecord |
| 247 | r.must(r.s.db.G.Where("specialist_id = ? AND action = ?", sp.ID, "used").Find(&used).Error) |
| 248 | if len(used) != 1 || used[0].Revision != sp.Revision || !strings.Contains(used[0].Detail, "task="+task) || !strings.Contains(used[0].Detail, "step="+s.ID) || !strings.Contains(used[0].Detail, "capabilities=branch.edit,page.render") { |
| 249 | t.Fatalf("used rows %+v", used) |
| 250 | } |
| 251 | n := 0 |
| 252 | for _, a := range *audit { |
| 253 | if strings.Contains(a, " specialist.used "+sp.ID+"@") { |
| 254 | n++ |
| 255 | } |
| 256 | } |
| 257 | if n != 1 { |
| 258 | t.Fatalf("instance audit %q", *audit) |
| 259 | } |
| 260 | // Placed again (a new attempt): bound again, audited again. |
| 261 | r.must(r.tx(func(tx *txn) error { |
| 262 | k, err := tx.loadTask(task) |
| 263 | if err != nil { |
| 264 | return err |
| 265 | } |
| 266 | st := tx.mustStep(s.ID) |
| 267 | if err := tx.replace(&k, &st); err != nil { |
| 268 | return err |
| 269 | } |
| 270 | return tx.saveStep(&st, &k) |
| 271 | })) |
| 272 | if s2 := r.stepNow(s.ID); s2.Attempt != 2 || s2.SpecialistID != sp.ID || r.count("SELECT count(*) FROM chat_specialist_audit WHERE specialist_id = ? AND action = 'used'", sp.ID) != 2 { |
| 273 | t.Fatalf("second attempt %+v", s2) |
| 274 | } |
| 275 | // An attempt whose step is bound to something the grant no longer is |
| 276 | // (here, another revision) is not dispatched: it ends cancelled. |
| 277 | r.exec("UPDATE chat_steps SET specialist_revision = specialist_revision + 5 WHERE id = ?", s.ID) |
| 278 | r.must(r.tx(func(tx *txn) error { |
| 279 | k, err := tx.loadTask(task) |
| 280 | if err != nil { |
| 281 | return err |
| 282 | } |
| 283 | st := tx.mustStep(s.ID) |
| 284 | if err := tx.replace(&k, &st); err != nil { |
| 285 | return err |
| 286 | } |
| 287 | if st.State != wire.StepCancelled || st.Reason != reasonSpecialistNoGrant { |
| 288 | t.Fatalf("a mismatched binding dispatched %+v", st) |
| 289 | } |
| 290 | return tx.saveStep(&st, &k) |
| 291 | })) |
| 292 | |
| 293 | // A team row: its scope is the team's. |
| 294 | team := r.propose(l, "", "") |
| 295 | r.must(r.answer(paul, team, wire.ChoiceAdd, 1)) |
| 296 | r.must(r.s.db.G.Where("id = ?", team.ID).First(&team).Error) |
| 297 | k := r.teamTask(l, "k_suite_team", "p_suite", team.ID) |
| 298 | ts := r.makerStep(k) |
| 299 | if ts.SpecialistID != team.ID || ts.SpecialistProject != "" || ts.SpecialistRevision != team.Revision { |
| 300 | t.Fatalf("team binding %+v", ts) |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // mustStep reads a step in the transaction (test helper). |
| 305 | func (t *txn) mustStep(id string) stepRecord { |
| 306 | var s stepRecord |
| 307 | _ = t.db.Where("id = ?", id).First(&s).Error |
| 308 | return s |
| 309 | } |
| 310 | |
| 311 | // teamTask records a task in project for Paul with maker as its maker |
| 312 | // and starts it (as task 17's routing will): its step dispatched. |
| 313 | func (r *hkRig) teamTask(l Lease, id, project, maker string) string { |
| 314 | r.t.Helper() |
| 315 | r.must(r.s.db.G.Create(&taskRecord{ID: id, ProjectID: project, Title: "Team design", Stage: wire.StageNoted, Creator: paul, Revision: 1, CreatedAt: r.now, UpdatedAt: r.now, Branch: "equipo/" + id}).Error) |
| 316 | r.must(r.tx(func(tx *txn) error { |
| 317 | res, err := tx.startTask(turnRecord{ID: "t_team_" + id, Person: paul, Kind: wire.TurnPerson}, id, "", maker) |
| 318 | if err == nil && res.IsError { |
| 319 | r.t.Fatalf("start %s: %+v", id, res) |
| 320 | } |
| 321 | return err |
| 322 | })) |
| 323 | return id |
| 324 | } |
| 325 | |
| 326 | // TestDesignRevokeBeforeStart: revoked between dispatch and start. A |
| 327 | // dispatch the runner never took is retired and the step ends cancelled |
| 328 | // at once (nothing ran); one it took gets the ordinary cancel (asked to |
| 329 | // stop, its end reported), and its start is refused from then on. |
| 330 | // Either way the task waits, not ended. |
| 331 | func TestDesignRevokeBeforeStart(t *testing.T) { |
| 332 | r, l, _ := grantRig(t) |
| 333 | sp, task, s := r.designStarted(l, "Patrix") |
| 334 | r.revoke(sp.ID) |
| 335 | got := r.stepNow(s.ID) |
| 336 | if got.State != wire.StepCancelled || got.Reason != reasonSpecialistNoGrant || !got.Terminated { |
| 337 | t.Fatalf("undelivered step after revoke %+v", got) |
| 338 | } |
| 339 | if c, _ := r.dispatched(s.ID); c.AckedAt == nil { |
| 340 | t.Fatal("its dispatch was not retired") |
| 341 | } |
| 342 | if n := r.cancels(s.ID); n != 0 { |
| 343 | t.Fatalf("%d cancels for a dispatch never delivered", n) |
| 344 | } |
| 345 | if refused := r.authorize(l, got, ""); refused == "" { |
| 346 | t.Fatal("started after revoke") |
| 347 | } |
| 348 | if k := r.load(task); wire.FinalStage(k.Stage) || k.MakerSpecialistID != sp.ID { |
| 349 | t.Fatalf("task after revoke %+v", k) |
| 350 | } |
| 351 | |
| 352 | // Delivered, not started: asked to stop; its start refused. |
| 353 | sp2, task2, s2 := r.designStarted(l, "Suite") |
| 354 | r.ackDispatches(s2.ID) |
| 355 | r.revoke(sp2.ID) |
| 356 | got = r.stepNow(s2.ID) |
| 357 | if got.State != wire.StepQueued || got.Terminated || r.cancels(s2.ID) != 1 { |
| 358 | t.Fatalf("delivered step after revoke %+v, %d cancels", got, r.cancels(s2.ID)) |
| 359 | } |
| 360 | if refused := r.authorize(l, got, ""); refused != reasonSpecialistNoGrant { |
| 361 | t.Fatalf("start of a revoked step: %q", refused) |
| 362 | } |
| 363 | if r.stepNow(s2.ID).StartedAt != nil { |
| 364 | t.Fatal("a refused start recorded a start") |
| 365 | } |
| 366 | r.must(r.tx(func(tx *txn) error { |
| 367 | return tx.stepReport(l, &wire.StepReport{TaskID: task2, StepID: s2.ID, Attempt: s2.Attempt, State: wire.StepCancelled, Reason: "cancelled", Termination: true}) |
| 368 | })) |
| 369 | if got := r.stepNow(s2.ID); got.State != wire.StepCancelled || !got.Terminated { |
| 370 | t.Fatalf("after its end %+v", got) |
| 371 | } |
| 372 | if k := r.load(task2); wire.FinalStage(k.Stage) { |
| 373 | t.Fatalf("task ended %+v", k) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // TestDesignRevokeBetweenRenders: a render is granted while the binding |
| 378 | // holds; after a revoke the next is refused, and so is the pre-push |
| 379 | // check; the running step gets one cancel. |
| 380 | func TestDesignRevokeBetweenRenders(t *testing.T) { |
| 381 | r, l, _ := grantRig(t) |
| 382 | sp, _, s := r.designStarted(l, "Patrix") |
| 383 | // Started before its dispatch's ack was applied (both in flight). |
| 384 | if refused := r.authorize(l, s, ""); refused != "" { |
| 385 | t.Fatalf("start %q", refused) |
| 386 | } |
| 387 | if refused := r.authorize(l, s, wire.LeasePurposeRender); refused != "" { |
| 388 | t.Fatalf("first render %q", refused) |
| 389 | } |
| 390 | r.revoke(sp.ID) |
| 391 | if refused := r.authorize(l, s, wire.LeasePurposeRender); refused == "" { |
| 392 | t.Fatal("a render after revoke") |
| 393 | } |
| 394 | if refused := r.authorize(l, s, ""); refused == "" { |
| 395 | t.Fatal("a push check after revoke") |
| 396 | } |
| 397 | if got := r.stepNow(s.ID); got.StartedAt == nil || wire.StepTerminal(got.State) || r.cancels(s.ID) != 1 { |
| 398 | t.Fatalf("running step after revoke %+v, %d cancels", got, r.cancels(s.ID)) |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | // TestRenderLeaseNeedsPageRender: a page_render check is granted only |
| 403 | // to a step bound to a specialist whose binding holds page.render: never |
| 404 | // to Engineering's own step, nor to a Research step, nor below |
| 405 | // ProtocolDesign; an unknown purpose is refused for every step and |
| 406 | // records no start; merge_request only on a merge step. |
| 407 | func TestRenderLeaseNeedsPageRender(t *testing.T) { |
| 408 | r, l, _ := grantRig(t) |
| 409 | _, _, s := r.designStarted(l, "Patrix") |
| 410 | eng := r.plainTask(l, "k_plain") |
| 411 | for _, purpose := range []string{wire.LeasePurposeRender, "page_render2", "something", wire.LeasePurposeMerge} { |
| 412 | if refused := r.authorize(l, eng, purpose); refused == "" { |
| 413 | t.Fatalf("Engineering's step granted %q", purpose) |
| 414 | } |
| 415 | } |
| 416 | if got := r.stepNow(eng.ID); got.StartedAt != nil { |
| 417 | t.Fatal("a refused purpose recorded a start") |
| 418 | } |
| 419 | if refused := r.authorize(l, eng, ""); refused != "" { |
| 420 | t.Fatalf("Engineering's start %q", refused) |
| 421 | } |
| 422 | for _, purpose := range []string{"page_render2", "something", wire.LeasePurposeMerge} { |
| 423 | if refused := r.authorize(l, s, purpose); refused == "" { |
| 424 | t.Fatalf("Design's step granted %q", purpose) |
| 425 | } |
| 426 | } |
| 427 | // Bound, but its capabilities as recorded lack page.render: the |
| 428 | // binding no longer matches, refused. |
| 429 | r.exec("UPDATE chat_steps SET specialist_caps = ? WHERE id = ?", `["branch.edit"]`, s.ID) |
| 430 | if refused := r.authorize(l, s, wire.LeasePurposeRender); refused == "" { |
| 431 | t.Fatal("render without page.render in the binding") |
| 432 | } |
| 433 | r.exec("UPDATE chat_steps SET specialist_caps = ? WHERE id = ?", designCaps, s.ID) |
| 434 | if refused := r.authorize(l, s, wire.LeasePurposeRender); refused != "" { |
| 435 | t.Fatalf("render restored %q", refused) |
| 436 | } |
| 437 | // The task's maker is not the specialist the step is bound to. |
| 438 | k := r.load(s.TaskID) |
| 439 | r.exec("UPDATE chat_tasks SET maker_specialist_id = '' WHERE id = ?", k.ID) |
| 440 | if refused := r.authorize(l, s, ""); refused == "" { |
| 441 | t.Fatal("a bound step on a task with no maker") |
| 442 | } |
| 443 | other, _, _ := r.designStarted(l, "Suite") |
| 444 | r.exec("UPDATE chat_tasks SET maker_specialist_id = ? WHERE id = ?", other.ID, k.ID) |
| 445 | if refused := r.authorize(l, s, ""); refused == "" { |
| 446 | t.Fatal("a step bound to one specialist on a task made by another") |
| 447 | } |
| 448 | r.exec("UPDATE chat_tasks SET maker_specialist_id = ? WHERE id = ?", k.MakerSpecialistID, k.ID) |
| 449 | // Below ProtocolDesign a Design step is never granted. |
| 450 | old := l |
| 451 | old.Protocol = wire.ProtocolDesign - 1 |
| 452 | if refused := r.authorize(old, s, ""); refused == "" { |
| 453 | t.Fatal("a Design step's lease below 38") |
| 454 | } |
| 455 | // A Research step (bound to web.research) never renders. |
| 456 | rs := r.researchStep(l, "k_research") |
| 457 | if refused := r.authorize(l, rs, ""); refused != "" { |
| 458 | t.Fatalf("Research start %q", refused) |
| 459 | } |
| 460 | if refused := r.authorize(l, rs, wire.LeasePurposeRender); refused == "" { |
| 461 | t.Fatal("a Research step rendered") |
| 462 | } |
| 463 | // A maker whose binding holds but has no page.render (research-v1 as |
| 464 | // a maker, which routing will refuse): it starts, never renders. |
| 465 | var research specialistRecord |
| 466 | r.must(r.s.db.G.Where("profile = ? AND state = ?", specialist.ProfileResearch, wire.SpecialistActive).First(&research).Error) |
| 467 | rm := r.makerStep(r.teamTask(l, "k_rmaker", "p_patrix", research.ID)) |
| 468 | if rm.SpecialistID != research.ID || rm.SpecialistCaps != `["web.research"]` { |
| 469 | t.Fatalf("research maker binding %+v", rm) |
| 470 | } |
| 471 | if refused := r.authorize(l, rm, ""); refused != "" { |
| 472 | t.Fatalf("research maker start %q", refused) |
| 473 | } |
| 474 | if refused := r.authorize(l, rm, wire.LeasePurposeRender); refused == "" { |
| 475 | t.Fatal("a maker without page.render rendered") |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // plainTask records and starts an Engineering task for Paul in Patrix. |
| 480 | func (r *hkRig) plainTask(l Lease, id string) stepRecord { |
| 481 | r.t.Helper() |
| 482 | r.teamTask(l, id, "p_patrix", "") |
| 483 | s := r.makerStep(id) |
| 484 | if s.SpecialistID != "" || s.SpecialistCaps != "" { |
| 485 | r.t.Fatalf("Engineering bound %+v", s) |
| 486 | } |
| 487 | return s |
| 488 | } |
| 489 | |
| 490 | // researchStep is a Research step of a new task in Patrix placed on |
| 491 | // RUNNER1 under the team's research-v1 grant, bound as dispatch binds |
| 492 | // it. |
| 493 | func (r *hkRig) researchStep(l Lease, task string) stepRecord { |
| 494 | r.t.Helper() |
| 495 | var sp specialistRecord |
| 496 | r.s.db.G.Where("profile = ? AND state = ? AND project_id = ''", specialist.ProfileResearch, wire.SpecialistActive).Limit(1).Find(&sp) |
| 497 | if sp.ID == "" { |
| 498 | sp = approvedResearch(r, paul) |
| 499 | } |
| 500 | var g grantRecord |
| 501 | r.must(r.s.db.G.Where("specialist_id = ? AND revoked_at IS NULL", sp.ID).First(&g).Error) |
| 502 | r.must(r.s.db.G.Create(&taskRecord{ID: task, ProjectID: "p_patrix", Title: "Look it up", Stage: wire.StageInProgress, Creator: paul, Revision: 1, CreatedAt: r.now, UpdatedAt: r.now, Branch: "equipo/" + task}).Error) |
| 503 | s := stepRecord{ID: "s_" + task, TaskID: task, Idx: 1, Specialist: wire.SpecialistResearch, Person: paul, State: wire.StepQueued, Attempt: 1, RunnerID: "RUNNER1", ResearchGrant: g.ID, ResearchGrantRevision: sp.Revision, SpecialistID: sp.ID, SpecialistRevision: sp.Revision, SpecialistCaps: `["web.research"]`, CreatedAt: r.now, UpdatedAt: r.now} |
| 504 | r.must(r.s.db.G.Create(&s).Error) |
| 505 | return s |
| 506 | } |
| 507 | |
| 508 | // TestResearchLegacyEmptyBinding: a Research step queued or running at |
| 509 | // the upgrade has no binding (only its grant id): its start is checked |
| 510 | // as before, against the grant the task's project resolves to now; a |
| 511 | // new one's binding is checked too; revoking cancels both. |
| 512 | func TestResearchLegacyEmptyBinding(t *testing.T) { |
| 513 | r, l, _ := grantRig(t) |
| 514 | bound := r.researchStep(l, "k_bound") |
| 515 | legacy := r.researchStep(l, "k_legacy") |
| 516 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_caps = '' WHERE id = ?", legacy.ID) |
| 517 | running := r.researchStep(l, "k_running") |
| 518 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_caps = '', state = ?, started_at = ? WHERE id = ?", wire.StepRunning, r.now, running.ID) |
| 519 | for _, s := range []stepRecord{bound, legacy, r.stepNow(running.ID)} { |
| 520 | if refused := r.authorize(l, s, ""); refused != "" { |
| 521 | t.Fatalf("%s start %q", s.ID, refused) |
| 522 | } |
| 523 | } |
| 524 | // A bound step whose binding no longer matches is refused. |
| 525 | r.exec("UPDATE chat_steps SET specialist_revision = specialist_revision + 1 WHERE id = ?", bound.ID) |
| 526 | if refused := r.authorize(l, r.stepNow(bound.ID), ""); refused != wire.ReasonResearchNoGrant { |
| 527 | t.Fatalf("a mismatched binding: %q", refused) |
| 528 | } |
| 529 | var sp specialistRecord |
| 530 | r.must(r.s.db.G.Where("profile = ? AND state = ?", specialist.ProfileResearch, wire.SpecialistActive).First(&sp).Error) |
| 531 | r.revoke(sp.ID) |
| 532 | if got := r.stepNow(legacy.ID); got.State != wire.StepCancelled || got.Reason != wire.ReasonResearchNoGrant { |
| 533 | t.Fatalf("legacy queued after revoke %+v", got) |
| 534 | } |
| 535 | if got := r.stepNow(running.ID); got.State != wire.StepRunning || r.cancels(running.ID) != 1 { |
| 536 | t.Fatalf("legacy running after revoke %+v", got) |
| 537 | } |
| 538 | if refused := r.authorize(l, r.stepNow(running.ID), ""); refused != wire.ReasonResearchNoGrant { |
| 539 | t.Fatalf("legacy running start after revoke %q", refused) |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // TestResearchDispatchBinds: dispatching Research records the full |
| 544 | // binding of the grant the task's project resolves to. |
| 545 | func TestResearchDispatchBinds(t *testing.T) { |
| 546 | r, l, _ := grantRig(t) |
| 547 | s := r.researchStep(l, "k_r") |
| 548 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_caps = '', research_grant = '', runner_id = '' WHERE id = ?", s.ID) |
| 549 | r.must(r.s.db.G.Create(&researchSourceRecord{StepID: s.ID, Idx: 0, SourceID: "src1", URL: "https://example.com/", Origin: wire.ResearchOriginPerson, Approved: true}).Error) |
| 550 | r.exec("CREATE TABLE runners (id text, person text, role text, label text, revoked_at datetime, removing_at datetime, tainted_at datetime)") |
| 551 | r.exec("INSERT INTO runners(id, person, role) VALUES ('RUNNER1', ?, 'primary')", paul) |
| 552 | var sp specialistRecord |
| 553 | r.must(r.s.db.G.Where("profile = ? AND state = ?", specialist.ProfileResearch, wire.SpecialistActive).First(&sp).Error) |
| 554 | dispatch := func() stepRecord { |
| 555 | t.Helper() |
| 556 | var st stepRecord |
| 557 | r.must(r.tx(func(tx *txn) error { |
| 558 | k, err := tx.loadTask(s.TaskID) |
| 559 | if err != nil { |
| 560 | return err |
| 561 | } |
| 562 | st = tx.mustStep(s.ID) |
| 563 | if err := tx.dispatchResearch(&k, &st); err != nil { |
| 564 | return err |
| 565 | } |
| 566 | return tx.saveStep(&st, &k) |
| 567 | })) |
| 568 | return st |
| 569 | } |
| 570 | if st := dispatch(); st.SpecialistID != sp.ID || st.SpecialistRevision != sp.Revision || st.SpecialistProject != "" || st.SpecialistCaps != `["web.research"]` { |
| 571 | t.Fatalf("Research binding %+v", st) |
| 572 | } |
| 573 | // Patrix gets its own research-v1 row: a Patrix task's Research binds |
| 574 | // to it (the project's row over the team's), and is sent, audited. |
| 575 | own := researchRow(r, "sp_rpatrix", "p_patrix") |
| 576 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_caps = '', research_grant = '', placement = '' WHERE id = ?", s.ID) |
| 577 | r.must(r.s.db.G.Create(&researchAddressRecord{RunnerID: "RUNNER1", Source: "hello", Address: "93.184.216.34", SeenAt: r.now}).Error) |
| 578 | r.must(r.s.db.G.Create(&researchPrefixRecord{RunnerID: "RUNNER1", Prefix: "93.184.216.0/24", ConfirmedBy: paul, ConfirmedAt: r.now}).Error) |
| 579 | st := dispatch() |
| 580 | if st.SpecialistID != own.ID || st.SpecialistProject != "p_patrix" || st.RunnerID != "RUNNER1" { |
| 581 | t.Fatalf("Patrix Research binding %+v", st) |
| 582 | } |
| 583 | if n := r.count("SELECT count(*) FROM chat_specialist_audit WHERE specialist_id = ? AND action = 'used' AND detail LIKE ?", own.ID, "%step="+s.ID+"%"); n != 1 { |
| 584 | t.Fatalf("%d used rows for the Research dispatch", n) |
| 585 | } |
| 586 | if refused := r.authorize(l, r.stepNow(s.ID), ""); refused != "" { |
| 587 | t.Fatalf("Patrix Research start %q", refused) |
| 588 | } |
| 589 | // Bound earlier to what no longer holds: ends cancelled at dispatch. |
| 590 | r.exec("UPDATE chat_steps SET specialist_revision = specialist_revision + 1 WHERE id = ?", s.ID) |
| 591 | if st := dispatch(); st.State != wire.StepCancelled || st.Reason != wire.ReasonResearchNoGrant { |
| 592 | t.Fatalf("a mismatched Research binding dispatched %+v", st) |
| 593 | } |
| 594 | // research asks for the task's project's grant: Patrix's own, with |
| 595 | // the team's revoked. |
| 596 | r.revoke(sp.ID) |
| 597 | turn := r.asked("t_research", wire.LaneCoordinator, "Look up https://example.com/ for the Patrix task.") |
| 598 | r.must(r.s.db.G.Create(&taskRecord{ID: "k_r2", ProjectID: "p_patrix", Title: "Look again", Stage: wire.StageNoted, Creator: paul, Revision: 1, CreatedAt: r.now, UpdatedAt: r.now, Branch: "equipo/k_r2"}).Error) |
| 599 | if res := r.call(l, turn.ID, wire.ToolResearch, map[string]any{"task": "k_r2", "question": "What does the page say?", "urls": []string{"https://example.com/"}}); res.IsError { |
| 600 | t.Fatalf("research in Patrix with Patrix's own grant: %+v", res) |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | // researchRow writes an active research-v1 row for project, granted by |
| 605 | // Paul as an Add does (the menu does not propose one per project yet). |
| 606 | func researchRow(r *hkRig, id, project string) specialistRecord { |
| 607 | r.t.Helper() |
| 608 | sp := specialistRecord{ID: id, Profile: specialist.ProfileResearch, ProjectID: project, Name: "Research", Brief: "Look things up.", State: wire.SpecialistActive, Revision: 2, Approver: paul, ApprovedAt: &r.now, CreatedAt: r.now, UpdatedAt: r.now} |
| 609 | r.must(r.s.db.G.Create(&sp).Error) |
| 610 | r.must(r.s.db.G.Create(&grantRecord{ID: "g_" + id, SpecialistID: id, Capability: specialist.CapabilityWebResearch, Revision: 2, GrantedBy: paul, GrantedAt: r.now}).Error) |
| 611 | return sp |
| 612 | } |
| 613 | |
| 614 | // TestResearchRevokeByScope: revoking one research-v1 row cancels only |
| 615 | // the Research that runs under it: steps bound to it, steps from before |
| 616 | // bindings dispatched under its grant, and steps not yet dispatched in a |
| 617 | // project that resolves to it. Research under Patrix's own row is left |
| 618 | // when the team's is revoked, and the other way round. |
| 619 | func TestResearchRevokeByScope(t *testing.T) { |
| 620 | r, l, _ := grantRig(t) |
| 621 | team := approvedResearch(r, paul) |
| 622 | r.must(r.s.db.G.Where("id = ?", team.ID).First(&team).Error) |
| 623 | own := researchRow(r, "sp_rpatrix", "p_patrix") |
| 624 | ownBound := r.researchStep(l, "k_own_bound") |
| 625 | r.exec("UPDATE chat_steps SET specialist_id = ?, specialist_project = 'p_patrix', research_grant = 'g_sp_rpatrix' WHERE id = ?", own.ID, ownBound.ID) |
| 626 | ownLegacy := r.researchStep(l, "k_own_legacy") |
| 627 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_caps = '', research_grant = 'g_sp_rpatrix' WHERE id = ?", ownLegacy.ID) |
| 628 | patrixWaiting := r.researchStep(l, "k_patrix_waiting") |
| 629 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_caps = '', research_grant = '', runner_id = '', placement = ? WHERE id = ?", wire.PlacementBoundary, patrixWaiting.ID) |
| 630 | suiteWaiting := r.researchStep(l, "k_suite_waiting") |
| 631 | r.exec("UPDATE chat_tasks SET project_id = 'p_suite' WHERE id = 'k_suite_waiting'") |
| 632 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_caps = '', research_grant = '', runner_id = '', placement = ? WHERE id = ?", wire.PlacementBoundary, suiteWaiting.ID) |
| 633 | teamBound := r.researchStep(l, "k_team_bound") |
| 634 | ended := func(s stepRecord) bool { return r.stepNow(s.ID).State == wire.StepCancelled } |
| 635 | r.revoke(team.ID) |
| 636 | if !ended(teamBound) || !ended(suiteWaiting) { |
| 637 | t.Fatalf("the team's Research after its revoke: %+v %+v", r.stepNow(teamBound.ID), r.stepNow(suiteWaiting.ID)) |
| 638 | } |
| 639 | for _, s := range []stepRecord{ownBound, ownLegacy, patrixWaiting} { |
| 640 | if ended(s) { |
| 641 | t.Fatalf("Patrix's own Research %s cancelled by the team's revoke", s.ID) |
| 642 | } |
| 643 | } |
| 644 | // A Suite step waiting with no grant at all: not Patrix's to cancel. |
| 645 | suiteLater := r.researchStep(l, "k_suite_later") |
| 646 | r.exec("UPDATE chat_tasks SET project_id = 'p_suite' WHERE id = 'k_suite_later'") |
| 647 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_caps = '', research_grant = '', runner_id = '', placement = ? WHERE id = ?", wire.PlacementBoundary, suiteLater.ID) |
| 648 | r.revoke(own.ID) |
| 649 | for _, s := range []stepRecord{ownBound, ownLegacy, patrixWaiting} { |
| 650 | if !ended(s) { |
| 651 | t.Fatalf("Patrix's Research %s after Patrix's revoke %+v", s.ID, r.stepNow(s.ID)) |
| 652 | } |
| 653 | } |
| 654 | if ended(suiteLater) { |
| 655 | t.Fatal("Patrix's revoke cancelled Suite's Research") |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // TestDesignRevokeBeforePush: revoked between commit and push. On the |
| 660 | // primary the pre-push check is refused; a worker's commit waiting for |
| 661 | // the primary to push it is withdrawn at the revoke (its publish |
| 662 | // command retired, its transfers closed) and its publication check is |
| 663 | // refused; a binding that stops holding without a revoke (the approver |
| 664 | // demoted) refuses the publication too. |
| 665 | func TestDesignRevokeBeforePush(t *testing.T) { |
| 666 | r, l, _ := grantRig(t) |
| 667 | sp, task, s := r.designStarted(l, "Patrix") |
| 668 | r.ackDispatches(s.ID) |
| 669 | if refused := r.authorize(l, s, ""); refused != "" { |
| 670 | t.Fatalf("start %q", refused) |
| 671 | } |
| 672 | // A worker's commit, as if it ran there: the primary is to push it. |
| 673 | r.exec("UPDATE chat_steps SET runner_id = 'WORKER1', state = ?, candidate_sha = ?, publication = ?, publication_id = 'pb_1', publication_runner = 'RUNNER1' WHERE id = ?", wire.StepDone, strings.Repeat("a", 40), wire.PublicationPending, s.ID) |
| 674 | r.must(r.s.db.G.Create(&transferRecord{ID: "tr_out", Person: paul, FromRunner: "WORKER1", ToRunner: "RUNNER1", TaskID: task, StepID: s.ID, Attempt: s.Attempt, Direction: wire.TransferOutbound, State: transferComplete, ExpiresAt: r.now.Add(wire.TransferTTL), CreatedAt: r.now}).Error) |
| 675 | r.must(r.s.db.G.Create(&commandRecord{Person: paul, RunnerID: "RUNNER1", Kind: wire.FramePublish, Body: `{"publication":"pb_1","task_id":"` + task + `","step_id":"` + s.ID + `","attempt":1}`, CreatedAt: r.now}).Error) |
| 676 | publication := func() string { |
| 677 | t.Helper() |
| 678 | refused, err := r.s.AuthorizePublication(r.ctx, l, s.ID, s.Attempt, "pb_1") |
| 679 | r.must(err) |
| 680 | return refused |
| 681 | } |
| 682 | if refused := publication(); refused != "" { |
| 683 | t.Fatalf("publication while bound %q", refused) |
| 684 | } |
| 685 | r.exec("UPDATE members SET role = 'member' WHERE address = ?", paul) |
| 686 | if refused := publication(); refused == "" { |
| 687 | t.Fatal("publication with the approver demoted") |
| 688 | } |
| 689 | r.exec("UPDATE members SET role = 'owner' WHERE address = ?", paul) |
| 690 | if refused := publication(); refused != "" { |
| 691 | t.Fatalf("publication restored %q", refused) |
| 692 | } |
| 693 | r.revoke(sp.ID) |
| 694 | got := r.stepNow(s.ID) |
| 695 | if got.Publication != wire.PublicationFailed || got.Reason != "cancelled" { |
| 696 | t.Fatalf("publication after revoke %+v", got) |
| 697 | } |
| 698 | if n := r.count("SELECT count(*) FROM runner_commands WHERE kind = ? AND acked_at IS NULL", wire.FramePublish); n != 0 { |
| 699 | t.Fatalf("%d publish commands still to send", n) |
| 700 | } |
| 701 | if n := r.count("SELECT count(*) FROM runner_transfers WHERE id = 'tr_out' AND state = ?", transferClosed); n != 1 { |
| 702 | t.Fatal("its transfer is open") |
| 703 | } |
| 704 | if refused := publication(); refused == "" { |
| 705 | t.Fatal("publication after revoke") |
| 706 | } |
| 707 | |
| 708 | // On the primary: its own pre-push check after the revoke. A commit |
| 709 | // still being uploaded is never pushed: its transfer is closed. |
| 710 | sp2, task2, s2 := r.designStarted(l, "Suite") |
| 711 | r.ackDispatches(s2.ID) |
| 712 | if refused := r.authorize(l, s2, ""); refused != "" { |
| 713 | t.Fatalf("Suite start %q", refused) |
| 714 | } |
| 715 | r.must(r.s.db.G.Create(&transferRecord{ID: "tr_up", Person: paul, FromRunner: "WORKER1", ToRunner: "RUNNER1", TaskID: task2, StepID: s2.ID, Attempt: s2.Attempt, Direction: wire.TransferOutbound, State: transferOpen, ExpiresAt: r.now.Add(wire.TransferTTL), CreatedAt: r.now}).Error) |
| 716 | r.revoke(sp2.ID) |
| 717 | if refused := r.authorize(l, s2, ""); refused != reasonSpecialistNoGrant { |
| 718 | t.Fatalf("Suite pre-push after revoke %q", refused) |
| 719 | } |
| 720 | if n := r.count("SELECT count(*) FROM runner_transfers WHERE id = 'tr_up' AND state = ?", transferClosed); n != 1 { |
| 721 | t.Fatal("an upload of a revoked step's commit is open") |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | // TestPublicationNeedsTheMaker: a maker step's publication is refused |
| 726 | // when the step is bound to another specialist than the task's maker, or |
| 727 | // to none at all. |
| 728 | func TestPublicationNeedsTheMaker(t *testing.T) { |
| 729 | r, l, _ := grantRig(t) |
| 730 | _, task, s := r.designStarted(l, "Patrix") |
| 731 | other, _, _ := r.designStarted(l, "Suite") |
| 732 | r.exec("UPDATE chat_steps SET runner_id = 'WORKER1', state = ?, candidate_sha = ?, publication = ?, publication_id = 'pb_1', publication_runner = 'RUNNER1' WHERE id = ?", wire.StepDone, strings.Repeat("a", 40), wire.PublicationPending, s.ID) |
| 733 | publication := func() string { |
| 734 | t.Helper() |
| 735 | refused, err := r.s.AuthorizePublication(r.ctx, l, s.ID, s.Attempt, "pb_1") |
| 736 | r.must(err) |
| 737 | return refused |
| 738 | } |
| 739 | if refused := publication(); refused != "" { |
| 740 | t.Fatalf("bound %q", refused) |
| 741 | } |
| 742 | k := r.load(task) |
| 743 | r.exec("UPDATE chat_tasks SET maker_specialist_id = ? WHERE id = ?", other.ID, task) |
| 744 | if refused := publication(); refused == "" { |
| 745 | t.Fatal("published for a task made by another specialist") |
| 746 | } |
| 747 | r.exec("UPDATE chat_tasks SET maker_specialist_id = ? WHERE id = ?", k.MakerSpecialistID, task) |
| 748 | r.exec("UPDATE chat_steps SET specialist_id = '', specialist_revision = 0, specialist_project = '', specialist_caps = '' WHERE id = ?", s.ID) |
| 749 | if refused := publication(); refused == "" { |
| 750 | t.Fatal("published an unbound maker step") |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | // TestDesignApproverDemoted: the approver stops being an owner: no |
| 755 | // start, no render; an owner again, both are granted (the binding is |
| 756 | // unchanged); a lookup error denies. |
| 757 | func TestDesignApproverDemoted(t *testing.T) { |
| 758 | r, l, _ := grantRig(t) |
| 759 | _, _, s := r.designStarted(l, "Patrix") |
| 760 | r.exec("UPDATE members SET role = 'member' WHERE address = ?", paul) |
| 761 | if refused := r.authorize(l, s, ""); refused != reasonSpecialistNoGrant { |
| 762 | t.Fatalf("start with the approver demoted %q", refused) |
| 763 | } |
| 764 | if refused := r.authorize(l, s, wire.LeasePurposeRender); refused == "" { |
| 765 | t.Fatal("render with the approver demoted") |
| 766 | } |
| 767 | r.exec("UPDATE members SET active = 0, role = 'owner' WHERE address = ?", paul) |
| 768 | if refused := r.authorize(l, s, ""); refused == "" { |
| 769 | t.Fatal("start with the approver removed") |
| 770 | } |
| 771 | r.exec("UPDATE members SET active = 1 WHERE address = ?", paul) |
| 772 | if refused := r.authorize(l, s, ""); refused != "" { |
| 773 | t.Fatalf("an owner again %q", refused) |
| 774 | } |
| 775 | if refused := r.authorize(l, s, wire.LeasePurposeRender); refused != "" { |
| 776 | t.Fatalf("render, an owner again %q", refused) |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | // TestDesignProfileOffTheMenu: a binding to a profile no longer on the |
| 781 | // menu never holds, at start, render or dispatch. |
| 782 | func TestDesignProfileOffTheMenu(t *testing.T) { |
| 783 | r, l, _ := grantRig(t) |
| 784 | sp, task, s := r.designStarted(l, "Patrix") |
| 785 | r.exec("UPDATE chat_specialists SET profile = 'design-v0' WHERE id = ?", sp.ID) |
| 786 | if refused := r.authorize(l, s, ""); refused == "" { |
| 787 | t.Fatal("start under a profile off the menu") |
| 788 | } |
| 789 | if refused := r.authorize(l, s, wire.LeasePurposeRender); refused == "" { |
| 790 | t.Fatal("render under a profile off the menu") |
| 791 | } |
| 792 | r.must(r.tx(func(tx *txn) error { |
| 793 | k, err := tx.loadTask(task) |
| 794 | if err != nil { |
| 795 | return err |
| 796 | } |
| 797 | st := tx.mustStep(s.ID) |
| 798 | if err := tx.replace(&k, &st); err != nil { |
| 799 | return err |
| 800 | } |
| 801 | if st.State != wire.StepCancelled || st.Reason != reasonSpecialistNoGrant { |
| 802 | t.Fatalf("dispatched under a profile off the menu %+v", st) |
| 803 | } |
| 804 | return tx.saveStep(&st, &k) |
| 805 | })) |
| 806 | } |
| 807 | |
| 808 | // TestDesignProjectRowOverTeamRow: a step bound to the team's Design in |
| 809 | // Patrix stops holding once Patrix has its own Design (grantFor now |
| 810 | // finds that row), while the team's binding in Suite holds. |
| 811 | func TestDesignProjectRowOverTeamRow(t *testing.T) { |
| 812 | r, l, _ := grantRig(t) |
| 813 | team := r.propose(l, "", "") |
| 814 | r.must(r.answer(paul, team, wire.ChoiceAdd, 1)) |
| 815 | r.must(r.s.db.G.Where("id = ?", team.ID).First(&team).Error) |
| 816 | patrix := r.makerStep(r.teamTask(l, "k_tp", "p_patrix", team.ID)) |
| 817 | suite := r.makerStep(r.teamTask(l, "k_ts", "p_suite", team.ID)) |
| 818 | for _, s := range []stepRecord{patrix, suite} { |
| 819 | if refused := r.authorize(l, s, wire.LeasePurposeRender); refused != "" { |
| 820 | t.Fatalf("%s render under the team's %q", s.ID, refused) |
| 821 | } |
| 822 | } |
| 823 | own := r.propose(l, "Patrix", "p_patrix") |
| 824 | r.must(r.answer(paul, own, wire.ChoiceAdd, 1)) |
| 825 | if refused := r.authorize(l, patrix, wire.LeasePurposeRender); refused == "" { |
| 826 | t.Fatal("the team's binding held in Patrix over Patrix's own row") |
| 827 | } |
| 828 | if refused := r.authorize(l, suite, wire.LeasePurposeRender); refused != "" { |
| 829 | t.Fatalf("Suite under the team's %q", refused) |
| 830 | } |
| 831 | // A new step of the team-made Patrix task is not dispatched under |
| 832 | // Patrix's own row (it is not the task's maker): it ends cancelled. |
| 833 | r.must(r.tx(func(tx *txn) error { |
| 834 | k, err := tx.loadTask("k_tp") |
| 835 | if err != nil { |
| 836 | return err |
| 837 | } |
| 838 | n := stepRecord{ID: "s_tp2", TaskID: k.ID, Idx: patrix.Idx + 1, Specialist: wire.SpecialistEngineering, Person: paul, State: wire.StepQueued, Attempt: 1, CreatedAt: r.now, UpdatedAt: r.now} |
| 839 | if err := tx.db.Create(&n).Error; err != nil { |
| 840 | return err |
| 841 | } |
| 842 | if err := tx.dispatchStep(&k, &n); err != nil { |
| 843 | return err |
| 844 | } |
| 845 | if n.State != wire.StepCancelled || n.SpecialistID != "" { |
| 846 | t.Fatalf("dispatched under Patrix's row %+v", n) |
| 847 | } |
| 848 | return tx.saveStep(&n, &k) |
| 849 | })) |
| 850 | } |
| 851 | |
| 852 | // TestDesignRevokeOneProject: revoking Patrix's Design cancels Patrix's |
| 853 | // Design step only; Suite's Design and Engineering's own step go on, |
| 854 | // with no cancel and their starts granted. |
| 855 | func TestDesignRevokeOneProject(t *testing.T) { |
| 856 | r, l, audit := grantRig(t) |
| 857 | patrix, _, ps := r.designStarted(l, "Patrix") |
| 858 | _, _, ss := r.designStarted(l, "Suite") |
| 859 | eng := r.plainTask(l, "k_plain") |
| 860 | for _, s := range []stepRecord{ps, ss, eng} { |
| 861 | r.ackDispatches(s.ID) |
| 862 | if refused := r.authorize(l, s, ""); refused != "" { |
| 863 | t.Fatalf("%s start %q", s.ID, refused) |
| 864 | } |
| 865 | } |
| 866 | r.exec("UPDATE chat_steps SET publication = ?, publication_id = 'pb_suite' WHERE id = ?", wire.PublicationPending, ss.ID) |
| 867 | before := len(*audit) |
| 868 | r.revoke(patrix.ID) |
| 869 | if got := r.stepNow(ss.ID); got.Publication != wire.PublicationPending { |
| 870 | t.Fatalf("Suite's publication after Patrix's revoke %+v", got) |
| 871 | } |
| 872 | if r.cancels(ps.ID) != 1 || r.cancels(ss.ID) != 0 || r.cancels(eng.ID) != 0 { |
| 873 | t.Fatalf("cancels %d %d %d", r.cancels(ps.ID), r.cancels(ss.ID), r.cancels(eng.ID)) |
| 874 | } |
| 875 | if refused := r.authorize(l, ps, ""); refused == "" { |
| 876 | t.Fatal("Patrix's Design after its revoke") |
| 877 | } |
| 878 | for _, s := range []stepRecord{ss, eng} { |
| 879 | if refused := r.authorize(l, s, ""); refused != "" { |
| 880 | t.Fatalf("%s after Patrix's revoke %q", s.ID, refused) |
| 881 | } |
| 882 | if got := r.stepNow(s.ID); wire.StepTerminal(got.State) { |
| 883 | t.Fatalf("%s ended %+v", s.ID, got) |
| 884 | } |
| 885 | } |
| 886 | if refused := r.authorize(l, ss, wire.LeasePurposeRender); refused != "" { |
| 887 | t.Fatalf("Suite's render after Patrix's revoke %q", refused) |
| 888 | } |
| 889 | for _, a := range (*audit)[before:] { |
| 890 | if strings.Contains(a, "specialist.used") { |
| 891 | t.Fatalf("a revoke audited a use: %q", a) |
| 892 | } |
| 893 | } |
| 894 | // Engineering's step is not bound and audits no use. |
| 895 | if r.count("SELECT count(*) FROM chat_specialist_audit WHERE action = 'used' AND detail LIKE ?", "%step="+eng.ID+"%") != 0 { |
| 896 | t.Fatal("Engineering's dispatch audited a specialist use") |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // TestDesignHeldStepAfterRevoke: a Design step not yet dispatched (held |
| 901 | // for Research) ends cancelled at the revoke, and a dispatch later finds |
| 902 | // no grant: it never runs as plain Engineering. |
| 903 | func TestDesignHeldStepAfterRevoke(t *testing.T) { |
| 904 | r, l, _ := grantRig(t) |
| 905 | sp, task, s := r.designStarted(l, "Patrix") |
| 906 | held := stepRecord{ID: "s_held", TaskID: task, Idx: s.Idx + 1, Specialist: wire.SpecialistEngineering, Person: paul, State: wire.StepQueued, Attempt: 1, Placement: wire.PlacementResearch, CreatedAt: r.now, UpdatedAt: r.now} |
| 907 | r.must(r.s.db.G.Create(&held).Error) |
| 908 | r.revoke(sp.ID) |
| 909 | if got := r.stepNow(held.ID); got.State != wire.StepCancelled || got.Reason != reasonSpecialistNoGrant || !got.Terminated { |
| 910 | t.Fatalf("held step after revoke %+v", got) |
| 911 | } |
| 912 | // A new step of the task (a fix round, say) finds no grant at dispatch. |
| 913 | r.must(r.tx(func(tx *txn) error { |
| 914 | k, err := tx.loadTask(task) |
| 915 | if err != nil { |
| 916 | return err |
| 917 | } |
| 918 | n := stepRecord{ID: "s_next", TaskID: task, Idx: s.Idx + 2, Specialist: wire.SpecialistEngineering, Person: paul, State: wire.StepQueued, Attempt: 1, CreatedAt: r.now, UpdatedAt: r.now} |
| 919 | if err := tx.db.Create(&n).Error; err != nil { |
| 920 | return err |
| 921 | } |
| 922 | if err := tx.dispatchStep(&k, &n); err != nil { |
| 923 | return err |
| 924 | } |
| 925 | if n.State != wire.StepCancelled || n.Reason != reasonSpecialistNoGrant || n.SpecialistID != "" { |
| 926 | t.Fatalf("dispatched after revoke %+v", n) |
| 927 | } |
| 928 | return tx.saveStep(&n, &k) |
| 929 | })) |
| 930 | if n := r.count("SELECT count(*) FROM runner_commands WHERE kind = ? AND body LIKE ?", wire.FrameDispatch, `%"step_id":"s_next"%`); n != 0 { |
| 931 | t.Fatalf("%d dispatches after revoke", n) |
| 932 | } |
| 933 | } |
| 934 | |