Skip to content
quarry Artifact [c7802a0d64]
Login

Artifact [c7802a0d64]

Artifact c7802a0d64f04b439b21f2d01f5aff2ba26fb1c114423bfa124fa3bb49a60ee0:


package api

import (
	"strings"
	"testing"

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

const loginLabelForTest = "mac-mini, pi-c"

func loginCtx(label string) (c ops.TokenAuth) {
	return ops.TokenAuth{ID: "login-token-1", Name: label, Caps: func(string) ops.Caps { return oltp.CapSet("oi") }}
}

// A landing accepted on a login token is the person's accept, said "via"
// the token's label: the merge's author is the person, its message and
// the forge journal name the machine.
func TestALandingAcceptedOnALoginTokenIsThePersonsViaItsLabel(t *testing.T) {
	dir, ids, ix, pipeSync := premergeStore(t)
	root := ztip(t, dir, "trunk")
	ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
	ci.green(ztip(t, dir, "one"), root)
	acts := &recordedActs{}
	d := Deps{Version: "v", Checks: mergeChecks{}, CI: ci, Acts: acts, Repos: NewRepos(map[string]*Repo{
		"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
	ctx := ops.WithToken(ops.WithUser(t.Context(), "dan"), loginCtx(loginLabelForTest))
	if got := ops.Via(ctx); got != "login-token-1" || ops.ViaLabel(ctx) != loginLabelForTest {
		t.Errorf("FAIL: via is %q / %q", got, ops.ViaLabel(ctx))
	}
	if _, err := d.landingSay(ctx, landingSayIn{Repo: "test", ID: ids[0]}, "accept"); err != nil {
		t.Fatal(err)
	}
	merges, err := ix.MergesForLandings([]string{ids[0]})
	if err != nil {
		t.Fatal(err)
	}
	m := merges[ids[0]]
	if m.User != "dan" || !strings.Contains(m.Comment, "via "+loginLabelForTest) {
		t.Errorf("FAIL: the merge is by %q, comment %q", m.User, m.Comment)
	}
	if len(acts.acts) != 1 || acts.acts[0].Actor != "dan via "+loginLabelForTest || acts.acts[0].Op != "landing.accept" || acts.acts[0].Repo != "test" {
		t.Errorf("FAIL: the journal: %+v", acts.acts)
	}
	if l, ok, _ := ix.LandingByPrefix(ids[0]); !ok || l.State != "accepted" {
		t.Errorf("FAIL: the landing is %+v", l)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// An accept pressed by the person alone says no "via".
func TestAnAcceptWithoutATokenSaysNoVia(t *testing.T) {
	dir, ids, ix, pipeSync := premergeStore(t)
	root := ztip(t, dir, "trunk")
	ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
	ci.green(ztip(t, dir, "one"), root)
	acts := &recordedActs{}
	d := Deps{Version: "v", Checks: mergeChecks{}, CI: ci, Acts: acts, Repos: NewRepos(map[string]*Repo{
		"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
	if _, err := d.landingSay(as("dan", "i"), landingSayIn{Repo: "test", ID: ids[0]}, "accept"); err != nil {
		t.Fatal(err)
	}
	merges, _ := ix.MergesForLandings([]string{ids[0]})
	if strings.Contains(merges[ids[0]].Comment, "via") || len(acts.acts) != 0 {
		t.Errorf("FAIL: a person's own accept says via: %q %+v", merges[ids[0]].Comment, acts.acts)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// The deferred accept of a landing armed on a login token reads the same.
func TestAnAutoLandArmedOnALoginTokenIsViaItsLabel(t *testing.T) {
	dir, ids, ix, pipeSync := premergeStore(t)
	root := ztip(t, dir, "trunk")
	ci := &mergeRuns{pairRuns: &pairRuns{runs: map[string][]CIRun{}}}
	ci.green(ztip(t, dir, "two"), root)
	acts := &recordedActs{}
	d := Deps{Version: "v", ZitVersion: zitcli.MinVersion, Checks: mergeChecks{}, CI: ci, Notify: &fakeNotify{}, Acts: acts,
		Repos: NewRepos(map[string]*Repo{"test": {Chat: "test", Index: ix, Zit: zitcli.New(dir), Sync: pipeSync}})}
	store := &sweepStub{rows: []oltp.AutoAccept{{Repo: "test", Landing: ids[1], Account: "dan", Tip: ztip(t, dir, "two"), Via: loginLabelForTest}}}
	if landed := d.AutoAcceptSweep(store); len(landed) != 1 {
		t.Fatalf("FAIL: the sweep landed %v", landed)
	}
	merges, _ := ix.MergesForLandings([]string{ids[1]})
	m := merges[ids[1]]
	if m.User != "dan" || !strings.Contains(m.Comment, "via "+loginLabelForTest) {
		t.Errorf("FAIL: the merge is by %q, comment %q", m.User, m.Comment)
	}
	if len(acts.acts) != 1 || acts.acts[0].Actor != "dan via "+loginLabelForTest {
		t.Errorf("FAIL: the journal: %+v", acts.acts)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}