Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/source_test.go
T

168 lines
7.6 KiB
Go

package index
import (
"context"
"reflect"
"strings"
"testing"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
)
const otherMerge = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
// srcT is a source revision's heads as the tests write them; applyS says it as the verb source answers it.
type srcT struct {
Group string
Heads map[string]string
}
func sourceOf(repo, head string) srcT { return srcT{Group: "g", Heads: map[string]string{repo: head}} }
func applyS(ix *Indexer, ctx context.Context, rev uint64, s srcT) error {
a := meshdelivery.SourceAnswer{Branch: "main", Group: s.Group, Revision: int64(rev)}
for r, h := range s.Heads {
a.Repositories = append(a.Repositories, meshdelivery.SourceHead{Repository: r, Head: h, Trunk: "main"})
}
return ix.ApplySource(ctx, a)
}
func TestASourceRevisionStampsTheModulesOfTheRepositoriesHeadItNames(t *testing.T) { // 1344/AC1
ix, mods := modulesIndexer(t)
ctx := context.Background()
if err := ix.Apply(ctx, mergedMsg("novox/mesh-delivery", map[string]string{"modules/a": "newtree"})); err != nil {
t.Fatal(err)
}
before, _ := moduleOf(t, mods, "a")
if before.Src != 0 {
t.Fatalf("module before the source = %+v, want Src 0", before)
}
// The source names the head merged for the delivery repository, and another repository's head no module of ours is built from.
s := srcT{Group: "g", Heads: map[string]string{"novox/mesh-delivery": merge, "novox/mesh-catalog": otherMerge}}
if err := applyS(ix, ctx, 7, s); err != nil {
t.Fatal(err)
}
m, ok := moduleOf(t, mods, "a")
want := before
want.Src, want.FpSrc = 7, 7 // its fingerprint moved at that merge (ADR 0601 §1)
if !ok || !reflect.DeepEqual(m, want) {
t.Errorf("module a = %+v (%v), want %+v: the fingerprint and the commit as the merge wrote them, with Src 7", m, ok, want)
}
if c, ok := moduleOf(t, mods, "c"); ok {
t.Errorf("module c = %+v: written for a head no merge message of the indexer carried", c)
}
// The same revision again changes nothing, and so does a later one: Src is the first revision naming the commit.
if err := applyS(ix, ctx, 7, s); err != nil {
t.Fatal(err)
}
if err := applyS(ix, ctx, 9, s); err != nil {
t.Fatal(err)
}
if m, _ := moduleOf(t, mods, "a"); m.Src != 7 {
t.Errorf("module a Src = %d, want 7", m.Src)
}
}
func TestAMergeHeardAfterTheSourceNamesItsHeadIsStampedWhenWritten(t *testing.T) { // 1344/AC1 (the source first)
// A source revision older than the one held is left (merged here from a test of its own, for the test budget).
t.Run("an older source revision is left", func(t *testing.T) {
ix, mods := modulesIndexer(t, rec(t, "build-1", "a", "novox/mesh-delivery", "modules/a", "main", "", "2026-10-19T09:00:00Z", aInputs))
ctx := context.Background()
_ = ix.Apply(ctx, mergedMsg("novox/mesh-delivery", map[string]string{"modules/a": "newtree"}))
_ = applyS(ix, ctx, 9, sourceOf("novox/mesh-delivery", merge))
_ = applyS(ix, ctx, 7, sourceOf("novox/mesh-delivery", merge))
if m, _ := moduleOf(t, mods, "a"); m.Src != 9 {
t.Errorf("module a Src = %d, want 9", m.Src)
}
})
ix, mods := modulesIndexer(t, rec(t, "build-1", "a", "novox/mesh-delivery", "modules/a", "main", "", "2026-10-19T09:00:00Z", aInputs))
ctx := context.Background()
if err := applyS(ix, ctx, 7, sourceOf("novox/mesh-delivery", merge)); err != nil {
t.Fatal(err)
}
if _, ok := moduleOf(t, mods, "a"); ok {
t.Fatal("a module was written from a source revision alone: its tree is the merge message's")
}
if err := ix.Apply(ctx, mergedMsg("novox/mesh-delivery", map[string]string{"modules/a": "newtree"})); err != nil {
t.Fatal(err)
}
if m, ok := moduleOf(t, mods, "a"); !ok || m.Src != 7 || m.Commit != merge || m.Path != "modules/a" {
t.Errorf("module a = %+v (%v), want the merge's fingerprint with Src 7", m, ok)
}
}
func TestAMergeThatNoSourceRevisionNamesIsWrittenAsToday(t *testing.T) { // 1344/AC2
ix, mods := modulesIndexer(t, rec(t, "build-1", "a", "novox/mesh-delivery", "modules/a", "main", "", "2026-10-19T09:00:00Z", aInputs))
ctx := context.Background()
// No source revision yet.
if err := ix.Apply(ctx, mergedMsg("novox/mesh-delivery", map[string]string{"modules/a": "newtree"})); err != nil {
t.Fatal(err)
}
if m, ok := moduleOf(t, mods, "a"); !ok || m.Src != 0 || m.Path != "modules/a" {
t.Fatalf("module a = %+v (%v), want the merge written with Src 0", m, ok)
}
// A source revision that names another head of the repository: a merge of the head it does not name keeps Src 0.
if err := applyS(ix, ctx, 7, sourceOf("novox/mesh-delivery", otherMerge)); err != nil {
t.Fatal(err)
}
if m, _ := moduleOf(t, mods, "a"); m.Src != 0 {
t.Errorf("module a Src = %d after a source naming another head, want 0", m.Src)
}
merged := mergedMsg("novox/mesh-delivery", map[string]string{"modules/a": "newertree"})
merged.Merge = "cccccccccccccccccccccccccccccccccccccccc"
if err := ix.Apply(ctx, merged); err != nil {
t.Fatal(err)
}
if m, _ := moduleOf(t, mods, "a"); m.Src != 0 || m.Commit != merged.Merge {
t.Errorf("module a = %+v, want the new merge with Src 0", m)
}
}
func TestAModuleBuiltAgainstASourceHeadIsComputedAgainWhenThatHeadMoves(t *testing.T) { // design 47 G A-365
held := map[string]meshcatalog.ModulesByModuleValue{"a": {Packages: map[string]string{"git.novox.be/novox/mesh-contracts/go/seats/findings": otherMerge}}}
for _, row := range []struct {
name string
before, now map[string]string
want bool
}{
{"the head its version came from moved", map[string]string{"novox/mesh-contracts": otherMerge}, map[string]string{"novox/mesh-contracts": merge}, true},
{"the head stands", map[string]string{"novox/mesh-contracts": otherMerge}, map[string]string{"novox/mesh-contracts": otherMerge}, false},
{"another repository's head moved", map[string]string{"novox/mesh-contracts": otherMerge}, map[string]string{"novox/mesh-contracts": otherMerge, "novox/mesh-sdk": merge}, false},
{"no source seen before", nil, map[string]string{"novox/mesh-contracts": merge}, false},
} {
if got := movedPackages(held, row.before, row.now); got != row.want {
t.Errorf("%s: movedPackages = %v, want %v", row.name, got, row.want)
}
}
}
// Design 47 G A-365: through ApplySource, a held module whose package no module publishes was built against the head the source
// named; when the source moves that repository's head, the module's fingerprint is computed again against the new head.
func TestApplySourceComputesAgainTheModuleBuiltAgainstTheHeadThatMoved(t *testing.T) {
ctx := context.Background()
const pkg = "git.novox.be/novox/mesh-contracts/go/seats/findings"
ix, mods := newFormIndexer(t, mod{"avahi", catalogueManifest(t, "avahi")})
reportToolchain(t, ix, "mesh-build-go", "novox", "go", "go1.27.1")
ix.Packages = newMem()
setPackages(t, mods, "avahi", []string{pkg})
h1, h2 := strings.Repeat("1", 40), strings.Repeat("2", 40)
if err := applyS(ix, ctx, 1, sourceOf("novox/mesh-contracts", h1)); err != nil {
t.Fatal(err)
}
if err := ix.Recompute(ctx); err != nil {
t.Fatal(err)
}
first, _ := moduleOf(t, mods, "avahi")
if first.Fp2 == "" || first.Packages[pkg] != h1 {
t.Fatalf("before the head moves: fp2 %q none2 %q packages %v, want a fingerprint built against %s", first.Fp2, first.None2, first.Packages, h1)
}
if err := applyS(ix, ctx, 2, sourceOf("novox/mesh-contracts", h2)); err != nil {
t.Fatal(err)
}
moved, _ := moduleOf(t, mods, "avahi")
if moved.Packages[pkg] != h2 || moved.Fp2 == first.Fp2 {
t.Errorf("after the head moves: fp2 %q (was %q) packages %v, want it computed again against %s", moved.Fp2, first.Fp2, moved.Packages, h2)
}
}