Skip to content
quarry Artifact [edcfb8d811]
Login

Artifact [edcfb8d811]

Artifact edcfb8d8110f20da7de06c8edf1300354ab3a142c4e54aaacd9d46a8e9035cf5:


package api

import (
	"testing"

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

// ownersOnly protects trunk: only the named owner may land on it.
type ownersOnly struct{ owner string }

func (o ownersOnly) MayJoin(_, user, branch string) bool { return branch != "trunk" || user == o.owner }
func (o ownersOnly) Protections(string) ([]oltp.Protection, error) {
	return []oltp.Protection{{Branch: "trunk", Owners: []string{o.owner}}}, nil
}

// The Landings tab lists the decisions the reader could make: every
// open landing where the accept would not refuse them, and none where
// it would.
func TestLandingQueueListsWhatTheReaderMayAccept(t *testing.T) {
	dir, ids, ix, pipeSync := premergeStore(t)
	letters := map[string]string{"dan": "i", "eve": "2", "own": "i", "sam": "i"}
	queue := func(user string, protect ProtectStore) []queuedLanding {
		t.Helper()
		r := ops.NewRegistry()
		deps := Deps{Version: "v", Protect: protect,
			CapsFor: func(who, _ string) CapSet { return oltp.CapSet(letters[who]) },
			Repos: NewRepos(map[string]*Repo{
				"test": {Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
		if err := RegisterAll(r, deps); err != nil {
			t.Fatal(err)
		}
		out, err := r.Invoke(ops.WithUser(t.Context(), user), "landing.queue", landingQueueIn{})
		if err != nil {
			t.Fatal(err)
		}
		return out.(landingQueueOut).Landings
	}
	has := func(ls []queuedLanding, id string) bool {
		for _, l := range ls {
			if l.ID == id && l.Repo == "test" {
				return true
			}
		}
		return false
	}

	if got := queue("dan", nil); !has(got, ids[0]) || !has(got, ids[1]) {
		t.Errorf("FAIL: an account with the check-in letter misses open landings: %+v", got)
	} else {
		t.Log("ok: both open landings are queued for an accepter")
	}
	if got := queue("eve", nil); len(got) != 0 {
		t.Errorf("FAIL: an account without the check-in letter is queued %d landings", len(got))
	} else {
		t.Log("ok: nothing is queued for a reader who may not accept")
	}
	if got := queue("sam", ownersOnly{owner: "own"}); len(got) != 0 {
		t.Errorf("FAIL: a non-owner is queued landings onto a protected trunk: %+v", got)
	} else {
		t.Log("ok: a protected target queues nothing for a non-owner")
	}
	if got := queue("own", ownersOnly{owner: "own"}); !has(got, ids[0]) || !has(got, ids[1]) {
		t.Errorf("FAIL: the protected branch's owner misses its landings: %+v", got)
	} else {
		t.Log("ok: the owner of the protected target is queued its landings")
	}
}