Files
mesh-catalog/modules/mesh-build-node/cmd/mesh-build-node/package_integration_test.go
T
jschoubben 64fa2f5abf A-285: the Node fetch packs each of the group's packages and places it by name
The fetch takes `group-packages` (a member's repository, commit, ref and the
package's directory there) in place of the single `package` and its tagged form;
it packs each package a module of the tree takes, publishing nothing, and places
it in every module whose package.json names it, the SDK in every module, in place
of the registry's whatever the lock names.

Builds: design 47 G A-284, A-285

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 15:37:30 +01:00

134 lines
6.8 KiB
Go

//go:build integration
package main
import (
"context"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun"
"git.novox.be/novox/mesh-sdk/go/forgekey"
)
// sdkFixture is a package named as the SDK at version 9.9.9 with no dependency, whose build script writes dist/: what
// the group's package member is to its TypeScript members.
var sdkFixture = map[string]string{
"module.json": `{"module":"mesh-sdk","version":"1","build":{"artifacts":[{"name":"lib","kind":"package","language":"typescript"}]}}`,
"package.json": `{"name":"@novox/mesh-sdk","version":"9.9.9","type":"module","files":["dist"],"scripts":{"build":"node build.mjs"}}`,
"package-lock.json": `{"name":"@novox/mesh-sdk","version":"9.9.9","lockfileVersion":3,"requires":true,"packages":{"":{"name":"@novox/mesh-sdk","version":"9.9.9"}}}`,
"build.mjs": "import fs from 'node:fs';\nfs.mkdirSync('dist', {recursive: true});\nfs.writeFileSync('dist/index.js', 'export const v = \"9.9.9\";\\n');\n",
}
// forgeRepo makes a bare repository o/<name> under root/forge holding files at one commit, and answers the commit.
func forgeRepo(t *testing.T, root, name string, files map[string]string) string {
t.Helper()
bare, work := filepath.Join(root, "forge", "o", name+".git"), filepath.Join(root, "work-"+name)
os.MkdirAll(bare, 0o755)
os.MkdirAll(work, 0o755)
git(t, bare, "init", "-q", "--bare")
git(t, work, "init", "-q", "-b", "main")
for n, c := range files {
write(t, filepath.Join(work, filepath.FromSlash(n)), c, 0o644)
}
git(t, work, "add", ".")
git(t, work, "commit", "-q", "-m", "c")
git(t, work, "push", "-q", bare, "HEAD:refs/heads/main")
return git(t, work, "rev-parse", "HEAD")
}
// design 47 G A-284, A-285 (ADR 0500 §5), on real git, npm and bubblewrap, try 2's case: a group changes the SDK (at
// its member's root) and adds a contracts package of another member (at its directory there); the fetch of a third
// member packs both (their build scripts and npm pack shut off, nothing published) and places each by its name in the
// module that names it, the SDK whatever its lock names; a package no module names is not built.
func TestTheFetchPacksTheGroupsPackagesAndPlacesThemByName(t *testing.T) {
needBubblewrap(t)
for _, tool := range []string{"node", "npm", "git"} {
if _, err := exec.LookPath(tool); err != nil {
t.Skipf("NOT RUN: no %s where this check runs; every build node has it", tool)
}
}
root := t.TempDir()
sdkCommit := forgeRepo(t, root, "sdk", sdkFixture)
contracts := func(name string) map[string]string {
dir := "modules/c/ts/seats/" + name + "/"
pkg := "@novox/mesh-delivery-contracts-" + name
return map[string]string{
dir + "package.json": `{"name":"` + pkg + `","version":"0.1.0","type":"module","files":["dist"],"scripts":{"build":"node build.mjs"}}`,
dir + "package-lock.json": `{"name":"` + pkg + `","version":"0.1.0","lockfileVersion":3,"requires":true,"packages":{"":{"name":"` + pkg + `","version":"0.1.0"}}}`,
dir + "build.mjs": "import fs from 'node:fs';\nfs.mkdirSync('dist', {recursive: true});\nfs.writeFileSync('dist/index.js', 'export const seat = \"" + name + "\";\\n');\n",
}
}
files := contracts("git")
for n, c := range contracts("unnamed") {
files[n] = c
}
deliveryCommit := forgeRepo(t, root, "delivery", files)
m, _, _ := newMachine(t)
m.Run = forgekey.ExecRunner
m.RemoteBase = "file://" + filepath.Join(root, "forge") + "/"
m.FetchOptions = []steprun.Option{steprun.AllowFileRemote()}
fakeRegistry(t, m, nil) // the registry holds neither: only the group can serve them
member := typeScriptModule(`{"name":"m","dependencies":{"@novox/mesh-sdk":"^0.1.19","@novox/mesh-delivery-contracts-git":"^0.1.0"}}`)
member["modules/m/package-lock.json"] = `{"name":"m","lockfileVersion":3,"requires":true,"packages":{"":{"name":"m"},"node_modules/@novox/mesh-sdk":{"version":"0.1.19","integrity":"sha512-old"}}}`
commit := forgeRepo(t, root, "catalog", member)
out, err := m.fetch(context.Background(), map[string]any{"repository": "o/catalog", "commit": commit, "group-packages": []any{
map[string]any{"repository": "o/sdk", "commit": sdkCommit},
map[string]any{"repository": "o/delivery", "commit": deliveryCommit, "path": "modules/c/ts/seats/git"},
map[string]any{"repository": "o/delivery", "commit": deliveryCommit, "path": "modules/c/ts/seats/unnamed"},
}})
if err != nil {
t.Fatal(err)
}
res := out.(FetchResult)
group := strings.Split(res.Group, ", ")
if len(group) != 2 || !strings.HasPrefix(group[0], "@novox/mesh-delivery-contracts-git@0.1.0 sha512-") || !strings.HasPrefix(group[1], "@novox/mesh-sdk@9.9.9 sha512-") {
t.Errorf("the fetch answered the group %q, want the two packages a module names", res.Group)
}
nm := filepath.Join(m.Work, res.Workspace, "tree", "modules", "m", "node_modules", "@novox")
for pkg, want := range map[string]string{"mesh-sdk": "9.9.9", "mesh-delivery-contracts-git": `"git"`} {
if raw, err := os.ReadFile(filepath.Join(nm, pkg, "dist", "index.js")); err != nil || !strings.Contains(string(raw), want) {
t.Errorf("the module's %s is not the group's built package: %q %v", pkg, raw, err)
}
}
if _, err := os.Stat(filepath.Join(nm, "mesh-delivery-contracts-unnamed")); err == nil {
t.Error("a package no module names was placed")
}
if res.SDK["modules/m"] != "9.9.9" {
t.Errorf("the SDK placed is %v, want the group's 9.9.9", res.SDK)
}
}
// ADR 0499 §4 (issue 1758): a fetch asked with the module's directory and its named paths leaves only those, the
// manifest and the lock files; another module of the commit is not listed or installed.
func TestAFetchWithNamedPathsNarrowsTheWorkspace(t *testing.T) {
root := t.TempDir()
files := typeScriptModule(`{"name":"m"}`)
files["modules/m/tools/index.ts"] = "export {}\n"
files["modules/m/docs/a.md"] = "not named\n"
files["modules/other/module.json"] = files["modules/m/module.json"]
files["README.md"] = "the repository\n"
commit := forgeRepo(t, root, "narrow", files)
m, _, _ := newMachine(t)
m.Run = forgekey.ExecRunner
m.RemoteBase = "file://" + filepath.Join(root, "forge") + "/"
m.FetchOptions = []steprun.Option{steprun.AllowFileRemote()}
out, err := m.fetch(context.Background(), map[string]any{"repository": "o/narrow", "commit": commit, "dir": "modules/m", "paths": []any{"tools"}})
if err != nil {
t.Fatal(err)
}
r := out.(FetchResult)
tree := filepath.Join(m.Work, r.Workspace, "tree")
for _, gone := range []string{"README.md", "modules/m/docs/a.md", "modules/other/module.json"} {
if _, err := os.Stat(filepath.Join(tree, gone)); err == nil {
t.Errorf("%s is still in the workspace", gone)
}
}
if _, err := os.Stat(filepath.Join(tree, "modules/m/tools/index.ts")); err != nil || strings.Join(r.Modules, ",") != "modules/m" {
t.Errorf("the named path or the module went: %v, modules %v", err, r.Modules)
}
}