Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/packages_gone_test.go
T
jschoubben d5af4213c3 I11a: an npm mesh package's digest in the packages state, by its npm name
A TypeScript module's fingerprint takes its imported mesh packages by content
(design 47 H2 H-A-523): a take-in of a repository holding an npm package a held
module imports publishes it in the packages state by its npm name, its
repository and directory those of the module graph entry whose artifact
publishes that name, its digest its files under that path but node_modules and
node --test's test files, its requirements' digest its package.json and lock
file.

Builds: design 47 H2 H-A-523, H-A-532 (DQ-6, DMC-3), H-A-546 (1)(2), H-A-547 (Q4)
2026-11-23 12:02:57 +01:00

240 lines
11 KiB
Go

package index
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"testing"
"time"
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"mesh-delivery-indexer/internal/fingerprint"
)
// Design 47 H H-A-232 (1), H-A-233, H-A-234: a repository's packages are those in the merge's tree. The kept key of a
// package whose directory the trunk's merge no longer holds is dropped, said, and the repository's other packages are
// published all the same (issue 1585's one-directory fallback); a branch's take-in and a dry run, which share the live
// packages state, drop nothing. A repository whose every asked directory is gone, at a commit that holds a tree, drops them
// and raises nothing; a listing that fails for another reason, or a commit with no tree, raises the finding
// packages-not-published (scope merge, id the repository), cleared at the next publish that lists.
func TestARepositorysPackagesAreThoseInTheMergesTree(t *testing.T) {
const (
live = "git.novox.be/novox/mesh-catalog/modules/disk-load/diskreadings"
gone = "git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep"
liveDir = "modules/disk-load/diskreadings"
goneDir = "modules/mesh-build-go/buildstep"
)
ctx := context.Background()
old, merge := strings.Repeat("d", 40), strings.Repeat("e", 40)
reaching := func(m string) map[string]meshcatalog.ModulesByModuleValue {
return map[string]meshcatalog.ModulesByModuleValue{
"user": {Repository: catalogueRepo, Inputs2: &meshcatalog.ModulesByModuleValueInputs2{Packages: []string{m}}},
}
}
type setup struct {
ix *Indexer
findings *memFindings
said *[]string
down *error // the forge's listing fails for a reason that is not a gone directory
noTree *error // the seat's trees of the commit, without files, fail
}
newSetup := func(t *testing.T, kept ...string) setup {
t.Helper()
var down, noTree error
said := []string{}
files := func(_ context.Context, repository, sha string, dirs []string) (map[string]map[string]string, error) {
if down != nil {
return nil, down
}
out := map[string]map[string]string{}
for _, d := range dirs {
if d != liveDir {
// as filesVia says the seat's not-found naming the directory
return nil, fmt.Errorf("the directory %s is not in %s at %s: %w", d, repository, sha, ErrNotInTree)
}
out[d] = map[string]string{"go.mod": "m1", "readings.go": "r-" + sha[:1]}
}
return out, nil
}
trees := func(context.Context, string, string) ([]string, error) { return []string{"modules/disk-load"}, noTree }
findings := newFindings()
ix := &Indexer{Module: testSelf, Packages: newMem(), Files: files, Trunk: trees, Findings: findings,
Logf: func(format string, args ...any) { said = append(said, fmt.Sprintf(format, args...)) }}
dirOf := map[string]string{live: liveDir, gone: goneDir}
for _, m := range kept {
key, err := PackagesKey(m)
if err != nil {
t.Fatal(err)
}
raw, err := EncodePublishedPackages(meshcatalog.PackagesByModuleValue{Module: m, Repository: catalogueRepo, Dir: dirOf[m], Commit: old,
Requires: "req", At: ptr(time.Unix(0, 0).UTC()), Packages: []meshcatalog.PackagesByModuleValuePackagesItem{{Package: m, Digest: "x"}}})
if err != nil {
t.Fatal(err)
}
ix.Packages.(*memStore).put(key, raw)
}
return setup{ix: ix, findings: findings, said: &said, down: &down, noTree: &noTree}
}
notPublished := func(s setup) bool {
for _, f := range s.findings.Held() {
if f.Kind == PackagesNotPublishedKind && f.Scope == mcc.ScopeMerge && f.ID == keyID(catalogueRepo) {
return true
}
}
return false
}
dropped := "package " + gone + " of " + catalogueRepo + ": its directory is gone at " + merge + ": its kept key dropped"
t.Run("the trunk's merge drops a gone package's key and publishes the others; a failed listing is a finding until one lists", func(t *testing.T) {
s := newSetup(t, live, gone)
*s.down = errors.New("seat:git.trees (failed): the forge did not answer")
if err := s.ix.publish(ctx, catalogueRepo, merge, reaching(live), false, TrunkBranch); err == nil {
t.Fatal("a listing that failed published")
}
if !notPublished(s) {
t.Fatalf("a listing that failed raised no finding %s: %v", PackagesNotPublishedKind, s.findings.Held())
}
*s.down = nil
if err := s.ix.publish(ctx, catalogueRepo, merge, reaching(live), false, TrunkBranch); err != nil {
t.Fatal(err)
}
have := s.ix.publishedAll(ctx)
if have[live].Commit != merge {
t.Errorf("packages of %s at %q, want the merge %s: a gone sibling kept it from being published", live, have[live].Commit, merge)
}
if _, kept := have[gone]; kept {
t.Errorf("the kept key of %s, whose directory the merge does not hold, stays", gone)
}
if !strings.Contains(strings.Join(*s.said, "\n"), dropped) {
t.Errorf("the journal does not say %q:\n%s", dropped, strings.Join(*s.said, "\n"))
}
if notPublished(s) {
t.Error("the finding packages-not-published stays after a publish that listed")
}
})
for _, row := range []struct {
name string
dry, scratch bool
branch string
}{
{"a branch's take-in keeps the key: only the trunk's merge says a directory is gone", false, false, "feat/x"},
{"a dry run keeps the key: its packages are a working copy over the live state", true, false, TrunkBranch},
// Design 47 H2 §804: over the state dry-runs, a dry run's packages are kept there alone, under its branch and call,
// read by indexer_dry_runs and gone at its end; the live packages state is left as it was.
{"a dry run over the state dry-runs writes its packages there alone and drops them at its end", true, true, TrunkBranch},
} {
t.Run(row.name, func(t *testing.T) {
s := newSetup(t, live, gone)
if row.scratch {
s.ix.DryRuns = newMem()
}
before := s.ix.publishedAll(ctx)
ix, d := s.ix, (*dryRun)(nil)
if row.dry {
d = s.ix.dry("beta", "t")
ix = d.ix
}
if err := ix.publish(ctx, catalogueRepo, merge, reaching(live), false, row.branch); err != nil {
t.Fatal(err)
}
if _, kept := s.ix.publishedAll(ctx)[gone]; !kept {
t.Errorf("the kept key of %s was dropped", gone)
}
if !row.scratch {
return
}
if got := s.ix.publishedAll(ctx); !reflect.DeepEqual(got, before) {
t.Errorf("the live packages state is %+v after the dry run, want %+v as before", got, before)
}
if mine := ix.publishedAll(ctx); mine[live].Commit != merge {
t.Errorf("the dry run reads %s at %q, want its own write at %s", live, mine[live].Commit, merge)
}
read, err := DryRunsTools(s.ix.DryRuns)[0].Run(map[string]any{})
a, _ := read.(DryRunsAnswer)
if err != nil || a.Total == 0 || len(a.Keys) != a.Total || !strings.HasPrefix(a.Keys[0].Key, DryRunPrefix("beta", "t")+PackagesState+".") {
t.Errorf("indexer_dry_runs = %+v (%v), want the dry run's packages under beta.t.packages.", read, err)
}
d.end(ctx)
if a, err := ReadDryRuns(ctx, s.ix.DryRuns); err != nil || a.Total != 0 {
t.Errorf("dry-runs holds %+v (%v) after the dry run's end, want none", a, err)
}
})
}
t.Run("a one-key repository whose directory is gone drops it and raises nothing, when the commit holds a tree", func(t *testing.T) {
s := newSetup(t, gone)
if err := s.ix.publish(ctx, catalogueRepo, merge, reaching(gone), false, TrunkBranch); err != nil {
t.Fatal(err)
}
if _, kept := s.ix.publishedAll(ctx)[gone]; kept {
t.Errorf("the kept key of %s, the repository's one package, whose directory is gone, stays", gone)
}
if notPublished(s) {
t.Error("a gone directory raised packages-not-published")
}
})
t.Run("every asked directory missing at a commit with no tree is the tree's failure: the keys stay and it is a finding", func(t *testing.T) {
s := newSetup(t, gone)
*s.noTree = errors.New("seat:git.trees (not-found): Gitea API GET: 404 Not Found")
if err := s.ix.publish(ctx, catalogueRepo, merge, reaching(gone), false, TrunkBranch); err == nil {
t.Fatal("a commit with no tree published")
}
if _, kept := s.ix.publishedAll(ctx)[gone]; !kept {
t.Errorf("the kept key of %s was dropped at a commit with no tree", gone)
}
if !notPublished(s) {
t.Error("a commit with no tree raised no finding")
}
})
// I11a/AC1 (design 47 H2 H-A-523, H-A-546 (1)(2), H-A-547): a take-in of a repository holding an npm mesh package a held module
// imports publishes it by its npm name, its repository and directory those of the module graph entry whose artifact publishes
// that name, its digest its files under that artifact's path.
t.Run("an npm package a held module imports is published by its npm name", func(t *testing.T) {
const npmName = "@novox/mesh-sdk"
manifest := `{"module":"mesh-sdk","version":"0.1","build":{"artifacts":[{"name":"ts","kind":"package","language":"typescript","paths":["ts"]}]}}`
digest := meshcatalogclient.ManifestDigest([]byte(manifest))
kept, err := meshcatalogclient.EncodeManifest(meshcatalog.ManifestsByDigestValue{Module: "mesh-sdk", Manifest: manifest})
if err != nil {
t.Fatal(err)
}
listing := map[string]string{"package.json": "p1", "src/stdio.ts": "a", "src/stdio.test.ts": "t"}
var asked []string
ix := &Indexer{Module: testSelf, Packages: newMem(), Manifests: newMem(), Findings: newFindings(), Logf: t.Logf,
Files: func(_ context.Context, repository, _ string, dirs []string) (map[string]map[string]string, error) {
asked = append(asked, repository+" "+strings.Join(dirs, ","))
return map[string]map[string]string{"ts": listing}, nil
},
ReadFile: func(_ context.Context, repository, _, file string) ([]byte, error) {
if repository == "novox/mesh-sdk" && file == "ts/package.json" {
return []byte(`{"name":"` + npmName + `"}`), nil
}
return nil, nil
}}
key, _ := meshcatalogclient.ManifestsKey(digest)
ix.Manifests.(*memStore).put(key, kept)
held := map[string]meshcatalog.ModulesByModuleValue{
"mesh-sdk": {Repository: "novox/mesh-sdk", Path: ".", Commit: merge, Manifest: digest},
"view": {Repository: catalogueRepo, Inputs2: &meshcatalog.ModulesByModuleValueInputs2{Imports: []string{npmName}}},
}
if err := ix.publish(ctx, "novox/mesh-sdk", merge, held, false, TrunkBranch); err != nil {
t.Fatal(err)
}
_, want, _ := fingerprint.NpmPackage(npmName, listing)
got, ok := ix.publishedAll(ctx)[npmName]
if !ok || got.Repository != "novox/mesh-sdk" || got.Dir != "ts" || got.Commit != merge || !reflect.DeepEqual(got.Packages, want) {
t.Fatalf("packages of %s = %+v (%v), want its digest %+v from novox/mesh-sdk at ts; asked %v", npmName, got, ok, want, asked)
}
// A take-in of a repository no held module's npm import is published from publishes none.
if err := ix.publish(ctx, catalogueRepo, merge, held, false, TrunkBranch); err != nil || len(asked) != 1 {
t.Errorf("a take-in of %s asked %v (%v), want no npm package read", catalogueRepo, asked, err)
}
})
}