package thintest import ( "encoding/json" "net/http" "net/url" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // setBoard has the owner's Base answer the board with off turned off (at // a new revision) and gives Equipo the hint, as Base does on a change. func (o *opsRig) setBoard(off ...string) { o.t.Helper() o.boardRev++ o.base.SetBoard(o.base.Token, o.boardRev+1, "Paul", off...) status, _, body := raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "") if status == http.StatusAccepted { // A hint within 5 s of the last fetch waits its turn. time.Sleep(5100 * time.Millisecond) status, _, body = raw(o.t, http.MethodPost, o.inst.URL+"/api/v1/assistant/abilities/refresh", o.client.Token, nil, "") } var out wire.AbilitiesRefreshed if status != http.StatusOK || json.Unmarshal(body, &out) != nil || out.Revision != o.boardRev+1 { o.t.Fatalf("hint: %d %s", status, body) } } func refusedOff(t *testing.T, what string, res wire.ToolResult) { t.Helper() if !res.IsError || res.Code != wire.CodeAbilityOff { t.Errorf("%s with building off: %+v", what, res) } } // With building off, team_request, team_reply and team_nudge are refused // ability_off, and team_status and team_cancel still run. func TestTeamOpsRefusedBuildOff(t *testing.T) { o := newTeamRig(t) o.ask("Fix the sign in page.") request, _ := o.handoff("r1", "Fix the sign in page.", "") o.reply(o.turn) o.setBoard(wire.AbilityBuild) o.ask("Fix the shop too.") refusedOff(t, "team_request", o.use("r2", "team_request", map[string]any{"words": "Fix the shop too."})) refusedOff(t, "team_reply", o.use("rp", "team_reply", map[string]any{"request": request, "answer": "The web one."})) refusedOff(t, "team_nudge", o.use("nd", "team_nudge", map[string]any{"request": request, "words": "Fix the shop too."})) if res := o.use("st", "team_status", map[string]any{}); res.IsError { t.Errorf("team_status: %+v", res) } if res := o.use("cn", "team_cancel", map[string]any{"request": request}); res.IsError && res.Code == wire.CodeAbilityOff { t.Errorf("team_cancel: %+v", res) } if n := o.rows("SELECT count(*) FROM chat_handoffs"); n != 1 { t.Errorf("%d hand-offs", n) } } // Building turned off while a job's thread turn runs: starting or changing // work is refused, reads and status still run, and the job is held. A // message posted to its thread afterwards waits: the thread's turns // aren't dispatched. Answering one of its cards from Equipo's page that // would start work is refused. func TestJobLaneTurnsHeld(t *testing.T) { o := newTeamRig(t) owner := o.inst.Cookie() o.ask("Make me a website.") request, cmd := o.handoff("j1", "Make me a website.", "") lane := wire.JobLane(request) o.in(cmd.TurnID, func() { if res := o.call("ja", wire.ToolAskPerson, map[string]any{"question": "Which website?", "options": []string{"A new one", "One I have"}}); res.Code != "asked" { t.Fatalf("ask %+v", res) } }) o.setBoard(wire.AbilityBuild) o.in(cmd.TurnID, func() { refusedOff(t, "create_project", o.call("cp", wire.ToolCreateProject, map[string]any{"repo": "amadan.net/oficina/site"})) refusedOff(t, "record_task", o.call("rt", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."})) if res := o.call("st", wire.ToolStatus, map[string]any{}); res.IsError { t.Errorf("status: %+v", res) } }) if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NOT NULL", request); n != 1 { t.Fatal("the job isn't held") } o.reply(cmd.TurnID) card, rev := o.threadQuestion(request) if status, body := o.post(owner, "/conversation/cards/"+card+"/answer", url.Values{"choice": {"A new one"}, "revision": {itoa(int(rev))}}); status != http.StatusForbidden { t.Errorf("answered from Equipo's page: %d %.200s", status, body) } if status, _ := o.post(owner, "/jobs/"+request+"/messages", url.Values{"body": {"A new one."}, "request_id": {"held-1"}}); status != http.StatusSeeOther { t.Fatalf("thread message: %d", status) } if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ? AND dispatched = 0 AND state = ?", lane, wire.TurnQueued); n != 1 { t.Errorf("%d turns waiting in the held job's thread", n) } // Building on again, or a runner reconnecting, resumes nothing. o.setBoard() if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ? AND dispatched = 0 AND state = ?", lane, wire.TurnQueued); n != 1 { t.Errorf("building back on dispatched the held job's turn") } } // A step of a job dispatched before building went off isn't granted its // start; a step already started keeps its grant (and finishes). Building // back on doesn't release it: the job is held. func TestBuildOffRefusesStart(t *testing.T) { o := newTeamRig(t) o.ask("Fix the README.") request, cmd := o.handoff("b1", "Fix the README.", "") var first, second *wire.DispatchCommand o.in(cmd.TurnID, func() { _, first = o.started("b1", "Fix teh") _, second = o.started("b2", "Fix more") }) if refused := o.lease(o.runner, "l-first", wire.LeaseCheck{StepID: first.StepID, Attempt: first.Attempt}); refused != "" { t.Fatalf("first step's start refused %q", refused) } o.setBoard(wire.AbilityBuild) if refused := o.lease(o.runner, "l-first-again", wire.LeaseCheck{StepID: first.StepID, Attempt: first.Attempt}); refused != "" { t.Errorf("a started step lost its grant: %q", refused) } if refused := o.lease(o.runner, "l-second", wire.LeaseCheck{StepID: second.StepID, Attempt: second.Attempt}); refused != wire.CodeAbilityOff { t.Errorf("a step not started yet: %q", refused) } if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NOT NULL", request); n != 1 { t.Error("the job isn't held") } o.setBoard() // The refused attempt was settled as not started: it never starts. if refused := o.lease(o.runner, "l-second-again", wire.LeaseCheck{StepID: second.StepID, Attempt: second.Attempt}); refused == "" { t.Errorf("building back on released the held step") } } // project_created isn't refused for building being off: it records a // repository an earlier create_project already authorised, bound to that // authorisation (projectCreated refuses one bound to none), so nothing is // left stranded. Everything that would start work is refused. func TestProjectCreatedAfterSwitchOff(t *testing.T) { o := newTeamRig(t) o.ask("Make me a website.") _, cmd := o.handoff("pc", "Make me a website.", "") o.setBoard(wire.AbilityBuild) o.in(cmd.TurnID, func() { if res := o.call("other/created", wire.ToolProjectCreated, map[string]any{"repo": "amadan.net/oficina/other"}); res.Code != "invalid" { t.Errorf("unbound project_created: %+v", res) } refusedOff(t, "create_project", o.call("cp", wire.ToolCreateProject, map[string]any{"repo": "amadan.net/oficina/site"})) refusedOff(t, "record_task", o.call("rt", wire.ToolRecordTask, map[string]any{"repo": "amadan.net/oficina/site", "title": "Site", "brief": "Build it."})) }) } // Research for a job whose person can't have web pages read is refused // research_off, with the instance's text; with web on it isn't. func TestResearchNeedsWeb(t *testing.T) { o := newTeamRigAt(t, wire.ProtocolCurrent) o.ask("Make mockups for the barber shop.") _, cmd := o.handoff("rw", "Make mockups for the barber shop.", "") o.in(cmd.TurnID, func() { id := o.shopTask("rw") o.setBoard(wire.AbilityWeb) 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." { t.Errorf("research with web off: %+v", res) } o.setBoard() if res := o.research("rw-r2", id); res.Code == wire.CodeResearchOff { t.Errorf("research with web on: %+v", res) } }) } // A legacy hand-off's turn in the shared Coordinator lane is held as a // job thread's is: with building off, its work-changing tools are refused // (Astra phase 2 round 2, P1). func TestLegacyHandoffBuildOff(t *testing.T) { o := newTeamRig(t) o.ask("Fix the README.") _, turn := o.legacyHandoff("lg", "Fix the README.") o.setBoard(wire.AbilityBuild) o.in(turn, func() { refusedOff(t, "record_task", o.call("lg-rt", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Fix", "brief": "Fix it.", "separate": true})) }) } // A finished task's Publish is going live, which putting live governs: // building off doesn't stop it (Astra phase 2 round 2, P2). func TestPublishNotHeldByBuild(t *testing.T) { o := newTeamRigAt(t, wire.ProtocolCurrent) _, id, merge := o.paulsWebsite(false) publish := o.paulsPublish(id, merge) o.setBoard(wire.AbilityBuild) // Whatever else the publish start waits for, building being off isn't // it, and the attempt isn't settled for a hold. if refused := o.lease(o.runner, "pb-pub", wire.LeaseCheck{StepID: publish.StepID, Attempt: publish.Attempt, Purpose: wire.LeasePurposePublish}); refused == wire.CodeAbilityOff { t.Errorf("publish refused for building off") } if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE ability_held_at IS NOT NULL"); n != 0 { t.Errorf("the job was held for a publish") } }