Files
mesh-delivery/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go
T
jschoubben daa7737392 Item 5 (join 11): an asker's build-seat queue is served by the seat's holders, so the indexer draws its kind-6 edge
An asker names the queue it asks as seat:<seat>.accept.<queue> (A-231); no module's serves lists that form, so
graph.Servers matched nothing and the indexer drew no served-verb edge from a build seat's holders to the asker.
Servers now reads the form through mesh-sdk's catalogue.QueueInvoke and answers every module serving a verb of the
seat; the kind-6 edge asks the module graph's Servers, the one rule, instead of a match of its own.

Builds: design 47 G §544, §605, A-231

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 13:08:40 +01:00

230 lines
13 KiB
Go

package index
import (
"context"
"slices"
"testing"
"time"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-sdk/go/seatdef"
)
// Design 47 G §47, §121, A-208, A-212: an entry's edges between artifact references are written from its own words and
// the other entries of the revision, kinds 1 (an imported package), 2 (a `needs`), 3 (build module and bases), 4 (an
// awaited provision) and 6 (an invoked verb, none that lies on a cycle), each from B to A.
func TestAnEntryCarriesItsEdgesOfKindsOneToFourAndSix(t *testing.T) {
entries := map[string]meshcatalog.CatalogEntry{
"lib": {Module: "lib", Artifacts: []meshcatalog.Artifact{{Ref: "lib:go", Language: "go", Kind: "package", Package: &meshcatalog.ArtifactPackage{Ecosystem: EcosystemGo, Name: "git.example/lib/go"}}}},
"build-go": {Module: "build-go", Artifacts: []meshcatalog.Artifact{{Ref: "build-go:program", Language: "go"}},
Provides: []string{"builder"}, Serves: []string{"seat:mesh-builds.build"}},
"db": {Module: "db", Artifacts: []meshcatalog.Artifact{{Ref: "db:image", Kind: "image"}}, Provides: []string{"sql"}},
"cfg": {Module: "cfg", Artifacts: []meshcatalog.Artifact{{Ref: "cfg:archive", Kind: "archive"}}},
// A build seat's holder: the queue an asker names in its invokes, `seat:<seat>.accept.<queue>`, is its (A-231).
"node-go": {Module: "node-go", Artifacts: []meshcatalog.Artifact{{Ref: "node-go:program", Language: "go"}}, Serves: []string{"seat:node-build-go.steps"}},
"app": {Module: "app", Packages: map[string]string{"git.example/lib/go": "1.2.3"}, Bases: []string{"db"},
Toolchains: map[string]meshcatalog.BuildEdge{"go": {Module: "build-go"}},
Awaits: []string{"sql"}, Invokes: []string{"seat:mesh-builds.build", "seat:mesh-builds.cycle", "seat:node-build-go.accept.check"}, LeftOut: []string{"seat:mesh-builds.cycle"},
Manifest: map[string]any{"build": map[string]any{"artifacts": []any{map[string]any{"name": "program", "needs": []any{"cfg:archive", "nothing:here"}}}}},
Artifacts: []meshcatalog.Artifact{{Ref: "app:program", Language: "go"}}},
}
before := entries["app"]
got := WithEdges(entries)["app"].Edges
want := []meshcatalog.Edge{
{Kind: 1, From: "lib:go", To: "app:program", Stale: true, Word: "packages", Why: "git.example/lib/go"},
{Kind: 2, From: "cfg:archive", To: "app:program", Declared: true, Word: "needs", Why: "cfg:archive"},
{Kind: 3, From: "build-go:program", To: "app:program", Stale: true, Word: "toolchains", Why: "go"},
{Kind: 3, From: "db:image", To: "app:program", Stale: true, Word: "build.on", Why: "db"},
{Kind: 4, From: "db:image", To: "app:program", Word: "requires", Why: "sql"},
{Kind: 6, From: "build-go:program", To: "app:program", Word: "seat:mesh-builds.build", Why: "seat:mesh-builds.build"},
{Kind: 6, From: "node-go:program", To: "app:program", Word: "seat:node-build-go.accept.check", Why: "seat:node-build-go.accept.check"},
}
if !slices.Equal(got, want) {
t.Errorf("edges\n got %+v\nwant %+v", got, want)
}
if len(before.Edges) != 0 || len(entries["app"].Edges) != 0 {
t.Errorf("the entries given were changed")
}
}
// Design 47 G A-208: a revision kept holds each entry with its edges, so the builder and the assigner read them from the entry.
func TestAKeptRevisionHoldsTheEdgesOfItsEntries(t *testing.T) {
ctx := context.Background()
ix := &Indexer{Revisions: newMemObjects(1 << 20), Now: func() time.Time { return time.Date(2026, 11, 1, 3, 0, 0, 0, time.UTC) }}
err := ix.KeepBranchEntries(ctx, "main", 1, map[string]meshcatalog.CatalogEntry{
"b": {Module: "b", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{{Ref: "b:p"}}, Serves: []string{"seat:s.v"}},
"a": {Module: "a", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{{Ref: "a:p"}}, Invokes: []string{"seat:s.v"}},
})
if err != nil {
t.Fatal(err)
}
_, entries, err := ReadRevision(ctx, ix.Revisions, "trunk.1")
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
if e.Module == "a" && (len(e.Edges) != 1 || e.Edges[0].Kind != 6 || e.Edges[0].From != "b:p" || e.Edges[0].To != "a:p") {
t.Errorf("the kept entry of a holds %+v, want one edge of kind 6 from b:p", e.Edges)
}
}
}
// Design 47 G §558, A-208: a verb invoked in the long form {verb, fields} gives its kind-6 edge the fields it relies on,
// read by the second reader release; the short form names none.
func TestAKindSixEdgeNamesTheFieldsALongFormInvokeRelies(t *testing.T) {
entries := map[string]meshcatalog.CatalogEntry{
"holder": {Module: "holder", Artifacts: []meshcatalog.Artifact{{Ref: "holder:p"}}, Serves: []string{"seat:s.list", "seat:s.get"}},
"user": {Module: "user", Artifacts: []meshcatalog.Artifact{{Ref: "user:p"}}, Invokes: []string{"seat:s.get", "seat:s.list"},
Manifest: map[string]any{"module": "user", "version": "1.0", "invokes": []any{"seat:s.get", map[string]any{"verb": "seat:s.list", "fields": []any{"binding", "node"}}}}},
}
got := WithEdges(entries)["user"].Edges
if len(got) != 2 || got[0].Why != "seat:s.get" || got[1].Why != "seat:s.list fields binding, node" || got[1].Word != "seat:s.list" {
t.Errorf("edges %+v, want the short verb as it is and the long form with its fields", got)
}
}
// Design 47 G §605, §545, A-208: a module built against a package whose artifact takes `calls` is ordered after the artifact
// that `states` each word, by an edge of kind 6 naming the word; a module built against no such package has none.
func TestAModuleBuiltOnAPackageThatCallsTheToolRunnerIsOrderedAfterTheArtifactThatStatesTheWords(t *testing.T) {
artifactsOf := func(module string, art map[string]any) map[string]any {
return map[string]any{"module": module, "build": map[string]any{"artifacts": []any{art}}}
}
entries := map[string]meshcatalog.CatalogEntry{
"sdk": {Module: "sdk", Artifacts: []meshcatalog.Artifact{{Ref: "sdk:go", Kind: "package", Language: "go", Package: &meshcatalog.ArtifactPackage{Ecosystem: EcosystemGo, Name: "git.example/sdk/go"}}},
Manifest: artifactsOf("sdk", map[string]any{"name": "go", "calls": []any{"put-sessions", "finding-calls"}})},
"runner": {Module: "runner", Artifacts: []meshcatalog.Artifact{{Ref: "runner:runtime"}},
Manifest: artifactsOf("runner", map[string]any{"name": "runtime", "states": []any{"put-sessions"}})},
"app": {Module: "app", Artifacts: []meshcatalog.Artifact{{Ref: "app:program", Language: "go"}}, Packages: map[string]string{"git.example/sdk/go": "1.0.4"}},
"other": {Module: "other", Artifacts: []meshcatalog.Artifact{{Ref: "other:program", Language: "go"}}},
}
got := WithEdges(entries)
var six []meshcatalog.Edge
for _, e := range got["app"].Edges {
if e.Kind == 6 {
six = append(six, e)
}
}
want := []meshcatalog.Edge{{Kind: 6, From: "runner:runtime", To: "app:program", Word: "put-sessions", Why: "calls of git.example/sdk/go"}}
if !slices.Equal(six, want) {
t.Errorf("app's edges of kind 6 are %+v, want %+v", six, want)
}
if len(got["other"].Edges) != 0 {
t.Errorf("other builds on no such package, its edges %+v", got["other"].Edges)
}
}
func actingEntries() map[string]meshcatalog.CatalogEntry {
rule := func(seat, step, on string) meshcatalog.ActingRule {
return meshcatalog.ActingRule{Seat: seat, Step: step, On: on}
}
return map[string]meshcatalog.CatalogEntry{
"grants": {Module: "grants", Artifacts: []meshcatalog.Artifact{{Ref: "grants:program", Language: "go"}},
Packages: map[string]string{"git.example/lib/go": "1.0.0"}, Toolchains: map[string]meshcatalog.BuildEdge{"go": {Module: "build-go"}},
Acts: []meshcatalog.ActingRule{rule("mesh-grants", "grant", "every"), rule("mesh-grants", "take-in", "every"), rule("mesh-grants", "validate", "")}},
"lib": {Module: "lib", Artifacts: []meshcatalog.Artifact{{Ref: "lib:go", Language: "go", Kind: "package", Package: &meshcatalog.ArtifactPackage{Ecosystem: EcosystemGo, Name: "git.example/lib/go"}}}},
"build-go": {Module: "build-go", Artifacts: []meshcatalog.Artifact{{Ref: "build-go:program", Language: "go"}}, Acts: []meshcatalog.ActingRule{rule("node-build-go", "build", "go")}},
"composer": {Module: "composer", Artifacts: []meshcatalog.Artifact{{Ref: "composer:program", Language: "go"}}, Acts: []meshcatalog.ActingRule{rule("mesh-compose", "compose", "kind:image")}},
"db": {Module: "db", Artifacts: []meshcatalog.Artifact{{Ref: "db:image", Kind: "image"}}},
"web": {Module: "web", Artifacts: []meshcatalog.Artifact{{Ref: "web:program", Language: "typescript"}}},
"bare": {Module: "bare"},
}
}
// Design 47 G §711: an acting rule of a step that acts on a delivered version gives kind 5 from the acting entry's delivered
// artifact to each other entry the rule's `on` matches, with the word `acts` and the why `<seat> <step>`; none to the entry
// itself, to one it is built from (a package, a build module), to one with no artifact; take-in, validate and build give none.
func TestAnActingRuleGivesKindFiveFromTheActingEntryToTheEntriesItActsOn(t *testing.T) {
got := WithEdges(actingEntries())
five := func(m string) []meshcatalog.Edge {
var out []meshcatalog.Edge
for _, e := range got[m].Edges {
if e.Kind == 5 {
out = append(out, e)
}
}
return out
}
grant := func(to string) meshcatalog.Edge {
return meshcatalog.Edge{Kind: 5, From: "grants:program", To: to, Word: "acts", Why: "mesh-grants grant"}
}
for module, want := range map[string][]meshcatalog.Edge{
"web": {grant("web:program")},
"composer": {grant("composer:program")},
"db": {{Kind: 5, From: "composer:program", To: "db:image", Word: "acts", Why: "mesh-compose compose"}, grant("db:image")},
"build-go": nil, // grants is built with it
"lib": nil, // and from it
"grants": nil, // never to itself
"bare": nil, // no artifact
} {
if g := five(module); !slices.Equal(g, want) {
t.Errorf("kind 5 into %s: got %+v, want %+v", module, g, want)
}
}
for _, e := range got["web"].Edges {
if e.Stale || e.Declared {
t.Errorf("an edge of kind 5 only orders: %+v", e)
}
}
}
// Design 47 G §711: `acts` is filled from the definitions of each seat the manifest claims, as {seat, step, on, bound},
// sorted by seat then step; an entry claiming no seat with a rule has none.
func TestAnEntryCarriesTheActingRulesOfTheSeatsItClaims(t *testing.T) {
claims := func(seats ...string) map[string]any {
var list []any
for _, s := range seats {
list = append(list, map[string]any{"name": s})
}
return map[string]any{"module": "m", "version": "1.0", "claims": list}
}
defs := map[string]seatdef.Definition{
"mesh-grants": {Acts: []seatdef.ActingRule{{Step: "grant", On: "every", Bound: "5m"}, {Step: "build", On: "go"}}},
"a-seat": {Acts: []seatdef.ActingRule{{Step: "place"}}},
"unclaimed": {Acts: []seatdef.ActingRule{{Step: "compose"}}},
"plain": {},
}
got := WithActs(map[string]meshcatalog.CatalogEntry{
"m": {Module: "m", Manifest: claims("mesh-grants", "a-seat", "plain")},
"other": {Module: "other", Manifest: claims("plain")},
}, defs)
want := []meshcatalog.ActingRule{{Seat: "a-seat", Step: "place"}, {Seat: "mesh-grants", Step: "build", On: "go"}, {Seat: "mesh-grants", Step: "grant", On: "every", Bound: "5m"}}
if !slices.Equal(got["m"].Acts, want) {
t.Errorf("acts %+v, want %+v", got["m"].Acts, want)
}
if got["other"].Acts != nil {
t.Errorf("a module claiming no seat with a rule has acts %+v", got["other"].Acts)
}
}
// Design 47 G §711: the indexer reads the rules from the seat definitions it keeps when it keeps a revision, so the kept entry
// carries its acts and the entries it acts on carry the edge.
func TestAKeptRevisionHoldsTheActsAndTheKindFiveEdges(t *testing.T) {
ctx := context.Background()
defs := newMem()
_ = defs.Create(ctx, "mesh-grants.trunk", []byte(`{"seat":"mesh-grants","source":"trunk","digest":"d","definition":{"seat":"mesh-grants","acts":[{"step":"grant","on":"every"}]}}`))
ix := &Indexer{Revisions: newMemObjects(1 << 20), SeatDefinitions: defs, Now: func() time.Time { return time.Date(2026, 11, 1, 3, 0, 0, 0, time.UTC) }}
err := ix.KeepBranchEntries(ctx, "main", 1, map[string]meshcatalog.CatalogEntry{
"grants": {Module: "grants", Manifest: map[string]any{"module": "grants", "version": "1.0", "claims": []any{map[string]any{"name": "mesh-grants"}}}, Artifacts: []meshcatalog.Artifact{{Ref: "grants:p"}}},
"app": {Module: "app", Manifest: map[string]any{}, Artifacts: []meshcatalog.Artifact{{Ref: "app:p"}}},
})
if err != nil {
t.Fatal(err)
}
_, entries, err := ReadRevision(ctx, ix.Revisions, "trunk.1")
if err != nil {
t.Fatal(err)
}
for _, e := range entries {
switch e.Module {
case "grants":
if len(e.Acts) != 1 || e.Acts[0].Seat != "mesh-grants" || e.Acts[0].Step != "grant" {
t.Errorf("grants holds acts %+v", e.Acts)
}
case "app":
if len(e.Edges) != 1 || e.Edges[0].Kind != 5 || e.Edges[0].From != "grants:p" || e.Edges[0].Why != "mesh-grants grant" {
t.Errorf("app holds edges %+v, want one of kind 5 from grants:p", e.Edges)
}
}
}
}