package thin import ( "context" "os" "path/filepath" "strconv" "testing" "amadan.net/oficina/equipo/internal/wire" ) // Merge identity (Astra, previews on done tasks): the commit a landing // names is publication authority only when it is proven to be the merge // of the approved candidate. These are the shapes, in real git, where the // runner's reconciled landing (inspectMerge) names something else: a // nested merge, or a merge more than 200 first-parent commits back, are // named by the default branch's tip, which holds later work. The // instance builds the approved candidate then (chat.verifiedMerge); only // the hub's own answer, with the candidate as second parent, is the merge. // landedThenMoved merges the candidate through the hub, then lets main // move on with work in a clone of the remote. func landedThenMoved(t *testing.T, more func(work string)) (*Engineer, *hubForge, stepRow, string, string) { t.Helper() e, f, r, cand := hubRig(t) rep := e.Exec(context.Background(), r, func(wire.StepReport) {}) if rep.State != wire.StepDone || rep.MergeSource != wire.MergeSourceHub || len(rep.MergeParents) != 2 || rep.MergeParents[1] != cand { t.Fatalf("hub merge %+v", rep) } work := filepath.Join(t.TempDir(), "later") gitOut(t, filepath.Dir(work), "clone", "-q", f.dir, work) more(work) gitOut(t, work, "push", "-q", "-f", "origin", "HEAD:main") return e, f, r, cand, rep.MergeSHA } func commitFile(t *testing.T, work, name, body string) { t.Helper() if err := os.WriteFile(filepath.Join(work, name), []byte(body), 0o644); err != nil { t.Fatal(err) } gitOut(t, work, "add", "-A") gitOut(t, work, "commit", "-q", "-m", name) } // TestMergeIdentityLaterCommits: later commits after the merge leave the // merge commit itself the one that brought the candidate in. func TestMergeIdentityLaterCommits(t *testing.T) { e, f, r, cand, merge := landedThenMoved(t, func(work string) { for i := range 3 { commitFile(t, work, "later-"+strconv.Itoa(i), "later\n") } }) st, err := e.inspectMerge(context.Background(), r.Repo, r.Branch, cand) tip := gitOut(t, f.dir, "rev-parse", "main") if err != nil || !st.Landed || st.MergeSHA != merge || tip == merge { t.Fatalf("inspect %+v %v (merge %s, tip %s)", st, err, merge, tip) } } // TestMergeIdentityFallsBackToTheTip: a nested merge (the candidate came // in through another branch's merge) and a merge more than 200 // first-parent commits back are both named by the tip, whose parents do // not have the candidate second: never proof of the merge. func TestMergeIdentityFallsBackToTheTip(t *testing.T) { for name, more := range map[string]func(string){ "nested": func(work string) { // The candidate reaches main only through another branch's // merge: integration merges it, then main merges integration. gitOut(t, work, "checkout", "-q", "-b", "integration", "main~1") gitOut(t, work, "merge", "-q", "--no-ff", "-m", "integration", "main^2") commitFile(t, work, "integration-only", "x\n") gitOut(t, work, "checkout", "-q", "-B", "main", "origin/main~1") gitOut(t, work, "merge", "-q", "--no-ff", "-m", "bring integration", "integration") }, "far": func(work string) { for i := range 201 { commitFile(t, work, "far", strconv.Itoa(i)+"\n") } }, } { t.Run(name, func(t *testing.T) { e, f, r, cand, _ := landedThenMoved(t, more) st, err := e.inspectMerge(context.Background(), r.Repo, r.Branch, cand) tip := gitOut(t, f.dir, "rev-parse", "main") if err != nil || !st.Landed || st.MergeSHA != tip { t.Fatalf("inspect %+v %v (tip %s)", st, err, tip) } if parents := e.mergeParents(context.Background(), r.Repo, tip); len(parents) == 2 && parents[1] == cand { t.Fatalf("the tip reads as the candidate's merge: %v", parents) } }) } }