Skip to content
quarry Artifact [c7001a6703]
Login

Artifact [c7001a6703]

Artifact c7001a67032666e8f46b2c6e9ccfaf124ff12abe057afcac8cf01251089fd220:


package index

import (
	"strings"
	"testing"

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

// tieChain folds a root and a chain of three check-ins committed in the
// same second, whose hashes fall as the chain grows: ordered by hash,
// each parent would sort above its child.
func tieChain(t *testing.T) *Index {
	t.Helper()
	ix := openIndex(t)
	h := func(b string) string { return strings.Repeat(b, 32) }
	ci := func(hash, date, parent string) Parsed {
		art := &artifact.Artifact{Type: artifact.Manifest, User: "dan", Date: date, Comment: hash[:2]}
		if parent != "" {
			art.Parents = []string{parent}
		}
		return Parsed{Hash: hash, Art: art}
	}
	batch := []Parsed{
		ci(h("11"), "2026-10-25T22:00:00", ""),
		ci(h("ee"), "2026-10-25T22:48:06", h("11")),
		ci(h("cc"), "2026-10-25T22:48:06", h("ee")),
		ci(h("aa"), "2026-10-25T22:48:06", h("cc")),
	}
	if err := ix.FoldNow(batch); err != nil {
		t.Fatal(err)
	}
	return ix
}

func tieOrder(es []TimelineEntry) string {
	var b []string
	for _, e := range es {
		b = append(b, e.Hash[:2])
	}
	return strings.Join(b, " ")
}

// Check-ins in the same second are shown child above parent, as the
// graph draws them; their hashes do not decide it.
func TestTimelineShowsASameSecondChildAboveItsParent(t *testing.T) {
	ix := tieChain(t)
	es, _, err := ix.Timeline(TimelineFilter{Kind: "ci", Limit: 10})
	if err != nil {
		t.Fatal(err)
	}
	if got := tieOrder(es); got != "aa cc ee 11" {
		t.Fatalf("FAIL: order %q; want aa cc ee 11", got)
	}
	t.Log("ok: a same-second chain reads child first")
}

// Paging through a same-second run still shows every check-in once: the
// rows a cursor is taken from stay where the query put them.
func TestTimelinePagesThroughASameSecondRunOnce(t *testing.T) {
	ix := tieChain(t)
	seen := map[string]int{}
	f := TimelineFilter{Kind: "ci", Limit: 2}
	for range 5 {
		es, more, err := ix.Timeline(f)
		if err != nil {
			t.Fatal(err)
		}
		for _, e := range es {
			seen[e.Hash[:2]]++
		}
		if !more {
			break
		}
		last := es[len(es)-1]
		f.Before = last.Date + "~" + last.Hash
	}
	for _, h := range []string{"11", "ee", "cc", "aa"} {
		if seen[h] != 1 {
			t.Fatalf("FAIL: pages showed %v; want each check-in once", seen)
		}
	}
	t.Log("ok: paging shows each check-in once")
}