Files
mesh-delivery/modules/mesh-delivery-watchman/cmd/mesh-delivery-watchman-tools/main_test.go
T

209 lines
7.3 KiB
Go

package main
import (
"encoding/json"
"errors"
"os"
"reflect"
"slices"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshwatchdogs"
stdio "git.novox.be/novox/mesh-sdk/go"
"mesh-delivery-watchman/internal/socket"
)
type fakeState map[string]string
func (f fakeState) Keys() ([]string, error) {
out := []string{}
for k := range f {
out = append(out, k)
}
return out, nil
}
func (f fakeState) Get(key string) (*stdio.StateEntry, error) {
v, ok := f[key]
if !ok {
return nil, nil
}
return &stdio.StateEntry{Key: key, Value: json.RawMessage(v), Revision: 3, At: time.Date(2026, 11, 10, 3, 0, 0, 0, time.UTC)}, nil
}
// 1952/WM1, ADR 0564 §12: every state the manifest keeps is read by a tool, every act the program starts by hand is
// started by one, and every verb the claim serves and every tool the manifest names is served; and each answers.
func TestEveryStateAndActHasATool(t *testing.T) {
raw, err := os.ReadFile("../../module.json")
if err != nil {
t.Fatal(err)
}
var m struct {
Claims []struct {
Name string `json:"name"`
Serves []string `json:"serves"`
} `json:"claims"`
Tools []string `json:"tools"`
State []struct {
Name string `json:"name"`
} `json:"state"`
}
if err := json.Unmarshal(raw, &m); err != nil {
t.Fatal(err)
}
asked := []socket.Ask{}
states := map[string]fakeState{"findings": {"machine.ace.silent": `{"v":1}`, "core.doctor.self-check-silent": `{"v":1}`},
"presence": {"ace": `{"v":1,"online":true}`, "g14": `{"v":1,"online":false}`, "part.mesh-delivery-builder.ace": `{"v":1,"online":true}`}}
tools := Tools(func(name string) state { return states[name] }, func(a socket.Ask) (json.RawMessage, error) {
asked = append(asked, a)
if a.Act != "" {
return nil, errors.New("refused")
}
return json.RawMessage(`{}`), nil
}, func(map[string]any) (json.RawMessage, error) { return json.RawMessage(`{"conditions":[]}`), nil })
served := map[string]stdio.Tool{}
for _, tool := range tools {
served[tool.Name] = tool
}
var names []string
for _, c := range m.Claims {
if c.Name != Seat || !slices.Equal(c.Serves, Verbs) {
t.Errorf("claim %+v, want %s serving %v", c, Seat, Verbs)
}
for _, v := range c.Serves {
names = append(names, Seat+"."+v)
}
}
if !slices.Equal(m.Tools, ModuleTools) {
t.Errorf("tools = %v, want %v", m.Tools, ModuleTools)
}
names = append(names, m.Tools...)
if len(served) != len(names) {
t.Errorf("served %d tools, the manifest names %v", len(served), names)
}
for _, n := range names {
if _, ok := served[n]; !ok {
t.Errorf("%s is named and not served", n)
}
}
read := map[string]bool{}
for _, r := range Reads {
for _, s := range r {
read[s] = true
}
}
for _, s := range m.State {
if !read[s.Name] {
t.Errorf("the state %s is read by no tool", s.Name)
}
}
acts := map[string]bool{}
for _, a := range Acts {
for _, s := range a {
acts[s] = true
}
}
for _, a := range []string{socket.AskDeadLetters, socket.AskResync} {
if !acts[a] {
t.Errorf("the program's act %s is started by no tool", a)
}
}
t.Run("a read answers every key and how many", func(t *testing.T) {
out, err := served[ToolFindings].Run(map[string]any{"prefix": "machine."})
if err != nil {
t.Fatal(err)
}
b, _ := json.Marshal(out)
var got struct {
Holds int `json:"holds"`
Keys []struct {
Key string `json:"key"`
} `json:"keys"`
}
_ = json.Unmarshal(b, &got)
if got.Holds != 2 || len(got.Keys) != 1 || got.Keys[0].Key != "machine.ace.silent" {
t.Fatalf("answer = %s", b)
}
if _, err := served[ToolFindings].Run(map[string]any{"key": "no.such.key"}); err == nil {
t.Fatal("a key not held is answered")
}
})
// Issue 1966, ADR 0596 §2 (review round 3): presence with the seat's machine answers that machine's online and since in
// the seat's declared answer; one the watchman holds no word of is not online; without it, the keys as before.
t.Run("presence answers one machine as the seat declares", func(t *testing.T) {
for machine, want := range map[string]meshwatchdogs.PresenceAnswer{
"ace": {Machine: "ace", Online: true, Since: "2026-11-10T03:00:00Z"},
"g14": {Machine: "g14", Since: "2026-11-10T03:00:00Z"},
"novox": {Machine: "novox"},
} {
out, err := served[Seat+"."+VerbPresence].Run(map[string]any{"machine": machine})
if err != nil {
t.Fatal(err)
}
b, _ := json.Marshal(out)
var got meshwatchdogs.PresenceAnswer
if err := json.Unmarshal(b, &got); err != nil || !reflect.DeepEqual(got, want) {
t.Errorf("presence of %s = %s (%v), want %+v", machine, b, err, want)
}
}
out, _ := served[Seat+"."+VerbPresence].Run(map[string]any{"prefix": "part."})
b, _ := json.Marshal(out)
var whole meshwatchdogs.PresenceAnswer
if err := json.Unmarshal(b, &whole); err != nil || whole.Holds != 3 || len(whole.Keys) != 1 || whole.State != "presence" {
t.Errorf("presence whole = %s (%v)", b, err)
}
})
t.Run("dead-letters and resync ask the program", func(t *testing.T) {
if _, err := served[Seat+"."+VerbDeadLetters].Run(map[string]any{"consumer": "ace_web", "limit": float64(3), "id": float64(7)}); err != nil {
t.Fatal(err)
}
if _, err := served[Seat+"."+VerbDeadLetters].Run(map[string]any{"act": "drop", "why": "x"}); err == nil {
t.Fatal("a refused act answered")
}
if _, err := served[Seat+"."+VerbResync].Run(nil); err != nil {
t.Fatal(err)
}
want := []socket.Ask{{Ask: socket.AskDeadLetters, Consumer: "ace_web", Limit: 3, ID: 7}, {Ask: socket.AskDeadLetters, Act: "drop", Why: "x"}, {Ask: socket.AskResync}}
if !slices.Equal(asked, want) {
t.Fatalf("asked %+v", asked)
}
})
}
// Design 47 G §83, §501, §609 (WM8): watchman_views answers the keeper's view-waits conditions as the parts and views they name,
// oldest first, and lists no other condition.
func TestWatchmanViewsAnswersTheKeepersViewWaits(t *testing.T) {
var got map[string]any
tools := Tools(func(string) state { return fakeState{} }, nil, func(args map[string]any) (json.RawMessage, error) {
got = args
return json.RawMessage(`{"conditions":[
{"key":"module.mesh-catalog.main.view-waits","kind":"view-waits","raised":"2026-11-12T10:05:00Z","summary":"b waits","subject":{"scope":"module","id":"mesh-delivery-builder","machine":"ace"}},
{"key":"module.x.unit-failed","kind":"first-view-unanswered","raised":"2026-11-12T10:00:00Z","subject":{"scope":"module","id":"x"}},
{"key":"module.git.main.view-waits","kind":"view-waits","raised":"2026-11-12T10:01:00Z","summary":"i waits","subject":{"scope":"module","id":"mesh-delivery-indexer","machine":"novox"}}]}`), nil
})
var tool stdio.Tool
for _, x := range tools {
if x.Name == ToolViews {
tool = x
}
}
if tool.Run == nil {
t.Fatalf("%s is not served", ToolViews)
}
out, err := tool.Run(map[string]any{})
if err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(out)
want := `{"count":2,"waiting":[{"module":"mesh-delivery-indexer","node":"novox","view":"git.main","since":"2026-11-12T10:01:00Z","summary":"i waits","key":"module.git.main.view-waits"},` +
`{"module":"mesh-delivery-builder","node":"ace","view":"mesh-catalog.main","since":"2026-11-12T10:05:00Z","summary":"b waits","key":"module.mesh-catalog.main.view-waits"}]}`
if string(raw) != want {
t.Errorf("answered %s\nwant %s", raw, want)
}
if got["scope"] != "module" {
t.Errorf("asked the keeper %v", got)
}
}