| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestPlacementReviewOrdinaryCardLabel(t *testing.T) { |
| 11 | r := newToolRigSaying(t, wire.ProtocolCurrent, "Help with the calendar") |
| 12 | if got := r.call("ordinary", wire.ToolAskPerson, map[string]any{"question": "Which option?", "options": []string{"Create a new project for it", "Keep it here"}}); got.Code != "asked" { |
| 13 | t.Fatal(got) |
| 14 | } |
| 15 | if err := r.runner.Send(r.ctx, "reply-ordinary", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Which option?"}); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | r.wait("reply", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) |
| 19 | snap, _ := r.client.Snapshot(r.ctx) |
| 20 | var card wire.Card |
| 21 | for _, c := range snap.Cards { |
| 22 | if c.Kind == wire.CardQuestion { |
| 23 | card = c |
| 24 | } |
| 25 | } |
| 26 | if _, err := r.client.Answer(r.ctx, card.ID, "Create a new project for it", card.Revision); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | _, body := r.expect(wire.FrameMessage) |
| 30 | r.turn = body.(*wire.MessageCommand).TurnID |
| 31 | if got := r.call("label-consent", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/fresh"}); got.Code != "placement_required" { |
| 32 | t.Fatal("ordinary label granted", got) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestPlacementReviewRepeatedCreates(t *testing.T) { |
| 37 | r := newToolRigSaying(t, wire.ProtocolCurrent, "Create a new project for the calendar") |
| 38 | if got := r.call("first-create", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/fresh"}); got.Code != "create" { |
| 39 | t.Fatal(got) |
| 40 | } |
| 41 | for _, repo := range []string{"amadan.net/oficina/fresh", "amadan.net/oficina/second"} { |
| 42 | if got := r.call("repeat-"+repo, wire.ToolCreateProject, map[string]string{"repo": repo}); got.Code != "placement_required" { |
| 43 | t.Fatal("reused grant", got) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestPlacementReviewAbandonAPIAndLateReport(t *testing.T) { |
| 49 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 50 | id := r.shopTask("abandon") |
| 51 | r.begin("abandon-start", id) |
| 52 | old := r.nextDispatch() |
| 53 | before := r.task(id) |
| 54 | if err := r.movePlacement(id, "oficina/equipo", before.Revision); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | r.expect(wire.FrameCancelStep) |
| 58 | held := r.task(id) |
| 59 | if held.MoveHoldReason != "runner" { |
| 60 | t.Fatal(held.MoveHoldReason) |
| 61 | } |
| 62 | if err := r.client.do(r.ctx, http.MethodPost, "/api/v1/tasks/"+id+"/move", wire.MoveTask{Abandon: true, Revision: held.Revision}, nil); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if stopping := r.task(id); !stopping.Stopping || wire.FinalStage(stopping.Stage) { |
| 66 | t.Fatal("abandon finished without proof", stopping) |
| 67 | } |
| 68 | r.step("late-abandoned", wire.StepReport{TaskID: id, StepID: old.StepID, Attempt: old.Attempt, State: wire.StepDone, Termination: true, Pushed: true, BaseSHA: baseX, CandidateSHA: candX}) |
| 69 | after := r.task(id) |
| 70 | if after.Moving || after.Repo != before.Repo || after.Branch != before.Branch || after.Stage != wire.StageCancelled || after.CandidateSHA != "" { |
| 71 | t.Fatal("abandon changed work", after) |
| 72 | } |
| 73 | abandoned, pushed := false, false |
| 74 | for _, e := range after.Thread { |
| 75 | abandoned = abandoned || strings.Contains(e.Text, "Move abandoned") |
| 76 | pushed = pushed || strings.Contains(e.Text, "Pushed to "+before.Branch+" at "+candX+" after the move was abandoned") |
| 77 | } |
| 78 | if !abandoned || !pushed || after.Stopping { |
| 79 | t.Fatal("missing abandonment or late push history", after) |
| 80 | } |
| 81 | var count int64 |
| 82 | r.inst.DB.G.Table("runner_commands").Where("kind = ?", wire.FrameDispatch).Count(&count) |
| 83 | if count != 1 { |
| 84 | t.Fatal("abandon restarted", count) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestPlacementReviewRemoveCorrectionAPI(t *testing.T) { |
| 89 | r := newToolRigAt(t, wire.ProtocolCurrent) |
| 90 | id := r.shopTask("remove-correction") |
| 91 | k := r.task(id) |
| 92 | if err := r.movePlacement(id, "oficina/equipo", k.Revision); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | notes, err := r.client.Notebook(r.ctx) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | if len(notes.Entries) != 1 || notes.Entries[0].Source != wire.NoteSystem { |
| 100 | t.Fatal(notes) |
| 101 | } |
| 102 | if err := r.client.do(r.ctx, http.MethodDelete, "/api/v1/notebook/corrections/"+notes.Entries[0].ID, nil, nil); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | notes, err = r.client.Notebook(r.ctx) |
| 106 | if err != nil || len(notes.Entries) != 0 { |
| 107 | t.Fatal(notes, err) |
| 108 | } |
| 109 | } |
| 110 | |