Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/steps_test.go
T
jschoubben d29e1426d0 Asker rename R2: every asker of a build seat asks under its seat's own name, and its step is keyed so
A-163: the asker in `accept.<queue>.<asker>` is the asking seat's name (mesh-pipeline-runner,
mesh-catalog, mesh-wants, mesh-builds), never a nickname: the builder asks as mesh-builds (was
"builder"), the indexer's validation as mesh-catalog (was "indexer"), the assigner's runs as
mesh-wants (was "assigner"), the runner's steps on `accept.check.mesh-pipeline-runner` and
`accept.delivery.mesh-pipeline-runner` (were `accept.check.step` and `accept.delivery.step`, where
the holder read `step` as the asker). An asker naming none (the checker) asks on main's subject
(A-184). A build seat's queues are delivery, check and recheck alone (A-170, A-184): buildstep's
AskKinds holds no step, a holder takes a step from check or delivery, and the grants part makes a
worker per queue, never per kind. A holder keys a named ask's step `<asker>.<node>.<id>` and main's
`<node>.<id>` (§481, §483); readers watch their own name's keys. The composition step's step-progress
names the one caller mesh-wants' definition lists for step, read from the seat list (A-294).

Builds: design 47 G §481, §483, §543, §600, A-163, A-170, A-184, A-248, A-294

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

378 lines
18 KiB
Go

