diff --git a/modules/mesh-delivery-indexer/internal/index/catalog_edges.go b/modules/mesh-delivery-indexer/internal/index/catalog_edges.go index e9b00c19..8bfb3749 100644 --- a/modules/mesh-delivery-indexer/internal/index/catalog_edges.go +++ b/modules/mesh-delivery-indexer/internal/index/catalog_edges.go @@ -134,6 +134,26 @@ func edgesOf(a meshcatalog.CatalogEntry, all map[string]meshcatalog.CatalogEntry } } } + // Kind 6 again: a package it builds against whose artifact takes `calls` (the capability words its code calls of the tool + // runner) orders it after the artifact that `states` each word, so no manifest names the tool runner's module (§605, §545). + for _, name := range slices.Sorted(maps.Keys(a.Packages)) { + for _, m := range modules { + for _, art := range all[m].Artifacts { + if art.Package == nil || art.Package.Name != name { + continue + } + for _, word := range wordsOf(all[m], art.Ref, "calls") { + for _, other := range modules { + for _, stating := range all[other].Artifacts { + if other != a.Module && slices.Contains(wordsOf(all[other], stating.Ref, "states"), word) { + add(meshcatalog.Edge{Kind: 6, From: stating.Ref, To: delivers, Word: word, Why: "calls of " + name}) + } + } + } + } + } + } + } slices.SortFunc(out, func(x, y meshcatalog.Edge) int { return cmp.Or(cmp.Compare(x.Kind, y.Kind), strings.Compare(x.From, y.From), strings.Compare(x.To, y.To), strings.Compare(x.Word, y.Word), strings.Compare(x.Why, y.Why)) }) @@ -153,6 +173,26 @@ func manifestOf(e meshcatalog.CatalogEntry) catalogue.Manifest { return m } +// wordsOf are the words an artifact's entry in the entry's manifest lists under key (`calls` or `states`). +func wordsOf(e meshcatalog.CatalogEntry, ref meshcatalog.ArtifactRef, key string) []string { + build, _ := e.Manifest["build"].(map[string]any) + arts, _ := build["artifacts"].([]any) + var out []string + for _, x := range arts { + art, _ := x.(map[string]any) + if name, _ := art["name"].(string); ArtifactRefOf(e.Module, name) != ref { + continue + } + list, _ := art[key].([]any) + for _, w := range list { + if word, _ := w.(string); word != "" { + out = append(out, word) + } + } + } + return out +} + type need struct{ by, ref string } // needsOf are the artifact references each artifact of the entry's manifest names in its `needs`, with the artifact that names them. diff --git a/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go b/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go index 8beaa9d5..06256d08 100644 --- a/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go +++ b/modules/mesh-delivery-indexer/internal/index/catalog_edges_test.go @@ -78,3 +78,33 @@ func TestAKindSixEdgeNamesTheFieldsALongFormInvokeRelies(t *testing.T) { 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) + } +}