Skip to content
quarry Artifact [f6ea92cb21]
Login

Artifact [f6ea92cb21]

Artifact f6ea92cb21fae8616dbf15afd22a60e11a5e0f7e9336c820c992f95150152811:


package ingest

import (
	"context"
	"os/exec"
	"strings"
	"testing"

	"craftdesign.group/quarry/internal/lease"
	"craftdesign.group/quarry/internal/zitcli"
)

// versions commits n revisions of one large file, so a pack has
// lineages to re-encode and has real work to do.
func versions(t *testing.T, repo string, n int) {
	t.Helper()
	body := strings.Repeat("a line of text that repeats\n", 400)
	for i := 0; i < n; i++ {
		commitFile(t, repo, "big.txt", body+strings.Repeat("x", i+1)+"\n", "revise")
	}
}

// A caught-up store that is packed leaves the saved cursor valid: the
// next round reads zero artifacts and starts no rebuild.
func TestPackOfCaughtUpStoreReReadsNothing(t *testing.T) {
	pipe, _, repo := testPipeline(t)
	versions(t, repo, 6)
	if _, err := pipe.Sync(); err != nil {
		t.Fatalf("FAIL: first round: %v", err)
	}
	s := watchSpans(t)
	res, err := zitcli.New(repo).Pack(lease.Unguarded("test"), context.Background())
	if err != nil {
		t.Fatalf("FAIL: pack: %v", err)
	}
	if res.Nothing || res.Reclaimed != 0 {
		t.Fatalf("FAIL: pack result = %+v, want a rewrite that dropped nothing", res)
	}
	n, err := pipe.Sync()
	if err != nil {
		t.Fatalf("FAIL: round after pack: %v", err)
	}
	if n != 0 {
		t.Errorf("FAIL: the round after a pack read %d artifacts, want 0", n)
	}
	if _, in := s.counts(); in != 0 {
		t.Errorf("FAIL: rebuilds started = %d, want 0", in)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// A pack that drops something (a pending shun) kills the cursor, and
// the next round takes the rebuild path.
func TestPackThatDropsArtifactsRebuilds(t *testing.T) {
	pipe, _, repo := testPipeline(t)
	versions(t, repo, 3)
	if _, err := pipe.Sync(); err != nil {
		t.Fatalf("FAIL: first round: %v", err)
	}
	head, err := exec.Command("zit", "ls", "--long", "-R", repo, "tip").Output()
	if err != nil {
		t.Fatal(err)
	}
	run(t, repo, "shun", "--defer", "--force", strings.Fields(string(head))[0])
	s := watchSpans(t)
	res, err := zitcli.New(repo).Pack(lease.Unguarded("test"), context.Background())
	if err != nil {
		t.Fatalf("FAIL: pack: %v", err)
	}
	if res.Reclaimed != 1 {
		t.Errorf("FAIL: reclaimed = %d, want 1", res.Reclaimed)
	}
	if _, err := pipe.Sync(); err != nil {
		t.Fatalf("FAIL: round after pack: %v", err)
	}
	if _, in := s.counts(); in != 1 {
		t.Errorf("FAIL: rebuilds started = %d, want 1", in)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}