Skip to content
quarry Artifact [73dbd98928]
Login

Artifact [73dbd98928]

Artifact 73dbd98928c92d1a938187f0968c7c1d9ce20078855627f6368c2a0d7f64a06c:


package index

import (
	"path/filepath"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/artifact"
)

// The merge page asks for the same branch's tip several times a request.
// The memo answers without the database until a fold moves the
// generation, and then answers the tip the fold made.
func TestBranchTipMemoHoldsUntilAFold(t *testing.T) {
	ix, err := Open(filepath.Join(t.TempDir(), "index.db"))
	if err != nil {
		t.Fatal(err)
	}
	defer ix.Close()
	h := func(c byte) string {
		return string([]byte{c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c})
	}
	ci := func(hash, date string, parents []string) Parsed {
		return Parsed{Hash: hash, Art: &artifact.Artifact{Type: artifact.Manifest, Date: date, User: "dan", Comment: "c", Parents: parents}}
	}
	if err := ix.FoldNow([]Parsed{ci(h('a'), "2026-01-01T00:00:00", nil)}); err != nil {
		t.Fatal(err)
	}
	tip, err := ix.BranchTip("trunk")
	if err != nil || tip != h('a') {
		t.Fatalf("FAIL: first read %q %v, want %q", tip, err, h('a'))
	}
	// Planted where only the memo could return it: no fold has said the
	// answer moved, so the database is not asked again.
	ix.tipMu.Lock()
	for k, e := range ix.tipMemo {
		e.tip = "memo-only"
		ix.tipMemo[k] = e
	}
	ix.tipMu.Unlock()
	if got, _ := ix.BranchTip("trunk"); got != "memo-only" {
		t.Errorf("FAIL: a repeat read asked the database again: %q", got)
	}
	if err := ix.FoldNow([]Parsed{ci(h('b'), "2026-01-02T00:00:00", []string{h('a')})}); err != nil {
		t.Fatal(err)
	}
	if got, _ := ix.BranchTip("trunk"); got != h('b') {
		t.Errorf("FAIL: after a fold the memo answered %q, want the new tip %q", got, h('b'))
	}
	for i := 0; i < tipMemoMax+10; i++ {
		ix.BranchTip(string(rune('A'+i%26)) + string(rune('a'+i/26%26)) + string(rune('0'+i/676)))
	}
	ix.tipMu.Lock()
	n := len(ix.tipMemo)
	ix.tipMu.Unlock()
	if n > tipMemoMax {
		t.Errorf("FAIL: the memo holds %d names, bound %d", n, tipMemoMax)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A crosslink rebuild folds check-ins with FoldCheckins: a tip read
// after it moved a branch is the new one, not what was kept before.
func TestBranchTipAfterFoldCheckinsIsTheNewOne(t *testing.T) {
	ix, err := Open(filepath.Join(t.TempDir(), "index.db"))
	if err != nil {
		t.Fatal(err)
	}
	defer ix.Close()
	a, b := strings.Repeat("a", 40), strings.Repeat("b", 40)
	rec := func(h string, parents []string, date string) CheckinRecord {
		return CheckinRecord{Hash: h, Date: date, User: "u", Comment: "c", Parents: parents}
	}
	if err := ix.FoldCheckins([]CheckinRecord{rec(a, nil, "2026-01-01T00:00:00.000")}); err != nil {
		t.Fatal(err)
	}
	if got, _ := ix.BranchTip("trunk"); got != a {
		t.Fatalf("FAIL: first tip %q, want %q", got, a)
	}
	if err := ix.FoldCheckins([]CheckinRecord{rec(b, []string{a}, "2026-01-02T00:00:00.000")}); err != nil {
		t.Fatal(err)
	}
	if got, _ := ix.BranchTip("trunk"); got != b {
		t.Errorf("FAIL: after FoldCheckins the tip is %q, want %q", got, b)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}