Files
mesh-catalog/modules/mesh-build-node/cmd/mesh-build-node/steps_test.go
T
jschoubben f1f09151c0 mesh-build-node: steps answers lint only where there is TypeScript to check
A TypeScript artifact with no .ts file (a contracts module's generated
JavaScript package) had steps answer lint, so its lint was asked of a build
node and waited minutes there to end nothing to run in a second. steps now
answers lint only when one of the artifact's packages holds a TypeScript
file, by the same test the lint step uses, so the pipeline runner can end
it at placement without an ask.

Builds: design 47 H2 H-A-515 second addendum
2026-11-23 10:28:25 +01:00

101 lines
4.2 KiB
Go

package main
import (
"context"
"errors"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
)
// Design 47 G §22, §307, §328 (BLD7): steps answers the type-check as lint where a package holds a TypeScript file (design 47
// H2 H-A-515 second addendum: none for a tree with none, whose lint would end nothing to run), the packages with a test
// script as unit, those with test:integration as integration, and leaves out a level with none.
func TestTheStepsOfATypeScriptArtifactAreFoundByItsScripts(t *testing.T) {
put := func(dir, name, body string) {
if err := os.MkdirAll(filepath.Join(dir), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
for name, c := range map[string]struct {
pkg string
noTS bool
narrowed bool
want []DetectedStep
}{
"both levels": {`{"scripts":{"test":"node t.js","test:integration":"node i.js"}}`, false, false,
[]DetectedStep{{Step: "lint"}, {Step: "unit", Packages: []string{"."}}, {Step: "integration", Packages: []string{"."}}}},
"unit only": {`{"scripts":{"test":"node t.js"}}`, false, false, []DetectedStep{{Step: "lint"}, {Step: "unit", Packages: []string{"."}}}},
"no script": {`{"scripts":{}}`, false, false, []DetectedStep{{Step: "lint"}}},
// No TypeScript file (only a declaration, as a generated package of JavaScript has): nothing to type-check, no lint.
"no TypeScript file": {`{"scripts":{}}`, true, false, []DetectedStep{}},
// A folder module's tree narrowed to ts/seats/a (H-A-136): the steps are those the narrowed levels run there.
"narrowed folder": {"", false, true, []DetectedStep{{Step: "lint"}, {Step: "unit", Packages: []string{"ts/seats/a"}}}},
} {
dir := t.TempDir()
if c.narrowed {
for p, body := range folderFiles("") {
if !strings.HasPrefix(p, "ts/seats/b/") {
put(filepath.Join(dir, filepath.Dir(p)), filepath.Base(p), body)
}
}
} else {
put(dir, "package.json", c.pkg)
if c.noTS {
put(dir, "index.d.ts", "export declare const v: number;\n")
put(dir, "index.js", "export const v = 1;\n")
} else {
put(dir, "index.ts", "export const v = 1;\n")
}
}
got, err := detectSteps(dir, c.narrowed)
if err != nil || !reflect.DeepEqual(got, c.want) {
t.Errorf("%s: steps = %+v (%v), want %+v", name, got, err, c.want)
}
}
}
// The verb is served beside the seat's others and refuses an ask that names no artifact.
func TestTheStepsVerbIsServedAndRefusesAnAskWithoutAnArtifact(t *testing.T) {
m, _, _ := newMachine(t)
served := false
for _, tool := range m.SeatTools(m.Puller(nil, nil)) {
served = served || tool.Name == Seat+".steps"
}
if !served {
t.Fatal("the module serves no verb steps")
}
if _, err := m.steps(context.Background(), map[string]any{"module": "m", "fetch": map[string]any{"repository": "novox/m", "commit": "0123456789abcdef0123456789abcdef01234567"}}); err == nil {
t.Error("an ask with no artifact was answered")
}
}
// Design 47 G §463, §480 (BN9): steps with drop writes the hand-act log first, then deletes the key; a log that
// refuses deletes nothing; a drop without why is refused.
func TestTheStepsDropWritesTheHandActLogFirst(t *testing.T) {
var order []string
m := &Module{
ForgetDetected: func(key string) error { order = append(order, "delete "+key); return nil },
RecordAct: func(what, why string) error { order = append(order, "log "+why); return nil },
}
args := map[string]any{"module": "mod", "artifact": "mod:web", "drop": true, "why": "stale"}
got, err := m.dropDetected(args, "shanks.person")
if a, _ := got.(StepsAnswer); err != nil || a.Dropped != "mod:web" || !reflect.DeepEqual(order, []string{"log stale", "delete mod.web"}) {
t.Fatalf("drop = %+v, %v, order %v", got, err, order)
}
if _, err := m.dropDetected(map[string]any{"module": "mod", "artifact": "mod:web", "drop": true}, "shanks.person"); err == nil {
t.Error("a drop without why was taken")
}
order = nil
m.RecordAct = func(string, string) error { return errors.New("log down") }
got, err = m.dropDetected(args, "shanks.person")
if a, _ := got.(StepsAnswer); err != nil || !strings.Contains(a.Error, "hand-act log") || len(order) != 0 {
t.Fatalf("a refusing log: %+v, %v, order %v", got, err, order)
}
}