| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo" |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Research's sources, card and scheduling (Research spec §2, slice R2). |
| 14 | // The coordinator's research tool only requests a run; it starts at the |
| 15 | // task's next fixed point (task start, or the boundary before the next |
| 16 | // Engineering round), one per fixed point and three per task. While it is |
| 17 | // pending or running, that Engineering round is held, and it is released |
| 18 | // when Research ends, however it ends. |
| 19 | |
| 20 | // personWords is the person's message on the task: a URL and a bare |
| 21 | // domain, both theirs, so read without asking. |
| 22 | const personWords = "Make mockups for the barber shop. Its site is shop.example.com and the hours are on https://shop.example.com/hours." |
| 23 | |
| 24 | // researchOn adds Research to the team: proposed, then the owner's Add. |
| 25 | func (r *toolRig) researchOn() { |
| 26 | r.t.Helper() |
| 27 | if res := r.call("on-propose", wire.ToolProposeSpecialist, researchArgs()); res.IsError { |
| 28 | r.t.Fatalf("propose: %+v", res) |
| 29 | } |
| 30 | card := r.specialistCard(wire.CardOpen) |
| 31 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAdd, card.Revision); err != nil { |
| 32 | r.t.Fatal(err) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // inventoryOn gives the primary a confirmed site inventory, so a |
| 37 | // Research step may be dispatched: its hello reports one address, and |
| 38 | // that address's prefix is recorded as an owner's Confirm records it |
| 39 | // (research_inventory_contract_test.go confirms through the card). |
| 40 | func (r *toolRig) inventoryOn() { |
| 41 | r.t.Helper() |
| 42 | r.addresses(1, "81.2.69.7") |
| 43 | if _, err := r.inst.DB.Writer().Exec("INSERT INTO chat_research_prefixes(runner_id, prefix, confirmed_by, confirmed_at, card_id) VALUES (?,?,?,?,?) ON CONFLICT DO NOTHING", r.runner.Welcome.RunnerID, "81.2.69.7/32", Owner, time.Now().UTC().Format(time.RFC3339Nano), ""); err != nil { |
| 44 | r.t.Fatal(err) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // shopTask records a change task for the shop. |
| 49 | func (r *toolRig) shopTask(key string) string { |
| 50 | r.t.Helper() |
| 51 | return taskID(r.t, r.call(key+"-rec", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Shop mockups", "brief": "Mockups that match the shop.", "separate": true})) |
| 52 | } |
| 53 | |
| 54 | func (r *toolRig) research(key, task string, urls ...string) wire.ToolResult { |
| 55 | r.t.Helper() |
| 56 | if urls == nil { |
| 57 | urls = []string{} |
| 58 | } |
| 59 | return r.call(key, wire.ToolResearch, map[string]any{"task": task, "question": "Its colours, logo and opening hours.", "urls": urls}) |
| 60 | } |
| 61 | |
| 62 | func (r *toolRig) begin(key, task string) { |
| 63 | r.t.Helper() |
| 64 | if res := r.call(key, wire.ToolStartTask, map[string]string{"task": task, "brief": "Make them."}); res.IsError { |
| 65 | r.t.Fatalf("start: %+v", res) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func stepsOf(k wire.Task, specialist string) []wire.Step { |
| 70 | var out []wire.Step |
| 71 | for _, s := range k.Steps { |
| 72 | if s.Specialist == specialist { |
| 73 | out = append(out, s) |
| 74 | } |
| 75 | } |
| 76 | return out |
| 77 | } |
| 78 | |
| 79 | // newest is the task's newest step of specialist. |
| 80 | func (r *toolRig) newest(id, specialist string) wire.Step { |
| 81 | r.t.Helper() |
| 82 | all := stepsOf(r.task(id), specialist) |
| 83 | if len(all) == 0 { |
| 84 | r.t.Fatalf("no %s step", specialist) |
| 85 | } |
| 86 | return all[len(all)-1] |
| 87 | } |
| 88 | |
| 89 | // heldForResearch checks the task's newest Engineering step waits for Research and |
| 90 | // nothing was sent for it. |
| 91 | func (r *toolRig) heldForResearch(id string) wire.Step { |
| 92 | r.t.Helper() |
| 93 | e := r.newest(id, wire.SpecialistEngineering) |
| 94 | if e.State != wire.StepQueued || e.Placement != wire.PlacementResearch || e.RunnerID != "" { |
| 95 | r.t.Fatalf("Engineering is not held: %+v", e) |
| 96 | } |
| 97 | if n := r.commandsLike(wire.FrameDispatch, `"step_id":"`+e.ID+`"`); n != 0 { |
| 98 | r.t.Fatalf("a held Engineering step was dispatched %d times", n) |
| 99 | } |
| 100 | return e |
| 101 | } |
| 102 | |
| 103 | func (r *toolRig) sourcesCard(state string) wire.Card { |
| 104 | r.t.Helper() |
| 105 | var card wire.Card |
| 106 | r.wait("sources card "+state, func(c *Conversation) bool { |
| 107 | for _, k := range c.Cards { |
| 108 | if k.Kind == wire.CardResearchSources && k.State == state { |
| 109 | card = k |
| 110 | return true |
| 111 | } |
| 112 | } |
| 113 | return false |
| 114 | }) |
| 115 | return card |
| 116 | } |
| 117 | |
| 118 | func (r *toolRig) sourcesCards() int { |
| 119 | r.t.Helper() |
| 120 | n := 0 |
| 121 | for _, c := range r.snapshotCards() { |
| 122 | if c.Kind == wire.CardResearchSources { |
| 123 | n++ |
| 124 | } |
| 125 | } |
| 126 | return n |
| 127 | } |
| 128 | |
| 129 | func (r *toolRig) researchDispatches() int { |
| 130 | r.t.Helper() |
| 131 | return r.commandsLike(wire.FrameDispatch, `"specialist":"research"`) |
| 132 | } |
| 133 | |
| 134 | // TestResearchAtTaskStartHoldsEngineering: research before start_task |
| 135 | // waits for the task's start; the person's own URL and bare domain are |
| 136 | // its sources, with no card. At start it is at its fixed point and |
| 137 | // Engineering is held; while the primary has no confirmed site inventory |
| 138 | // (here its hello has not said which addresses it has, so there is |
| 139 | // nothing to confirm yet and no card) it is never dispatched, and says |
| 140 | // so. |
| 141 | func TestResearchAtTaskStartHoldsEngineering(t *testing.T) { |
| 142 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 143 | r.researchOn() |
| 144 | id := r.shopTask("a") |
| 145 | res := r.research("a-r", id) |
| 146 | if res.IsError || res.Code != "requested" || res.Text == "" { |
| 147 | t.Fatalf("request: %+v", res) |
| 148 | } |
| 149 | s := r.newest(id, wire.SpecialistResearch) |
| 150 | if s.State != wire.StepQueued || s.Placement != wire.PlacementBoundary || s.RunnerID != "" { |
| 151 | t.Fatalf("before the start %+v", s) |
| 152 | } |
| 153 | if r.sourcesCards() != 0 { |
| 154 | t.Fatal("a card for the person's own URLs") |
| 155 | } |
| 156 | if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE step_id = ? AND origin = ? AND approved = 1", s.ID, wire.ResearchOriginPerson); n != 2 { |
| 157 | t.Fatalf("%d of the person's sources", n) |
| 158 | } |
| 159 | if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE step_id = ? AND url IN (?, ?)", s.ID, "https://shop.example.com/", "https://shop.example.com/hours"); n != 2 { |
| 160 | t.Fatalf("the sources are not canonical: %d", n) |
| 161 | } |
| 162 | r.begin("a-start", id) |
| 163 | r.heldForResearch(id) |
| 164 | if s := r.newest(id, wire.SpecialistResearch); s.State != wire.StepQueued || s.Placement != wire.PlacementInventory { |
| 165 | t.Fatalf("at the start %+v", s) |
| 166 | } |
| 167 | if r.researchDispatches() != 0 || r.commands(wire.FrameDispatch) != 0 { |
| 168 | t.Fatal("dispatched without a confirmed inventory") |
| 169 | } |
| 170 | if r.inventoryCards() != 0 { |
| 171 | t.Fatal("an inventory card with no address to confirm") |
| 172 | } |
| 173 | if k := r.task(id); k.Stage != wire.StageInProgress || k.NeedsYou != "" || k.CandidateSHA != "" { |
| 174 | t.Fatalf("the task while Research waits %+v", k) |
| 175 | } |
| 176 | // The pages still render with a waiting Research step and a held one. |
| 177 | r.get(r.inst.SessionFor(t, Owner), "/tasks/"+id, 200) |
| 178 | r.get(r.inst.SessionFor(t, Owner), "/work", 200) |
| 179 | } |
| 180 | |
| 181 | // TestResearchRunsThenReleasesEngineering: with the inventory confirmed, |
| 182 | // the Research step is dispatched to the primary with its inputs and no |
| 183 | // repository, branch or SHA; Engineering goes when it is done, and |
| 184 | // Research never becomes the task's last word. |
| 185 | func TestResearchRunsThenReleasesEngineering(t *testing.T) { |
| 186 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 187 | r.researchOn() |
| 188 | r.inventoryOn() |
| 189 | id := r.shopTask("b") |
| 190 | r.research("b-r", id) |
| 191 | r.begin("b-start", id) |
| 192 | d := r.nextDispatch() |
| 193 | if d.Specialist != wire.SpecialistResearch || d.Repo != "" || d.Branch != "" || d.BaseSHA != "" || d.CandidateSHA != "" || d.Title != "Shop mockups" || d.Research == nil { |
| 194 | t.Fatalf("research dispatch %+v", d) |
| 195 | } |
| 196 | rd := d.Research |
| 197 | want := []wire.ResearchSource{{ID: "s1", URL: "https://shop.example.com/", Origin: wire.ResearchOriginPerson}, {ID: "s2", URL: "https://shop.example.com/hours", Origin: wire.ResearchOriginPerson}} |
| 198 | if rd.Question != "Its colours, logo and opening hours." || !slices.Equal(rd.Sources, want) || !slices.Equal(rd.Inventory, []string{"81.2.69.7/32"}) || !slices.Contains(rd.PersonWords, personWords) { |
| 199 | t.Fatalf("research inputs %+v", rd) |
| 200 | } |
| 201 | // The approved specialist's brief, at its revision (Astra security |
| 202 | // finding 6). |
| 203 | list, err := r.client.Specialists(r.ctx) |
| 204 | if err != nil || len(list.Items) != 1 { |
| 205 | t.Fatalf("%+v %v", list, err) |
| 206 | } |
| 207 | if rd.SpecialistBrief != researchArgs()["brief"] || rd.SpecialistRevision != list.Items[0].Revision || rd.SpecialistRevision < 1 { |
| 208 | t.Fatalf("specialist brief %q at %d", rd.SpecialistBrief, rd.SpecialistRevision) |
| 209 | } |
| 210 | r.heldForResearch(id) |
| 211 | r.step("b-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning}) |
| 212 | if k := r.task(id); k.NeedsYou != "" || r.newest(id, wire.SpecialistResearch).State != wire.StepRunning { |
| 213 | t.Fatalf("running %+v", k) |
| 214 | } |
| 215 | // A report that names a SHA is not a Research report. |
| 216 | r.step("b-sha", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: baseX, Termination: true}) |
| 217 | if s := r.newest(id, wire.SpecialistResearch); s.State != wire.StepRunning || r.task(id).BaseSHA != "" { |
| 218 | t.Fatalf("a report with a SHA counted %+v", s) |
| 219 | } |
| 220 | r.step("b-done", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Summary: "Read two pages."}) |
| 221 | e := r.nextDispatch() |
| 222 | if e.Specialist != wire.SpecialistEngineering || e.Round != 0 || e.Research != nil { |
| 223 | t.Fatalf("Engineering after Research %+v", e) |
| 224 | } |
| 225 | k := r.task(id) |
| 226 | if s := stepsOf(k, wire.SpecialistResearch); len(s) != 1 || s[0].State != wire.StepDone || s[0].Index >= stepsOf(k, wire.SpecialistEngineering)[0].Index { |
| 227 | t.Fatalf("steps %+v", k.Steps) |
| 228 | } |
| 229 | // Engineering's push is judged as ever: Research is not a judgement. |
| 230 | r.step("b-e-done", wire.StepReport{TaskID: id, StepID: e.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: baseX, CandidateSHA: candX, Pushed: true, Termination: true}) |
| 231 | r.dispatched(wire.SpecialistChecks, baseX, candX, 0) |
| 232 | if k := r.task(id); k.CandidateSHA != candX || k.Stage != wire.StageInReview || k.NeedsYou != "" { |
| 233 | t.Fatalf("after Engineering %+v", k) |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // TestResearchWaitsForTheRoundBoundary: a request while Engineering runs |
| 238 | // waits; checks go on as ever; the fix round is the next fixed point, so |
| 239 | // Research runs then with that round held, and a failed Research releases |
| 240 | // it. |
| 241 | func TestResearchWaitsForTheRoundBoundary(t *testing.T) { |
| 242 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 243 | r.researchOn() |
| 244 | r.inventoryOn() |
| 245 | id := r.shopTask("c") |
| 246 | r.begin("c-start", id) |
| 247 | eng := r.nextDispatch() |
| 248 | r.step("c-run", wire.StepReport{TaskID: id, StepID: eng.StepID, Attempt: 1, State: wire.StepRunning, BaseSHA: baseX}) |
| 249 | if res := r.research("c-r", id); res.IsError { |
| 250 | t.Fatalf("request mid-step %+v", res) |
| 251 | } |
| 252 | if s := r.newest(id, wire.SpecialistResearch); s.Placement != wire.PlacementBoundary || r.researchDispatches() != 0 { |
| 253 | t.Fatalf("started mid-step %+v", s) |
| 254 | } |
| 255 | r.step("c-done", wire.StepReport{TaskID: id, StepID: eng.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: baseX, CandidateSHA: candX, Pushed: true, Termination: true}) |
| 256 | c := r.dispatched(wire.SpecialistChecks, baseX, candX, 0) |
| 257 | if r.researchDispatches() != 0 { |
| 258 | t.Fatal("Research ran beside checks") |
| 259 | } |
| 260 | r.checks("c-c", c, wire.ChecksFailed, exit(2), "", "FAIL\n") |
| 261 | d := r.nextDispatch() |
| 262 | if d.Specialist != wire.SpecialistResearch { |
| 263 | t.Fatalf("at the round boundary %+v", d) |
| 264 | } |
| 265 | fix := r.heldForResearch(id) |
| 266 | if fix.Round != 1 || r.newest(id, wire.SpecialistResearch).Index >= fix.Index { |
| 267 | t.Fatalf("the held round %+v", fix) |
| 268 | } |
| 269 | r.step("c-r-fail", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 270 | e := r.nextDispatch() |
| 271 | if e.Specialist != wire.SpecialistEngineering || e.Round != 1 || e.StepID != fix.ID || e.BaseSHA != candX { |
| 272 | t.Fatalf("released round %+v", e) |
| 273 | } |
| 274 | if k := r.task(id); k.NeedsYou != "" || k.Stage != wire.StageInProgress { |
| 275 | t.Fatalf("a failed Research needs the person: %+v", k) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // TestResearchOnePerBoundaryAndThreePerTask: a second request for the |
| 280 | // same fixed point is refused, a cancelled run still counts, and a |
| 281 | // fourth run is refused. |
| 282 | func TestResearchOnePerBoundaryAndThreePerTask(t *testing.T) { |
| 283 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 284 | r.researchOn() |
| 285 | r.inventoryOn() |
| 286 | id := r.shopTask("d") |
| 287 | if res := r.research("d-1", id); res.IsError { |
| 288 | t.Fatalf("first %+v", res) |
| 289 | } |
| 290 | if res := r.research("d-1b", id); !res.IsError || res.Code != "boundary_taken" { |
| 291 | t.Fatalf("a second at the start %+v", res) |
| 292 | } |
| 293 | r.begin("d-start", id) |
| 294 | d1 := r.nextDispatch() |
| 295 | r.step("d-1-done", wire.StepReport{TaskID: id, StepID: d1.StepID, Attempt: 1, State: wire.StepDone, Termination: true}) |
| 296 | eng := r.nextDispatch() |
| 297 | if res := r.research("d-2", id); res.IsError { |
| 298 | t.Fatalf("second %+v", res) |
| 299 | } |
| 300 | if res := r.research("d-2b", id); !res.IsError || res.Code != "boundary_taken" { |
| 301 | t.Fatalf("a second at round 1 %+v", res) |
| 302 | } |
| 303 | r.step("d-e", wire.StepReport{TaskID: id, StepID: eng.StepID, Attempt: 1, State: wire.StepDone, BaseSHA: baseX, CandidateSHA: candX, Pushed: true, Termination: true}) |
| 304 | c := r.dispatched(wire.SpecialistChecks, baseX, candX, 0) |
| 305 | r.checks("d-c", c, wire.ChecksFailed, exit(2), "", "FAIL\n") |
| 306 | d2 := r.nextDispatch() |
| 307 | if d2.Specialist != wire.SpecialistResearch { |
| 308 | t.Fatalf("second run %+v", d2) |
| 309 | } |
| 310 | // Cancelled by the runner: Engineering goes, and the run counts. |
| 311 | r.step("d-2-cancel", wire.StepReport{TaskID: id, StepID: d2.StepID, Attempt: 1, State: wire.StepCancelled, Reason: "cancelled", Termination: true}) |
| 312 | if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering || e.Round != 1 { |
| 313 | t.Fatalf("after a cancelled Research %+v", e) |
| 314 | } |
| 315 | if res := r.research("d-3", id); res.IsError { |
| 316 | t.Fatalf("third %+v", res) |
| 317 | } |
| 318 | if res := r.research("d-4", id); !res.IsError || res.Code != "research_limit" { |
| 319 | t.Fatalf("a fourth %+v", res) |
| 320 | } |
| 321 | if n := len(stepsOf(r.task(id), wire.SpecialistResearch)); n != 3 { |
| 322 | t.Fatalf("%d Research steps", n) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // TestResearchRefused: no grant, a bad question, a proposal that breaks |
| 327 | // the rules, another person's task, an ended task or nothing to read are |
| 328 | // refused before any step or card. |
| 329 | func TestResearchRefused(t *testing.T) { |
| 330 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 331 | id := r.shopTask("e") |
| 332 | if res := r.research("e-ng", id); !res.IsError || res.Code != "no_grant" || res.Text == "" { |
| 333 | t.Fatalf("without a grant %+v", res) |
| 334 | } |
| 335 | r.researchOn() |
| 336 | q := func(question string) map[string]any { |
| 337 | return map[string]any{"task": id, "question": question, "urls": []string{}} |
| 338 | } |
| 339 | u := func(urls ...string) map[string]any { |
| 340 | return map[string]any{"task": id, "question": "Colours?", "urls": urls} |
| 341 | } |
| 342 | eleven := make([]string, 11) |
| 343 | for i := range eleven { |
| 344 | eleven[i] = "https://shop.example.com/p" + itoa(i) |
| 345 | } |
| 346 | for name, c := range map[string]struct { |
| 347 | args map[string]any |
| 348 | code string |
| 349 | }{ |
| 350 | "no question": {q(""), "question"}, |
| 351 | "a question too long": {q(strings.Repeat("q", wire.MaxResearchQuestion+1)), "question"}, |
| 352 | "a secret-looking one": {q("Use token=abcd1234efgh5678 to sign in"), "secret"}, |
| 353 | "a query": {u("https://shop.example.com/?ref=x"), "query_refused"}, |
| 354 | "an IP literal": {u("http://81.2.69.9/"), "ip_literal"}, |
| 355 | "a tailnet host": {u("https://box.example.ts.net/"), "host_refused"}, |
| 356 | "a secret in the path": {u("https://shop.example.com/ghp_abcdefghijklmnopqrstuvwxyz0123"), "secret_refused"}, |
| 357 | "a long path": {u("https://shop.example.com/" + strings.Repeat("a", 101)), "path_too_long"}, |
| 358 | "another scheme": {u("ftp://shop.example.com/"), "scheme_refused"}, |
| 359 | "eleven URLs": {u(eleven...), "too_many_urls"}, |
| 360 | "an unknown field": {map[string]any{"task": id, "question": "Colours?", "urls": []string{}, "depth": 2}, "invalid"}, |
| 361 | "no such task": {map[string]any{"task": "tk_nothing", "question": "Colours?", "urls": []string{}}, "not_found"}, |
| 362 | "urls missing, not empty": {map[string]any{"task": id, "question": "Colours?", "urls": "https://shop.example.com/"}, "invalid"}, |
| 363 | } { |
| 364 | res := r.call("e-"+strings.ReplaceAll(name, " ", "-"), wire.ToolResearch, c.args) |
| 365 | if !res.IsError || res.Code != c.code || res.Text == "" { |
| 366 | t.Errorf("%s: %+v", name, res) |
| 367 | } |
| 368 | } |
| 369 | // Another owner's coordinator may read the task but not research it. |
| 370 | b := personToolRigAt(t, r, "second@example.com", equipo.RoleOwner, wire.ProtocolCurrent) |
| 371 | if res := b.research("e-b", id); !res.IsError || res.Code != "not_yours" { |
| 372 | t.Fatalf("a teammate's turn %+v", res) |
| 373 | } |
| 374 | if n := len(stepsOf(r.task(id), wire.SpecialistResearch)); n != 0 || r.sourcesCards() != 0 || r.rows("SELECT count(*) FROM chat_research_sources") != 0 { |
| 375 | t.Fatal("a refused request left something") |
| 376 | } |
| 377 | // A task with no URL in the person's words and none proposed has |
| 378 | // nothing to read. |
| 379 | own := b.shopTask("e-own") |
| 380 | if res := b.research("e-none", own); !res.IsError || res.Code != "no_sources" { |
| 381 | t.Fatalf("nothing to read %+v", res) |
| 382 | } |
| 383 | // An ended task. |
| 384 | if _, err := r.client.CancelTask(r.ctx, id); err != nil { |
| 385 | t.Fatal(err) |
| 386 | } |
| 387 | if res := r.research("e-ended", id); !res.IsError || res.Code != "task_ended" { |
| 388 | t.Fatalf("an ended task %+v", res) |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // TestResearchSourcesCard: the person's own words (their message, not a |
| 393 | // report the coordinator added) are read without asking; the coordinator's |
| 394 | // proposals raise one card with every URL in full, bound to the step and |
| 395 | // that list; a stale revision, another person or another answer is |
| 396 | // refused; the step waits for the card at its fixed point, and Allow |
| 397 | // sends every source. |
| 398 | func TestResearchSourcesCard(t *testing.T) { |
| 399 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 400 | r.researchOn() |
| 401 | id := r.shopTask("f") |
| 402 | if res := r.call("f-rep", wire.ToolAddReport, map[string]string{"task": id, "text": "And the prices, on shop.example.com/prices."}); res.IsError { |
| 403 | t.Fatalf("report %+v", res) |
| 404 | } |
| 405 | proposed := []string{"https://maps.example.org/place/barber-shop-on-main-street", "https://shop.example.com/hours", "https://www.example.net/reviews/barber-shop"} |
| 406 | if res := r.research("f-r", id, proposed...); res.IsError { |
| 407 | t.Fatalf("request %+v", res) |
| 408 | } |
| 409 | card := r.sourcesCard(wire.CardOpen) |
| 410 | s := r.newest(id, wire.SpecialistResearch) |
| 411 | v := card.Research |
| 412 | if v == nil || v.StepID != s.ID || card.TaskID != id || card.Question == "" || v.Question != "Its colours, logo and opening hours." || !slices.Equal(card.Options, []string{wire.ChoiceAllow, wire.ChoiceSkip}) { |
| 413 | t.Fatalf("card %+v %+v", card, v) |
| 414 | } |
| 415 | // The person's own hours page needs no asking; the two proposals are |
| 416 | // shown exactly as they will be requested. |
| 417 | if !slices.Equal(v.URLs, []string{"https://maps.example.org/place/barber-shop-on-main-street", "https://www.example.net/reviews/barber-shop"}) { |
| 418 | t.Fatalf("card URLs %v", v.URLs) |
| 419 | } |
| 420 | if r.sourcesCards() != 1 { |
| 421 | t.Fatal("more than one card") |
| 422 | } |
| 423 | // The conversation still renders with the card in it (slice R5 draws it). |
| 424 | r.get(r.inst.SessionFor(t, Owner), "/conversation", 200) |
| 425 | // The report the coordinator added is its own text, not the person's |
| 426 | // (Astra security finding 1): its prices page is not a source. |
| 427 | if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE step_id = ? AND origin = ? AND approved = 1", s.ID, wire.ResearchOriginPerson); n != 2 { |
| 428 | t.Fatalf("%d of the person's sources", n) |
| 429 | } |
| 430 | if n := r.rows("SELECT count(*) FROM chat_research_sources WHERE step_id = ? AND origin = ? AND approved = 0", s.ID, wire.ResearchOriginProposed); n != 2 { |
| 431 | t.Fatalf("%d proposals waiting", n) |
| 432 | } |
| 433 | for _, rev := range []int64{card.Revision + 1, card.Revision - 1} { |
| 434 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAllow, rev); Status(err) != 409 { |
| 435 | t.Fatalf("revision %d: %v", rev, err) |
| 436 | } |
| 437 | } |
| 438 | if _, err := r.client.Answer(r.ctx, card.ID, "maybe", card.Revision); Status(err) != 422 { |
| 439 | t.Fatalf("another answer: %v", err) |
| 440 | } |
| 441 | b := personToolRigAt(t, r, "second@example.com", equipo.RoleOwner, wire.ProtocolCurrent) |
| 442 | if _, err := b.client.Answer(r.ctx, card.ID, wire.ChoiceAllow, card.Revision); Status(err) != 404 { |
| 443 | t.Fatalf("a teammate answered: %v", err) |
| 444 | } |
| 445 | r.inventoryOn() |
| 446 | r.begin("f-start", id) |
| 447 | r.heldForResearch(id) |
| 448 | if s := r.newest(id, wire.SpecialistResearch); s.Placement != wire.PlacementSources || r.researchDispatches() != 0 { |
| 449 | t.Fatalf("at the start before the answer %+v", s) |
| 450 | } |
| 451 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAllow, card.Revision); err != nil { |
| 452 | t.Fatal(err) |
| 453 | } |
| 454 | if done := r.sourcesCard(wire.CardAnswered); done.Choice != wire.ChoiceAllow { |
| 455 | t.Fatalf("answered %+v", done) |
| 456 | } |
| 457 | d := r.nextDispatch() |
| 458 | if d.Specialist != wire.SpecialistResearch || d.Research == nil { |
| 459 | t.Fatalf("after Allow %+v", d) |
| 460 | } |
| 461 | var urls, origins []string |
| 462 | for _, src := range d.Research.Sources { |
| 463 | urls, origins = append(urls, src.URL), append(origins, src.Origin) |
| 464 | } |
| 465 | if !slices.Equal(urls, []string{"https://shop.example.com/", "https://shop.example.com/hours", "https://maps.example.org/place/barber-shop-on-main-street", "https://www.example.net/reviews/barber-shop"}) || !slices.Equal(origins, []string{"person", "person", "proposed", "proposed"}) { |
| 466 | t.Fatalf("sources %v %v", urls, origins) |
| 467 | } |
| 468 | if !slices.Equal(d.Research.PersonWords, []string{personWords}) { |
| 469 | t.Fatalf("person words %v", d.Research.PersonWords) |
| 470 | } |
| 471 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAllow, card.Revision); Status(err) != 409 { |
| 472 | t.Fatalf("answered twice: %v", err) |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // TestResearchCardOwner: only the card's person answers it, and only |
| 477 | // while they are an owner, read in the answer's transaction. |
| 478 | func TestResearchCardOwner(t *testing.T) { |
| 479 | a := newToolRigAt(t, wire.ProtocolCurrent) |
| 480 | a.researchOn() |
| 481 | b := personToolRigAt(t, a, "second@example.com", equipo.RoleOwner, wire.ProtocolCurrent) |
| 482 | id := b.shopTask("g") |
| 483 | if res := b.research("g-r", id, "https://maps.example.org/place/shop"); res.IsError { |
| 484 | t.Fatalf("B's request %+v", res) |
| 485 | } |
| 486 | card := b.sourcesCard(wire.CardOpen) |
| 487 | if _, err := a.client.Answer(a.ctx, card.ID, wire.ChoiceAllow, card.Revision); Status(err) != 404 { |
| 488 | t.Fatalf("another person answered: %v", err) |
| 489 | } |
| 490 | var revision int64 |
| 491 | if err := a.inst.DB.Writer().QueryRow("SELECT revision FROM members WHERE address = ?", "second@example.com").Scan(&revision); err != nil { |
| 492 | t.Fatal(err) |
| 493 | } |
| 494 | if err := a.inst.Store.SetMember(a.ctx, equipo.Actor{Address: Owner}, "second@example.com", equipo.RoleMember, true, revision); err != nil { |
| 495 | t.Fatal(err) |
| 496 | } |
| 497 | if _, err := b.client.Answer(a.ctx, card.ID, wire.ChoiceAllow, card.Revision); Status(err) != 403 { |
| 498 | t.Fatalf("a teammate allowed it: %v", err) |
| 499 | } |
| 500 | if a.rows("SELECT count(*) FROM chat_research_sources WHERE approved = 1") != 0 { |
| 501 | t.Fatal("approved without an owner") |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // TestResearchSkipWithNothingLeft: Skip drops the proposals; with none of |
| 506 | // the person's own, the step ends done with a gap and Engineering goes. |
| 507 | func TestResearchSkipWithNothingLeft(t *testing.T) { |
| 508 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 509 | r.researchOn() |
| 510 | r.inventoryOn() |
| 511 | id := r.shopTask("h") |
| 512 | r.research("h-r", id, "https://maps.example.org/place/shop") |
| 513 | card := r.sourcesCard(wire.CardOpen) |
| 514 | r.begin("h-start", id) |
| 515 | r.heldForResearch(id) |
| 516 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceSkip, card.Revision); err != nil { |
| 517 | t.Fatal(err) |
| 518 | } |
| 519 | e := r.nextDispatch() |
| 520 | if e.Specialist != wire.SpecialistEngineering { |
| 521 | t.Fatalf("after Skip %+v", e) |
| 522 | } |
| 523 | s := r.newest(id, wire.SpecialistResearch) |
| 524 | if s.State != wire.StepDone || s.Reason != wire.ReasonResearchNoSources || r.researchDispatches() != 0 { |
| 525 | t.Fatalf("Research after Skip %+v", s) |
| 526 | } |
| 527 | if r.rows("SELECT count(*) FROM chat_research_sources WHERE approved = 1") != 0 { |
| 528 | t.Fatal("Skip approved something") |
| 529 | } |
| 530 | if done := r.sourcesCard(wire.CardAnswered); done.Choice != wire.ChoiceSkip { |
| 531 | t.Fatalf("card %+v", done) |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // TestResearchRevokeCancelsWaiting: revoking Research cancels its steps |
| 536 | // that have not run, withdraws their cards, and releases Engineering. |
| 537 | func TestResearchRevokeCancelsWaiting(t *testing.T) { |
| 538 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 539 | r.researchOn() |
| 540 | id := r.shopTask("i") |
| 541 | r.research("i-r", id, "https://maps.example.org/place/shop") |
| 542 | card := r.sourcesCard(wire.CardOpen) |
| 543 | r.begin("i-start", id) |
| 544 | r.heldForResearch(id) |
| 545 | list, err := r.client.Specialists(r.ctx) |
| 546 | if err != nil || len(list.Items) != 1 { |
| 547 | t.Fatalf("%+v %v", list, err) |
| 548 | } |
| 549 | if _, err := r.client.RevokeSpecialist(r.ctx, list.Items[0].ID, list.Items[0].Revision); err != nil { |
| 550 | t.Fatal(err) |
| 551 | } |
| 552 | if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering { |
| 553 | t.Fatalf("after revoke %+v", e) |
| 554 | } |
| 555 | if s := r.newest(id, wire.SpecialistResearch); s.State != wire.StepCancelled || s.Reason != wire.ReasonResearchNoGrant || !s.Terminated { |
| 556 | t.Fatalf("Research after revoke %+v", s) |
| 557 | } |
| 558 | gone := r.sourcesCard(wire.CardResolved) |
| 559 | if gone.ID != card.ID { |
| 560 | t.Fatalf("withdrawn %+v", gone) |
| 561 | } |
| 562 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAllow, gone.Revision); Status(err) != 409 { |
| 563 | t.Fatalf("answered a withdrawn card: %v", err) |
| 564 | } |
| 565 | if r.researchDispatches() != 0 { |
| 566 | t.Fatal("dispatched after revoke") |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | // TestResearchOlderPrimary: a primary at protocol 11 gets the update |
| 571 | // message for research, and nothing is requested. |
| 572 | func TestResearchOlderPrimary(t *testing.T) { |
| 573 | r := newToolRigSaying(t, wire.ProtocolPreviewHost, personWords) |
| 574 | id := r.shopTask("j") |
| 575 | res := r.research("j-r", id) |
| 576 | if !res.IsError || res.Code != "research_unsupported" || res.Text != "Equipo needs an update before it can research." { |
| 577 | t.Fatalf("at 11: %+v", res) |
| 578 | } |
| 579 | if len(stepsOf(r.task(id), wire.SpecialistResearch)) != 0 || r.researchDispatches() != 0 { |
| 580 | t.Fatal("requested at 11") |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // TestResearchFencedOnAnOlderPrimary (mixed version): a Research step |
| 585 | // dispatched at 13 whose primary comes back at 12 (metered connections, |
| 586 | // no Research) or 11 is cancelled research_unsupported, its dispatch is |
| 587 | // never delivered there, and Engineering is released to the older |
| 588 | // runner. |
| 589 | func TestResearchFencedOnAnOlderPrimary(t *testing.T) { |
| 590 | for _, back := range []int{wire.Protocol12, wire.Protocol11} { |
| 591 | t.Run(itoa(back), func(t *testing.T) { researchFencedAt(t, back) }) |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | func researchFencedAt(t *testing.T, back int) { |
| 596 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 597 | r.researchOn() |
| 598 | r.inventoryOn() |
| 599 | id := r.shopTask("k") |
| 600 | r.research("k-r", id) |
| 601 | r.begin("k-start", id) |
| 602 | if d := r.nextDispatch(); d.Specialist != wire.SpecialistResearch { |
| 603 | t.Fatalf("%+v", d) |
| 604 | } |
| 605 | old, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, primaryAt(wire.Hello{Generation: r.runner.Welcome.Generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}, back)) |
| 606 | if err != nil { |
| 607 | t.Fatal(err) |
| 608 | } |
| 609 | defer old.Close() |
| 610 | // The copy the runner may hold is cancelled; then Engineering arrives. |
| 611 | var body any |
| 612 | for { |
| 613 | f, b, err := old.Next(r.ctx) |
| 614 | if err != nil { |
| 615 | t.Fatal(err) |
| 616 | } |
| 617 | if f.Type == wire.FrameDispatch { |
| 618 | body = b |
| 619 | break |
| 620 | } |
| 621 | } |
| 622 | if e := body.(*wire.DispatchCommand); e.Specialist != wire.SpecialistEngineering { |
| 623 | t.Fatalf("to the older runner %+v", e) |
| 624 | } |
| 625 | for _, raw := range old.Received { |
| 626 | if strings.Contains(string(raw), `"specialist":"research"`) { |
| 627 | t.Fatalf("a research dispatch reached protocol %d: %s", back, raw) |
| 628 | } |
| 629 | } |
| 630 | if s := r.newest(id, wire.SpecialistResearch); s.State != wire.StepCancelled || s.Reason != wire.ReasonResearchUnsupported { |
| 631 | t.Fatalf("fenced %+v", s) |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // TestResearchCardBindsTheList: Allow approves exactly the URLs the card |
| 636 | // showed. If the step's proposals no longer match them, the answer is a |
| 637 | // conflict and nothing is approved. |
| 638 | func TestResearchCardBindsTheList(t *testing.T) { |
| 639 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 640 | r.researchOn() |
| 641 | id := r.shopTask("l") |
| 642 | r.research("l-r", id, "https://maps.example.org/place/shop", "https://www.example.net/reviews/shop") |
| 643 | card := r.sourcesCard(wire.CardOpen) |
| 644 | s := r.newest(id, wire.SpecialistResearch) |
| 645 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_research_sources SET url = ? WHERE step_id = ? AND idx = 2", "https://www.example.net/other", s.ID); err != nil { |
| 646 | t.Fatal(err) |
| 647 | } |
| 648 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAllow, card.Revision); Status(err) != 409 { |
| 649 | t.Fatalf("Allow on a changed list: %v", err) |
| 650 | } |
| 651 | if r.rows("SELECT count(*) FROM chat_research_sources WHERE approved = 1") != 0 { |
| 652 | t.Fatal("approved a list the card did not show") |
| 653 | } |
| 654 | if _, err := r.inst.DB.Writer().Exec("UPDATE chat_research_sources SET url = ? WHERE step_id = ? AND idx = 2", "https://www.example.net/reviews/shop", s.ID); err != nil { |
| 655 | t.Fatal(err) |
| 656 | } |
| 657 | // Allowed before the task starts, it still waits for its fixed point. |
| 658 | r.inventoryOn() |
| 659 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAllow, card.Revision); err != nil { |
| 660 | t.Fatal(err) |
| 661 | } |
| 662 | if r.rows("SELECT count(*) FROM chat_research_sources WHERE step_id = ? AND approved = 1", s.ID) != 2 { |
| 663 | t.Fatal("Allow did not approve the list") |
| 664 | } |
| 665 | if s := r.newest(id, wire.SpecialistResearch); s.State != wire.StepQueued || s.Placement != wire.PlacementBoundary || r.researchDispatches() != 0 { |
| 666 | t.Fatalf("Allow started it before the task's start %+v", s) |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // TestResearchGrantCheckedAtDispatch: the grant is checked again when a |
| 671 | // step reaches dispatch: its approver is no longer an owner, so the step |
| 672 | // ends research_no_grant and Engineering goes. |
| 673 | func TestResearchGrantCheckedAtDispatch(t *testing.T) { |
| 674 | a := newToolRigAt(t, wire.ProtocolCurrent) |
| 675 | a.researchOn() |
| 676 | a.inventoryOn() |
| 677 | b := personToolRigAt(t, a, "second@example.com", equipo.RoleOwner, wire.ProtocolCurrent) |
| 678 | id := b.shopTask("m") |
| 679 | if res := b.research("m-r", id, "https://maps.example.org/place/shop"); res.IsError { |
| 680 | t.Fatalf("request %+v", res) |
| 681 | } |
| 682 | card := b.sourcesCard(wire.CardOpen) |
| 683 | if _, err := b.client.Answer(b.ctx, card.ID, wire.ChoiceAllow, card.Revision); err != nil { |
| 684 | t.Fatal(err) |
| 685 | } |
| 686 | var revision int64 |
| 687 | if err := a.inst.DB.Writer().QueryRow("SELECT revision FROM members WHERE address = ?", Owner).Scan(&revision); err != nil { |
| 688 | t.Fatal(err) |
| 689 | } |
| 690 | if err := a.inst.Store.SetMember(a.ctx, equipo.Actor{Address: "second@example.com"}, Owner, equipo.RoleMember, true, revision); err != nil { |
| 691 | t.Fatal(err) |
| 692 | } |
| 693 | b.begin("m-start", id) |
| 694 | if e := b.nextDispatch(); e.Specialist != wire.SpecialistEngineering { |
| 695 | t.Fatalf("after the grant lapsed %+v", e) |
| 696 | } |
| 697 | if s := b.newest(id, wire.SpecialistResearch); s.State != wire.StepCancelled || s.Reason != wire.ReasonResearchNoGrant { |
| 698 | t.Fatalf("Research without a grant %+v", s) |
| 699 | } |
| 700 | if b.researchDispatches() != 0 { |
| 701 | t.Fatal("dispatched without a grant") |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // TestResearchNeverDispatchedToAnOlderPrimary (mixed version): a step |
| 706 | // that reaches dispatch while its primary speaks 11 is never sent: it |
| 707 | // ends research_unsupported and Engineering goes to that runner. |
| 708 | func TestResearchNeverDispatchedToAnOlderPrimary(t *testing.T) { |
| 709 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 710 | r.researchOn() |
| 711 | r.inventoryOn() |
| 712 | id := r.shopTask("n") |
| 713 | r.research("n-r", id, "https://maps.example.org/place/shop") |
| 714 | card := r.sourcesCard(wire.CardOpen) |
| 715 | r.begin("n-start", id) |
| 716 | r.heldForResearch(id) |
| 717 | old, err := DialRunnerHello(r.ctx, r.inst.URL, r.rig.runner, primaryAt(wire.Hello{Generation: r.runner.Welcome.Generation, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}}, wire.ProtocolPreviewHost)) |
| 718 | if err != nil { |
| 719 | t.Fatal(err) |
| 720 | } |
| 721 | defer old.Close() |
| 722 | r.wait("the primary at 11", func(c *Conversation) bool { return c.Runner.Online }) |
| 723 | if _, err := r.client.Answer(r.ctx, card.ID, wire.ChoiceAllow, card.Revision); err != nil { |
| 724 | t.Fatal(err) |
| 725 | } |
| 726 | f, body, err := old.Expect(r.ctx, wire.FrameDispatch) |
| 727 | if err != nil { |
| 728 | t.Fatal(err) |
| 729 | } |
| 730 | if e := body.(*wire.DispatchCommand); f.Type != wire.FrameDispatch || e.Specialist != wire.SpecialistEngineering { |
| 731 | t.Fatalf("to the older runner %+v", e) |
| 732 | } |
| 733 | if s := r.newest(id, wire.SpecialistResearch); s.State != wire.StepCancelled || s.Reason != wire.ReasonResearchUnsupported { |
| 734 | t.Fatalf("at 11 %+v", s) |
| 735 | } |
| 736 | if r.researchDispatches() != 0 { |
| 737 | t.Fatal("a research dispatch was written for a primary at 11") |
| 738 | } |
| 739 | } |
| 740 | |