Files
mesh-delivery/modules/mesh-delivery-builder/internal/build/imports_test.go
T
jschoubben 23533893bb B2: the builder carries no imports on its done key or its build log
A build module says a module's import set as its build seat's event
imports-reported, which the indexer hears (design 47 H2 H-A-517, DQ-7): the
builder's done key and its build log's end entry no longer carry the imports
a build answered, so nothing reads another part's state for them.

Builds: design 47 H2 H-A-517, H-A-532 (DQ-7)
2026-11-23 11:56:02 +01:00

130 lines
4.9 KiB
Go

package build
import (
"encoding/json"
"strings"
"sync"
"testing"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
)
// B2/AC1 (design 47 H2 H-A-517, H-A-532 DQ-7): the imports a Go build module answers at a build's end reach neither the done
// key nor the log's end entry; the build module says them as imports-reported, its own build seat's event.
func importsABuildAnswersReachNeitherTheDoneKeyNorTheLog(t *testing.T) {
r := newRig(t, goBundle)
imports := []string{"git.novox.be/novox/mesh-sdk/go", "git.novox.be/novox/mesh-sdk/go/announce"}
r.asks.Answer = func(a buildstep.StepAsk) buildstep.Step {
step := r.answer(a)
var answer map[string]any
if err := json.Unmarshal(step.Result, &answer); err == nil && a.Language == "go" {
answer["imports"] = imports
step.Result, _ = json.Marshal(answer)
}
return step
}
var logged [][]byte
var mu sync.Mutex
r.b.Node, r.b.Log = "novox", func(e builds.BuildLogEntry) {
raw, err := builds.EncodeBuildLogEntry(e)
if err != nil {
t.Errorf("a log entry does not encode: %v", err)
}
mu.Lock()
logged = append(logged, raw)
mu.Unlock()
}
if v := r.consider(t, "modules/m"); v.State != builds.BuildDone {
t.Fatalf("done key %+v, want it done", v)
}
if raw := string(r.st.vals[r.key]); strings.Contains(raw, `"imports"`) || strings.Contains(raw, "announce") {
t.Errorf("the done key %s carries the imports", raw)
}
mu.Lock()
defer mu.Unlock()
if len(logged) != 2 {
t.Fatalf("log %d entries, want the start and the end", len(logged))
}
for _, raw := range logged {
if strings.Contains(string(raw), `"imports"`) || strings.Contains(string(raw), "announce") {
t.Errorf("a log entry %s carries the imports", raw)
}
}
}
// Design 26 (the lead's ruling on A2i's finding 2): the out-of-step files a contracts module's build module answers, in a
// failed step's result too, reach the log's end entry, sorted and each once, where the indexer raises its finding; a
// build that answers none names none.
func outOfStepFilesABuildAnswersReachTheLog(t *testing.T) {
for _, row := range []struct {
name string
fail bool
files []string
want string
}{
{"a passed build naming files", false, []string{"go/b.go", "go/a.go", "go/b.go"}, "go/a.go,go/b.go"},
{"a failed build naming files", true, []string{"ts/x.ts"}, "ts/x.ts"},
{"a build naming none", false, nil, ""},
} {
t.Run(row.name, func(t *testing.T) {
r := newRig(t, goBundle)
r.asks.Answer = func(a buildstep.StepAsk) buildstep.Step {
step := r.answer(a)
answer := map[string]any{}
_ = json.Unmarshal(step.Result, &answer)
if row.files != nil {
answer["out-of-step"] = row.files
}
step.Result, _ = json.Marshal(answer)
if row.fail {
step.State, step.Reason = buildstep.StepFailed, "the generated files are out of step"
}
return step
}
var logged []builds.BuildLogEntry
var mu sync.Mutex
r.b.Node, r.b.Log = "novox", func(e builds.BuildLogEntry) { mu.Lock(); logged = append(logged, e); mu.Unlock() }
r.consider(t, "modules/m")
mu.Lock()
defer mu.Unlock()
if len(logged) != 2 || strings.Join(logged[1].OutOfStep, ",") != row.want || len(logged[0].OutOfStep) != 0 {
t.Fatalf("log %+v, want the end naming %q", logged, row.want)
}
// A failed build's end carries its reason, so the log never hides why it failed; a passed one no error.
if end := logged[1]; row.fail != strings.Contains(end.Error, "out of step") || !row.fail && end.Error != "" ||
logged[0].Error != "" || logged[0].Step != "" {
t.Errorf("log %+v, want a failed build's end alone to carry its error", logged)
}
})
}
}
// Design 47 G §671: a build the build node kept instead of storing it, which answers its digest and no reference, is done,
// and its end in the log names the node that keeps it once; a build not kept names none.
func aKeptBuildNamesItsNodeInItsEnd(t *testing.T) {
r := newRig(t, goBundle)
r.asks.Answer = func(a buildstep.StepAsk) buildstep.Step {
step := r.answer(a)
var answer buildstep.BuildAnswer
if err := json.Unmarshal(step.Result, &answer); err == nil && a.Language == "go" && len(answer.Artifacts) > 0 {
for i := range answer.Artifacts {
answer.Artifacts[i].Reference = ""
}
answer.Kept = strings.TrimPrefix(answer.Artifacts[0].Digest, "sha256:")
step.Result, _ = json.Marshal(answer)
}
return step
}
var logged []builds.BuildLogEntry
var mu sync.Mutex
r.b.Node, r.b.Log = "novox", func(e builds.BuildLogEntry) { mu.Lock(); logged = append(logged, e); mu.Unlock() }
v := r.consider(t, "modules/m")
if v.State != builds.BuildDone || v.KeptOn == "" {
t.Fatalf("done key %+v, want it done and kept on its node", v)
}
if len(logged) != 2 || logged[1].KeptOn != v.KeptOn || logged[0].KeptOn != "" {
t.Fatalf("log %+v, want kept-on %q on the end alone", logged, v.KeptOn)
}
}