package versions import ( "encoding/json" "context" "errors" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds" "reflect" "testing" "time" ) type reserveMem struct { *mem onUpdate func() // runs once before the next Update } func (m *reserveMem) Delete(_ context.Context, key string) error { m.mu.Lock() defer m.mu.Unlock() delete(m.vals, key) delete(m.revs, key) return nil } func (m *reserveMem) Update(ctx context.Context, key string, value []byte, rev uint64) (uint64, error) { if f := m.onUpdate; f != nil { m.onUpdate = nil f() } return m.mem.Update(ctx, key, value, rev) } // TestReservation: design 47 G §369 (test 102): two builds close together reserve two versions, the key goes on // version-released, a built reservation with an artifact unpublished past the bound is unreleased, and nothing is reserved // before versions-filled. func TestReservation(t *testing.T) { ctx := context.Background() now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC) released := []string{"0.1.18", "0.1.19", "0.2.7"} filled := true store := &reserveMem{mem: newMem()} r := &Reserver{Store: store, Filled: func() bool { return filled }, Now: func() time.Time { return now }, ReleasedOf: func(context.Context, string) ([]string, error) { return released, nil }} arts := []string{"m:a", "m:b"} filled = false if _, err := r.Reserve(ctx, "m", "0.1", arts, Source{}); !errors.Is(err, ErrNotFilled) { t.Fatalf("before versions-filled: %v, want ErrNotFilled", err) } if got, _ := r.All(ctx); len(got) != 0 { t.Fatalf("a reservation was written before the fill: %v", got) } filled = true first, err := r.Reserve(ctx, "m", "0.1", arts, Source{}) if err != nil || first.Version != "0.1.20" { t.Fatalf("first build reserves %q (%v), want 0.1.20 after the highest released 0.1.19", first.Version, err) } var tookOlder, tookNewer []string r.Took = func(_ context.Context, older, newer ReservedVersion) { tookOlder, tookNewer = append(tookOlder, older.Version), append(tookNewer, newer.Version) } second, err := r.Reserve(ctx, "m", "0.1", arts, Source{}) if len(tookOlder) != 1 || tookOlder[0] != "0.1.20" || tookNewer[0] != "0.1.21" { t.Fatalf("takeover of the older reservation told %v by %v, want 0.1.20 by 0.1.21 (design 47 G A-455)", tookOlder, tookNewer) } if err != nil || second.Version != "0.1.21" { t.Fatalf("second build reserves %q (%v), want 0.1.21: never the first's", second.Version, err) } // Two builds at once: the rival reserves between this one's read and write, and this one takes the next. store.onUpdate = func() { if _, err := r.Reserve(ctx, "m", "0.1", arts, Source{}); err != nil { t.Error(err) } } third, err := r.Reserve(ctx, "m", "0.1", arts, Source{}) if err != nil || third.Version != "0.1.23" { t.Fatalf("a build that lost the compare-and-set reserves %q (%v), want 0.1.23", third.Version, err) } if v, _ := r.Reserve(ctx, "m", "0.3", arts, Source{}); v.Version != "0.3.0" { t.Errorf("a series with nothing released reserves %q, want 0.3.0", v.Version) } // The artifacts' digests, then the bound. for _, a := range arts { if ok, err := r.Built(ctx, "m", "0.1.23", a, "sha256:"+a, nil); err != nil || !ok { t.Fatalf("built %s: %v %v", a, ok, err) } } if ok, _ := r.Built(ctx, "m", "0.1.22", "m:a", "x", nil); ok { t.Error("a digest was written under a version a newer build superseded") } published := func(_, _, a string) bool { return a == "m:a" } if got, _ := r.Overdue(ctx, published); len(got) != 0 { t.Fatalf("unreleased before the bound: %v", got) } now = now.Add(UnreleasedBound) got, err := r.Overdue(ctx, published) want := []Unreleased{{Module: "m", Version: "0.1.23", Missing: []string{"m:b"}}} if err != nil || !reflect.DeepEqual(got, want) { t.Fatalf("past the bound: %v (%v), want %v", got, err, want) } // version-released: its key goes, and the other series' keys of the module (the manifest moved on). if gone, err := r.Released(ctx, "m", "0.1.23"); err != nil || len(gone) != 2 { t.Fatalf("released 0.1.23 deleted %v (%v), want the 0.1 key and the 0.3 key", gone, err) } if all, _ := r.All(ctx); len(all) != 0 { t.Errorf("reservations left: %v", all) } if again, _ := r.Reserve(ctx, "m", "0.1", arts, Source{}); again.Version != "0.1.20" { t.Logf("with the catalog list unchanged the next is %s", again.Version) } if ok, _ := r.Drop(ctx, "m", "0.1"); !ok { t.Error("builder_reserved_drop found no reservation") } } // TestReservationIsReadAndMarkedPublished: design 47 G §369 — the reservation is read by builder_reserved and by the seat's // verb reserved with each artifact's digest, a registry seat's published marks the artifacts of that version and digest, and // the missing artifacts it names are the unpublished ones. func TestReservationIsReadAndMarkedPublished(t *testing.T) { ctx := context.Background() store := &reserveMem{mem: newMem()} r := &Reserver{Store: store, ReleasedOf: func(context.Context, string) ([]string, error) { return []string{"1.4.2"}, nil }} v, err := r.Reserve(ctx, "m", "1.4", []string{"m:a", "m:b"}, Source{Branch: "main", Revision: 41, CatalogRevision: 44}) if err != nil || v.Version != "1.4.3" { t.Fatalf("reserved %q (%v), want 1.4.3", v.Version, err) } deps := []meshbuilds.Dependency{{Kind: 1, Part: "mesh-sdk:go", Version: "0.1.14", Digest: "sha256:dd"}, {Kind: 3, Part: "go-toolchain:program", Version: "go1.27", Digest: "sha256:tt"}} if _, err := r.Built(ctx, "m", "1.4.3", "m:a", "sha256:aa", deps); err != nil { t.Fatal(err) } rd := Reader{ Keys: func() ([]string, error) { return store.KeysOf(ctx, AllFilter) }, Get: func(key string) (json.RawMessage, bool, error) { raw, _, ok, err := store.Get(ctx, key) return raw, ok, err }} ans, err := AnswerReservedVerb(rd, "m", "") want := meshbuilds.ReservedAnswer{Module: "m", Version: "1.4.3", Artifacts: []meshbuilds.ReservedAnswerArtifactsItem{{Artifact: "m:a", Digest: "sha256:aa"}, {Artifact: "m:b"}}} if err != nil || !reflect.DeepEqual(ans, want) { t.Fatalf("reserved answers %+v (%v), want %+v", ans, err, want) } if _, err := AnswerReservedVerb(rd, "m", "1.4.9"); err == nil { t.Error("a version never reserved was answered") } if got, err := AnswerReserved(rd, "other"); err != nil || len(got["reserved"].([]ReservedVersion)) != 0 { t.Errorf("builder_reserved of another module = %v (%v), want none", got, err) } marked, _, err := r.Published(ctx, "m-a", "v1.4.3", "sha256:aa") if err != nil || len(marked) != 1 { t.Fatalf("a published of 1.4.3 with a built digest marked %v (%v)", marked, err) } // Design 47 G §52, §673, A-57: the package-published of that artifact carries the digest published, the branch, the source // revision and the catalog revision (H-A-218) the reservation was made for, and the dependencies its step result named. at := time.Unix(100, 0) said := marked[0].PackagePublished("sha256:aa", at) atUTC := at.UTC() if wantSaid := (meshbuilds.PackagePublished{V: 1, Artifact: "m:a", At: &atUTC, Branch: "main", Source: 41, Revision: 44, Module: "m", Version: "1.4.3", Digest: "sha256:aa", Dependencies: deps}); !reflect.DeepEqual(said, wantSaid) { t.Errorf("package-published = %+v, want %+v", said, wantSaid) } if marked, _, _ := r.Published(ctx, "m-a", "1.4.3", "sha256:other"); len(marked) != 0 { t.Errorf("a published with another digest marked %v", marked) } cur, _, _ := r.At(ctx, ReservedKey("m", "1.4")) if got := cur.Missing(); !reflect.DeepEqual(got, []string{"m:b"}) { t.Errorf("missing = %v, want m:b", got) } if key, ok := ReservedBoundOf(ReservedBoundKey("m", "1.4")); !ok || key != ReservedKey("m", "1.4") || ReservedBoundFor(key) != ReservedBoundKey("m", "1.4") { t.Errorf("the bound key does not name the reservation back: %q %v", key, ok) } } // TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes: design 47 G A-456a — the version-released of a version records // that the build of its fingerprint was released, so a rerun of its step finds it after the reservation is deleted. func TestAReleaseIsRecordedByFingerprintBeforeItsReservationGoes(t *testing.T) { ctx := context.Background() rel := &reserveMem{mem: newMem()} r := &Reserver{Store: &reserveMem{mem: newMem()}, Releases: rel, ReleasedKept: 2, Filled: func() bool { return true }, ReleasedOf: func(context.Context, string) ([]string, error) { return nil, nil }} v, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "src2:aa"}) if err != nil { t.Fatal(err) } if ok, _ := r.ReleasedFor(ctx, "m", "src2:aa"); ok { t.Fatal("a build not released is said released") } if _, err := r.Released(ctx, "m", v.Version); err != nil { t.Fatal(err) } if ok, err := r.ReleasedFor(ctx, "m", "src2:aa"); !ok || err != nil { t.Errorf("released for the fingerprint: %v (%v), want true", ok, err) } if ok, _ := r.ReleasedFor(ctx, "m", "src2:bb"); ok { t.Error("another fingerprint is said released") } if all, _ := r.All(ctx); len(all) != 0 { t.Errorf("reservations left: %v", all) } // One key per module holds the last releases, the oldest dropped (A-456b). for _, fp := range []string{"src2:b1", "src2:b2"} { n, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: fp}) if err != nil { t.Fatal(err) } if _, err := r.Released(ctx, "m", n.Version); err != nil { t.Fatal(err) } } if keys, _ := rel.KeysOf(ctx, AllFilter); len(keys) != 1 { t.Errorf("release keys %v, want one per module", keys) } if old, _ := r.ReleasedFor(ctx, "m", "src2:aa"); old { t.Error("the oldest release is kept past released-kept") } if ok, _ := r.ReleasedFor(ctx, "m", "src2:b2"); !ok { t.Error("the newest release is not kept") } } // TestPublishedAnswersByTheBuildFingerprint: design 47 H-A-328 — the verb published answers published for a reservation of the // fingerprint whose artifact a registry's record marked published, with its version and digest, and for a release of it; an // artifact not yet marked, and another fingerprint, are not published. func TestPublishedAnswersByTheBuildFingerprint(t *testing.T) { ctx := context.Background() heard := time.Date(2026, 11, 21, 19, 0, 0, 0, time.UTC) store := &reserveMem{mem: newMem()} r := &Reserver{Store: store, Releases: &reserveMem{mem: newMem()}, ReleasedKept: 2, Filled: func() bool { return true }, ReleasedOf: func(context.Context, string) ([]string, error) { return nil, nil }, Now: func() time.Time { return heard }} v, err := r.Reserve(ctx, "mesh-grants", "0.1", []string{"mesh-grants:busgrants", "mesh-grants:grantsstate"}, Source{Fingerprint: "src2:aa"}) if err != nil { t.Fatal(err) } if _, err := r.Built(ctx, "mesh-grants", v.Version, "mesh-grants:busgrants", "sha256:bb", nil); err != nil { t.Fatal(err) } ask := func(artifact, fp string) meshbuilds.PublishedAnswer { t.Helper() a, err := r.PublishedOf(ctx, "mesh-grants", artifact, fp) if err != nil { t.Fatal(err) } return a } if a := ask("mesh-grants:busgrants", "src2:aa"); a.Published { t.Errorf("a built artifact no registry holds is answered %+v, want not published", a) } if marked, _, err := r.Published(ctx, "mesh-grants-busgrants", v.Version, "sha256:bb"); err != nil || len(marked) != 1 { t.Fatalf("the registry's published marked %v (%v)", marked, err) } // H-A-331: the mark keeps when the publish was heard, answered as at. if a := ask("mesh-grants:busgrants", "src2:aa"); !a.Published || a.Version != v.Version || a.Digest != "sha256:bb" || a.At == nil || !a.At.Equal(heard) { t.Errorf("published answered %+v, want %s at sha256:bb, heard %s", a, v.Version, heard) } // A mark written before the publish time was kept answers no time, never an invented one; a withdrawn number is never // answered published. key := ReservedKey("mesh-grants", "0.1") cur, rev, _, _ := r.get(ctx, key) older := cur older.PublishedAt = nil raw, _ := json.Marshal(older) if rev, err = store.Update(ctx, key, raw, rev); err != nil { t.Fatal(err) } if a := ask("mesh-grants:busgrants", "src2:aa"); !a.Published || a.At != nil { t.Errorf("an older mark answered %+v, want published with no at", a) } withdrawn := older withdrawn.Withdrawn = map[string]string{v.Version: "src2:aa"} raw, _ = json.Marshal(withdrawn) if rev, err = store.Update(ctx, key, raw, rev); err != nil { t.Fatal(err) } if a := ask("mesh-grants:busgrants", "src2:aa"); a.Published { t.Errorf("a withdrawn number is answered %+v, want not published", a) } raw, _ = json.Marshal(cur) if _, err = store.Update(ctx, key, raw, rev); err != nil { t.Fatal(err) } if a := ask("mesh-grants:grantsstate", "src2:aa"); a.Published { t.Errorf("an artifact not marked is answered %+v", a) } if a := ask("mesh-grants:busgrants", "src2:other"); a.Published { t.Errorf("another fingerprint is answered %+v", a) } if _, err := r.Released(ctx, "mesh-grants", v.Version); err != nil { t.Fatal(err) } if a := ask("mesh-grants:grantsstate", "src2:aa"); !a.Published || a.Version != v.Version { t.Errorf("after its release the fingerprint's artifact is answered %+v, want published at %s", a, v.Version) } // H-A-331: the kept release keeps each published digest with its version. if a := ask("mesh-grants:busgrants", "src2:aa"); !a.Published || a.Digest != "sha256:bb" { t.Errorf("after its release busgrants is answered %+v, want its digest sha256:bb", a) } if _, err := r.PublishedOf(ctx, "mesh-grants", "", "src2:aa"); err == nil { t.Error("an ask naming no artifact is answered") } } // TestTheWithdrawnVersionsAreDroppedOnceReleasedPast: design 47 H H-A-225, H-A-226, H-A-244 — a dropped number stays a gap, kept in // the series key's Withdrawn while it is above the catalog's highest released; at each write those at or below it go, so the // map never grows without end. func TestTheWithdrawnVersionsAreDroppedOnceReleasedPast(t *testing.T) { ctx := context.Background() store := &reserveMem{mem: newMem()} highest := "0.1.3" r := &Reserver{Store: store, Filled: func() bool { return true }, ReleasedOf: func(context.Context, string) ([]string, error) { return []string{highest}, nil }} for _, fp := range []string{"src2:a", "src2:b", "src2:c"} { if _, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: fp}); err != nil { t.Fatal(err) } } cur, _, _, _ := r.get(ctx, ReservedKey("m", "0.1")) if cur.Version != "0.1.6" || len(cur.Withdrawn) != 2 || cur.Withdrawn["0.1.4"] != "src2:a" || cur.Withdrawn["0.1.5"] != "src2:b" { t.Fatalf("reservation %+v; want 0.1.6 with 0.1.4 and 0.1.5 withdrawn", cur) } // H-A-244: the build whose number was withdrawn finds the reservation its series moved to; the one standing finds none. if by, took, err := r.TakenOver(ctx, "m", "src2:a"); err != nil || !took || by.Fingerprint != "src2:c" || by.Version != "0.1.6" { t.Errorf("taken over from src2:a by %+v (%v, %v); want src2:c 0.1.6", by, took, err) } if _, took, err := r.TakenOver(ctx, "m", "src2:c"); err != nil || took { t.Errorf("the standing build src2:c is said taken over (%v, %v)", took, err) } highest = "0.1.5" // the catalog released past both withdrawn numbers but the newest got, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "src2:d"}) if err != nil || got.Version != "0.1.7" || len(got.Withdrawn) != 1 || got.Withdrawn["0.1.6"] != "src2:c" { t.Errorf("reserved %+v (%v); want 0.1.7 keeping only 0.1.6 withdrawn", got, err) } } // TestARebuildReservesAboveTheCatalogsHighestWhateverItsStandingNumberWas: design 47 G A-490b as H-A-225 amends it — a build again // of a done key, or a re-run after a restart, keeps its fingerprint's standing reservation while it is unreleased and above the // catalog's highest; one at or below it is replaced (Took told) by the next above the catalog's highest alone; another // fingerprint's standing reservation stays a floor and is withdrawn. func TestARebuildReservesAboveTheCatalogsHighestWhateverItsStandingNumberWas(t *testing.T) { ctx := context.Background() for _, c := range []struct { name, standing, standingFp string want string rebuild, kept bool }{ {name: "standing below the catalog's highest", standing: "0.1.0", standingFp: "fp", want: "0.1.17", rebuild: true}, {name: "H-A-225: standing above the catalog's highest is kept by a build again", standing: "0.1.30", standingFp: "fp", want: "0.1.30", rebuild: true, kept: true}, {name: "H-A-225: a re-run after a restart keeps its standing number", standing: "0.1.111", standingFp: "fp", want: "0.1.111", kept: true}, {name: "another fingerprint's standing number stays a floor", standing: "0.1.30", standingFp: "other", want: "0.1.31", rebuild: true}, } { t.Run(c.name, func(t *testing.T) { store := &reserveMem{mem: newMem()} r := &Reserver{Store: store, Filled: func() bool { return true }, ReleasedOf: func(context.Context, string) ([]string, error) { return []string{"0.1.16"}, nil }} raw, _ := json.Marshal(ReservedVersion{V: 1, Module: "m", Series: "0.1", Version: c.standing, Fingerprint: c.standingFp, Artifacts: map[string]string{"m:a": "d"}}) if _, err := store.Create(ctx, ReservedKey("m", "0.1"), raw); err != nil { t.Fatal(err) } var took []string r.Took = func(_ context.Context, older, newer ReservedVersion) { took = append(took, older.Version+">"+newer.Version) } got, err := r.Reserve(ctx, "m", "0.1", []string{"m:a"}, Source{Fingerprint: "fp", Rebuild: c.rebuild}) if c.kept { if err != nil || got.Version != c.want || len(took) != 0 || got.Artifacts["m:a"] != "d" { t.Fatalf("reserved %+v (%v), took %v; want the standing %s kept as it stands", got, err, took, c.want) } return } if err != nil || got.Version != c.want || len(took) != 1 || took[0] != c.standing+">"+c.want { t.Fatalf("reserved %q (%v), took %v; want %s replacing %s", got.Version, err, took, c.want, c.standing) } if w := got.Withdrawn[c.standing]; (c.standingFp != "fp") != (w == c.standingFp) { t.Errorf("withdrawn %v; want %s withdrawn only when another fingerprint stood", got.Withdrawn, c.standing) } }) } } // TestANumberARegistryHoldsIsNeverReserved: design 47 H H-A-275 (the rule of H-A-245 for a registry's record) — a reserve skips // every number a registry seat holds of the module's artifacts and says the skipped ones; a read that fails lets it proceed; a // done build whose number a registry holds at another digest has it withdrawn as a gap (H-A-226), and its build again reserves the // next free number with fresh digests (H-A-279), the index following it. func TestANumberARegistryHoldsIsNeverReserved(t *testing.T) { ctx := context.Background() store := &reserveMem{mem: newMem()} held := []string{"0.1.37", "0.1.38", "0.2.0"} var readErr error var skipped []string r := &Reserver{Store: store, ReleasedOf: func(context.Context, string) ([]string, error) { return []string{"0.1.36"}, nil }, HeldNumbers: func(_ context.Context, module, fp string, refs []string) ([]string, error) { if module != "node-tools" || fp != "fp1" || len(refs) != 1 { t.Errorf("the registries are read for %s %s %v", module, fp, refs) } return held, readErr }, HeldElsewhere: func(_ context.Context, _ string, s []string) { skipped = append(skipped, s...) }} v, err := r.Reserve(ctx, "node-tools", "0.1", []string{"node-tools:program"}, Source{Fingerprint: "fp1", GoPackage: true}) if err != nil || v.Version != "0.1.39" || len(skipped) != 2 { t.Fatalf("reserved %q (%v), skipped %v: want 0.1.39 past the two numbers the registry holds", v.Version, err, skipped) } // The build ends; the registry holds 0.1.39 at another digest meanwhile: the number is withdrawn, and the build again of the same // fingerprint reserves the next number no registry holds, with no digest yet (H-A-279). if _, err := r.Built(ctx, "node-tools", "0.1.39", "node-tools:program", "sha256:7c9b10", nil); err != nil { t.Fatal(err) } if w, ok, err := r.Withdraw(ctx, "node-tools", "0.1.39"); err != nil || !ok || w.Withdrawn["0.1.39"] != "fp1" { t.Fatalf("withdrew %+v (%v %v), want 0.1.39 withdrawn for fp1", w, ok, err) } held = []string{"0.1.37", "0.1.38", "0.1.39", "0.1.40", "0.2.0"} // The build again of a done key reserves as a rebuild (A-490b): a withdrawn number is never its floor's to reuse. again, err := r.Reserve(ctx, "node-tools", "0.1", []string{"node-tools:program"}, Source{Fingerprint: "fp1", GoPackage: true, Rebuild: true}) if err != nil || again.Version != "0.1.41" || again.Artifacts["node-tools:program"] != "" || again.Withdrawn["0.1.39"] != "fp1" || again.Tag != GoTagOf("0.1.41") { t.Fatalf("the build again reserved %+v (%v), want 0.1.41 with no digest and 0.1.39 kept withdrawn", again, err) } if at, _ := r.AtVersion(ctx, "0.1.39"); len(at) != 0 { t.Errorf("the index by version still names the withdrawn number: %v", at) } if _, ok, _ := r.Withdraw(ctx, "node-tools", "0.1.39"); ok { t.Error("a number no reservation stands at any more was withdrawn") } // A registry that cannot be read lets the reserve proceed. readErr, skipped = errors.New("registry down"), nil if v, err := r.Reserve(ctx, "node-tools", "0.2", []string{"node-tools:program"}, Source{Fingerprint: "fp1"}); err != nil || v.Version != "0.2.1" { t.Errorf("with the registry down reserved %q (%v), want 0.2.1 (what it answered still skipped)", v.Version, err) } }