package thintest import ( "errors" "net/http" "strings" "testing" "amadan.net/oficina/equipo/internal/wire" ) const heldOff = "Building is turned off for you. Paul can turn it back on, or you can drop this." const heldBack = "Building is on again. You can carry on with this, or drop it." // heldCard is the request's job card, as a client that knows paused // work reads it, once its detail is detail. func (o *teamRig) heldCard(request, detail string) wire.AssistantCard { o.t.Helper() return o.jobKnowing(request, "the job card saying "+detail, func(c wire.AssistantCard) bool { return c.Detail == detail }) } // heldJob is a job whose second step was refused its start once building // went off for the owner: the request and the refused step's dispatch. func (o *teamRig) heldJob() (string, *wire.DispatchCommand) { o.t.Helper() o.ask("Fix the README.") request, cmd := o.handoff("hj", "Fix the README.", "") var d *wire.DispatchCommand o.in(cmd.TurnID, func() { _, d = o.started("hj", "Fix teh") }) o.reply(cmd.TurnID) o.reply(o.turn) o.setBoard(wire.AbilityBuild) if refused := o.lease(o.runner, "hj-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != wire.CodeAbilityOff { o.t.Fatalf("start: %q", refused) } // The runner gives up the attempt it couldn't start. 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}) return request, d } // The held job's card says why, with no Carry on now while building is // off; once on again it says so and offers Carry on now, with no other // plan (no carry_on_connection), and without building it is refused 403 // ability_off with the card. func TestBuildOffStopRowHasCarryOn(t *testing.T) { o := newTeamRig(t) request, _ := o.heldJob() off := o.heldCard(request, heldOff) if off.State != wire.JobStopped || off.CarryOn || off.Result != "Stopped" { t.Errorf("held, building off: %+v", off) } var s *ErrStatus 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) { t.Errorf("carry on with building off: %v", err) } o.setBoard() back := o.heldCard(request, heldBack) if back.State != wire.JobStopped || !back.CarryOn { t.Errorf("held, building back on: %+v", back) } } // Carry on now resumes the held job: the refused step is tried afresh, // once, however often it is tapped. func TestAbilityHoldResumes(t *testing.T) { o := newTeamRig(t) request, d := o.heldJob() o.setBoard() card := o.heldCard(request, heldBack) out, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "resume-1") if err != nil { t.Fatal(err) } if out.Card.CarryOn || out.Card.Detail == heldBack { t.Errorf("after Carry on now: %+v", out.Card) } again := o.nextDispatch() if again.StepID != d.StepID || again.Attempt <= d.Attempt { t.Errorf("the refused step wasn't tried afresh: %+v", again) } if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NULL", request); n != 1 { t.Error("still held") } if n := o.rows("SELECT count(*) FROM chat_cards WHERE handoff_id = ? AND kind = ? AND coalesce(carry_on_connection, '') <> ''", request, wire.CardJob); n != 0 { t.Error("a held job's card named another plan") } if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "resume-1"); err != nil { t.Errorf("the same tap again: %v", err) } if _, err := o.a.CarryOn(o.ctx, out.Card.ID, out.Card.Revision, "resume-2"); err == nil { t.Error("a second tap resumed again") } if refused := o.lease(o.runner, "resumed-start", wire.LeaseCheck{StepID: again.StepID, Attempt: again.Attempt}); refused != "" { t.Errorf("the resumed step's start: %q", refused) } } // Asked in the conversation, with building on again, team_nudge resumes // a held job, as Carry on now does: for an app that can't draw it. func TestNudgeResumesHeldJob(t *testing.T) { o := newTeamRig(t) request, d := o.heldJob() o.setBoard() o.ask("Please carry on with the README.") if res := o.use("nd", "team_nudge", map[string]any{"request": request, "words": "Please carry on with the README."}); res.IsError { t.Fatalf("nudge: %+v", res) } if again := o.nextDispatch(); again.StepID != d.StepID { t.Errorf("not resumed: %+v", again) } } // A nudge without the person's own words resumes nothing (Astra phase 2, // P1-4). func TestNudgeNeedsPersonsWordsToResume(t *testing.T) { o := newTeamRig(t) request, _ := o.heldJob() o.setBoard() o.ask("How is the README going?") if res := o.use("nd", "team_nudge", map[string]any{"request": request, "words": "Carry on with everything now."}); !res.IsError { t.Errorf("a nudge with made-up words: %+v", res) } if n := o.rows("SELECT count(*) FROM chat_handoffs WHERE id = ? AND ability_held_at IS NOT NULL", request); n != 1 { t.Error("resumed without the person's words") } } // Carry on before the runner reports the refused start still gives the // step a fresh attempt: the refusal settled it (Astra phase 2, P2-6). func TestResumeBeforeRefusalReported(t *testing.T) { o := newTeamRig(t) o.ask("Fix the README.") request, cmd := o.handoff("rb", "Fix the README.", "") var d *wire.DispatchCommand o.in(cmd.TurnID, func() { _, d = o.started("rb", "Fix teh") }) o.reply(cmd.TurnID) o.reply(o.turn) o.setBoard(wire.AbilityBuild) if refused := o.lease(o.runner, "rb-start", wire.LeaseCheck{StepID: d.StepID, Attempt: d.Attempt}); refused != wire.CodeAbilityOff { t.Fatalf("start: %q", refused) } o.setBoard() card := o.heldCard(request, heldBack) if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "rb-resume"); err != nil { t.Fatal(err) } if again := o.nextDispatch(); again.StepID != d.StepID || again.Attempt <= d.Attempt { t.Errorf("no fresh attempt: %+v", again) } } // tasklessHeld is a job held before it reached any work: its // record_task refused with building off, its turn then ended, building on // again. It returns the request, its first turn and its thread. func (o *teamRig) tasklessHeld(key, context string) (string, string, string) { o.t.Helper() o.ask("Make me a website.") request, cmd := o.handoff(key, "Make me a website.", context) o.setBoard(wire.AbilityBuild) o.in(cmd.TurnID, func() { refusedOff(o.t, "record_task", o.call(key+"-rt", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."})) }) o.reply(cmd.TurnID) o.reply(o.turn) o.setBoard() return request, cmd.TurnID, wire.JobLane(request) } // A job held before it reached any work is started again by Carry on now: // the person's request goes to its thread once more, with the hand-off's // context, once however often and however it is tapped; held again // later, it is started again once more (Astra phase 2 round 4, P2; round // 5, P2). func TestTasklessHoldResumes(t *testing.T) { o := newTeamRig(t) request, first, lane := o.tasklessHeld("tl", "They want a new site.") card := o.heldCard(request, heldBack) if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "tl-1"); err != nil { t.Fatal(err) } again := o.message(lane) 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") { t.Fatalf("the request wasn't started again with its context: %+v", again) } for _, key := range []string{"tl-1", "tl-2"} { _, _ = o.a.CarryOn(o.ctx, card.ID, card.Revision, key) } if res := o.use("tl-nd", "team_nudge", map[string]any{"request": request, "words": "Make me a website."}); !res.IsError { t.Errorf("a nudge while it runs again: %+v", res) } if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ?", lane); n != 2 { t.Errorf("%d turns in the job's thread, want 2", n) } // Held again: its turn's record_task refused again. o.setBoard(wire.AbilityBuild) o.in(again.TurnID, func() { refusedOff(t, "record_task", o.call("tl-rt2", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."})) }) o.reply(again.TurnID) o.setBoard() card = o.heldCard(request, heldBack) if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "tl-3"); err != nil { t.Fatal(err) } if third := o.message(lane); third.TurnID == again.TurnID { t.Fatalf("held again, not started again: %+v", third) } } // A held job whose turn left a question open for the person isn't sent // its request again: it waits on their answer. func TestTasklessHoldWaitsOnQuestion(t *testing.T) { o := newTeamRig(t) o.ask("Make me a website.") request, cmd := o.handoff("tq", "Make me a website.", "") lane := wire.JobLane(request) o.setBoard(wire.AbilityBuild) o.in(cmd.TurnID, func() { refusedOff(t, "record_task", o.call("tq-rt", wire.ToolRecordTask, map[string]any{"title": "Site", "brief": "Build it."})) 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" { t.Fatalf("ask %+v", res) } }) o.reply(cmd.TurnID) o.reply(o.turn) o.setBoard() card := o.heldCard(request, heldBack) if _, err := o.a.CarryOn(o.ctx, card.ID, card.Revision, "tq-1"); err != nil { t.Fatal(err) } if n := o.rows("SELECT count(*) FROM chat_turns WHERE lane = ?", lane); n != 1 { t.Errorf("%d turns in the job's thread, want 1", n) } }