Files
mesh-catalog/modules/mesh-conditions/cmd/mesh-conditions/keeper_test.go
T

697 lines
31 KiB
Go

package main
import (
"encoding/json"
"fmt"
nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
catseat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
meshwants "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshwants"
"slices"
"sort"
"strings"
"sync"
"testing"
"time"
cc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
seat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
meshhealers "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhealers"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// fakeStore is the bus's states in memory, with revisions, as the runtime answers them.
type fakeStore struct {
mu sync.Mutex
vals map[string][]byte
revs map[string]uint64
next uint64
// ttls are the times to live the keys were created with.
ttls map[string]time.Duration
}
func newFakeStore() *fakeStore {
return &fakeStore{vals: map[string][]byte{}, revs: map[string]uint64{}}
}
func (f *fakeStore) Get(state, key string) ([]byte, uint64, bool, error) {
f.mu.Lock()
defer f.mu.Unlock()
v, ok := f.vals[state+"/"+key]
return v, f.revs[state+"/"+key], ok, nil
}
// Get2 says whether the key is held.
func (f *fakeStore) Get2(state, key string) ([]byte, bool) {
v, _, ok, _ := f.Get(state, key)
return v, ok
}
func (f *fakeStore) put(state, key string, value any) uint64 {
raw, _ := json.Marshal(value)
f.next++
f.vals[state+"/"+key], f.revs[state+"/"+key] = raw, f.next
return f.next
}
func (f *fakeStore) Create(state, key string, value any) (uint64, error) {
f.mu.Lock()
defer f.mu.Unlock()
if _, ok := f.vals[state+"/"+key]; ok {
return 0, fmt.Errorf("%s: %w", key, stdio.ErrChanged)
}
return f.put(state, key, value), nil
}
func (f *fakeStore) Update(state, key string, value any, rev uint64) (uint64, error) {
f.mu.Lock()
defer f.mu.Unlock()
if f.revs[state+"/"+key] != rev {
return 0, fmt.Errorf("%s: %w", key, stdio.ErrChanged)
}
return f.put(state, key, value), nil
}
func (f *fakeStore) Delete(state, key string) error {
f.mu.Lock()
defer f.mu.Unlock()
delete(f.vals, state+"/"+key)
delete(f.revs, state+"/"+key)
return nil
}
func (f *fakeStore) Keys(state string) ([]string, error) {
f.mu.Lock()
defer f.mu.Unlock()
var out []string
for k := range f.vals {
if s, key, _ := strings.Cut(k, "/"); s == state {
out = append(out, key)
}
}
sort.Strings(out)
return out, nil
}
func (f *fakeStore) CreateTTL(state, key string, value any, ttl time.Duration) error {
f.mu.Lock()
if f.ttls == nil {
f.ttls = map[string]time.Duration{}
}
f.ttls[state+"/"+key] = ttl
f.mu.Unlock()
_, err := f.Create(state, key, value)
return err
}
// rig is a keeper over a fake store, its clock and what it said.
type rig struct {
k *Keeper
st *fakeStore
now time.Time
said []Event
}
func newRig() *rig {
r := &rig{st: newFakeStore(), now: time.Date(2026, 10, 29, 12, 0, 0, 0, time.UTC)}
r.k = &Keeper{Store: r.st, Now: func() time.Time { return r.now }, Say: func(e Event) error {
r.said = append(r.said, e)
return nil
}}
// The catalog half of the watched branch: the modules the tests hear findings from declare the finding events (§365);
// the self-check's holder claims a seat held once for the mesh.
r.half(watchmanModule, "node")
r.half(selfCheckModule, "mesh")
return r
}
// half puts a module in the watched branch's catalog half, declaring the finding events and claiming one seat of scope.
func (r *rig) half(module, scope string) {
r.k.mu.Lock()
r.k.init()
if r.k.halves["main"] == nil {
r.k.halves["main"] = map[string]catseat.CatalogEntry{}
}
r.k.halves["main"][module] = catseat.CatalogEntry{Module: module, Manifest: map[string]any{
"claims": []any{map[string]any{"name": module, "scope": scope}},
"streams": []any{map[string]any{"name": module + "-findings", "events": []any{"finding-written.*", "finding-cleared.*", "findings-stated.*"}}},
}}
r.k.mu.Unlock()
}
func (r *rig) changes() string {
var out []string
for _, e := range r.said {
out = append(out, e.Change+" "+e.Key)
}
r.said = nil
return strings.Join(out, ", ")
}
func (r *rig) open(key string) (Condition, bool) {
raw, _, ok, _ := r.st.Get(ConditionsState, key)
var c Condition
if ok {
// The seat's layout, read strictly: a field the seat does not name is a fault of the keeper's write.
v, err := seat.DecodeConditionsByKeyValue(raw)
if err != nil {
panic(fmt.Sprintf("the condition %s is not the seat's layout: %v: %s", key, err, raw))
}
c = conditionOf(v)
}
return c, ok
}
// encodeFinding writes a finding as an observer does: the seat's layout, checked.
func encodeFinding(f seat.FindingsByKeyValue) ([]byte, error) {
f.V = cc.FindingVersion
if f.Evidence == nil {
f.Evidence = []seat.Evidence{}
}
if err := cc.CheckFinding(f); err != nil {
return nil, err
}
return json.Marshal(f)
}
// finding is an observer's finding as mesh-sdk's layout writes it (encodeFinding).
func finding(t *testing.T, observer, scope, id, kind, source string, sev string, at time.Time) (string, []byte) {
t.Helper()
f := seat.FindingsByKeyValue{Key: cc.Key(scope, id, kind), Kind: kind, Subject: seat.Subject{Scope: scope, ID: id},
Severity: sev, Summary: kind + " of " + id, Headline: kind, Explanation: "It is " + kind + ".", Source: source,
Observer: observer, At: at, Evidence: []seat.Evidence{{At: at, Said: kind}}}
raw, err := encodeFinding(f)
if err != nil {
t.Fatal(err)
}
return f.Key, raw
}
func mustJSON(t *testing.T, v any) []byte {
t.Helper()
raw, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
return raw
}
// eventFinding is the machine g14's silent finding as an observer says it in finding-written.
func eventFinding(observer string, at time.Time) seat.Finding {
return seat.Finding{Scope: cc.ScopeMachine, ID: "g14", Kind: "silent", Subject: seat.Subject{Scope: cc.ScopeMachine, ID: "g14"},
Severity: cc.Warning, Summary: "silent of g14", Headline: "silent", Explanation: "It is silent.", Source: "S1",
Observer: observer, At: at, Since: at, Evidence: []seat.Evidence{{At: at, Said: "silent"}}}
}
// ADR 0564 clauses 2, 3, 7, 8 (issue 1952 C2, C3): a finding raises, changes and clears its condition; a blind observer
// clears nothing; the healers' attempts reach the condition; a reopening within ten
// minutes is the same fault; a silence is a transition in history.
func TestTheKeeperRaisesChangesAndClearsFromFindings(t *testing.T) {
r := newRig()
key, raw := finding(t, watchman, cc.ScopeMachine, "g14", "silent", "S1", cc.Warning, r.now)
steps := []struct {
name string
do func() error
said string
check func(c Condition, open bool) string
}{
{name: "a finding written raises", do: func() error { return r.k.Finding(watchman, key, raw, false) },
said: "raised machine.g14.silent", check: func(c Condition, open bool) string {
if !open || c.Severity != cc.Warning || c.Resolver != cc.ResolverSelf || c.Epoch != 0 || c.Count != 1 {
return fmt.Sprintf("%+v", c)
}
return ""
}},
{name: "the same finding again says nothing", do: func() error { return r.k.Finding(watchman, key, raw, false) }},
{name: "an observer gone blind clears nothing", do: func() error { r.k.Blind(watchman, "refused"); _, err := r.k.Resync(); return err },
check: func(c Condition, open bool) string {
if !open {
return "cleared by blindness"
}
return ""
}},
{name: "a healer's attempt reaches tried and the resolver", do: func() error {
a, _ := json.Marshal(meshhealers.AttemptsByAttemptValue{Healer: "H3", Condition: key, Act: "asserted", Outcome: meshhealers.AttemptsByAttemptValueOutcomeActed, Begun: r.now})
return r.k.Attempt("h3-1", a, false)
}, said: "resolver machine.g14.silent", check: func(c Condition, _ bool) string {
if c.Resolver != "healer:H3" || len(c.Tried) != 1 || c.Tried[0].By != "healer H3" {
return fmt.Sprintf("%+v", c)
}
return ""
}},
{name: "an escalation makes it urgent, the operator's", do: func() error {
a, _ := json.Marshal(meshhealers.AttemptsByAttemptValue{Healer: "H3", Condition: key, Act: "handed over", Outcome: meshhealers.AttemptsByAttemptValueOutcomeEscalated,
Begun: r.now.Add(time.Minute)})
return r.k.Attempt("h3-2", a, false)
}, said: "severity machine.g14.silent, resolver machine.g14.silent", check: func(c Condition, _ bool) string {
if c.Severity != cc.Urgent || c.Resolver != cc.ResolverOperator || len(c.Tried) != 2 {
return fmt.Sprintf("%+v", c)
}
return ""
}},
{name: "a silence is a transition with who and why", do: func() error {
_, err := r.k.Silence(key, time.Hour, "jochen", "known, a cable")
return err
}, said: "silenced machine.g14.silent"},
{name: "the finding deleted clears", do: func() error { return r.k.Finding(watchman, key, nil, true) },
said: "cleared machine.g14.silent", check: func(_ Condition, open bool) string {
if open {
return "still open"
}
return ""
}},
{name: "raised again within ten minutes is a reopening", do: func() error {
r.now = r.now.Add(5 * time.Minute)
return r.k.Finding(watchman, key, raw, false)
}, said: "reopened machine.g14.silent", check: func(c Condition, _ bool) string {
if c.Count != 2 || len(c.Gaps) != 1 {
return fmt.Sprintf("%+v", c)
}
return ""
}},
// Design 47 G §383: the same findings heard as events on the observer's stream.
{name: "finding-cleared clears", do: func() error {
return r.k.FindingCleared(watchman, "ace", mustJSON(t, seat.FindingCleared{At: r.now, ID: "g14", Kind: "silent", Scope: cc.ScopeMachine, Why: "back"}))
}, said: "cleared machine.g14.silent", check: func(_ Condition, open bool) string {
if open {
return "still open"
}
return ""
}},
{name: "finding-written raises", do: func() error {
return r.k.FindingWritten(watchman, "ace", mustJSON(t, eventFinding(watchman, r.now)))
}, said: "reopened machine.g14.silent"},
{name: "a body naming another observer changes nothing", do: func() error {
return r.k.FindingWritten(watchman, "ace", mustJSON(t, eventFinding(selfCheck, r.now)))
}},
{name: "a module that is no source changes nothing", do: func() error {
return r.k.FindingWritten("stranger", "ace", mustJSON(t, eventFinding("stranger", r.now)))
}},
{name: "changed words change the condition", do: func() error {
f := eventFinding(watchman, r.now)
f.Needs = "a cable"
return r.k.FindingWritten(watchman, "ace", mustJSON(t, f))
}, said: "words machine.g14.silent"},
}
for _, s := range steps {
if err := s.do(); err != nil {
t.Fatalf("%s: %v", s.name, err)
}
if got := r.changes(); s.said != "" && got != s.said || s.said == "" && got != "" {
t.Errorf("%s: said %q, want %q", s.name, got, s.said)
}
if s.check != nil {
c, open := r.open(key)
if why := s.check(c, open); why != "" {
t.Errorf("%s: %s", s.name, why)
}
}
}
// Every transition is kept in history, and each event carries the keeper's show.
keys, _ := r.st.Keys(HistoryState)
if len(keys) < 7 {
t.Errorf("history keeps %d transitions", len(keys))
}
raw0, _, _, _ := r.st.Get(HistoryState, keys[0])
if !strings.Contains(string(raw0), `"show":"mesh-conditions.conditions key=machine.g14.silent"`) {
t.Errorf("the first transition is %s", raw0)
}
}
// 1126/A5/AC2: the keeper carries no controller condition: the controller's bucket, as Controller hands it, opens no
// condition and changes none (no escalation, no silence). What it carried while the controller ran, still open, is cleared
// once by EndCarried, said as cleared; a condition an observer's finding holds stays; a second EndCarried clears nothing.
func TestTheKeeperCarriesNoControllerConditionAndClearsWhatItCarriedOnce(t *testing.T) {
r := newRig()
key, raw := finding(t, watchman, cc.ScopeMachine, "g14", "silent", "S1", cc.Warning, r.now)
if err := r.k.Finding(watchman, key, raw, false); err != nil {
t.Fatal(err)
}
r.said = nil
// Carried while the controller ran: two of kinds that ended with it, still open in conditions.
for _, c := range []Condition{
{Key: "core.controller.deaf", Kind: "controller-deaf", Severity: cc.Urgent, Resolver: "self", Count: 1},
{Key: "seat.vault.ace.holder-silent", Kind: "holder-silent", Severity: cc.Warning, Resolver: "self", Count: 1},
} {
if _, err := r.st.Create(ConditionsState, c.Key, c.Stored()); err != nil {
t.Fatal(err)
}
}
n, err := r.k.EndCarried()
if err != nil || n != 2 || r.changes() != "cleared core.controller.deaf, cleared seat.vault.ace.holder-silent" {
t.Fatalf("EndCarried cleared %d (%v)", n, err)
}
if _, open := r.open(key); !open {
t.Error("a condition a finding holds was cleared")
}
if n, err := r.k.EndCarried(); err != nil || n != 0 || r.changes() != "" {
t.Errorf("a second EndCarried cleared %d (%v)", n, err)
}
}
// ADR 0564 (issue 1952 C5; the lead 2026-10-29): every kind the controller raised has its home in the table, and the
// kinds that ended with it are marked carried, which EndCarried clears once (issue 1126). compare, take-over and
// hand-back went with the controller (ADR 0590): there is no other side to compare and nothing to hand back to.
func TestEveryControllerKindHasItsHome(t *testing.T) {
// The lead's table of 2026-10-29: each kind has its home, lease-split and image-missing do not end, and the 22
// that end are carried; with the 11 kinds of the probes the operator dropped on 2026-10-30, 33.
carried := 0
for _, h := range Homes {
if h.Carried {
carried++
}
}
for kind, observer := range map[string]string{"needs-operator": watchman, "machine-network": watchman, "lease-split": selfCheck,
"data-shrank": selfCheck, "retire-waiting": provisioner, "asks-undelivered": messenger,
"image-missing": artifactStore, "build-failed": builder, "floor-silent": builder, "old-form-held": builder,
"tag-removal-not-done": builder, "seat-definition-unparsed": indexer, "generated-types-out-of-step": indexer,
"seat-defined-twice": indexer, "accepted-unsaid": indexer, "manifest-held": indexer, "packages-token-refused": gitea,
"publish-refused": builder, "publish-unanswered": builder, "seat-outcome-unread": builder} {
if h, ok := Homes[kind]; !ok || h.Observer != observer || h.Carried {
t.Errorf("%s's home is %+v", kind, h)
}
}
for _, kind := range []string{"resolver-wrong", "holder-silent", "archives-missing", "archives-unheld", "binding-kept",
"binding-moved", "binding-moving", "grant-composes-nothing", "root-not-free", "cleanup-waiting", "data-held-twice"} {
if h := Homes[kind]; !h.Carried || h.Observer != "" || h.Why != Dropped {
t.Errorf("the dropped probe's kind %s is %+v", kind, h)
}
}
// And the controller's own bus upgrade, carried (the operator, 2026-10-31): 34; and the bus-consumer probe's three
// kinds, ended with the controller (issue 1126): 37.
for _, kind := range []string{"consumer-wrong", "consumer-lost", "consumer-behind"} {
if h := Homes[kind]; !h.Carried || h.Observer != "" || h.Why != EndedD6 {
t.Errorf("the ended probe's kind %s is %+v", kind, h)
}
}
// The readers' unanswered first view and the owners' views too large to say are every reader's and owner's own.
for _, kind := range []string{"first-view-unanswered", "view-too-large", "view-unsaid", "family-not-heard"} {
if h := Homes[kind]; h.Observer != "" || h.Carried || h.Controller {
t.Errorf("%s's home is %+v", kind, h)
}
}
// 19 of ADR 0564, the 63 of the table, plan-failed and stalled, mesh-config's four (design 32), the readers' unanswered
// first view, the owners' view too large, the builder's four, the indexer's four and the package seats' token refused.
// And the builder's three publishing kinds and the delivery parts' family not heard: 103.
// And the owners' changes not said, view-unsaid: 104. And the indexer's mesh-wide hold past its bound, manifest-held
// (ADR 0632 §2): 105. (The bus module's own conditions, nats.status-unwritten among them, are said to the keeper
// directly, never through findings, so they have no home here.)
if len(Homes) != 105 || carried != 37 {
t.Errorf("the table holds %d kinds, %d carried", len(Homes), carried)
}
}
// ADR 0634 §1, §4 (issue 1966): the keeper reads a condition written before the seat named its layout (the controller's
// body: `silenced` null, `actions` empty, the times with an offset) and writes it back in the seat's layout.
func TestTheKeeperReadsTheOldLayoutAndWritesTheSeatsLayout(t *testing.T) {
r := newRig()
key, raw := finding(t, watchman, cc.ScopeMachine, "g14", "silent", "S1", cc.Warning, r.now)
old := `{"key":"` + key + `","kind":"silent","subject":{"scope":"machine","id":"g14"},"severity":"warning",` +
`"summary":"old","headline":"","explanation":"","resolved":"","needs":"","actions":[],"evidence":[],"source":"S1",` +
`"raised":"2026-10-29T13:00:00+01:00","last-observed":"2026-10-29T13:00:00+01:00","observations":1,"count":1,` +
`"resolver":"self","silenced":null,"epoch":0}`
if _, err := r.st.Create(ConditionsState, key, json.RawMessage(old)); err != nil {
t.Fatal(err)
}
if err := r.k.Finding(watchman, key, raw, false); err != nil {
t.Fatal(err)
}
c, ok := r.open(key)
if !ok || c.Summary != "silent of g14" || !c.Raised.Equal(r.now) || c.Count != 1 {
t.Fatalf("the old condition, settled: %+v (open %v)", c, ok)
}
if _, err := r.k.Silence(key, time.Hour, "tester", "known"); err != nil {
t.Fatal(err)
}
if c, _ := r.open(key); c.Silenced == nil || !c.Silenced.Until.Equal(r.now.Add(time.Hour)) {
t.Errorf("the silence is not kept in the seat's layout: %+v", c.Silenced)
}
last := r.said[len(r.said)-1].Said()
if last.Change != cc.ChangeSilenced || last.Silenced == nil || last.Subject == nil || last.Subject.ID != "g14" || last.Show == "" {
t.Errorf("the event the seat hears: %+v", last)
}
}
// Design 47 G §384: the keeper sets its view of an observer on a node to the set it states: what is new is raised, a
// pending key is kept unchanged, every other condition of the observer on that node is cleared with its reason, a pending key
// nothing named within its bound is cleared, and for an observer held once the holder's set replaces every node's.
func TestTheKeeperSetsItsViewFromWhatAnObserverStates(t *testing.T) {
r := newRig()
holder := "ace"
r.k.Holder = func(o string) (string, bool) { return holder, o == selfCheck }
f := func(observer, id string) seat.Finding {
g := eventFinding(observer, r.now)
g.ID, g.Subject.ID = id, id
return g
}
stated := func(observer, node, asked string, at time.Time, pend []seat.FindingsStatedPendingItem, fs ...seat.Finding) error {
return r.k.FindingsStated(observer, node, mustJSON(t, seat.FindingsStated{At: at, Asked: asked, Findings: fs, Pending: pend}))
}
why := func() string {
defer func() { r.said = nil }()
if len(r.said) == 0 || r.said[len(r.said)-1].Change != cc.ChangeCleared {
return ""
}
return r.said[len(r.said)-1].Why
}
t0 := r.now
var asked []map[string]any
var acts []string
r.k.Publish = func(_ string, b any) error { asked = append(asked, b.(map[string]any)); return nil }
r.k.Record = func(what, why, verb string) error { acts = append(acts, verb+": "+what); return nil }
unstated := cc.Key(cc.ScopeModule, watchman+".ace", KindFindingsUnstated)
var ask string
steps := []struct {
name string
do func() error
open []string // the keys open after
why string
}{
{"a start states two findings: both raised", func() error {
return stated(watchman, "ace", "", t0, nil, f(watchman, "a"), f(watchman, "b"))
}, []string{"machine.a.silent", "machine.b.silent"}, ""},
{"a later set without b clears it, with its reason", func() error {
return stated(watchman, "ace", "ask-1", t0.Add(time.Minute), nil, f(watchman, "a"))
}, []string{"machine.a.silent"}, "not stated when asked"},
{"a restart that states nothing clears the rest", func() error {
return stated(watchman, "ace", "", t0.Add(2*time.Minute), nil)
}, nil, "not stated at " + watchman + "'s start"},
{"a pending key's condition stays open and unsaid", func() error {
_ = r.k.FindingWritten(watchman, "ace", mustJSON(t, f(watchman, "c")))
r.said = nil
return stated(watchman, "ace", "", t0.Add(3*time.Minute), []seat.FindingsStatedPendingItem{{Key: "machine.c.silent", Since: t0, Bound: "5m"}})
}, []string{"machine.c.silent"}, ""},
{"a pending key nothing named within its bound is cleared", func() error {
return r.k.PendingPassed(pendingKey(watchman, "ace", "machine.c.silent"))
}, nil, notJudged},
{"a set of another node clears nothing of ace", func() error {
_ = r.k.FindingWritten(watchman, "ace", mustJSON(t, f(watchman, "d")))
r.said = nil
return stated(watchman, "g14", "", t0.Add(4*time.Minute), nil)
}, []string{"machine.d.silent"}, ""},
{"an older set is left out", func() error {
return stated(watchman, "ace", "", t0.Add(-time.Hour), nil)
}, []string{"machine.d.silent"}, ""},
{"a held-once observer's old holder raises nothing after the new holder's set", func() error {
if err := stated(selfCheck, "ace", "", t0.Add(5*time.Minute), nil, f(selfCheck, "e")); err != nil {
return err
}
holder = "g14"
if err := stated(selfCheck, "g14", "", t0.Add(6*time.Minute), nil, f(selfCheck, "g")); err != nil {
return err
}
return r.k.FindingWritten(selfCheck, "ace", mustJSON(t, func() seat.Finding { x := f(selfCheck, "h"); x.At = t0.Add(7 * time.Minute); return x }()))
}, []string{"machine.d.silent", "machine.g.silent"}, "not stated at " + selfCheck + "'s start"},
// Design 47 G §390: the ask, findings-unstated and the hand act.
{"an ask for every observer waits five minutes on each", func() (err error) {
ask, err = r.k.Ask("jochen", "")
return err
}, []string{"machine.d.silent", "machine.g.silent"}, ""},
{"an observer silent past the ask is findings-unstated", func() error {
return r.k.AskPassed(askedKey(watchman, "ace", ask))
}, []string{"machine.d.silent", "machine.g.silent", unstated}, ""},
{"its next findings-stated clears it", func() error {
return stated(watchman, "ace", ask, t0.Add(10*time.Minute), nil, f(watchman, "d"))
}, []string{"machine.d.silent", "machine.g.silent"}, "its finding went"},
{"a machine away is not named, and a machine back is asked again", func() error {
if _, err := r.k.Ask("jochen", ""); err != nil {
return err
}
last := asked[len(asked)-1]["ask"].(string)
away := mustJSON(t, map[string]any{"machine": "g14", "at": r.now})
if err := r.k.FindingEvent(stdio.Envelope{Key: "mesh-watchdogs.machine-away", Node: "ace", Body: away}); err != nil {
return err
}
if err := r.k.AskPassed(askedKey(selfCheck, "g14", last)); err != nil {
return err
}
before := len(asked)
if err := r.k.FindingEvent(stdio.Envelope{Key: "mesh-watchdogs.machine-back", Node: "ace", Body: away}); err != nil {
return err
}
if len(asked) != before+2 || asked[before]["module"] != watchman || asked[before+1]["module"] != selfCheck {
t.Errorf("a machine back was asked %v", asked[before:])
}
return nil
}, []string{"machine.d.silent", "machine.g.silent"}, ""},
{"resync names one module: its hand act and its ask", func() error {
if _, err := r.k.ResyncVerb("person.jochen", selfCheck); err != nil {
return err
}
if len(acts) != 1 || asked[len(asked)-1]["module"] != selfCheck || len(r.k.FindingsViews()) == 0 {
t.Errorf("hand acts %v, last ask %v", acts, asked[len(asked)-1])
}
return nil
}, []string{"machine.d.silent", "machine.g.silent"}, ""},
// Design 47 G §365: the sources are the catalog's, a module that left the catalog or the node loses its conditions.
{"a module not declaring the finding events is no source, one the catalog half declares is heard with no code change", func() error {
if err := r.k.FindingWritten("fixture", "ace", mustJSON(t, f("fixture", "z"))); err != nil {
return err
}
if _, ok := r.open("machine.z.silent"); ok {
t.Errorf("a module in no catalog half raised a condition")
}
r.half("fixture", "node")
return r.k.FindingWritten("fixture", "ace", mustJSON(t, f("fixture", "z")))
}, []string{"machine.d.silent", "machine.g.silent", "machine.z.silent"}, ""},
{"an observer claiming only mesh seats is held where the assignments place it, with no fixed name", func() error {
r.k.holders["g14.mesh-self-check"] = selfCheck
if node, once := r.k.catalogHolder(selfCheck); node != "g14" || !once {
t.Errorf("the holder of %s is %q, %v", selfCheck, node, once)
}
if _, once := r.k.catalogHolder("fixture"); once {
t.Errorf("a node seat's module is held once")
}
return nil
}, []string{"machine.d.silent", "machine.g.silent", "machine.z.silent"}, ""},
{"stopped but still placed, or placed by no revision held, clears nothing", func() error {
body := mustJSON(t, nodeengine.Running{Module: "fixture", Node: "ace", Stopped: true, At: r.now})
if err := r.k.EngineRunning("ace", body); err != nil {
return err
}
return r.k.AssignmentHeld(meshwants.AssignmentRevision{Entries: []meshwants.NodeEntryRef{{Module: "fixture", Node: "ace"}}})
}, []string{"machine.d.silent", "machine.g.silent", "machine.z.silent"}, ""},
{"stopped and no longer placed is cleared as left the node", func() error {
return r.k.AssignmentHeld(meshwants.AssignmentRevision{Revision: 2})
}, []string{"machine.d.silent", "machine.g.silent"}, whyLeftNode},
{"an assignment revision alone clears nothing, and a module that left the catalog is cleared", func() error {
if err := r.k.FindingWritten("fixture", "ace", mustJSON(t, f("fixture", "z"))); err != nil {
return err
}
r.said = nil
if err := r.k.AssignmentHeld(meshwants.AssignmentRevision{Revision: 3}); err != nil {
return err
}
if _, ok := r.open("machine.z.silent"); !ok {
t.Errorf("an assignment revision alone cleared a module's condition")
}
return r.k.CatalogHeld("main", map[string]catseat.CatalogEntry{watchmanModule: {}, selfCheckModule: {}})
}, []string{"machine.d.silent", "machine.g.silent"}, whyLeftCatalog},
// Design 47 G §388: a restarted keeper hears its kept sources before its first view of the catalog half.
{"a restarted keeper with no view of the catalog half hears its kept sources, and the view takes one away", func() error {
if _, ok := r.st.Get2(ViewsState, viewKey(watchman, "ace")); !ok {
t.Errorf("the source %s on ace was not kept in %s", watchman, ViewsState)
}
k2 := &Keeper{Store: r.st, Now: func() time.Time { return r.now }, Say: func(e Event) error { r.said = append(r.said, e); return nil }}
k2.RestoreViews()
if err := k2.FindingWritten(watchman, "ace", mustJSON(t, f(watchman, "k"))); err != nil {
return err
}
if _, ok := r.open("machine.k.silent"); !ok {
t.Errorf("a kept source's finding was not raised with no view of the catalog half")
}
if err := k2.FindingWritten("fixture", "ace", mustJSON(t, f("fixture", "q"))); err != nil {
return err
}
if _, ok := r.open("machine.q.silent"); ok {
t.Errorf("a module that was never a source raised a condition")
}
return k2.CatalogHeld("main", map[string]catseat.CatalogEntry{selfCheckModule: {}})
}, []string{"machine.d.silent", "machine.g.silent"}, whyLeftCatalog},
// Design 47 G §532: a module only a second watched branch lists is a source, and the branch dropped takes it away.
{"a module listed only by a second branch is a source", func() error {
if err := r.k.CatalogHeld("feature", map[string]catseat.CatalogEntry{"fixture": {Streams: []string{EventFindingWritten}}}); err != nil {
return err
}
return r.k.FindingWritten("fixture", "ace", mustJSON(t, f("fixture", "z")))
}, []string{"machine.d.silent", "machine.g.silent", "machine.z.silent"}, ""},
{"the second branch dropped takes its source away", func() error {
return r.k.CatalogDropped("feature")
}, []string{"machine.d.silent", "machine.g.silent"}, whyLeftCatalog},
// Design 47 G §663: a part whose finding-events entry the bus module made is a source, asked once.
{"a created finding-events entry makes its part a source, asked once", func() error {
before := len(asked)
created := mustJSON(t, map[string]any{"module": "newpart", "template": FindingEventsTemplate, "bus-name": "newpart_finding-events", "local-name": "finding-events"})
other := mustJSON(t, map[string]any{"module": "otherpart", "template": "state", "bus-name": "otherpart_x", "local-name": "x"})
for _, body := range [][]byte{created, other, created} {
if err := r.k.FindingEvent(stdio.Envelope{Key: "mesh-broker.created", Body: body}); err != nil {
return err
}
}
if len(asked) != before+1 || asked[before]["module"] != "newpart" {
t.Errorf("asked %v, want newpart once", asked[before:])
}
if err := r.k.FindingWritten("otherpart", "ace", mustJSON(t, f("otherpart", "o"))); err != nil {
return err
}
return r.k.FindingWritten("newpart", "ace", mustJSON(t, f("newpart", "w")))
}, []string{"machine.d.silent", "machine.g.silent", "machine.w.silent"}, ""},
// Design 47 G §663, §690: a part whose findings move keeps its condition, is asked once on the new stream, and its
// old source is dropped once its entry declares no finding stream.
{"a part that moves its finding stream is asked once and keeps its condition", func() error {
entry := func(stream string) map[string]catseat.CatalogEntry {
e := catseat.CatalogEntry{Module: "movedpart", Manifest: map[string]any{}}
if stream != "" {
e.Manifest["streams"] = []any{map[string]any{"name": stream, "events": []any{"finding-written.*"}}}
}
return map[string]catseat.CatalogEntry{"movedpart": e}
}
if err := r.k.CatalogHeld("moves", entry("old-findings")); err != nil {
return err
}
if err := r.k.FindingWritten("movedpart", "ace", mustJSON(t, f("movedpart", "m"))); err != nil {
return err
}
before := len(asked)
if err := r.k.CatalogHeld("moves", entry("old-findings")); err != nil {
return err
}
if len(asked) != before {
t.Errorf("the same entry asked %v", asked[before:])
}
if err := r.k.CatalogHeld("moves", entry("new-findings")); err != nil {
return err
}
if len(asked) != before+1 || asked[before]["module"] != "movedpart" {
t.Errorf("asked %v, want movedpart once on the new stream", asked[before:])
}
// Its entry declares no finding stream now (the findings moved to a template): the old source is dropped.
if err := r.k.CatalogHeld("moves", entry("")); err != nil {
return err
}
if err := r.k.FindingWritten("movedpart", "ace", mustJSON(t, f("movedpart", "late"))); err != nil {
return err
}
if _, ok := r.open("machine.late.silent"); ok {
t.Errorf("the dropped source raised a condition")
}
return nil
}, []string{"machine.d.silent", "machine.g.silent", "machine.m.silent"}, ""}, // newpart, listed by no revision, leaves with the first view
}
for _, s := range steps {
if err := s.do(); err != nil {
t.Fatalf("%s: %v", s.name, err)
}
var open []string
for _, c := range func() []Condition { c, _ := r.k.Open(); return c }() {
open = append(open, c.Key)
}
sort.Strings(open)
if !slices.Equal(open, s.open) {
t.Errorf("%s: open %v, want %v", s.name, open, s.open)
}
if got := why(); got != s.why {
t.Errorf("%s: cleared because %q, want %q", s.name, got, s.why)
}
}
}