| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | const heldOff = "Building is turned off for you. Paul can turn it back on, or you can drop this." |
| 13 | const heldBack = "Building is on again. You can carry on with this, or drop it." |
| 14 | |
| 15 | // heldCard is the request's job card, as a client that knows paused |
| 16 | // work reads it, once its detail is detail. |
| 17 | func (o *teamRig) heldCard(request, detail string) wire.AssistantCard { |
| 18 | o.t.Helper() |
| 19 | return o.jobKnowing(request, "the job card saying "+detail, func(c wire.AssistantCard) bool { return c.Detail == detail }) |
| 20 | } |
| 21 | |
| 22 | // heldJob is a job whose second step was refused its start once building |
| 23 | // went off for the owner: the request and the refused step's dispatch. |
| 24 | func (o *teamRig) heldJob() (string, *wire.DispatchCommand) { |
| 25 | o.t.Helper() |
| 26 | o.ask("Fix the README.") |
| 27 | request, cmd := o.handoff("hj", "Fix the README.", "") |
| 28 | var d *wire.DispatchCommand |
| 29 | o.in(cmd.TurnID, func() { _, d = o.started("hj", "Fix teh") }) |
| 30 | o.reply(cmd.TurnID) |
| 31 | o.reply(o.turn) |
| 32 | o.setBoard(wire.AbilityBuild) |
| 33 | if refused := o.lease(o.runner, "hj-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != wire.CodeAbilityOff { |
| 34 | o.t.Fatalf("start: %q", refused) |
| 35 | } |
| 36 | // The runner gives up the attempt it couldn't start. |
| 37 | o.step("hj-gone", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepCancelled, Reason: wire.CodeAbilityOff, BaseSHA: d.BaseSHA, Termination: true}) |
| 38 | return request, d |
| 39 | } |
| 40 | |
| 41 | // The held job's card says why, with no Carry on now while building is |
| 42 | // off; once on again it says so and offers Carry on now, with no other |
| 43 | // plan (no carry_on_connection), and without building it is refused 403 |
| 44 | // ability_off with the card. |
| 45 | func TestBuildOffStopRowHasCarryOn(t *testing.T) { |
| 46 | o := newTeamRig(t) |
| 47 | request, _ := o.heldJob() |
| 48 | off := o.heldCard(request, heldOff) |
| 49 | if off.State != wire.JobStopped || off.CarryOn || off.Result != "Stopped" { |
| 50 | t.Errorf("held, building off: %+v", off) |
| 51 | } |
| 52 | var s *ErrStatus |
| 53 | if _, err := o.a.CarryOn(o.ctx, off.ID, off.Revision, "co-off"); !errors.As(err, &s) || s.Status != http.StatusForbidden || !strings.Contains(string(s.Body), `"ability_off"`) || !strings.Contains(string(s.Body), off.ID) { |
| 54 | t.Errorf("carry on with building off: %v", err) |
| 55 | } |
| 56 | o.setBoard() |
| 57 | back := o.heldCard(request, heldBack) |
| 58 | if back.State != wire.JobStopped || !back.CarryOn { |
| 59 | t.Errorf("held, building back on: %+v", back) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Carry on now resumes the held job: the refused step is tried afresh, |
| 64 | // once, however often it is tapped. |
| 65 | func TestAbilityHoldResumes(t *testing.T) { |
| 66 | o := newTeamRig(t) |
| 67 | request, d := o.heldJob() |
| 68 | o.setBoard() |
| 69 | card := o.heldCard(request, heldBack) |
| 70 | out, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "resume-1") |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if out.Card.CarryOn || out.Card.Detail == heldBack { |
| 75 | t.Errorf("after Carry on now: %+v", out.Card) |
| 76 | } |
| 77 | again := o.nextDispatch() |
| 78 | if again.StepID != d.StepID || again.Attempt <= d.Attempt { |
| 79 | t.Errorf("the refused step wasn't tried afresh: %+v", again) |
| 80 | } |
| 81 | if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NULL", request); n != 1 { |
| 82 | t.Error("still held") |
| 83 | } |
| 84 | if n := o.rows("SELECT count(*) FROM chat_cards WHERE handoff_id = ? AND kind = ? AND coalesce(carry_on_connection, '') <> ''", request, wire.CardJob); n != 0 { |
| 85 | t.Error("a held job's card named another plan") |
| 86 | } |
| 87 | if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "resume-1"); err != nil { |
| 88 | t.Errorf("the same tap again: %v", err) |
| 89 | } |
| 90 | if _, err := o.a.CarryOn(o.ctx, out.Card.ID, out.Card.Revision, "resume-2"); err == nil { |
| 91 | t.Error("a second tap resumed again") |
| 92 | } |
| 93 | if refused := o.lease(o.runner, "resumed-start", wire.LeaseCheck{StepID: again.StepID, Attempt: again.Attempt}); refused != "" { |
| 94 | t.Errorf("the resumed step's start: %q", refused) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Asked in the conversation, with building on again, team_nudge resumes |
| 99 | // a held job, as Carry on now does: for an app that can't draw it. |
| 100 | func TestNudgeResumesHeldJob(t *testing.T) { |
| 101 | o := newTeamRig(t) |
| 102 | request, d := o.heldJob() |
| 103 | o.setBoard() |
| 104 | o.ask("Please carry on with the README.") |
| 105 | if res := o.use("nd", "team_nudge", map[string]any{"request": request, "words": "Please carry on with the README."}); res.IsError { |
| 106 | t.Fatalf("nudge: %+v", res) |
| 107 | } |
| 108 | if again := o.nextDispatch(); again.StepID != d.StepID { |
| 109 | t.Errorf("not resumed: %+v", again) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // A nudge without the person's own words resumes nothing (Astra phase 2, |
| 114 | // P1-4). |
| 115 | func TestNudgeNeedsPersonsWordsToResume(t *testing.T) { |
| 116 | o := newTeamRig(t) |
| 117 | request, _ := o.heldJob() |
| 118 | o.setBoard() |
| 119 | o.ask("How is the README going?") |
| 120 | if res := o.use("nd", "team_nudge", map[string]any{"request": request, "words": "Carry on with everything now."}); !res.IsError { |
| 121 | t.Errorf("a nudge with made-up words: %+v", res) |
| 122 | } |
| 123 | if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NOT NULL", request); n != 1 { |
| 124 | t.Error("resumed without the person's words") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Carry on before the runner reports the refused start still gives the |
| 129 | // step a fresh attempt: the refusal settled it (Astra phase 2, P2-6). |
| 130 | func TestResumeBeforeRefusalReported(t *testing.T) { |
| 131 | o := newTeamRig(t) |
| 132 | o.ask("Fix the README.") |
| 133 | request, cmd := o.handoff("rb", "Fix the README.", "") |
| 134 | var d *wire.DispatchCommand |
| 135 | o.in(cmd.TurnID, func() { _, d = o.started("rb", "Fix teh") }) |
| 136 | o.reply(cmd.TurnID) |
| 137 | o.reply(o.turn) |
| 138 | o.setBoard(wire.AbilityBuild) |
| 139 | if refused := o.lease(o.runner, "rb-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != wire.CodeAbilityOff { |
| 140 | t.Fatalf("start: %q", refused) |
| 141 | } |
| 142 | o.setBoard() |
| 143 | card := o.heldCard(request, heldBack) |
| 144 | if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "rb-resume"); err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | if again := o.nextDispatch(); again.StepID != d.StepID || again.Attempt <= d.Attempt { |
| 148 | t.Errorf("no fresh attempt: %+v", again) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // tasklessHeld is a job held before it reached any work: its |
| 153 | // record_task refused with building off, its turn then ended, building on |
| 154 | // again. It returns the request, its first turn and its thread. |
| 155 | func (o *teamRig) tasklessHeld(key, context string) (string, string, string) { |
| 156 | o.t.Helper() |
| 157 | o.ask("Make me a website.") |
| 158 | request, cmd := o.handoff(key, "Make me a website.", context) |
| 159 | o.setBoard(wire.AbilityBuild) |
| 160 | o.in(cmd.TurnID, func() { |
| 161 | refusedOff(o.t, "record_task", o.call(key+"-rt", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."})) |
| 162 | }) |
| 163 | o.reply(cmd.TurnID) |
| 164 | o.reply(o.turn) |
| 165 | o.setBoard() |
| 166 | return request, cmd.TurnID, wire.JobLane(request) |
| 167 | } |
| 168 | |
| 169 | // A job held before it reached any work is started again by Carry on now: |
| 170 | // the person's request goes to its thread once more, with the hand-off's |
| 171 | // context, once however often and however it is tapped; held again |
| 172 | // later, it is started again once more (Astra phase 2 round 4, P2; round |
| 173 | // 5, P2). |
| 174 | func TestTasklessHoldResumes(t *testing.T) { |
| 175 | o := newTeamRig(t) |
| 176 | request, first, lane := o.tasklessHeld("tl", "They want a new site.") |
| 177 | card := o.heldCard(request, heldBack) |
| 178 | if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "tl-1"); err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | again := o.message(lane) |
| 182 | if again.TurnID == first || !strings.HasPrefix(again.Text, "Make me a website.\n\n[Reference from the Assistant, not the person's words: They want a new site.]\n") { |
| 183 | t.Fatalf("the request wasn't started again with its context: %+v", again) |
| 184 | } |
| 185 | for _, key := range []string{"tl-1", "tl-2"} { |
| 186 | _, _ = o.a.CarryOn(o.ctx, card.ID, card.Revision, key) |
| 187 | } |
| 188 | if res := o.use("tl-nd", "team_nudge", map[string]any{"request": request, "words": "Make me a website."}); !res.IsError { |
| 189 | t.Errorf("a nudge while it runs again: %+v", res) |
| 190 | } |
| 191 | if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ?", lane); n != 2 { |
| 192 | t.Errorf("%d turns in the job's thread, want 2", n) |
| 193 | } |
| 194 | // Held again: its turn's record_task refused again. |
| 195 | o.setBoard(wire.AbilityBuild) |
| 196 | o.in(again.TurnID, func() { |
| 197 | refusedOff(t, "record_task", o.call("tl-rt2", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."})) |
| 198 | }) |
| 199 | o.reply(again.TurnID) |
| 200 | o.setBoard() |
| 201 | card = o.heldCard(request, heldBack) |
| 202 | if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "tl-3"); err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | if third := o.message(lane); third.TurnID == again.TurnID { |
| 206 | t.Fatalf("held again, not started again: %+v", third) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // A held job whose turn left a question open for the person isn't sent |
| 211 | // its request again: it waits on their answer. |
| 212 | func TestTasklessHoldWaitsOnQuestion(t *testing.T) { |
| 213 | o := newTeamRig(t) |
| 214 | o.ask("Make me a website.") |
| 215 | request, cmd := o.handoff("tq", "Make me a website.", "") |
| 216 | lane := wire.JobLane(request) |
| 217 | o.setBoard(wire.AbilityBuild) |
| 218 | o.in(cmd.TurnID, func() { |
| 219 | refusedOff(t, "record_task", o.call("tq-rt", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."})) |
| 220 | if res := o.call("tq-ask", wire.ToolAskPerson, map[string]any{"question": "Which website?", "options": []string{"A new one", "One I have"}}); res.Code != "asked" { |
| 221 | t.Fatalf("ask %+v", res) |
| 222 | } |
| 223 | }) |
| 224 | o.reply(cmd.TurnID) |
| 225 | o.reply(o.turn) |
| 226 | o.setBoard() |
| 227 | card := o.heldCard(request, heldBack) |
| 228 | if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "tq-1"); err != nil { |
| 229 | t.Fatal(err) |
| 230 | } |
| 231 | if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ?", lane); n != 1 { |
| 232 | t.Errorf("%d turns in the job's thread, want 1", n) |
| 233 | } |
| 234 | } |
| 235 | |