Skip to content
quarry Artifact [0940be817c]
Login

Artifact [0940be817c]

Artifact 0940be817c16e1301b73814918230e118972ead1a56a4dcf3b87928e469c7505:


package ingest

import (
	"reflect"
	"testing"

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

func f(name, hash string) artifact.File { return artifact.File{Name: name, Hash: hash} }

func TestApplyDelta(t *testing.T) {
	base := []artifact.File{f("a", "1"), f("b", "2"), f("d", "4")}
	delta := []artifact.File{f("b", "9"), f("c", "3"), {Name: "d"}} // edit, add, delete
	got := applyDelta(base, delta)
	want := []artifact.File{f("a", "1"), f("b", "9"), f("c", "3")}
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("applyDelta = %v, want %v", got, want)
	}
}

func TestDiffLists(t *testing.T) {
	old := []artifact.File{f("a", "1"), f("b", "2"), f("d", "4")}
	new := []artifact.File{f("a", "1"), f("b", "9"), f("c", "3")}
	got := diffLists(old, new)
	want := []string{"b", "c", "d"} // edit, add, delete
	if !reflect.DeepEqual(got, want) {
		t.Fatalf("diffLists = %v, want %v", got, want)
	}
}

func TestChangesShapes(t *testing.T) {
	fl := newFileLists(nil)
	// A root baseline: every file is a change.
	root := &artifact.Artifact{Type: artifact.Manifest,
		Files: []artifact.File{f("a", "1"), f("src/x", "2")}}
	if got := fl.changes("r0", root); !reflect.DeepEqual(got, []string{"a", "src/x"}) {
		t.Fatalf("root changes = %v", got)
	}
	// A delta: its F cards are the changes, rename counts both sides.
	delta := &artifact.Artifact{Type: artifact.Manifest, Baseline: "r0",
		Parents: []string{"r0"},
		Files:   []artifact.File{{Name: "b", Hash: "3", OldName: "a"}}}
	if got := fl.changes("d1", delta); !reflect.DeepEqual(got, []string{"b", "a"}) {
		t.Fatalf("delta changes = %v", got)
	}
	// A child baseline diffs against the parent's expanded list.
	child := &artifact.Artifact{Type: artifact.Manifest, Parents: []string{"d1"},
		Files: []artifact.File{f("b", "3"), f("src/x", "9")}}
	if got := fl.changes("b2", child); !reflect.DeepEqual(got, []string{"src/x"}) {
		t.Fatalf("baseline-child changes = %v", got)
	}
}