| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // setBoard has the owner's Base answer the board with off turned off (at |
| 14 | // a new revision) and gives Equipo the hint, as Base does on a change. |
| 15 | func (o *opsRig) setBoard(off ...string) { |
| 16 | o.t.Helper() |
| 17 | o.boardRev++ |
| 18 | o.base.SetBoard(o.base.Token, o.boardRev+1, "Paul", off...) |
| 19 | status, _, body := raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "") |
| 20 | if status == http.StatusAccepted { |
| 21 | // A hint within 5 s of the last fetch waits its turn. |
| 22 | time.Sleep(5100 * time.Millisecond) |
| 23 | status, _, body = raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "") |
| 24 | } |
| 25 | var out wire.AbilitiesRefreshed |
| 26 | if status != http.StatusOK || json.Unmarshal(body, &out) != nil || out.Revision != o.boardRev+1 { |
| 27 | o.t.Fatalf("hint: %d %s", status, body) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func refusedOff(t *testing.T, what string, res wire.ToolResult) { |
| 32 | t.Helper() |
| 33 | if !res.IsError || res.Code != wire.CodeAbilityOff { |
| 34 | t.Errorf("%s with building off: %+v", what, res) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // With building off, team_request, team_reply and team_nudge are refused |
| 39 | // ability_off, and team_status and team_cancel still run. |
| 40 | func TestTeamOpsRefusedBuildOff(t *testing.T) { |
| 41 | o := newTeamRig(t) |
| 42 | o.ask("Fix the sign in page.") |
| 43 | request, _ := o.handoff("r1", "Fix the sign in page.", "") |
| 44 | o.reply(o.turn) |
| 45 | o.setBoard(wire.AbilityBuild) |
| 46 | o.ask("Fix the shop too.") |
| 47 | refusedOff(t, "team_request", o.use("r2", "team_request", map[string]any{"words": "Fix the shop too."})) |
| 48 | refusedOff(t, "team_reply", o.use("rp", "team_reply", map[string]any{"request": request, "answer": "The web one."})) |
| 49 | refusedOff(t, "team_nudge", o.use("nd", "team_nudge", map[string]any{"request": request, "words": "Fix the shop too."})) |
| 50 | if res := o.use("st", "team_status", map[string]any{}); res.IsError { |
| 51 | t.Errorf("team_status: %+v", res) |
| 52 | } |
| 53 | if res := o.use("cn", "team_cancel", map[string]any{"request": request}); res.IsError && res.Code == wire.CodeAbilityOff { |
| 54 | t.Errorf("team_cancel: %+v", res) |
| 55 | } |
| 56 | if n := o.rows("SELECT count(*) FROM chat_handoffs"); n != 1 { |
| 57 | t.Errorf("%d hand-offs", n) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Building turned off while a job's thread turn runs: starting or changing |
| 62 | // work is refused, reads and status still run, and the job is held. A |
| 63 | // message posted to its thread afterwards waits: the thread's turns |
| 64 | // aren't dispatched. Answering one of its cards from Equipo's page that |
| 65 | // would start work is refused. |
| 66 | func TestJobLaneTurnsHeld(t *testing.T) { |
| 67 | o := newTeamRig(t) |
| 68 | owner := o.inst.Cookie() |
| 69 | o.ask("Make me a website.") |
| 70 | request, cmd := o.handoff("j1", "Make me a website.", "") |
| 71 | lane := wire.JobLane(request) |
| 72 | o.in(cmd.TurnID, func() { |
| 73 | if res := o.call("ja", wire.ToolAskPerson, map[string]any{"question": "Which website?", "options": []string{"A new one", "One I have"}}); res.Code != "asked" { |
| 74 | t.Fatalf("ask %+v", res) |
| 75 | } |
| 76 | }) |
| 77 | o.setBoard(wire.AbilityBuild) |
| 78 | o.in(cmd.TurnID, func() { |
| 79 | refusedOff(t, "create_project", o.call("cp", wire.ToolCreateProject, map[string]any{"repo": "amadan.net/oficina/site"})) |
| 80 | refusedOff(t, "record_task", o.call("rt", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."})) |
| 81 | if res := o.call("st", wire.ToolStatus, map[string]any{}); res.IsError { |
| 82 | t.Errorf("status: %+v", res) |
| 83 | } |
| 84 | }) |
| 85 | if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NOT NULL", request); n != 1 { |
| 86 | t.Fatal("the job isn't held") |
| 87 | } |
| 88 | o.reply(cmd.TurnID) |
| 89 | card, rev := o.threadQuestion(request) |
| 90 | if status, body := o.post(owner, "/conversation/cards/"+card+"/answer", url.Values{"choice": {"A new one"}, "revision": {itoa(int(rev))}}); status != http.StatusForbidden { |
| 91 | t.Errorf("answered from Equipo's page: %d %.200s", status, body) |
| 92 | } |
| 93 | if status, _ := o.post(owner, "/jobs/"+request+"/messages", url.Values{"body": {"A new one."}, "request_id": {"held-1"}}); status != http.StatusSeeOther { |
| 94 | t.Fatalf("thread message: %d", status) |
| 95 | } |
| 96 | if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ? AND dispatched = 0 AND state = ?", lane, wire.TurnQueued); n != 1 { |
| 97 | t.Errorf("%d turns waiting in the held job's thread", n) |
| 98 | } |
| 99 | // Building on again, or a runner reconnecting, resumes nothing. |
| 100 | o.setBoard() |
| 101 | if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ? AND dispatched = 0 AND state = ?", lane, wire.TurnQueued); n != 1 { |
| 102 | t.Errorf("building back on dispatched the held job's turn") |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // A step of a job dispatched before building went off isn't granted its |
| 107 | // start; a step already started keeps its grant (and finishes). Building |
| 108 | // back on doesn't release it: the job is held. |
| 109 | func TestBuildOffRefusesStart(t *testing.T) { |
| 110 | o := newTeamRig(t) |
| 111 | o.ask("Fix the README.") |
| 112 | request, cmd := o.handoff("b1", "Fix the README.", "") |
| 113 | var first, second *wire.DispatchCommand |
| 114 | o.in(cmd.TurnID, func() { |
| 115 | _, first = o.started("b1", "Fix teh") |
| 116 | _, second = o.started("b2", "Fix more") |
| 117 | }) |
| 118 | if refused := o.lease(o.runner, "l-first", wire.LeaseCheck{StepID: first.StepID, Attempt: first.Attempt}); refused != "" { |
| 119 | t.Fatalf("first step's start refused %q", refused) |
| 120 | } |
| 121 | o.setBoard(wire.AbilityBuild) |
| 122 | if refused := o.lease(o.runner, "l-first-again", wire.LeaseCheck{StepID: first.StepID, Attempt: first.Attempt}); refused != "" { |
| 123 | t.Errorf("a started step lost its grant: %q", refused) |
| 124 | } |
| 125 | if refused := o.lease(o.runner, "l-second", wire.LeaseCheck{StepID: second.StepID, Attempt: second.Attempt}); refused != wire.CodeAbilityOff { |
| 126 | t.Errorf("a step not started yet: %q", refused) |
| 127 | } |
| 128 | if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NOT NULL", request); n != 1 { |
| 129 | t.Error("the job isn't held") |
| 130 | } |
| 131 | o.setBoard() |
| 132 | // The refused attempt was settled as not started: it never starts. |
| 133 | if refused := o.lease(o.runner, "l-second-again", wire.LeaseCheck{StepID: second.StepID, Attempt: second.Attempt}); refused == "" { |
| 134 | t.Errorf("building back on released the held step") |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // project_created isn't refused for building being off: it records a |
| 139 | // repository an earlier create_project already authorised, bound to that |
| 140 | // authorisation (projectCreated refuses one bound to none), so nothing is |
| 141 | // left stranded. Everything that would start work is refused. |
| 142 | func TestProjectCreatedAfterSwitchOff(t *testing.T) { |
| 143 | o := newTeamRig(t) |
| 144 | o.ask("Make me a website.") |
| 145 | _, cmd := o.handoff("pc", "Make me a website.", "") |
| 146 | o.setBoard(wire.AbilityBuild) |
| 147 | o.in(cmd.TurnID, func() { |
| 148 | if res := o.call("other/created", wire.ToolProjectCreated, map[string]any{"repo": "amadan.net/oficina/other"}); res.Code != "invalid" { |
| 149 | t.Errorf("unbound project_created: %+v", res) |
| 150 | } |
| 151 | refusedOff(t, "create_project", o.call("cp", wire.ToolCreateProject, map[string]any{"repo": "amadan.net/oficina/site"})) |
| 152 | refusedOff(t, "record_task", o.call("rt", wire.ToolRecordTask, map[string]any{"repo": "amadan.net/oficina/site", "title": "Site", "brief": "Build it."})) |
| 153 | }) |
| 154 | } |
| 155 | |
| 156 | // Research for a job whose person can't have web pages read is refused |
| 157 | // research_off, with the instance's text; with web on it isn't. |
| 158 | func TestResearchNeedsWeb(t *testing.T) { |
| 159 | o := newTeamRigAt(t, wire.ProtocolCurrent) |
| 160 | o.ask("Make mockups for the barber shop.") |
| 161 | _, cmd := o.handoff("rw", "Make mockups for the barber shop.", "") |
| 162 | o.in(cmd.TurnID, func() { |
| 163 | id := o.shopTask("rw") |
| 164 | o.setBoard(wire.AbilityWeb) |
| 165 | if res := o.research("rw-r", id); !res.IsError || res.Code != wire.CodeResearchOff || res.Text != "Refused: the person this work is for can't have web pages read. Carry on without Research." { |
| 166 | t.Errorf("research with web off: %+v", res) |
| 167 | } |
| 168 | o.setBoard() |
| 169 | if res := o.research("rw-r2", id); res.Code == wire.CodeResearchOff { |
| 170 | t.Errorf("research with web on: %+v", res) |
| 171 | } |
| 172 | }) |
| 173 | } |
| 174 | |
| 175 | // A legacy hand-off's turn in the shared Coordinator lane is held as a |
| 176 | // job thread's is: with building off, its work-changing tools are refused |
| 177 | // (Astra phase 2 round 2, P1). |
| 178 | func TestLegacyHandoffBuildOff(t *testing.T) { |
| 179 | o := newTeamRig(t) |
| 180 | o.ask("Fix the README.") |
| 181 | _, turn := o.legacyHandoff("lg", "Fix the README.") |
| 182 | o.setBoard(wire.AbilityBuild) |
| 183 | o.in(turn, func() { |
| 184 | refusedOff(t, "record_task", o.call("lg-rt", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Fix", "brief": "Fix it.", "separate": true})) |
| 185 | }) |
| 186 | } |
| 187 | |
| 188 | // A finished task's Publish is going live, which putting live governs: |
| 189 | // building off doesn't stop it (Astra phase 2 round 2, P2). |
| 190 | func TestPublishNotHeldByBuild(t *testing.T) { |
| 191 | o := newTeamRigAt(t, wire.ProtocolCurrent) |
| 192 | _, id, merge := o.paulsWebsite(false) |
| 193 | publish := o.paulsPublish(id, merge) |
| 194 | o.setBoard(wire.AbilityBuild) |
| 195 | // Whatever else the publish start waits for, building being off isn't |
| 196 | // it, and the attempt isn't settled for a hold. |
| 197 | if refused := o.lease(o.runner, "pb-pub", wire.LeaseCheck{StepID: publish.StepID, Attempt: publish.Attempt, Purpose: wire.LeasePurposePublish}); refused == wire.CodeAbilityOff { |
| 198 | t.Errorf("publish refused for building off") |
| 199 | } |
| 200 | if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE ability_held_at IS NOT NULL"); n != 0 { |
| 201 | t.Errorf("the job was held for a publish") |
| 202 | } |
| 203 | } |
| 204 | |