| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/copydesk" |
| 5 | "amadan.net/oficina/equipo/internal/wire" |
| 6 | "context" |
| 7 | "gorm.io/gorm" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestProposalFailureKeepsSuccessfulPublication(t *testing.T) { |
| 13 | for _, worker := range []bool{false, true} { |
| 14 | t.Run(map[bool]string{false: "local", true: "worker"}[worker], func(t *testing.T) { |
| 15 | r := placementRig(t) |
| 16 | r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RolePrimary, nil } |
| 17 | st := placementEdgeStep("s_proposal") |
| 18 | st.CandidateSHA = strings.Repeat("a", 40) |
| 19 | if worker { |
| 20 | st.State = wire.StepDone |
| 21 | st.Terminated = true |
| 22 | st.Publication = wire.PublicationPending |
| 23 | st.PublicationID, st.PublicationRunner = "pub_proposal", "RUNNER1" |
| 24 | } |
| 25 | if err := r.s.db.G.Create(&st).Error; err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | l := Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCurrent} |
| 29 | err := r.s.tx(context.Background(), func(tx *txn) error { |
| 30 | if worker { |
| 31 | return tx.published(l, &wire.PublishedReport{Publication: st.PublicationID, TaskID: st.TaskID, StepID: st.ID, Attempt: 1, CandidateSHA: st.CandidateSHA, Pushed: true, Reason: "proposal_failed"}) |
| 32 | } |
| 33 | return tx.stepReport(l, &wire.StepReport{TaskID: st.TaskID, StepID: st.ID, Attempt: 1, State: wire.StepDone, Termination: true, CandidateSHA: st.CandidateSHA, Pushed: true, Reason: "proposal_failed"}) |
| 34 | }) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | var got stepRecord |
| 39 | if err := r.s.db.G.First(&got, "id = ?", st.ID).Error; err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if got.State != wire.StepDone || !got.Pushed || got.Reason != "proposal_failed" { |
| 43 | t.Fatalf("successful push diagnostic: %+v", got) |
| 44 | } |
| 45 | if worker && got.Publication != wire.PublicationPublished { |
| 46 | t.Fatal("publication failed", got.Publication) |
| 47 | } |
| 48 | }) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestCopyProposalFailureKeepsSuccessfulPublication(t *testing.T) { |
| 53 | r := copyPendingRig(t, wire.StepQueued, "") |
| 54 | r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RolePrimary, nil } |
| 55 | r.exec("UPDATE chat_steps SET runner_id='RUNNER1' WHERE id='copy'") |
| 56 | c := &wire.CopyResult{Owner: hkOwner, Delegation: 1, Input: strings.Repeat("a", 40), Compared: strings.Repeat("b", 40), Extraction: copydesk.Extraction{Version: 1}, Review: copydesk.Review{Version: 1}, Tokens: 100, UsageKnown: true} |
| 57 | report := wire.StepReport{TaskID: "tkcopy", StepID: "copy", Attempt: 1, State: wire.StepDone, Termination: true, Pushed: true, CandidateSHA: c.Input, Copy: c, Reason: "proposal_failed"} |
| 58 | r.must(r.tx(func(tx *txn) error { |
| 59 | return tx.stepReport(Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCopy}, &report) |
| 60 | })) |
| 61 | var got stepRecord |
| 62 | r.must(r.s.db.G.First(&got, "id='copy'").Error) |
| 63 | if got.State != wire.StepDone || !got.Pushed || got.Reason != "proposal_failed" { |
| 64 | t.Fatalf("Copy push diagnostic: %+v", got) |
| 65 | } |
| 66 | } |
| 67 | |