Files
mesh-delivery/modules/mesh-delivery-assigner/internal/assign/wants_test.go
T
jschoubben bdbfa12aa1 A9: a node whose node-engine reads no want by version is held, awaiting its hand placement
The want of a node whose node-engine does not state the word want:version of
the seat node-engine, as the engine reads keeper holds it from
manifest-reads-changed, is written with hold and the words
awaits-hand-placement; once the engine states the word, the next pass writes
the want unheld.

Builds: design 47 H §772; H-A-45, H-A-73

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 04:57:45 +01:00

271 lines
15 KiB
Go

package assign
import (
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
"git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-assigner/asks"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
mwc "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshwantsclient"
"mesh-delivery-assigner/internal/puts"
)
func fpOf(c byte) string { return meshcatalogclient.SourcePrefix2 + strings.Repeat(string(c), 64) }
var (
fNew = fpOf('b')
fOld = fpOf('a')
)
// dig is the digest of the composition at a fingerprint in these tests: the fingerprint's last character repeated.
func dig(fp string) string { return "sha256:" + strings.Repeat(fp[len(fp)-1:], 64) }
// released is a module mail on ace and g14 whose newest version released on its branch with a passed pipeline is 1.2.3 at fNew,
// the second tier of the pipeline main.12.
func released() Module {
t := Target{Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2}
return Module{Name: "mail", Nodes: []string{"g14", "ace"}, Target: func(string) (Target, bool) { return t, true }}
}
func wantedOld() map[string]mwc.Want {
return map[string]mwc.Want{"ace": {Fp: fOld, Version: "1.2.2", Pipeline: "main.11", Rank: 1}, "g14": {Fp: fOld, Version: "1.2.2", Pipeline: "main.11", Rank: 1}}
}
// A6/AC1, A5/AC1 (design 47 H §750): a want names the newest released version whose pipeline passed, with the pipeline and the
// mover's rank, on every node the module is assigned to; nothing else holds it: no provider, canary, grant, composition, reader
// capability, provision, served verb or bus state is read. A node with no target keeps the want written; a node no longer
// assigned is removed; a want that stands is not written again and keeps the answers it carries.
func TestAWantNamesTheNewestPassedVersion(t *testing.T) {
want := mwc.Want{Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2}
for _, c := range []struct {
name string
module func() Module
before map[string]mwc.Want
wrote map[string]mwc.Want
}{
{"both nodes move to the passed version", released, wantedOld(), map[string]mwc.Want{"ace": want, "g14": want}},
{"a first want", released, nil, map[string]mwc.Want{"ace": want, "g14": want}},
{"a placed node composes its own", func() Module {
m := released()
m.Self = func(node string) bool { return node == "ace" }
return m
}, nil, map[string]mwc.Want{"ace": {Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2, Self: true}, "g14": want}},
{"no passed version yet: the want written stays", func() Module {
m := released()
m.Target = func(string) (Target, bool) { return Target{}, false }
return m
}, wantedOld(), map[string]mwc.Want{}},
{"a want that stands is not written again", released, map[string]mwc.Want{"ace": want, "g14": want}, map[string]mwc.Want{}},
{"the backup answer stays while the build does", released,
map[string]mwc.Want{"ace": {Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2, Backup: mwc.BackupSkip, Words: BackupAnswerWords("skip", 0)}, "g14": want},
map[string]mwc.Want{}},
{"A9/AC1 (§772, H-A-73): a node whose engine reads no want by version is held, awaiting its hand placement", func() Module {
m := released()
m.ReadsVersion = func(node string) bool { return node == "ace" }
return m
}, nil, map[string]mwc.Want{"ace": want, "g14": {Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2, Hold: true, Words: mwc.HoldAwaitsHandPlacement}}},
{"a node no longer assigned is removed", func() Module {
m := released()
m.Nodes = []string{"ace"}
return m
}, map[string]mwc.Want{"ace": want, "g14": want}, map[string]mwc.Want{"g14": {Removed: true, Words: "mail is no longer assigned to g14"}}},
} {
out := Wants(c.module(), c.before)
if len(out.Wants) != len(c.wrote) {
t.Errorf("%s: wrote %+v, want %+v", c.name, out.Wants, c.wrote)
continue
}
for node, w := range c.wrote {
if got, ok := out.Wants[node]; !ok || !mwc.SameWant(normal(got), w) {
t.Errorf("%s: %s's want = %+v (written %v), want %+v", c.name, node, got, ok, w)
}
}
}
}
// ADR 0546: a put owns the node's want from its asking until it ends; while it waits the rule writes nothing there, once wanted its
// want stands, marked put and held, and once released the rule writes the passed version again.
func TestAPutOwnsItsNodesWant(t *testing.T) {
m := released()
put := puts.Put{FP: fNew, Hold: true, Why: "the bus's user list", Composed: dig(fNew)}
for _, c := range []struct {
name string
step string
wanted map[string]mwc.Want
want *mwc.Want // nil: nothing written on ace
}{
{"waiting for the operator", puts.Person, wantedOld(), nil},
{"wanted", puts.Wanted, wantedOld(), &mwc.Want{Fp: fNew, Put: true, Hold: true, Words: "put: the bus's user list"}},
{"holding, written already", puts.Holding, map[string]mwc.Want{"ace": PutWant(put)}, nil},
{"released: the rule again", puts.Released, map[string]mwc.Want{"ace": PutWant(put)}, &mwc.Want{Fp: fNew, Version: "1.2.3", Pipeline: "main.12", Rank: 2}},
{"not heard: a held want at an older fingerprint stands", "", map[string]mwc.Want{"ace": {Fp: fOld, Put: true, Hold: true, Words: "put: back"}}, nil},
{"not heard: a held want the rule would rewrite stands", "", map[string]mwc.Want{"ace": PutWant(put)}, nil},
} {
p := put
p.Step = c.step
m.Puts = map[string]puts.Put{"ace": p}
if c.step == "" {
m.Puts = nil
}
got, wrote := Wants(m, c.wanted).Wants["ace"]
switch {
case c.want == nil && wrote:
t.Errorf("put %s: the rule wrote ace's want %+v", c.name, got)
case c.want != nil && (!wrote || !mwc.SameWant(normal(got), *c.want)):
t.Errorf("put %s: ace's want = %+v (written %v), want %+v", c.name, got, wrote, *c.want)
}
}
stepsOfAPut(t)
}
// §520: health is read from the node-engine seat's `running` alone, and a build of which nothing was heard is not healthy.
func TestHealthIsReadFromRunningAlone(t *testing.T) {
yes, no := true, false
n := Nodes{Haves: map[string]map[string]nodeengine.HaveByNodeModuleValue{"ace": {"mail": {Fp: fNew, Phase: nodeengineclient.PhaseFailed, Health: nodeengineclient.HealthFailed}}},
Running: map[string]map[string]nodeengine.Running{"ace": {"mail": {Fingerprint: fNew, Healthy: &yes}}, "g14": {"mail": {Fingerprint: fNew, Healthy: &no}}}}
if !n.RunsHealthy("ace", "mail", fNew) || n.RunsHealthy("g14", "mail", fNew) || n.RunsHealthy("ace", "mail", fOld) || n.RunsHealthy("novox", "mail", fNew) {
t.Errorf("health read from running: ace %v, g14 %v, ace at fOld %v, novox %v", n.RunsHealthy("ace", "mail", fNew),
n.RunsHealthy("g14", "mail", fNew), n.RunsHealthy("ace", "mail", fOld), n.RunsHealthy("novox", "mail", fNew))
}
}
// stepsOfAPut takes a put through its steps (ADR 0546 clauses 3 to 8): the build, the controller's composition, the
// operator's yes, the bus's snapshot, the want, and the have.
func stepsOfAPut(t *testing.T) {
t.Helper()
now := time.Date(2026, 10, 25, 22, 0, 0, 0, time.UTC)
base := puts.Put{FP: fNew, Hold: true, Why: "the bus's user list", At: now}.Said(now, puts.Asked, "asked")
d := ComposedDigest{Digest: dig(fNew), Judgement: JudgedCannot, Words: "the have names no composition"}
running := nodeengine.HaveByNodeModuleValue{Fp: fOld, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}
bus := func() PutWorld {
return PutWorld{Node: "novox", Module: "nats", Put: base, Assigned: true, Bus: true, Build: builds.BuildDone, Newest: fNew,
Composed: &d, Granted: true, Have: &running, Now: now}
}
cases := []struct {
name string
world func() PutWorld
step string
says string
ask, snap bool
warranted bool
}{
{"not assigned", func() PutWorld { w := bus(); w.Assigned = false; return w }, puts.Refused, "not assigned", false, false, false},
{"no build yet of the newest", func() PutWorld { w := bus(); w.Build = ""; return w }, puts.Build, "builds by itself", false, false, false},
{"no build of an older one", func() PutWorld { w := bus(); w.Build, w.Newest = "", fpOf('c'); return w }, puts.Refused, "holds no build", false, false, false},
// A-427: the build the node runs now is put from its running statement, held with the have's composition, the
// builder's store not asked (a part started by hand has no build there); a have naming no composition refuses it.
{"the build the node runs now, not in the builder's store: held there", func() PutWorld {
w := bus()
w.Build, w.Newest = "", fpOf('c')
w.Running = &nodeengine.Running{Node: "novox", Module: "nats", Fingerprint: fNew}
w.Have = &nodeengine.HaveByNodeModuleValue{Fp: fNew, Composed: dig(fNew), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}
return w
}, puts.Holding, "held there until released", false, false, false},
{"the build the node runs now, its have naming no composition: refused", func() PutWorld {
w := bus()
w.Build, w.Newest = "", fpOf('c')
w.Running = &nodeengine.Running{Node: "novox", Module: "nats", Fingerprint: fNew}
return w
}, puts.Refused, "names no composition", false, false, false},
{"a failed build", func() PutWorld { w := bus(); w.Build, w.BuildWords = builds.BuildFailed, "go vet"; return w }, puts.Refused, "go vet", false, false, false},
{"no composition yet: waits", func() PutWorld { w := bus(); w.Composed = nil; return w }, puts.Compose, "no composition of nats for novox", false, false, false},
{"composed, grant not naming the node", func() PutWorld { w := bus(); w.Granted = false; return w }, puts.Compose, "does not name novox", false, false, false},
{"none within the bound: refused", func() PutWorld {
w := bus()
w.Composed = nil
w.Put = w.Put.Said(now.Add(-ComposeWithin-time.Second), puts.Compose, "waits")
return w
}, puts.Refused, "mesh-hand-compose", false, false, false},
{"the bus asks the operator", bus, puts.Person, "waits for the operator's yes", true, false, false},
{"the operator said no", func() PutWorld { w := bus(); w.Answer = OptionNo; return w }, puts.Refused, "answered no", false, false, false},
{"the ask expired unanswered", func() PutWorld { w := bus(); w.AskExpired = true; return w }, puts.Refused, "expired unanswered", false, false, false},
{"yes: the snapshot is asked", func() PutWorld { w := bus(); w.Answer = OptionYes; return w }, puts.Snapshot, "asked novox's backup holder", false, true, false},
{"the snapshot is taken", func() PutWorld { w := bus(); w.Answer, w.Snapshotting = OptionYes, true; return w }, puts.Snapshot, "waits for novox's backup holder", false, false, false},
{"the snapshot failed", func() PutWorld { w := bus(); w.Answer, w.SnapshotFailed = OptionYes, "no restic"; return w }, puts.Refused, "no restic", false, false, false},
{"snapshotted: the want, warranted", func() PutWorld {
w := bus()
w.Answer, w.Snapshot = OptionYes, "novox's restore point"
w.Put.Ask, _ = asks.RecordAskKey("novox", "nats", d.Digest)
return w
}, puts.Wanted, "held until released; the streams were snapshotted: novox's restore point", false, false, true},
{"a user-list revision of the bus needs nobody", func() PutWorld {
w := bus()
ul := d
ul.UserListOnly = true
w.Composed, w.Have = &ul, &nodeengine.HaveByNodeModuleValue{Fp: fNew, Composed: dig(fOld), Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}
return w
}, puts.Wanted, "held until released", false, false, false},
{"a module not recorded needs nobody", func() PutWorld { w := bus(); w.Bus = false; return w }, puts.Wanted, "names", false, false, false},
{"a recorded module judged to recreate asks", func() PutWorld {
w := bus()
w.Bus, w.Recorded = false, true
r := d
r.Judgement = JudgedRecreates
w.Composed = &r
return w
}, puts.Person, "waits for the operator's yes", true, false, false},
{"wanted, the node runs it: holding", func() PutWorld {
w := bus()
w.Put = w.Put.Said(now, puts.Wanted, "wanted")
w.Put.Composed = d.Digest
w.Running = &nodeengine.Running{Node: "novox", Module: "nats", Fingerprint: fNew}
return w
}, puts.Holding, "held there until released", false, false, false},
{"wanted without hold: done", func() PutWorld {
w := bus()
w.Put.Hold = false
w.Put = w.Put.Said(now, puts.Wanted, "wanted")
w.Put.Composed = d.Digest
w.Running = &nodeengine.Running{Node: "novox", Module: "nats", Fingerprint: fNew}
return w
}, puts.Done, "the rules write its want again", false, false, false},
{"wanted without hold, the node composes its own: done at the fingerprint", func() PutWorld {
w := bus()
w.Put.Hold = false
w.Put = w.Put.Said(now, puts.Wanted, "wanted")
w.Put.Composed = d.Digest
w.Running = &nodeengine.Running{Node: "novox", Module: "nats", Fingerprint: fNew}
return w
}, puts.Done, "the rules write its want again", false, false, false},
{"wanted, the node's running names no fingerprint: stays wanted (the have runs it, which ends nothing)", func() PutWorld {
w := bus()
w.Put = w.Put.Said(now, puts.Wanted, "wanted")
w.Put.Composed = d.Digest
w.Running = &nodeengine.Running{Node: "novox", Module: "nats"}
w.Have = &nodeengine.HaveByNodeModuleValue{Fp: fNew, Composed: d.Digest, Phase: nodeengineclient.PhaseRunning, Health: nodeengineclient.HealthOK}
return w
}, puts.Wanted, "wanted", false, false, false},
{"wanted, the node's running says it stopped: stays wanted", func() PutWorld {
w := bus()
w.Put = w.Put.Said(now, puts.Wanted, "wanted")
w.Running = &nodeengine.Running{Node: "novox", Module: "nats", Fingerprint: fNew, Stopped: true}
return w
}, puts.Wanted, "wanted", false, false, false},
{"wanted, the node's running names another build: stays wanted", func() PutWorld {
w := bus()
w.Put = w.Put.Said(now, puts.Wanted, "wanted")
w.Running = &nodeengine.Running{Node: "novox", Module: "nats", Fingerprint: fOld}
return w
}, puts.Wanted, "wanted", false, false, false},
{"released: nothing more", func() PutWorld { w := bus(); w.Put = w.Put.Said(now, puts.Released, "released"); return w }, puts.Released, "released", false, false, false},
}
for _, c := range cases {
next := StepPut(c.world())
if next.Put.Step != c.step || !strings.Contains(next.Put.Words, c.says) {
t.Errorf("put, %s: at %s, %q; want %s saying %q", c.name, next.Put.Step, next.Put.Words, c.step, c.says)
}
if next.Ask != c.ask || next.Snapshot != c.snap || next.Warranted != c.warranted {
t.Errorf("put, %s: acts ask %v snapshot %v warranted %v; want %v %v %v", c.name, next.Ask, next.Snapshot, next.Warranted,
c.ask, c.snap, c.warranted)
}
if next.Ask && next.Put.Ask == "" {
t.Errorf("put, %s: an ask without its key", c.name)
}
}
}