package pipeline
import (
"errors"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
wants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
nodebuildgo "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/nodebuildgo"
"git.novox.be/novox/mesh-sdk/go/keys"
)
type memStore map[string]struct {
p seat.Pipeline
rev uint64
}
func (m memStore) Get(key string) (seat.Pipeline, uint64, error) {
e, ok := m[key]
if !ok {
return seat.Pipeline{}, 0, errors.New("no such key")
}
// A read is a copy, as one from the bus is: a change not written is not seen by the next read.
e.p.Steps = append([]seat.StepResult(nil), e.p.Steps...)
return e.p, e.rev, nil
}
func (m memStore) Update(key string, p seat.Pipeline, rev uint64) (uint64, error) {
if m[key].rev != rev {
return 0, ErrChanged
}
m[key] = struct {
p seat.Pipeline
rev uint64
}{p, rev + 1}
return rev + 1, nil
}
func (m memStore) Keys(filter string) ([]string, error) {
var out []string
for k := range m {
if ok, _ := matchKey(filter, k); ok {
out = append(out, k)
}
}
return out, nil
}
func matchKey(filter, key string) (bool, error) {
f, k := strings.Split(filter, "."), strings.Split(key, ".")
if len(f) != len(k) {
return false, nil
}
for i := range f {
if f[i] != "*" && f[i] != k[i] {
return false, nil
}
}
return true, nil
}
type askRecorder struct{ asked []oc.Ask }
func (a *askRecorder) Ask(x oc.Ask) error { a.asked = append(a.asked, x); return nil }
const pkey = "main.5.app"
// fixture is a pipeline of app with a required lint asked of node-build-go, an optional unit asked of the same seat and an
// approval step waiting, and a hearer over it.
func fixture() (*Hearer, memStore, *[]string, *askRecorder, map[string]seat.KeptResult, map[string]string) {
store := memStore{pkey: {p: seat.Pipeline{V: 1, Pipeline: pkey, Branch: "main", Source: 5, Module: "app", Commit: "c1", Fingerprint: "src2:aa",
Outcome: seat.PipelineOutcomeRunning, Steps: []seat.StepResult{
{Artifact: "program", Step: seat.StepResultStepLint, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: "a1", Seat: nodebuildgo.Seat},
{Artifact: "program", Step: seat.StepResultStepUnit, Status: seat.StepResultStatusOptional, Outcome: seat.StepResultOutcomeRunning, Ask: "a2", Seat: nodebuildgo.Seat},
{Artifact: "program", Step: seat.StepResultStepApproval, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeWaiting},
}}, rev: 1}}
said := &[]string{}
asker := &askRecorder{}
kept := map[string]seat.KeptResult{}
outs := map[string]string{}
h := &Hearer{Store: store, Name: "mesh-pipeline-runner", Asker: asker,
Say: func(subject string, _ any) error {
*said = append(*said, subject[strings.Index(subject, ".event.")+7:])
return nil
},
Log: func(seat.LogEntry) error { return nil },
Keep: func(k string, v seat.KeptResult) error { kept[k] = v; return nil },
Output: func(k string, d []byte) error { outs[k] = string(d); return nil },
Now: func() time.Time { return time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC) }}
return h, store, said, asker, kept, outs
}
func ended(ask, step, outcome string) StepEnded {
return StepEnded{ID: ask, Ask: ask, Node: "n1", Pipeline: pkey, Module: "app", Commit: "c1", Fingerprint: "src2:aa", Step: step, Outcome: outcome}
}
var goSeat = StepSeat{Seat: nodebuildgo.Seat, Prefix: "mesh.seat.node-build-go.event.step-ended."}
// A step's end is taken only as the runner asked it (design 47 G §24; ADR 0666 §21).
func TestStepEndedIsTakenOnlyAsAsked(t *testing.T) {
h, store, _, _, _, _ := fixture()
other := ended("a1", "lint", "passed")
other.Fingerprint = "src2:bb"
wrongNode := ended("a1", "lint", "passed")
wrongNode.Node = "n2"
unknownPipeline := ended("a1", "lint", "passed")
unknownPipeline.Pipeline = "main.9.app"
for name, c := range map[string]struct {
subject string
ev StepEnded
}{
"a subject of another seat": {"mesh.seat.node-build-node.event.step-ended.n1", ended("a1", "lint", "passed")},
"a body naming another node": {goSeat.Prefix + "n1", wrongNode},
"an unknown pipeline": {goSeat.Prefix + "n1", unknownPipeline},
"an ask nobody made": {goSeat.Prefix + "n1", ended("zz", "lint", "passed")},
"another fingerprint": {goSeat.Prefix + "n1", other},
"another step": {goSeat.Prefix + "n1", ended("a1", "unit", "passed")},
"an outcome step-ended lacks": {goSeat.Prefix + "n1", ended("a1", "lint", "waiting")},
} {
if _, err := h.OnStepEnded(goSeat, c.subject, c.ev); !errors.Is(err, ErrRefused) {
t.Errorf("%s: err %v, want a refusal", name, err)
}
}
if store[pkey].p.Steps[0].Outcome != seat.StepResultOutcomeRunning {
t.Error("a refused end changed the pipeline")
}
// The seat the step was asked of is the only one that ends it.
if _, err := h.OnStepEnded(StepSeat{Seat: "node-build-node", Prefix: "mesh.seat.node-build-node.event.step-ended."}, "mesh.seat.node-build-node.event.step-ended.n1", ended("a1", "lint", "passed")); !errors.Is(err, ErrRefused) {
t.Errorf("another seat's holder ended the step: %v", err)
}
}
// A taken end is written, kept and said; the pipeline ends once every step ended (§24, §27).
func TestStepEndedEndsThePipeline(t *testing.T) {
h, store, said, _, kept, outs := fixture()
h.Asker = nil
lint := ended("a1", "lint", "passed")
lint.Output = "all clean"
unit := ended("a2", "unit", "failed")
unit.Kept = []nodebuildgo.KeptResult{{Digest: "sha256:d1", Level: "unit", Package: "app/x"}}
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", lint); err != nil {
t.Fatal(err)
}
if p := store[pkey].p; p.Outcome != seat.PipelineOutcomeRunning || p.Steps[0].Node != "n1" || p.Steps[0].Output == "" {
t.Fatalf("after the lint: %+v", p)
}
if len(outs) != 1 {
t.Errorf("outputs kept %v", outs)
}
// Approval waits with no asker, so the pipeline does not end on the last test step alone.
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", unit); err != nil {
t.Fatal(err)
}
if p := store[pkey].p; p.Outcome != seat.PipelineOutcomeRunning {
t.Fatalf("a pipeline with its approval waiting ended: %+v", p)
}
if k := kept[seat.KeptByLevelDigestKey("unit", keys.EscapeName("sha256:d1"))]; k.Package != "app/x" || k.Pipeline != pkey {
t.Errorf("kept %+v", kept)
}
// A step heard again takes nothing.
n := len(*said)
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", unit); err != nil || len(*said) != n {
t.Errorf("a second end said %v (%v)", (*said)[n:], err)
}
}
// The approval step is asked last, only when every other required step passed, and ended by its warrant (§26, §37).
func TestApprovalIsAskedLastAndEndedByItsWarrant(t *testing.T) {
h, store, said, asker, _, _ := fixture()
var record *oc.Warrant
h.Where = func(string) (string, *oc.Warrant, error) { return "chosen", record, nil }
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a1", "lint", "passed")); err != nil || len(asker.asked) != 0 {
t.Fatalf("asked %d before the unit step ended (%v)", len(asker.asked), err)
}
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a2", "unit", "failed")); err != nil {
t.Fatal(err)
}
// An optional step that failed holds nothing: the approval is asked.
if len(asker.asked) != 1 || asker.asked[0].About != "app.main" {
t.Fatalf("asked %+v", asker.asked)
}
a := asker.asked[0]
if got := (*said)[len(*said)-1]; !strings.HasPrefix(got, "wait-held.app.main.c1") {
t.Errorf("the wait was said as %q", got)
}
warrant := func(option, outcome string) oc.Warrant {
return oc.Warrant{Asker: "mesh-pipeline-runner", Ask: a.ID, About: a.About, Outcome: outcome, Option: option, Level: operatorchannelclient.Approve,
AskDigest: operatorchannelclient.Digest(a), At: operatorchannelclient.FormatTime(time.Date(2026, 11, 11, 13, 0, 0, 0, time.UTC)),
By: &oc.Person{Who: operatorchannelclient.Operator, Identity: "op", Kind: "telegram", Verified: "linked"}}
}
// The event only wakes the runner (A-89): an event that says approve while the router's record holds none takes nothing.
event := warrant(OptionApprove, oc.WarrantOutcomeChosen)
if _, err := h.OnDecided(event); !errors.Is(err, ErrNoRecord) || store[pkey].p.Steps[2].Outcome != seat.StepResultOutcomeRunning {
t.Errorf("an event with no record was taken: %v", err)
}
// A record whose warrant is for another ask's digest is not taken; an event of another asker neither.
bad := warrant(OptionApprove, oc.WarrantOutcomeChosen)
bad.AskDigest = "sha256:other"
record = &bad
if _, err := h.OnDecided(event); err == nil || store[pkey].p.Steps[2].Outcome != seat.StepResultOutcomeRunning {
t.Errorf("a warrant for another ask was taken: %v", err)
}
notMine := event
notMine.Asker = "someone"
if _, err := h.OnDecided(notMine); !errors.Is(err, ErrRefused) {
t.Errorf("another asker's event: %v", err)
}
// The record says decline although the event body says approve: the record decides.
decline := warrant(OptionDecline, oc.WarrantOutcomeChosen)
record = &decline
if p, err := h.OnDecided(event); err != nil || p.Steps[2].Outcome != seat.StepResultOutcomeDeclined {
t.Fatalf("the record said decline: %+v (%v)", p.Steps[2], err)
}
// (the declined pipeline above is a fresh fixture's: approve it on a new one)
h, store, said, asker, _, _ = fixture()
h.Where = func(string) (string, *oc.Warrant, error) { return "chosen", record, nil }
for _, s := range []StepEnded{ended("a1", "lint", "passed"), ended("a2", "unit", "failed")} {
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", s); err != nil {
t.Fatal(err)
}
}
a = asker.asked[0]
approve := warrant(OptionApprove, oc.WarrantOutcomeChosen)
record = &approve
p, err := h.OnDecided(event)
if err != nil || p.Steps[2].Outcome != seat.StepResultOutcomePassed || p.Outcome != seat.PipelineOutcomePassed || p.EndCount != 1 {
t.Fatalf("approved: %+v (%v)", p, err)
}
if !strings.HasPrefix((*said)[len(*said)-1], "pipeline-ended.app.main.c1") {
t.Errorf("said %v", *said)
}
// A pipeline that ended takes no second decision.
record = &decline
if p, err := h.OnDecided(event); err != nil || p.Steps[2].Outcome != seat.StepResultOutcomePassed {
t.Errorf("a second warrant changed it: %+v (%v)", p.Steps[2], err)
}
}
// A decline, an expiry or a replacement ends the approval step declined or superseded, and a failed required step leaves it
// not asked with the pipeline failed (§26, §37).
func TestApprovalEnds(t *testing.T) {
for name, c := range map[string]struct {
outcome, option, want string
}{
"declined": {oc.WarrantOutcomeChosen, OptionDecline, seat.StepResultOutcomeDeclined},
"expired": {oc.WarrantOutcomeExpired, "", seat.StepResultOutcomeDeclined},
"refused": {oc.WarrantOutcomeRefused, "", seat.StepResultOutcomeDeclined},
"superseded": {oc.WarrantOutcomeReplaced, "", seat.StepResultOutcomeSuperseded},
} {
h, _, _, asker, _, _ := fixture()
for _, s := range []StepEnded{ended("a1", "lint", "passed"), ended("a2", "unit", "passed")} {
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", s); err != nil {
t.Fatal(err)
}
}
a := asker.asked[0]
rec := oc.Warrant{Asker: h.Name, Ask: a.ID, About: a.About, Outcome: c.outcome, Option: c.option, Level: operatorchannelclient.Approve,
AskDigest: operatorchannelclient.Digest(a), At: operatorchannelclient.FormatTime(time.Date(2026, 11, 11, 13, 0, 0, 0, time.UTC)),
By: &oc.Person{Who: operatorchannelclient.Operator, Identity: "op", Kind: "telegram", Verified: "linked"}}
h.Where = func(string) (string, *oc.Warrant, error) { return c.outcome, &rec, nil }
p, err := h.OnDecided(oc.Warrant{Asker: h.Name, Ask: a.ID, About: a.About})
if err != nil || p.Steps[2].Outcome != c.want || p.Outcome != seat.PipelineOutcomeFailed {
t.Errorf("%s: step %q, pipeline %q (%v)", name, p.Steps[2].Outcome, p.Outcome, err)
}
}
h, store, _, asker, _, _ := fixture()
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a1", "lint", "failed")); err != nil {
t.Fatal(err)
}
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a2", "unit", "passed")); err != nil {
t.Fatal(err)
}
p := store[pkey].p
if len(asker.asked) != 0 || p.Steps[2].Outcome != seat.StepResultOutcomeNotAsked || p.Outcome != seat.PipelineOutcomeFailed || !strings.Contains(p.Why, "lint") {
t.Errorf("asked %d; %+v", len(asker.asked), p)
}
}
// An optional step still waiting (a code review nobody has given) does not hold a pipeline's end; a required one does
// (design 47 G A-104).
func TestAnOptionalStepStillWaitingHoldsNoEnd(t *testing.T) {
now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
for name, c := range map[string]struct {
review string
ends bool
}{"optional review waiting": {seat.StepResultStatusOptional, true}, "required review waiting": {seat.StepResultStatusRequired, false}} {
p := seat.Pipeline{Outcome: seat.PipelineOutcomeRunning, Steps: []seat.StepResult{
{Step: seat.StepResultStepBuild, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomePassed},
{Step: seat.StepResultStepReview, Status: c.review, Outcome: seat.StepResultOutcomeWaiting},
{Step: seat.StepResultStepApproval, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomePassed},
}}
if got := endPipeline(&p, now); got != c.ends {
t.Errorf("%s: ended = %v, want %v", name, got, c.ends)
}
if c.ends && p.Outcome != seat.PipelineOutcomePassed {
t.Errorf("%s: outcome %s", name, p.Outcome)
}
}
}
// An ask no holder took within its bound ends its step `could not run` naming the seat, and with the rest ended the pipeline
// failed (design 47 G §32, §535, §647, A-104); a step that ended, or ask nobody placed, ends nothing.
func TestAnAskNoHolderTookEndsItsStepNamingTheSeat(t *testing.T) {
h, store, said, _, _, _ := fixture()
e := store[pkey]
e.p.Steps[1].Outcome = seat.StepResultOutcomePassed // the unit step ended; only lint is waiting for a holder
e.p.Steps[2].Outcome = seat.StepResultOutcomeNotAsked
store[pkey] = e
if err := h.NotTaken("nobody-asked", AskBound); err != nil || store[pkey].p.Steps[0].Outcome != seat.StepResultOutcomeRunning {
t.Fatalf("an ask no step names ended something: %v", err)
}
if err := h.NotTaken("a1", AskBound); err != nil {
t.Fatal(err)
}
p := store[pkey].p
if p.Steps[0].Outcome != seat.StepResultOutcomeCouldNotRun || !strings.Contains(p.Steps[0].Why, nodebuildgo.Seat) ||
p.Outcome != seat.PipelineOutcomeFailed || !strings.Contains(strings.Join(*said, " "), seat.EventPipelineEnded) {
t.Errorf("step %+v, pipeline %s (%s), said %v: want could not run naming %s and a failed pipeline", p.Steps[0], p.Outcome, p.Why, *said, nodebuildgo.Seat)
}
// Its end taken once: the bound's expiry heard again changes nothing.
n := len(*said)
if err := h.NotTaken("a1", AskBound); err != nil || len(*said) != n {
t.Errorf("a second expiry said %d more events: %v", len(*said)-n, err)
}
}
// The bound is armed before the ask is put, only for the build seats whose holder says ask-taken, and an ask whose bound is
// not armed is not put (§535, §647).
func TestTheBoundIsArmedBeforeAnAskIsPut(t *testing.T) {
var order []string
asker := askerFunc(func(accept, id string, _ any) error { order = append(order, "put "+id); return nil })
arm := Arm(func(ask string) error { order = append(order, "arm "+ask); return nil })
asks := []Asked{{Seat: nodebuildgo.Seat, ID: "step-1", Ask: 1}, {Seat: wants.Seat, ID: "step-2", Ask: 2}}
if err := askAll(asker, arm, "k", asks, false); err != nil || strings.Join(order, ",") != "arm step-1,put step-1,put step-2" {
t.Errorf("order %v, %v; want the build seat's bound armed first and mesh-wants' ask with none", order, err)
}
order = nil
failing := Arm(func(string) error { return errors.New("bucket gone") })
if err := askAll(asker, failing, "k", asks[:1], false); err == nil || len(order) != 0 {
t.Errorf("an ask whose bound cannot be armed was put or said nothing: %v %v", order, err)
}
}
type askerFunc func(accept, id string, body any) error
func (f askerFunc) AskStep(accept, id string, body any) error { return f(accept, id, body) }
// A recovery's steps are asked on the build seats' stream delivery, ahead of the first round; the seats with no such stream keep
// their own accept subject; every other commit's steps go on check (design 47 G §86, §415).
func TestARecoverysStepsAreAskedOnTheStreamDelivery(t *testing.T) {
asks := []Asked{{Seat: nodebuildgo.Seat, ID: "s1", Ask: 1}, {Seat: wants.Seat, ID: "s2", Ask: 2}}
for _, c := range []struct {
recovery bool
want string
}{{true, "mesh.seat.node-build-go.accept.delivery.mesh-pipeline-runner," + wants.AcceptSubjectStep}, {false, "mesh.seat.node-build-go.accept.check.mesh-pipeline-runner," + wants.AcceptSubjectStep}} {
var to []string
asker := askerFunc(func(accept, _ string, _ any) error { to = append(to, accept); return nil })
if err := askAll(asker, nil, "k", asks, c.recovery); err != nil || strings.Join(to, ",") != c.want {
t.Errorf("recovery %v: asked on %v (%v), want %s", c.recovery, to, err, c.want)
}
}
// Starter.recovered reads the commit; a failed read asks on check and says so.
var said []string
s := &Starter{Logf: func(f string, a ...any) { said = append(said, f) }, Recovered: func(c string) (bool, error) {
if c == "bad" {
return false, errors.New("unread")
}
return c == "rec", nil
}}
if !s.recovered("k", "rec") || s.recovered("k", "other") || s.recovered("k", "bad") || len(said) != 1 {
t.Errorf("recovered: said %v", said)
}
}