package selfcompose import ( "context" "crypto/sha256" "encoding/hex" "encoding/json" "errors" "strings" "sync" "testing" "time" "github.com/novox/mesh-host/internal/agent" "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" "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants" ) // Design 47 G A-430, A-430a (the lead's answer A-430b): a want naming its version is resolved to its artifacts' // digests through the verb versions of the registry seat of each artifact's kind, under `/`; an upstream copy // by the digest its manifest pins. The build is made from those digests. A lookup that fails is said, naming the module, the // version and the seat, and holds the module whole. func aWantNamingItsVersionIsResolvedThroughTheRegistrySeats(t *testing.T) { fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) d := func(c string) string { return "sha256:" + strings.Repeat(c, 64) } manifest := map[string]any{"module": "mail", "version": "1", "build": map[string]any{"artifacts": []any{ map[string]any{"name": "server", "kind": "upstream", "from": "alpine@" + d("9")}, map[string]any{"name": "files", "kind": "archive", "from": "files"}, map[string]any{"name": "web", "kind": "image", "from": "Dockerfile"}, }}} entry := meshcatalog.CatalogEntry{Module: "mail", Fp: fp, Manifest: manifest, Artifacts: []meshcatalog.Artifact{ {Ref: "mail:server", Kind: "upstream"}, {Ref: "mail:files", Kind: "archive"}, {Ref: "mail:web", Kind: "image"}}} // The seats hold mail/files and mail/web; versions newest first. held := map[string]string{ "oci-artifact-registry.mail/files": `{"name":"mail/files","versions":[{"name":"mail/files","kind":"archive","digest":"` + d("2") + `","version":"1.3.0"},{"name":"mail/files","kind":"archive","digest":"` + d("1") + `","version":"1.2.3"}]}`, "container-image-registry.mail/web": `{"name":"mail/web","versions":[{"name":"mail/web","kind":"image","digest":"` + d("3") + `","version":"1.2.3"}]}`, "container-image-registry.mail/other": `{"name":"mail/other","versions":[]}`, } for name, row := range map[string]struct { want meshwants.WantsByWantNodeModuleValue down string // a seat whose call fails parts map[string]string missing string // a word the reason holds said []string }{ "resolved": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true}, parts: map[string]string{"mail:server": d("9"), "mail:files": d("1"), "mail:web": d("3")}}, "a version a seat does not hold": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "2.0.0", Self: true}, missing: "2.0.0", said: []string{"mail at version 2.0.0", "oci-artifact-registry", "container-image-registry"}}, "a seat that does not answer": {want: meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true}, down: "container-image-registry", missing: "mail:web", said: []string{"mail at version 1.2.3: the seat container-image-registry"}}, } { var mu sync.Mutex var said []string env := Env{Say: func(line string) { mu.Lock(); said = append(said, line); mu.Unlock() }} calls := 0 env.Registry = EntryCall(func(_ context.Context, seat, verb string, args any) (json.RawMessage, error) { calls++ if verb != "versions" { t.Errorf("%s: asked %s.%s", name, seat, verb) } if seat == row.down { return nil, errors.New("no responders") } answer, ok := held[seat+"."+args.(map[string]string)["name"]] if !ok { return nil, errors.New("not held") } return json.RawMessage(answer), nil }) env.Entry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) { return nil, errors.New("not asked") }) c := New(env) c.wants["mail"] = row.want c.keptEnt[fp] = keptEntry{entry: entry} parts, missing := c.partsOf("mail", entry, row.want) b := c.buildOf("mail", fp) if row.missing != "" { if parts != nil || b != nil || !strings.Contains(missing, row.missing) { t.Errorf("%s: parts %v, build %v, missing %q: want the module held whole, naming %s", name, parts, b, missing, row.missing) } if c.entryHolds()["mail"] == "" { t.Errorf("%s: the module is not held", name) } for _, w := range row.said { if !strings.Contains(strings.Join(said, "\n"), w) { t.Errorf("%s: said %q, want a line naming %q", name, said, w) } } continue } if missing != "" || len(parts) != len(row.parts) { t.Fatalf("%s: parts %v, missing %q", name, parts, missing) } for ref, digest := range row.parts { if parts[ref] != digest { t.Errorf("%s: %s at %s, want %s", name, ref, parts[ref], digest) } } if b == nil || len(b.Artifacts) != 3 { t.Fatalf("%s: build %+v", name, b) } for _, a := range b.Artifacts { if a.Digest != row.parts["mail:"+a.Name] || a.Reference != referenceOf("mail", a.Name, a.Digest) { t.Errorf("%s: artifact %+v", name, a) } } // A resolution is asked once, and it names the node keeping the build its digests are of. before := calls c.partsOf("mail", entry, row.want) if calls != before { t.Errorf("%s: resolved again: %d calls, then %d", name, before, calls) } } } // Design 47 H ยง771: the order applies the holder of a seat first, found by the claim of the manifest of the build the want names. func TestAModulesClaimsAreReadFromTheEntryOfItsWantsBuild(t *testing.T) { fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) c := New(Env{}) c.keptEnt = map[string]keptEntry{} c.wants["bus"] = meshwants.WantsByWantNodeModuleValue{Fp: fp} c.keptEnt[fp] = keptEntry{entry: meshcatalog.CatalogEntry{Module: "bus", Fp: fp, Manifest: map[string]any{"module": "bus", "version": "1", "claims": []any{map[string]any{"name": "mesh-broker"}}}}} if claims, known := c.Claims(context.Background(), "bus", "mesh-broker"); !claims || !known { t.Errorf("the bus claims mesh-broker: claims %v, known %v", claims, known) } if claims, known := c.Claims(context.Background(), "bus", "node-backup"); claims || !known { t.Errorf("the bus claims no node-backup: claims %v, known %v", claims, known) } if claims, known := c.Claims(context.Background(), "ghost", "mesh-broker"); claims || known { t.Errorf("a module with no want has no manifest read: claims %v, known %v", claims, known) } } // Design 47 H, the lead's answer H-A-146 (A-430a: the bytes are fetched by the digest the versions answer gives): a resolution // whose fetch failed is forgotten and said so in the journal; the seats' verb versions is asked again at the module's next want, at // the next compose by hand, and after a bound while the module is held, never before; a resolution whose fetch succeeded is kept. func aResolutionWhoseFetchFailedIsForgotten(t *testing.T) { fp := meshcatalogclient.SourcePrefix2 + strings.Repeat("a", 64) body := []byte("the files") sum := sha256.Sum256(body) good := "sha256:" + hex.EncodeToString(sum[:]) wrong := "sha256:" + strings.Repeat("e", 64) // the store's answer of the manifest's digest where the blob's was due entry := meshcatalog.CatalogEntry{Module: "mail", Fp: fp, Manifest: map[string]any{"module": "mail", "version": "1"}, Artifacts: []meshcatalog.Artifact{{Ref: "mail:files", Kind: "archive"}}} want := meshwants.WantsByWantNodeModuleValue{Fp: fp, Version: "1.2.3", Self: true} for name, row := range map[string]struct { answer string // the digest versions answers fetch error // the store's answer to the fetch then string // what follows the fetch: "" (a pass within the bound), want, compose, bound asked bool // versions is asked again forgets bool // the journal says it forgot }{ "a fetch that succeeded keeps its resolution": {answer: good, then: "bound"}, "a 404, the next pass within the bound": {answer: wrong, fetch: errors.New("404 Not Found"), forgets: true}, "a 404, then the module's next want": {answer: wrong, fetch: errors.New("404 Not Found"), then: "want", asked: true, forgets: true}, "a 404, then a compose by hand": {answer: wrong, fetch: errors.New("404 Not Found"), then: "compose", asked: true, forgets: true}, "a 404, then the bound while it is held": {answer: wrong, fetch: errors.New("404 Not Found"), then: "bound", asked: true, forgets: true}, "bytes that are not of the digest": {answer: wrong, then: "want", asked: true, forgets: true}, } { now := time.Date(2026, 11, 20, 16, 0, 0, 0, time.UTC) var said []string env := Env{Node: "desk", Say: func(line string) { said = append(said, line) }, Now: func() time.Time { return now }} calls := 0 env.Registry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) { calls++ return json.RawMessage(`{"versions":[{"digest":"` + row.answer + `","version":"1.2.3"}]}`), nil }) env.Fetch = func(context.Context, string) ([]byte, error) { return body, row.fetch } env.Entry = EntryCall(func(context.Context, string, string, any) (json.RawMessage, error) { return nil, errors.New("not asked") }) c := New(env) c.wants["mail"] = want c.keptEnt[fp] = keptEntry{entry: entry} parts, _ := c.partsOf("mail", entry, want) _, err := reader{c: c}.Archive(context.Background(), referenceOf("mail", "files", parts["mail:files"]), parts["mail:files"]) if (err != nil) != row.forgets { t.Errorf("%s: the fetch: %v", name, err) } switch row.then { case "want": raw, _ := json.Marshal(want) c.takeWant(agent.Update{Key: meshwants.WantsByWantNodeModuleKey("desk", "mail"), Value: raw}) case "compose": c.watching.Store(true) ended, cancel := context.WithCancel(context.Background()) cancel() c.ComposeNow(ended) case "bound": now = now.Add(resolveAgain) } before := calls again, missing := c.partsOf("mail", entry, want) if asked := calls > before; asked != row.asked { t.Errorf("%s: versions asked again %v, want %v", name, asked, row.asked) } if row.forgets && !row.asked && (again != nil || !strings.Contains(missing, "forgotten")) { t.Errorf("%s: parts %v, missing %q: want the module held, saying the resolution was forgotten", name, again, missing) } if !row.forgets && again["mail:files"] != good { t.Errorf("%s: parts %v: want the resolution kept", name, again) } line := "resolution of mail 1.2.3 forgotten: " if got := strings.Contains(strings.Join(said, "\n"), line); got != row.forgets { t.Errorf("%s: said %q; want %q said: %v", name, said, line, row.forgets) } } }