| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // What a job keeps track of past its first task: the change an |
| 16 | // exploration it made becomes, a blocker only the person can clear |
| 17 | // before work starts, a preview that goes, every open question, and its |
| 18 | // card in a long Assistant lane. |
| 19 | |
| 20 | // TestContractHandoffExploreBuildFollowed: Build on a job's exploration |
| 21 | // makes a change the job follows, as work it made, in the answer's own |
| 22 | // transaction: the job works on while the change is built (never done), |
| 23 | // team_status tells of it, a failed build step is the job's to push |
| 24 | // through, and team_cancel stops it. |
| 25 | func TestContractHandoffExploreBuildFollowed(t *testing.T) { |
| 26 | o := newTeamRig(t) |
| 27 | o.ask("Make me some mockups for the barber app") |
| 28 | request, cmd := o.handoff("xb1", "mockups for the barber app", "") |
| 29 | var id string |
| 30 | o.in(cmd.TurnID, func() { |
| 31 | id, _ = o.explored("xb", "Barber app mockups", candX) |
| 32 | o.approved("xb", candX, true) |
| 33 | }) |
| 34 | var card wire.Card |
| 35 | o.until("the direction card", func() bool { |
| 36 | card = o.jobCard(request, wire.CardDirection, id) |
| 37 | return card.State == wire.CardOpen |
| 38 | }) |
| 39 | if _, err := o.client.Answer(o.ctx, card.ID, wire.ChoiceBuild, card.Revision); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | d := o.nextDispatch() |
| 43 | build := d.TaskID |
| 44 | if d.Specialist != wire.SpecialistEngineering || build == id { |
| 45 | t.Fatalf("the build's dispatch %+v", d) |
| 46 | } |
| 47 | if linked := o.linked(request); len(linked) != 2 || !linked[id] || !linked[build] { |
| 48 | t.Fatalf("the job follows %v, not the exploration %s and its build %s", linked, id, build) |
| 49 | } |
| 50 | building, _ := jobStatus("building") |
| 51 | job := o.jobOf(request, "building", func(c wire.AssistantCard) bool { return c.State == wire.JobWorking && c.Result == building }) |
| 52 | for _, e := range o.jobEvents(job.ID) { |
| 53 | if e.State == wire.JobDone { |
| 54 | t.Fatalf("the job said done while its build ran: %+v", e) |
| 55 | } |
| 56 | } |
| 57 | if got := o.status("xb-st", map[string]any{"request": request}); len(got) != 1 || !strings.Contains(got[0].Summary, equipo.RunnerCopy("assistant.team.stage."+wire.StageInProgress)) { |
| 58 | t.Fatalf("team_status %+v", got) |
| 59 | } |
| 60 | // Its build fails: the job's to push through. |
| 61 | o.step("xb-fail", wire.StepReport{TaskID: build, StepID: d.StepID, Attempt: 1, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 62 | o.noteFor("the failed note", failedStep(request, 2)) |
| 63 | o.reply(o.turn) |
| 64 | u := o.message(wire.LaneAssistant) |
| 65 | o.in(u.TurnID, func() { nudged(t, o.teamNudge("xb-nudge", request, "mockups for the barber app"), request, 1) }) |
| 66 | _, body := o.expect(wire.FrameDispatch) |
| 67 | if again := body.(*wire.DispatchCommand); again.TaskID != build || again.StepID != d.StepID || again.Attempt != 2 { |
| 68 | t.Fatalf("the retry's dispatch %+v", again) |
| 69 | } |
| 70 | o.reply(u.TurnID) |
| 71 | // team_cancel stops the build. |
| 72 | o.ask("Stop the barber app") |
| 73 | if res := o.use("xb-cancel", "team_cancel", map[string]any{"request": request}); res.IsError || res.Text != `{"cancelled":1,"still_running":0}` { |
| 74 | t.Fatalf("cancel %+v", res) |
| 75 | } |
| 76 | if n := o.rows("SELECT count(*) FROM chat_tasks WHERE id = ? AND (cancel_requested = 1 OR stage = ?)", build, wire.StageCancelled); n != 1 { |
| 77 | t.Fatalf("the build goes on after team_cancel: %+v", o.task(build)) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // stoppedOnResearch hands the barber shop's mockups to the team, whose |
| 82 | // thread records a task and asks for research with a source the person |
| 83 | // must approve, before any work starts: once the Assistant's team_update |
| 84 | // about it ends (the job card working until then), the job is stopped. |
| 85 | // It returns the request, the task and the stopped job card. |
| 86 | func (o *teamRig) stoppedOnResearch() (request, id string, stopped wire.AssistantCard) { |
| 87 | t := o.t |
| 88 | t.Helper() |
| 89 | o.in(o.own("Bring in research"), func() { o.researchOn() }) |
| 90 | o.ask(personWords) |
| 91 | request, cmd := o.handoff("rb1", "Make mockups for the barber shop.", "") |
| 92 | o.in(cmd.TurnID, func() { |
| 93 | id = o.shopTask("rb") |
| 94 | if res := o.research("rb-r", id, "https://maps.example.org/place/barber-shop-on-main-street"); res.IsError { |
| 95 | t.Fatalf("research %+v", res) |
| 96 | } |
| 97 | }) |
| 98 | o.reply(cmd.TurnID) |
| 99 | o.noteFor("the blocked note", blockedOn("they need to approve the research")) |
| 100 | o.reply(o.turn) |
| 101 | u := o.message(wire.LaneAssistant) |
| 102 | if u.Kind != wire.TurnTeamUpdate { |
| 103 | t.Fatalf("update frame %+v", u) |
| 104 | } |
| 105 | if now := o.jobOf(request, "the job card", nil); now.State != wire.JobWorking { |
| 106 | t.Fatalf("while the Assistant's turn runs %+v", now) |
| 107 | } |
| 108 | o.reply(u.TurnID) |
| 109 | stopped = o.jobOf(request, "stopped", func(c wire.AssistantCard) bool { return c.State == wire.JobStopped }) |
| 110 | return request, id, stopped |
| 111 | } |
| 112 | |
| 113 | // TestContractTeamJobStoppedBeforeWork (D5, D6, ruling P5j): a job whose |
| 114 | // research waits for the person to approve its sources before any work |
| 115 | // starts is stopped, as its note says, once the Assistant's team_update |
| 116 | // about it ends, and not before; working again once they approve. |
| 117 | func TestContractTeamJobStoppedBeforeWork(t *testing.T) { |
| 118 | o := newTeamRig(t) |
| 119 | request, id, stopped := o.stoppedOnResearch() |
| 120 | if status, detail := jobWords(wire.JobStopped); stopped.Result != status || stopped.Detail != detail { |
| 121 | t.Fatalf("stopped %+v", stopped) |
| 122 | } |
| 123 | card := o.jobCard(request, wire.CardResearchSources, id) |
| 124 | if _, err := o.client.Answer(o.ctx, card.ID, wire.ChoiceAllow, card.Revision); err != nil { |
| 125 | t.Fatal(err) |
| 126 | } |
| 127 | o.jobOf(request, "working once approved", func(c wire.AssistantCard) bool { return c.State == wire.JobWorking }) |
| 128 | } |
| 129 | |
| 130 | // TestContractTeamJobPreviewExpires: a preview that expires while its |
| 131 | // go-ahead waits is offered no more, with nothing else changed: a fresh |
| 132 | // snapshot drops "Try it" from the job card at once, and the minute's |
| 133 | // tick moves the job card and its go-ahead, once each, so the stream |
| 134 | // drops it too. |
| 135 | func TestContractTeamJobPreviewExpires(t *testing.T) { |
| 136 | o := newTeamRigAt(t, wire.ProtocolCurrent) |
| 137 | o.sitesOn(wire.ProtocolCurrent, false) |
| 138 | o.ask("Can we fix a bug in Tito? Login is broken") |
| 139 | request, cmd := o.handoff("pe1", "Login is broken", "") |
| 140 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 141 | var id string |
| 142 | o.in(cmd.TurnID, func() { id, _ = o.websiteReady("pe", "Quick website", base, cand, true) }) |
| 143 | pv := o.previewDispatch(cand) |
| 144 | o.built("pe-pv-built", pv, digestA) |
| 145 | expires := time.Now().Add(time.Hour) |
| 146 | link := "https://eq-" + id + ".docs-t1.example.ts.net/" |
| 147 | o.previewDone("pe-pv-done", pv, wire.PreviewResult{Phase: wire.PreviewDone, App: "eq-" + id, URL: link, Digest: digestA, ExpiresAt: &expires, Outcome: wire.PreviewDeployed}) |
| 148 | ready := o.jobOf(request, "ready to try", func(c wire.AssistantCard) bool { return c.State == wire.JobReady && len(c.Links) == 2 }) |
| 149 | if !slices.Equal(ready.Links, []wire.CardLink{{Label: equipo.RunnerCopy("assistant.outcome.preview"), URL: link}, o.equipoLink(request)}) { |
| 150 | t.Fatalf("ready %+v", ready) |
| 151 | } |
| 152 | ahead := o.assistantCard("the go-ahead", func(c wire.AssistantCard) bool { return isMergeOutcome(c) && len(c.Links) == 1 }) |
| 153 | jobs, aheads := len(o.jobEvents(ready.ID)), len(o.jobEvents(ahead.ID)) |
| 154 | o.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC().Add(2 * time.Hour) }) |
| 155 | t.Cleanup(func() { o.inst.Store.Chat().SetClock(func() time.Time { return time.Now().UTC() }) }) |
| 156 | if got := o.jobOf(request, "expired", nil); got.State != wire.JobReady || !slices.Equal(got.Links, []wire.CardLink{o.equipoLink(request)}) { |
| 157 | t.Fatalf("a fresh snapshot after the preview expired %+v", got) |
| 158 | } |
| 159 | for range 2 { |
| 160 | if err := o.inst.Store.Chat().HousekeepingTick(o.ctx); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | } |
| 164 | job, mirror := o.jobEvents(ready.ID), o.jobEvents(ahead.ID) |
| 165 | if len(job) != jobs+1 || job[len(job)-1].Revision <= ready.Revision || !slices.Equal(job[len(job)-1].Links, []wire.CardLink{o.equipoLink(request)}) { |
| 166 | t.Fatalf("job card events after the preview expired %+v (ready %+v)", job[jobs:], ready) |
| 167 | } |
| 168 | if len(mirror) != aheads+1 || mirror[len(mirror)-1].Revision <= ahead.Revision || len(mirror[len(mirror)-1].Links) != 0 || mirror[len(mirror)-1].State != wire.CardOpen { |
| 169 | t.Fatalf("go-ahead events after the preview expired %+v (was %+v)", mirror[aheads:], ahead) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | // TestContractTeamJobSnapshotKeepsActiveJobs (ruling R27): an unfinished |
| 174 | // job (one stopped on a research approval too) and the go-ahead open |
| 175 | // under one stay in the Assistant's snapshot however many newer cards |
| 176 | // there are, oldest first as ever; a cancelled job older than the newest |
| 177 | // cards does not. |
| 178 | func TestContractTeamJobSnapshotKeepsActiveJobs(t *testing.T) { |
| 179 | o := newTeamRig(t) |
| 180 | _, _, research := o.stoppedOnResearch() |
| 181 | o.ask("Can we fix a bug in Tito? Login is broken") |
| 182 | request, cmd := o.handoff("sk1", "Login is broken", "") |
| 183 | base, cand := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 184 | o.in(cmd.TurnID, func() { o.websiteReady("sk", "Quick website", base, cand, false) }) |
| 185 | job := o.jobOf(request, "ready", func(c wire.AssistantCard) bool { return c.State == wire.JobReady }) |
| 186 | ahead := o.assistantCard("the go-ahead", isMergeOutcome) |
| 187 | cancelled, _ := o.handoff("sk2", "Can we fix a bug", "") |
| 188 | if res := o.use("sk-cancel", "team_cancel", map[string]any{"request": cancelled}); res.IsError { |
| 189 | t.Fatalf("cancel %+v", res) |
| 190 | } |
| 191 | stopped := o.jobOf(cancelled, "stopped", func(c wire.AssistantCard) bool { return c.State == wire.JobStopped }) |
| 192 | newer := time.Now().UTC().Add(time.Minute) |
| 193 | for i := range wire.MaxProjectCards { |
| 194 | o.exec("INSERT INTO chat_cards(id, person, kind, state, question, options, links, choice, revision, created_at, updated_at, held, lane) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)", |
| 195 | "c_newer"+itoa(i), Owner, wire.CardQuestion, wire.CardAnswered, "Which one?", "[]", "[]", "This one", 2, newer.Add(time.Duration(i)*time.Second), newer, false, wire.LaneAssistant) |
| 196 | } |
| 197 | snap, err := o.a.Snapshot(o.ctx) |
| 198 | if err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | if n := len(snap.Cards); n != wire.MaxProjectCards+3 || snap.Cards[0].ID != research.ID || snap.Cards[1].ID != job.ID || snap.Cards[2].ID != ahead.ID || snap.Cards[n-1].ID != "c_newer"+itoa(wire.MaxProjectCards-1) { |
| 202 | t.Fatalf("%d cards, the first %+v %+v %+v; the stopped job %s, the job %s and its go-ahead %s", n, snap.Cards[0], snap.Cards[1], snap.Cards[2], research.ID, job.ID, ahead.ID) |
| 203 | } |
| 204 | if slices.ContainsFunc(snap.Cards, func(c wire.AssistantCard) bool { return c.ID == stopped.ID }) { |
| 205 | t.Fatalf("a cancelled job beyond the newest cards %+v", stopped) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // statusPage is one team_status answer: its requests, each with its |
| 210 | // question, and where the next page starts ("" on the last). |
| 211 | type statusPage struct { |
| 212 | Requests []struct { |
| 213 | Request string `json:"request"` |
| 214 | Question *struct { |
| 215 | Text string `json:"text"` |
| 216 | } `json:"question"` |
| 217 | } `json:"requests"` |
| 218 | Next string `json:"next"` |
| 219 | } |
| 220 | |
| 221 | // TestContractTeamElevenQuestions (D8): with eleven jobs asking, the |
| 222 | // person's next message carries the newest ten questions and says one |
| 223 | // more is open and how to read it; team_status reads every open |
| 224 | // request, ten at a time, each question with its request id, the next |
| 225 | // page after the last one's cursor. |
| 226 | func TestContractTeamElevenQuestions(t *testing.T) { |
| 227 | o := newTeamRig(t) |
| 228 | words := make([]string, 11) |
| 229 | for i := range words { |
| 230 | words[i] = fmt.Sprintf("job number %02d", i+1) |
| 231 | } |
| 232 | o.ask(strings.Join(words, ", ")) |
| 233 | asked := map[string]string{} |
| 234 | var requests []string |
| 235 | for i, w := range words { |
| 236 | request, cmd := o.handoff("eq"+itoa(i), w, "") |
| 237 | requests = append(requests, request) |
| 238 | asked[request] = "Which page for " + w + "?" |
| 239 | o.in(cmd.TurnID, func() { |
| 240 | if res := o.call("eq-ask"+itoa(i), wire.ToolAskPerson, map[string]any{"question": asked[request]}); res.Code != "asked" { |
| 241 | t.Fatalf("ask %+v", res) |
| 242 | } |
| 243 | }) |
| 244 | } |
| 245 | page := func(key string, in map[string]any) statusPage { |
| 246 | t.Helper() |
| 247 | res := o.use(key, "team_status", in) |
| 248 | var out statusPage |
| 249 | if res.IsError || json.Unmarshal([]byte(res.Text), &out) != nil { |
| 250 | t.Fatalf("team_status %+v", res) |
| 251 | } |
| 252 | return out |
| 253 | } |
| 254 | first := page("eq-st1", map[string]any{}) |
| 255 | second := page("eq-st2", map[string]any{"after": first.Next}) |
| 256 | if len(first.Requests) != 10 || first.Next != first.Requests[9].Request || len(second.Requests) != 1 || second.Next != "" { |
| 257 | t.Fatalf("pages %+v then %+v", first, second) |
| 258 | } |
| 259 | seen := map[string]bool{} |
| 260 | for _, r := range append(first.Requests, second.Requests...) { |
| 261 | if r.Question == nil || r.Question.Text != asked[r.Request] || seen[r.Request] { |
| 262 | t.Fatalf("request %s with %+v", r.Request, r.Question) |
| 263 | } |
| 264 | seen[r.Request] = true |
| 265 | } |
| 266 | if second.Requests[0].Request != requests[0] { |
| 267 | t.Fatalf("the second page %+v, not the oldest %s", second, requests[0]) |
| 268 | } |
| 269 | if res := o.use("eq-st3", "team_status", map[string]any{"after": "h_nosuchrequest"}); !res.IsError || res.Code != "not_found" { |
| 270 | t.Fatalf("after an unknown request %+v", res) |
| 271 | } |
| 272 | // Every team_update told, the person's next message carries the |
| 273 | // newest ten and the count of the rest. |
| 274 | o.reply(o.turn) |
| 275 | for range words { |
| 276 | u := o.message(wire.LaneAssistant) |
| 277 | if u.Kind != wire.TurnTeamUpdate { |
| 278 | t.Fatalf("update frame %+v", u) |
| 279 | } |
| 280 | o.reply(u.TurnID) |
| 281 | } |
| 282 | sent, err := o.a.Send(o.ctx, "Which one first?", "eq-next") |
| 283 | if err != nil { |
| 284 | t.Fatal(err) |
| 285 | } |
| 286 | cmd := o.message(wire.LaneAssistant) |
| 287 | if cmd.TurnID != sent.Turn.ID || strings.Count(cmd.Text, "\n- Job ") != 10 || strings.Contains(cmd.Text, requests[0]) || !strings.Contains(cmd.Text, "\n1 more job has an open question not listed here: team_status lists every open request with its question, ten at a time (pass its next as after to read on).\n") { |
| 288 | t.Fatalf("the person's next frame %q", cmd.Text) |
| 289 | } |
| 290 | } |
| 291 | |