package thintest import ( "amadan.net/oficina/equipo/internal/wire" "net/http" "strings" "testing" ) func TestPlacementReviewOrdinaryCardLabel(t *testing.T) { r := newToolRigSaying(t, wire.ProtocolCurrent, "Help with the calendar") 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" { t.Fatal(got) } if err := r.runner.Send(r.ctx, "reply-ordinary", wire.FrameReply, wire.Reply{TurnID: r.turn, Attempt: 1, Text: "Which option?"}); err != nil { t.Fatal(err) } r.wait("reply", func(c *Conversation) bool { return c.Turns[r.turn].State == wire.TurnReplied }) snap, _ := r.client.Snapshot(r.ctx) var card wire.Card for _, c := range snap.Cards { if c.Kind == wire.CardQuestion { card = c } } if _, err := r.client.Answer(r.ctx, card.ID, "Create a new project for it", card.Revision); err != nil { t.Fatal(err) } _, body := r.expect(wire.FrameMessage) r.turn = body.(*wire.MessageCommand).TurnID if got := r.call("label-consent", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/fresh"}); got.Code != "placement_required" { t.Fatal("ordinary label granted", got) } } func TestPlacementReviewRepeatedCreates(t *testing.T) { r := newToolRigSaying(t, wire.ProtocolCurrent, "Create a new project for the calendar") if got := r.call("first-create", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/fresh"}); got.Code != "create" { t.Fatal(got) } for _, repo := range []string{"amadan.net/oficina/fresh", "amadan.net/oficina/second"} { if got := r.call("repeat-"+repo, wire.ToolCreateProject, map[string]string{"repo": repo}); got.Code != "placement_required" { t.Fatal("reused grant", got) } } } func TestPlacementReviewAbandonAPIAndLateReport(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) id := r.shopTask("abandon") r.begin("abandon-start", id) old := r.nextDispatch() before := r.task(id) if err := r.movePlacement(id, "oficina/equipo", before.Revision); err != nil { t.Fatal(err) } r.expect(wire.FrameCancelStep) held := r.task(id) if held.MoveHoldReason != "runner" { t.Fatal(held.MoveHoldReason) } if err := r.client.do(r.ctx, http.MethodPost, "/api/v1/tasks/"+id+"/move", wire.MoveTask{Abandon: true, Revision: held.Revision}, nil); err != nil { t.Fatal(err) } if stopping := r.task(id); !stopping.Stopping || wire.FinalStage(stopping.Stage) { t.Fatal("abandon finished without proof", stopping) } 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}) after := r.task(id) if after.Moving || after.Repo != before.Repo || after.Branch != before.Branch || after.Stage != wire.StageCancelled || after.CandidateSHA != "" { t.Fatal("abandon changed work", after) } abandoned, pushed := false, false for _, e := range after.Thread { abandoned = abandoned || strings.Contains(e.Text, "Move abandoned") pushed = pushed || strings.Contains(e.Text, "Pushed to "+before.Branch+" at "+candX+" after the move was abandoned") } if !abandoned || !pushed || after.Stopping { t.Fatal("missing abandonment or late push history", after) } var count int64 r.inst.DB.G.Table("runner_commands").Where("kind = ?", wire.FrameDispatch).Count(&count) if count != 1 { t.Fatal("abandon restarted", count) } } func TestPlacementReviewRemoveCorrectionAPI(t *testing.T) { r := newToolRigAt(t, wire.ProtocolCurrent) id := r.shopTask("remove-correction") k := r.task(id) if err := r.movePlacement(id, "oficina/equipo", k.Revision); err != nil { t.Fatal(err) } notes, err := r.client.Notebook(r.ctx) if err != nil { t.Fatal(err) } if len(notes.Entries) != 1 || notes.Entries[0].Source != wire.NoteSystem { t.Fatal(notes) } if err := r.client.do(r.ctx, http.MethodDelete, "/api/v1/notebook/corrections/"+notes.Entries[0].ID, nil, nil); err != nil { t.Fatal(err) } notes, err = r.client.Notebook(r.ctx) if err != nil || len(notes.Entries) != 0 { t.Fatal(notes, err) } }