Skip to content
quarry Artifact [e989fc1e35]
Login

Artifact [e989fc1e35]

Artifact e989fc1e35add0872507171195e156547e6fdf0b844bc92edf17040f130b0e1e:


package api

import (
	"context"
	"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")
	}
}

// A repository under a takedown contributes no landing to the queue of
// somebody the takedown shuts out, and still does for whoever it admits.
func TestLandingQueueSkipsAHeldRepository(t *testing.T) {
	dir, ids, ix, pipeSync := premergeStore(t)
	queue := func(user string) []queuedLanding {
		t.Helper()
		r := ops.NewRegistry()
		deps := Deps{Version: "v",
			CapsFor: func(string, string) CapSet { return oltp.CapSet("i") },
			Held: func(ctx context.Context, _ string) string {
				if ops.User(ctx) == "keeper" {
					return ""
				}
				return "unavailable for legal reasons"
			},
			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
	}
	if got := queue("stranger"); len(got) != 0 {
		t.Errorf("FAIL: a held repository queued %d landings for somebody it shuts out", len(got))
	}
	if got := queue("keeper"); len(got) != len(ids) {
		t.Errorf("FAIL: the admitted account is queued %d landings, want %d", len(got), len(ids))
	}
	if !t.Failed() {
		t.Log("ok")
	}
}