Take the catalogue's composition rules into the node-engine
ADR 0593 §7: the composer with its refusals leaves mesh-sdk, so mesh-sdk carries no composition rule. The catalogue functions only the node-engine's composition reaches (Resolve, Resolution's Compose, Declaration, DataItems, LeftOut, Rules and the rest, the settings, placeholder and template refusals, the rendering helpers) are copied with their tests into internal/compose/resolution; a method of a mesh-sdk shape becomes a function taking it first. The composition calls its own copies, and the captured catalogue its tests read lives here now, shared by mesh-compose's tests. Claude-Session: https://claude.ai/code/session_01NcbgfJ9Gum47zkFKWRaovL
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
package resolution
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
mcat "git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// TestPlacedDirectoriesKeepTheirPaths is the check novox/hq issue 119 asks for before a definition
|
||||
// stops naming where its data lives: a converted manifest, resolved on a node with the default root,
|
||||
// names exactly the paths the manifest before it named. Data that a service is using must not move
|
||||
// because a definition stopped saying where it was.
|
||||
//
|
||||
// Two checkouts: MESH_CATALOGUE_BEFORE, the catalogue as it was, and MESH_CATALOGUE, as it is now.
|
||||
// Every module in both is resolved with the controller's own rule (dirsFor, dirFill) and compared
|
||||
// whole — not only the directories, but every string a directory's id was written into. Both
|
||||
// sides are resolved, so a manifest converted in two steps (issue 119, then issue 174) is judged
|
||||
// against the paths it named, not the text it used to name them with.
|
||||
func TestPlacedDirectoriesKeepTheirPaths(t *testing.T) {
|
||||
before, after := os.Getenv("MESH_CATALOGUE_BEFORE"), os.Getenv("MESH_CATALOGUE")
|
||||
if before == "" || after == "" {
|
||||
t.Skip("set MESH_CATALOGUE_BEFORE and MESH_CATALOGUE to two catalogue checkouts to run this")
|
||||
}
|
||||
found, _ := filepath.Glob(filepath.Join(after, "modules", "*", "module.json"))
|
||||
compared := 0
|
||||
for _, path := range found {
|
||||
module := filepath.Base(filepath.Dir(path))
|
||||
old, err := os.ReadFile(filepath.Join(before, "modules", module, "module.json"))
|
||||
if err != nil {
|
||||
continue // new since; nothing to keep
|
||||
}
|
||||
now, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(old) == string(now) {
|
||||
continue
|
||||
}
|
||||
m, err := decodeForTest(now)
|
||||
if err != nil {
|
||||
t.Errorf("%s: %v", module, err)
|
||||
continue
|
||||
}
|
||||
earlier, err := decodeForTest(old)
|
||||
if err != nil {
|
||||
t.Errorf("%s before: %v", module, err)
|
||||
continue
|
||||
}
|
||||
var was, is any
|
||||
if err := json.Unmarshal(old, &was); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(now, &is); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Both sides resolved: the manifest before may itself already place some directories
|
||||
// (issue 119's conversion), and what must not move is the path a machine sees.
|
||||
was = resolvedTree(was, dirsFor(earlier, mcat.Rendering{}), module, t)
|
||||
resolved := resolvedTree(is, dirsFor(m, mcat.Rendering{}), module, t)
|
||||
// An access named by id resolves to the path the definition still carries as its default
|
||||
// (issue 153) — the same rule as a placed directory: an assignment that says nothing moves
|
||||
// nothing.
|
||||
was = accessesResolved(was, earlier)
|
||||
resolved = accessesResolved(resolved, m)
|
||||
if !reflect.DeepEqual(was, resolved) {
|
||||
wasJSON, _ := json.MarshalIndent(was, "", " ")
|
||||
isJSON, _ := json.MarshalIndent(resolved, "", " ")
|
||||
t.Errorf("%s: resolved on the default root, the converted manifest is not the one before it\n--- before\n%s\n--- resolved now\n%s",
|
||||
module, firstDifference(string(wasJSON), string(isJSON)), "")
|
||||
}
|
||||
compared++
|
||||
}
|
||||
t.Logf("%d converted manifest(s) resolve to the paths they named before", compared)
|
||||
}
|
||||
|
||||
// resolvedTree is the manifest as a machine would see it: every ${dir:…} filled, a pathless
|
||||
// directory given the path it resolves to, and the placement word removed.
|
||||
func resolvedTree(node any, dirs map[string]string, module string, t *testing.T) any {
|
||||
switch v := node.(type) {
|
||||
case map[string]any:
|
||||
out := map[string]any{}
|
||||
for k, child := range v {
|
||||
if k == "place" {
|
||||
continue
|
||||
}
|
||||
out[k] = resolvedTree(child, dirs, module, t)
|
||||
}
|
||||
if out["type"] == "directory" {
|
||||
if _, has := out["path"]; !has {
|
||||
if id, ok := out["id"].(string); ok {
|
||||
out["path"] = dirs[id]
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
case []any:
|
||||
out := make([]any, len(v))
|
||||
for i, child := range v {
|
||||
out[i] = resolvedTree(child, dirs, module, t)
|
||||
}
|
||||
return out
|
||||
case string:
|
||||
filled, err := dirFill(v, dirs, module)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
return filled
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func firstDifference(a, b string) string {
|
||||
al, bl := strings.Split(a, "\n"), strings.Split(b, "\n")
|
||||
for i := range al {
|
||||
if i >= len(bl) || al[i] != bl[i] {
|
||||
from := i - 2
|
||||
if from < 0 {
|
||||
from = 0
|
||||
}
|
||||
to := i + 3
|
||||
if to > len(al) {
|
||||
to = len(al)
|
||||
}
|
||||
bt := i + 3
|
||||
if bt > len(bl) {
|
||||
bt = len(bl)
|
||||
}
|
||||
return "before:\n" + strings.Join(al[from:to], "\n") + "\nnow:\n" + strings.Join(bl[from:bt], "\n")
|
||||
}
|
||||
}
|
||||
return "(the difference is beyond the shorter document)"
|
||||
}
|
||||
|
||||
// accessesResolved fills every ${access:<id>} with the default path the definition carries for that
|
||||
// access, and drops the id, so a manifest that names its accesses is compared by the paths a machine
|
||||
// with no placement receives.
|
||||
func accessesResolved(node any, m mcat.Manifest) any {
|
||||
defaults := map[string]string{}
|
||||
for _, a := range m.Accesses {
|
||||
if a.ID != "" && a.Path != "" {
|
||||
defaults[a.ID] = a.Path
|
||||
}
|
||||
}
|
||||
var walk func(any) any
|
||||
walk = func(n any) any {
|
||||
switch v := n.(type) {
|
||||
case map[string]any:
|
||||
out := map[string]any{}
|
||||
for k, child := range v {
|
||||
if k == "id" {
|
||||
if _, isAccess := v["mode"]; isAccess && v["type"] == nil {
|
||||
if _, hasPath := v["path"]; hasPath {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
out[k] = walk(child)
|
||||
}
|
||||
return out
|
||||
case []any:
|
||||
out := make([]any, len(v))
|
||||
for i, child := range v {
|
||||
out[i] = walk(child)
|
||||
}
|
||||
return out
|
||||
case string:
|
||||
return accessRef.ReplaceAllStringFunc(v, func(ref string) string {
|
||||
if p, ok := defaults[accessRef.FindStringSubmatch(ref)[1]]; ok {
|
||||
return p
|
||||
}
|
||||
return ref
|
||||
})
|
||||
}
|
||||
return n
|
||||
}
|
||||
return walk(node)
|
||||
}
|
||||
Reference in New Issue
Block a user