package api
import (
"testing"
"craftdesign.group/quarry/internal/zitcli"
)
// zit#209: a push zit kept on an attempt branch waits as a landing onto
// the branch it was meant for, opened as the pusher and titled from the
// check-in; a second diversion onto the same branch opens no second
// landing, and a branch that already has one is left alone.
func TestOpenDivertedOpensOneLandingAsThePusher(t *testing.T) {
dir, _, ix, pipeSync := premergeStore(t)
const branch = "attempt/bob/0123456789ab"
zrun(t, dir, "update", "trunk")
zwrite(t, dir, "bob.txt", "bob\n")
zrun(t, dir, "add", "bob.txt")
zrun(t, dir, "commit", "-m", "bob wants trunk", "--branch", branch, "--user", "bob")
if _, err := pipeSync(); err != nil {
t.Fatal(err)
}
tip, err := ix.BranchTip(branch)
if err != nil || tip == "" {
t.Fatalf("attempt branch tip = %q, %v", tip, err)
}
d := Deps{LandingNumbers: landingNumberDB(t),
Repos: NewRepos(map[string]*Repo{"test": {Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
open := func() int {
ls, err := ix.Landings("open", 100)
if err != nil {
t.Fatal(err)
}
return len(ls)
}
before := open()
d.OpenDiverted("test", "bob", tip, branch, "trunk")
l, found, err := ix.OpenLandingOn(branch)
if err != nil || !found {
t.Fatalf("FAIL: no open landing from %s: %v", branch, err)
}
if l.Target != "trunk" || l.Author != "bob" || l.Title != "bob wants trunk" {
t.Fatalf("FAIL: landing = target %q author %q title %q", l.Target, l.Author, l.Title)
}
d.OpenDiverted("test", "bob", tip, branch, "trunk")
d.OpenDiverted("test", "bob", tip, "one", "trunk")
if got := open(); got != before+1 {
t.Fatalf("FAIL: %d open landings, want %d", got, before+1)
}
t.Log("ok: one landing from the attempt branch onto trunk, by bob, titled from the check-in")
}