Skip to content
quarry Artifact [f680b02df8]
Login

Artifact [f680b02df8]

Artifact f680b02df864a888b0627ca74c9c9fcd721521460dfc75ab0bd4d4c962270aac:


package api

import (
	"context"
	"encoding/json"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/index"
	"craftdesign.group/quarry/internal/oltp"
	"craftdesign.group/quarry/internal/ops"
)

func openLanding(mut func(*landingGetOut)) landingGetOut {
	out := landingGetOut{CanAccept: true, MayLand: true}
	out.Landing.LandingSummary = index.LandingSummary{Number: 41, Branch: "parse-fix", Target: "trunk", State: "open"}
	if mut != nil {
		mut(&out)
	}
	return out
}

func TestLandingWaitingNamesTheOneThingItWaitsOn(t *testing.T) {
	cases := []struct {
		name string
		mut  func(*landingGetOut)
		on   string
		who  string
		cmd  string
	}{
		{"settled", func(o *landingGetOut) { o.Landing.State = "accepted" }, "nothing", "", ""},
		{"conflict", func(o *landingGetOut) { o.Mergeable, o.Conflicts = "conflict", []string{"a.go", "b.go"} }, "conflict", "", ""},
		{"stacked", func(o *landingGetOut) {
			o.Mergeable, o.Landing.Position = "stacked", 2
			o.Stack.Segments = []index.StackSegment{{Number: 40, State: "open", Position: 1}, {Number: 41, State: "open", Position: 2}}
		}, "stack", "", ""},
		{"failed", func(o *landingGetOut) {
			o.Landing.CI, o.Required, o.Unmet = "ci-fail", []string{"test"}, []string{"test"}
		}, "checks", "", "quarry status !41 --log"},
		{"never ran", func(o *landingGetOut) { o.Untested, o.NeverRan, o.Unmet = true, []string{"test"}, []string{"test"} }, "never_ran", "", ""},
		{"stale", func(o *landingGetOut) { o.Stale = true }, "stale", "", ""},
		{"running", func(o *landingGetOut) {
			o.Landing.CI, o.Required, o.Unmet = "ci-waiting", []string{"test"}, []string{"test"}
		}, "checks", "", ""},
		{"protected", func(o *landingGetOut) {
			o.MayLand, o.MayLandReason, o.Owners = false, "trunk is protected; owners: sam", []string{"sam"}
		}, "protection", "sam", ""},
		{"armed", func(o *landingGetOut) { o.Landing.AutoBy = "alex" }, "nothing", "", ""},
		{"ready", nil, "nothing", "", "quarry api POST /r/{repo}/landing/41/auto -f on:=true"},
	}
	for _, c := range cases {
		w := landingWaitingOf(openLanding(c.mut))
		if w.On != c.on || w.Who != c.who {
			t.Errorf("FAIL: %s: on %q who %q, want %q %q", c.name, w.On, w.Who, c.on, c.who)
		}
		if strings.TrimSpace(w.Say) == "" || w.Next == nil || strings.TrimSpace(w.Next.Say) == "" {
			t.Errorf("FAIL: %s: no say or no next: %+v", c.name, w)
		}
		if strings.HasPrefix(w.Say, "!") || strings.Contains(w.Say, "!41:") {
			t.Errorf("FAIL: %s: say opens with the landing's own number: %q", c.name, w.Say)
		}
		if c.cmd != "" && w.Next.Cmd != c.cmd {
			t.Errorf("FAIL: %s: next cmd %q, want %q", c.name, w.Next.Cmd, c.cmd)
		}
	}
	// An armed landing and a ready one differ in what they offer.
	if armed := landingWaitingOf(openLanding(func(o *landingGetOut) { o.Landing.AutoBy = "alex" })); armed.Next.Cmd != "" {
		t.Errorf("FAIL: an armed landing offers %q", armed.Next.Cmd)
	}
}

func TestLandingWaitingWireKeys(t *testing.T) {
	b, err := json.Marshal(landingWaitingOf(openLanding(func(o *landingGetOut) {
		o.MayLand, o.MayLandReason, o.Owners = false, "trunk is protected; owners: sam", []string{"sam"}
	})))
	if err != nil {
		t.Fatal(err)
	}
	var m map[string]any
	json.Unmarshal(b, &m)
	for _, k := range []string{"on", "say", "who", "next"} {
		if _, ok := m[k]; !ok {
			t.Errorf("FAIL: waiting has no key %q: %s", k, b)
		}
	}
	whole, _ := json.Marshal(landingGetOut{Waiting: landingWaitingOf(openLanding(nil))})
	if !strings.Contains(string(whole), `"waiting":{`) {
		t.Errorf("FAIL: landing.get carries no waiting key")
	}
}

// landing.get itself carries the key, with a sentence and a next.
func TestLandingGetCarriesWaiting(t *testing.T) {
	tip := "f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1f1"
	d := Deps{Version: "v", CI: verdictRuns(), Checks: checksStub{required: []string{"quarry", "race"}},
		Repos: NewRepos(map[string]*Repo{"test": {Chat: "test", Index: openLandingIndex(t, "feature", tip)}})}
	r := ops.NewRegistry()
	if err := RegisterAll(r, d); err != nil {
		t.Fatal(err)
	}
	got, err := r.Invoke(ops.WithCaps(context.Background(), oltp.CapSet("o2")), "landing.get",
		landingGetIn{Repo: "test", ID: landingHash})
	if err != nil {
		t.Fatal(err)
	}
	w, _ := verdictJSON(t, got)["waiting"].(map[string]any)
	next, _ := w["next"].(map[string]any)
	if w["on"] == "" || w["say"] == "" || next["say"] == "" {
		t.Errorf("FAIL: landing.get waiting is %v", w)
	}
}