Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/steps_test.go
T
jschoubben ea5abba84d H-A-506 review 1: a shared run's ask key outlives the asking pipeline's end
At a pipeline's end every ask key of its steps went, a shared test run's
among them, while another pipeline still read the run: that reader's
progress found no key and its steps ended could not run. A shared run's key
now goes only with the run's end (EndRun, leaveRun, unmarkAsks), and a late
ask-taken clears a finding only while its pipeline runs (second addendum).
A declined step asked again deletes its earlier ask's key whatever its run.
The last reader's drop of a run's ask is asked beside the leave, its one
journal line saying dropped, or refused or not answered. A re-pin's
holderless end is tested to drop the open asks of the movers it keeps.

Builds: design 47 H2 H-A-500 addendum (1), H-A-506 corrected, H-A-506 addendum, H-A-506 second addendum
2026-11-23 00:18:55 +01:00

595 lines
31 KiB
Go

package pipeline
import (
"encoding/json"
"errors"
"fmt"
"slices"
"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"
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshbuilds"
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
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...)
e.p.Movers = append([]seat.Mover(nil), e.p.Movers...)
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"
// 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) {
store := memStore{pkey: {p: seat.Pipeline{V: 1, Pipeline: pkey, Branch: "main", Source: 5,
Outcome: seat.PipelineOutcomeRunning, Steps: []seat.StepResult{
{Module: "app", Artifact: "program", Step: seat.StepResultStepLint, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: "a1", Seat: nodebuildgo.Seat},
{Module: "app", Artifact: "program", Step: seat.StepResultStepUnit, Status: seat.StepResultStatusOptional, Outcome: seat.StepResultOutcomeRunning, Ask: "a2", Seat: nodebuildgo.Seat},
{Module: "app", Artifact: "program", Step: seat.StepResultStepApproval, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeWaiting}}, Movers: []seat.Mover{{Module: "app", Commit: "c1", Fingerprint: "src2:aa"}}}, rev: 1}}
said := &[]string{}
asker := &askRecorder{}
kept := map[string]seat.KeptResult{}
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 },
Now: func() time.Time { return time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC) }}
return h, store, said, asker, kept
}
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"
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")
}
// R2/AC1 (design 47 G §24, §69): the end of another asker's step (the assigner's composer build, which names no
// pipeline) is not the runner's: it writes and says nothing, read before any pipeline, and is acknowledged.
theirs := ended("b7", "build", "passed")
theirs.Asker, theirs.Pipeline = "mesh-delivery-assigner", ""
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", theirs); err != nil {
t.Errorf("another asker's step end: err %v, want it taken as nothing", err)
}
if store[pkey].p.Steps[0].Outcome != seat.StepResultOutcomeRunning || store[pkey].rev != 1 {
t.Error("another asker's step 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) {
t.Run("an optional step still waiting holds no end", testAnOptionalStepStillWaitingHoldsNoEnd)
h, store, said, _, kept := fixture()
h.Asker = nil
lint := ended("a1", "lint", "passed")
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" {
t.Fatalf("after the lint: %+v", p)
}
// 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)
}
// Design 47 H2 H-A-375, H-A-445: a step declined for a need its node lacks is put again under a new ask naming the nodes
// that declined it, once per node; with no node left that has the need, it ends could not run naming the need and them.
t.Run("declined", func(t *testing.T) {
h, store, _, _, _ := fixture()
h.Asker = nil
var put []Asked
var seats []string
h.NeedNodes = func(s []string) ([]string, bool) { seats = s; return []string{"n1", "n2"}, true }
h.AskDeclined = func(p seat.Pipeline, s seat.StepResult, _ time.Time) (*Asked, error) {
id := s.Ask + "x"
return &Asked{Seat: s.Seat, ID: id, Ask: nodebuildgo.StepAsk{ID: id}}, nil
}
h.PutAgain = func(_ string, a Asked) error { put = append(put, a); return nil }
declined := func(ask, node string) StepEnded {
ev := ended(ask, "lint", nodebuildgo.StepEndedOutcomeDeclined)
ev.Node, ev.Needs, ev.Lacks, ev.Said = node, []string{"node-build-node"}, []string{"node-build-node"}, "this node lacks node-build-node"
return ev
}
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", declined("a1", "n1")); err != nil {
t.Fatal(err)
}
lint := store[pkey].p.Steps[0]
if lint.Outcome != seat.StepResultOutcomeRunning || lint.Ask != "a1x" || !slices.Equal(lint.NotOn, []string{"n1"}) || lint.AskedAt == nil ||
len(put) != 1 || put[0].ID != "a1x" || !slices.Equal(put[0].Ask.(nodebuildgo.StepAsk).NotOn, []string{"n1"}) ||
!slices.Equal(seats, []string{nodebuildgo.Seat, "node-build-node"}) {
t.Fatalf("after the first decline: the step %+v, put %+v, nodes asked for %v; want it running under a1x, not on n1", lint, put, seats)
}
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n2", declined("a1x", "n2")); err != nil {
t.Fatal(err)
}
lint = store[pkey].p.Steps[0]
if want := "it needs node-build-node, and no node that has it is left to ask: asked of n1, n2, which declined it"; lint.Outcome != seat.StepResultOutcomeCouldNotRun ||
lint.Why != want || len(put) != 1 || lint.Ended == nil {
t.Errorf("after the second decline: the step %+v, %d puts; want could not run, %q", lint, len(put), want)
}
})
// H-A-506 addendum, corrected: a declined test-run step asked again keeps one ask key, its new ask's; the earlier
// ask's key is deleted as it is asked again, not at the run's end.
t.Run("declined test run", func(t *testing.T) {
h, store, _, _, _ := fixture()
h.Asker = nil
clock := &clockMem{runs: testRunsMem{}, bound: map[string]time.Duration{}}
h.Clock = clock
e := store[pkey]
e.p.Steps[0].Shared = []seat.Shared{{Run: "main.5.lint.d", Ask: "a1"}}
store[pkey] = e
_ = clock.MarkAsk("a1", AskMark{Pipeline: pkey, Module: "app", Step: "lint", Run: "main.5.lint.d"})
h.NeedNodes = func([]string) ([]string, bool) { return []string{"n1", "n2"}, true }
h.AskDeclined = func(_ seat.Pipeline, s seat.StepResult, _ time.Time) (*Asked, error) {
return &Asked{Seat: s.Seat, ID: s.Ask + "x", Ask: nodebuildgo.StepAsk{ID: s.Ask + "x"}}, nil
}
h.PutAgain = func(string, Asked) error { return nil }
ev := ended("a1", "lint", nodebuildgo.StepEndedOutcomeDeclined)
ev.Node, ev.Needs, ev.Lacks, ev.Said = "n1", []string{"node-build-node"}, []string{"node-build-node"}, "this node lacks node-build-node"
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ev); err != nil {
t.Fatal(err)
}
if lint := store[pkey].p.Steps[0]; lint.Ask != "a1x" {
t.Fatalf("the declined test-run step = %+v, want it asked again as a1x", lint)
}
if _, ok, _ := clock.AskOf("a1"); ok {
t.Error("the earlier ask's key of a declined test-run step outlives its ask")
}
})
}
// 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) {
t.Run("approval ends", testApprovalEnds)
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.main.5") {
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.main.5") {
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).
// testApprovalEnds is a row of TestApprovalIsAskedLastAndEndedByItsWarrant (design 47 H2 §819: the module's tests are folded under its budget).
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). A mover with no step placed, waiting on its registration (A-327) or on the publish of a mover it
// builds on (§746), holds the end while every placed mover's required steps passed or one is open, and once a placed one
// did not pass the pipeline ends failed, naming each with its own reason (H-A-163, H-A-171).
// testAnOptionalStepStillWaitingHoldsNoEnd is a row of TestStepEndedEndsThePipeline (design 47 H2 §819: the module's tests are folded under its budget).
func testAnOptionalStepStillWaitingHoldsNoEnd(t *testing.T) {
now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
// lib is registered and awaits core's package, which core's passed build has not published yet; contracts has no
// registration.
starter := &Starter{Registration: func(m string) (catalog.Registration, bool) { return catalog.Registration{Module: m}, m != "contracts" }}
view := func(string) (Half, bool) {
return Half{Entries: map[string]catalog.CatalogEntry{"lib": {Edges: []catalog.Edge{{From: "core:go", To: "lib:program", Kind: 1}}}}}, true
}
unplaced := func(p seat.Pipeline, m string) string { return starter.Unplaced(p, m, view) }
for name, c := range map[string]struct {
review, build, unit string
waiting []string
ends bool
outcome string
why []string
}{
"optional review waiting": {seat.StepResultStatusOptional, seat.StepResultOutcomePassed, seat.StepResultOutcomePassed, nil, true, seat.PipelineOutcomePassed, nil},
"required review waiting": {seat.StepResultStatusRequired, seat.StepResultOutcomePassed, seat.StepResultOutcomePassed, nil, false, "", nil},
"unregistered mover, the placed one passed": {seat.StepResultStatusOptional, seat.StepResultOutcomePassed, seat.StepResultOutcomePassed, []string{"contracts"}, false, "", nil},
"unregistered mover, the placed one failed": {seat.StepResultStatusOptional, seat.StepResultOutcomeFailed, seat.StepResultOutcomePassed, []string{"contracts"}, true, seat.PipelineOutcomeFailed, []string{"contracts has no registration"}},
"unregistered mover, failed and a step open": {seat.StepResultStatusOptional, seat.StepResultOutcomeFailed, seat.StepResultOutcomeRunning, []string{"contracts"}, false, "", nil},
"unregistered mover, the placed one could not run": {seat.StepResultStatusOptional, seat.StepResultOutcomePassed, seat.StepResultOutcomeCouldNotRun, []string{"contracts"}, true, seat.PipelineOutcomeFailed, []string{"contracts has no registration"}},
"a mover waiting on a publish, the placed one failed": {seat.StepResultStatusOptional, seat.StepResultOutcomeFailed, seat.StepResultOutcomePassed, []string{"contracts", "lib"}, true, seat.PipelineOutcomeFailed,
[]string{"contracts has no registration", "lib waits on the publish of core:go"}},
} {
p := seat.Pipeline{Outcome: seat.PipelineOutcomeRunning, Movers: []seat.Mover{{Module: "app"}, {Module: "core"}}, Steps: []seat.StepResult{
{Step: seat.StepResultStepBuild, Module: "app", Status: seat.StepResultStatusRequired, Outcome: c.build},
{Step: seat.StepResultStepUnit, Module: "app", Status: seat.StepResultStatusRequired, Outcome: c.unit},
{Step: seat.StepResultStepReview, Module: "app", Status: c.review, Outcome: seat.StepResultOutcomeWaiting},
{Step: seat.StepResultStepApproval, Module: "app", Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomePassed},
{Step: seat.StepResultStepBuild, Module: "core", Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomePassed},
}}
for _, m := range c.waiting {
p.Movers = append(p.Movers, seat.Mover{Module: m})
}
if got := endPipeline(&p, now, unplaced); got != c.ends {
t.Errorf("%s: ended = %v, want %v", name, got, c.ends)
}
if c.ends && p.Outcome != c.outcome {
t.Errorf("%s: outcome %s, want %s", name, p.Outcome, c.outcome)
}
for _, w := range c.why {
if !strings.Contains(p.Why, w) {
t.Errorf("%s: why %q, want it to name %q", name, p.Why, w)
}
}
if strings.Contains(p.Why, "lib has no registration") {
t.Errorf("%s: why %q names a registered mover as having no registration", name, p.Why)
}
}
}
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, "k", asks, func(string) bool { return c.recovery }, nil, nil); err != nil || strings.Join(to, ",") != c.want {
t.Errorf("recovery %v: asked on %v (%v), want %s", c.recovery, to, err, c.want)
}
}
// Each ask put is one journal line naming the pipeline, the mover, the step and the seat: the build ask on mesh-builds and a
// check ask alike (H-A-320 (1)); an ask refused writes none.
var lines []string
put := []Asked{{Seat: builds.Seat, ID: "b1", Ask: builds.StepRun{Step: builds.StepRunStepBuild, Entries: []builds.BuildEntry{{Module: "app"}}}},
{Seat: nodebuildgo.Seat, ID: "u1", Ask: nodebuildgo.StepAsk{Step: &nodebuildgo.StepRun{Module: "app", Step: "unit"}}},
{Seat: "no-seat", ID: "x1", Ask: 3}}
_ = askAll(askerFunc(func(string, string, any) error { return nil }), "main.4", put, nil, nil,
func(f string, a ...any) { lines = append(lines, fmt.Sprintf(f, a...)) })
if want := "main.4 app build asked on mesh-builds\nmain.4 app unit asked on node-build-go"; strings.Join(lines, "\n") != want {
t.Errorf("journal %q, want %q", lines, 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)
}
}
// A mover whose fingerprint a newer revision moved leaves a running pipeline (H-A-223, H-A-163): its open steps end
// superseded, never failed, the other mover's go on and the pipeline ends by them alone; one every mover left ends
// superseded.
func TestAMovedMoverLeavesARunningPipeline(t *testing.T) {
t0 := time.Date(2026, 11, 21, 1, 0, 0, 0, time.UTC)
running := func() seat.Pipeline {
return seat.Pipeline{V: 1, Pipeline: "main.54", Branch: "main", Source: 54, Revision: 54, Outcome: seat.PipelineOutcomeRunning, Started: &t0,
Movers: []seat.Mover{{Module: "a", Commit: "c1", Fingerprint: "src2:a1"}, {Module: "b", Commit: "c1", Fingerprint: "src2:b1"}},
Steps: []seat.StepResult{
{Module: "a", Step: seat.StepResultStepBuild, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: "x1", Started: &t0},
{Module: "b", Step: seat.StepResultStepBuild, Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: "x2", Started: &t0},
{Module: "b", Step: seat.StepResultStepLint, Artifact: "b:program", Status: seat.StepResultStatusRequired, Outcome: seat.StepResultOutcomeRunning, Ask: "x3", Started: &t0,
Seat: nodebuildgo.Seat, Node: "n2"}}}
}
rows := []struct {
name string
newer []seat.Mover
want string
}{
{"a newer revision moving b: b's steps end superseded, a's go on and end the pipeline passed alone",
[]seat.Mover{{Module: "b", Commit: "c2", Fingerprint: "src2:b2"}}, "running; a build running; b build superseded; b lint superseded | passed"},
{"a newer revision moving every mover ends the pipeline superseded",
[]seat.Mover{{Module: "a", Commit: "c2", Fingerprint: "src2:a2"}, {Module: "b", Commit: "c2", Fingerprint: "src2:b2"}},
"superseded; a build superseded; b build superseded; b lint superseded | superseded"},
{"a newer revision at the same fingerprint takes nothing", []seat.Mover{{Module: "b", Commit: "c1", Fingerprint: "src2:b1"}},
"running; a build running; b build running; b lint running | running"},
}
for _, r := range rows {
t.Run(r.name, func(t *testing.T) {
store := memStore{}
store["main.54"] = struct {
p seat.Pipeline
rev uint64
}{running(), 1}
var said, dropped []string
h := &Hearer{Store: store, Say: func(subject string, _ any) error { said = append(said, subject); return nil }, Now: func() time.Time { return t0 },
DropRun: func(buildSeat, node, ask, why string) error {
dropped = append(dropped, buildSeat+" "+node+" "+ask+" "+fmt.Sprint(strings.Contains(why, "main.57")))
return nil
}}
h.SupersedeRunning(seat.Pipeline{Pipeline: "main.57", Branch: "main", Revision: 57, Movers: r.newer})
// b's lint ask on its build node is dropped naming the newer pipeline; the build ask on mesh-builds, which
// serves no drop, is not (H-A-223).
wantDropped := ""
if r.newer[0].Fingerprint != "src2:b1" {
wantDropped = "node-build-go n2 x3 true"
}
if got := strings.Join(dropped, ","); got != wantDropped {
t.Errorf("dropped %q; want %q", got, wantDropped)
}
p, _, _ := store.Get("main.54")
got := []string{p.Outcome}
for _, s := range p.Steps {
got = append(got, s.Module+" "+s.Step+" "+s.Outcome)
if s.Outcome == seat.StepResultOutcomeSuperseded && !strings.Contains(s.Why, "main.57") {
t.Errorf("the superseded step's why %q names no newer pipeline", s.Why)
}
}
// The mover left holds nothing: a's build passing ends the pipeline by a alone.
if p.Outcome == seat.PipelineOutcomeRunning {
p.Steps[0].Outcome, p.Steps[0].Ended = seat.StepResultOutcomePassed, &t0
endPipeline(&p, t0, nil)
}
if s := strings.Join(got, "; ") + " | " + p.Outcome; s != r.want {
t.Errorf("got %s\nwant %s", s, r.want)
}
})
}
// The live path: a newer pipeline started through Start, a take-in's or a release revision's alike, takes the moved
// mover out of the older running pipeline over the same store (H-A-223, H-A-213).
starts := []struct {
name string
begin Begin
}{
{"a take-in", Begin{Branch: "main", Source: 55, Revision: 55, Call: "0123456789abcdef", Movers: []seat.Mover{{Module: "b", Commit: "c2", Fingerprint: "src2:b2"}}}},
{"a release revision", Begin{Branch: "main", Source: 54, Revision: 57, Call: "release of mesh-sdk 0.1.26", Movers: []seat.Mover{{Module: "b", Commit: "c1", Fingerprint: "src2:b2"}}}},
}
for _, r := range starts {
t.Run("started by "+r.name, func(t *testing.T) {
store := &MoverStore{Raw: newMemRaw()}
raw, _ := json.Marshal(running())
if err := store.Create("main.54", raw); err != nil {
t.Fatal(err)
}
h := &Hearer{Store: store, Say: func(string, any) error { return nil }, Now: func() time.Time { return t0 }}
s := &Starter{Pipelines: store, Store: store, Say: func(string, any) error { return nil }, Now: func() time.Time { return t0 },
Supersede: func(newer seat.Pipeline) { h.SupersedeRunning(newer) }}
if ok, err := s.Start(r.begin); !ok || err != nil {
t.Fatalf("start = %v, %v", ok, err)
}
p, _, _ := store.Get("main.54")
var got []string
for _, st := range p.Steps {
got = append(got, st.Module+" "+st.Step+" "+st.Outcome)
}
if s := p.Outcome + "; " + strings.Join(got, "; "); s != "running; a build running; b build superseded; b lint superseded" {
t.Errorf("the older pipeline after %s = %s; want b left it", r.name, s)
}
})
}
}
// Design 47 H2 §784: a step's end is one journal line, "step ended <pipeline> <mover> <step> <outcome> in <duration>",
// written where step-ended is said, once per end: the hearer's and a verb's.
func TestEachStepEndWritesItsLineOnce(t *testing.T) {
at := time.Date(2026, 11, 22, 1, 0, 0, 0, time.UTC)
later := at.Add(2 * time.Minute)
var lines []string
logf := func(f string, a ...any) { lines = append(lines, fmt.Sprintf(f, a...)) }
p := seat.Pipeline{Pipeline: "main.5", Branch: "main"}
if err := sayStepEnded(logf, nil, p, seat.StepResult{Module: "app", Step: "lint", Outcome: "passed", Started: &at, Ended: &later}, later); err != nil ||
len(lines) != 1 || lines[0] != "step ended main.5 app lint passed in 2m0s" {
t.Fatalf("the line %q (%v)", lines, err)
}
if _ = sayStepEnded(logf, nil, p, seat.StepResult{Step: "validate", Outcome: "not-asked"}, later); lines[1] != "step ended main.5 - validate not-asked in no start" {
t.Errorf("a step with no mover and no start: %q", lines[1])
}
h, _, _, _, _ := fixture()
lines = nil
h.Logf = logf
if _, err := h.OnStepEnded(goSeat, goSeat.Prefix+"n1", ended("a1", "lint", "passed")); err != nil {
t.Fatal(err)
}
if n := countLines(lines, "step ended "+pkey+" app lint passed in "); n != 1 {
t.Errorf("the hearer's end wrote %d lines in %q, want one", n, lines)
}
v := Verbs{Hearer: h, Acts: &actLog{}}
if _, err := v.Cancel("operator", seat.CancelInput{Pipeline: pkey, Why: "not needed"}); err != nil {
t.Fatal(err)
}
for _, step := range []string{"lint", "unit", "approval"} {
if n := countLines(lines, "step ended "+pkey+" app "+step+" "); n != 1 {
t.Errorf("the %s step's ends wrote %d lines in %q, want one", step, n, lines)
}
}
}