| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "strconv" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | // Merge identity (Astra, previews on done tasks): the commit a landing |
| 14 | // names is publication authority only when it is proven to be the merge |
| 15 | // of the approved candidate. These are the shapes, in real git, where the |
| 16 | // runner's reconciled landing (inspectMerge) names something else: a |
| 17 | // nested merge, or a merge more than 200 first-parent commits back, are |
| 18 | // named by the default branch's tip, which holds later work. The |
| 19 | // instance builds the approved candidate then (chat.verifiedMerge); only |
| 20 | // the hub's own answer, with the candidate as second parent, is the merge. |
| 21 | |
| 22 | // landedThenMoved merges the candidate through the hub, then lets main |
| 23 | // move on with work in a clone of the remote. |
| 24 | func landedThenMoved(t *testing.T, more func(work string)) (*Engineer, *hubForge, stepRow, string, string) { |
| 25 | t.Helper() |
| 26 | e, f, r, cand := hubRig(t) |
| 27 | rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) |
| 28 | if rep.State != wire.StepDone || rep.MergeSource != wire.MergeSourceHub || len(rep.MergeParents) != 2 || rep.MergeParents[1] != cand { |
| 29 | t.Fatalf("hub merge %+v", rep) |
| 30 | } |
| 31 | work := filepath.Join(t.TempDir(), "later") |
| 32 | gitOut(t, filepath.Dir(work), "clone", "-q", f.dir, work) |
| 33 | more(work) |
| 34 | gitOut(t, work, "push", "-q", "-f", "origin", "HEAD:main") |
| 35 | return e, f, r, cand, rep.MergeSHA |
| 36 | } |
| 37 | |
| 38 | func commitFile(t *testing.T, work, name, body string) { |
| 39 | t.Helper() |
| 40 | if err := os.WriteFile(filepath.Join(work, name), []byte(body), 0o644); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | gitOut(t, work, "add", "-A") |
| 44 | gitOut(t, work, "commit", "-q", "-m", name) |
| 45 | } |
| 46 | |
| 47 | // TestMergeIdentityLaterCommits: later commits after the merge leave the |
| 48 | // merge commit itself the one that brought the candidate in. |
| 49 | func TestMergeIdentityLaterCommits(t *testing.T) { |
| 50 | e, f, r, cand, merge := landedThenMoved(t, func(work string) { |
| 51 | for i := range 3 { |
| 52 | commitFile(t, work, "later-"+strconv.Itoa(i), "later\n") |
| 53 | } |
| 54 | }) |
| 55 | st, err := e.inspectMerge(context.Background(), r.Repo, r.Branch, cand) |
| 56 | tip := gitOut(t, f.dir, "rev-parse", "main") |
| 57 | if err != nil || !st.Landed || st.MergeSHA != merge || tip == merge { |
| 58 | t.Fatalf("inspect %+v %v (merge %s, tip %s)", st, err, merge, tip) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // TestMergeIdentityFallsBackToTheTip: a nested merge (the candidate came |
| 63 | // in through another branch's merge) and a merge more than 200 |
| 64 | // first-parent commits back are both named by the tip, whose parents do |
| 65 | // not have the candidate second: never proof of the merge. |
| 66 | func TestMergeIdentityFallsBackToTheTip(t *testing.T) { |
| 67 | for name, more := range map[string]func(string){ |
| 68 | "nested": func(work string) { |
| 69 | // The candidate reaches main only through another branch's |
| 70 | // merge: integration merges it, then main merges integration. |
| 71 | gitOut(t, work, "checkout", "-q", "-b", "integration", "main~1") |
| 72 | gitOut(t, work, "merge", "-q", "--no-ff", "-m", "integration", "main^2") |
| 73 | commitFile(t, work, "integration-only", "x\n") |
| 74 | gitOut(t, work, "checkout", "-q", "-B", "main", "origin/main~1") |
| 75 | gitOut(t, work, "merge", "-q", "--no-ff", "-m", "bring integration", "integration") |
| 76 | }, |
| 77 | "far": func(work string) { |
| 78 | for i := range 201 { |
| 79 | commitFile(t, work, "far", strconv.Itoa(i)+"\n") |
| 80 | } |
| 81 | }, |
| 82 | } { |
| 83 | t.Run(name, func(t *testing.T) { |
| 84 | e, f, r, cand, _ := landedThenMoved(t, more) |
| 85 | st, err := e.inspectMerge(context.Background(), r.Repo, r.Branch, cand) |
| 86 | tip := gitOut(t, f.dir, "rev-parse", "main") |
| 87 | if err != nil || !st.Landed || st.MergeSHA != tip { |
| 88 | t.Fatalf("inspect %+v %v (tip %s)", st, err, tip) |
| 89 | } |
| 90 | if parents := e.mergeParents(context.Background(), r.Repo, tip); len(parents) == 2 && parents[1] == cand { |
| 91 | t.Fatalf("the tip reads as the candidate's merge: %v", parents) |
| 92 | } |
| 93 | }) |
| 94 | } |
| 95 | } |
| 96 | |