Files
mesh-catalog/modules/nats/cmd/nats-tools/keptools_test.go
T
jschoubben c2e017a4c1 The bus module takes and restores a restore point of a module's kept data, beside the live data (milestone G, W14.2)
Seat verbs snapshot-kept, restore-kept, discard-aside and aside, handed to the keeper; state kept; tool nats_kept.

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 12:05:54 +01:00

78 lines
3.0 KiB
Go

package main
import (
"encoding/json"
"strings"
"testing"
"time"
stdio "git.novox.be/novox/mesh-sdk/go"
"nats-tools/internal/keeper"
)
// The seat mesh-broker's verbs for a module's kept data hand their request to the keeper as the proved caller, wait for
// its end on the watch of the requests, and say what a restore set aside (design 47, "A module's kept data on the bus").
func TestTheKeptDataVerbsAreHandedToTheKeeperAndReadBack(t *testing.T) {
held := map[string]keeper.Request{}
rs := requestStore{
Put: func(key string, r keeper.Request) error { held[key] = r; return nil },
Get: func(key string) (*keeper.Request, error) {
r, ok := held[key]
if !ok {
return nil, nil
}
return &r, nil
},
Now: func() time.Time { return time.Date(2026, 11, 9, 20, 0, 0, 0, time.UTC) },
// The keeper's end, as its watch hands it.
Ended: func(id string, within time.Duration) *keeper.Request {
r := held[id]
r.Done, r.Outcome, r.Dir = time.Unix(1, 0), "done", "/keeper/points/idx/modules/f0-abc"
held[id] = r
return &r
},
}
aside := []keeper.Aside{{ID: "idx.modules.1", Module: "idx", Local: "modules", Bytes: 12}}
tools := map[string]stdio.Tool{}
for _, tl := range keptTools(rs, func() ([]keeper.Point, []keeper.Aside, error) { return nil, aside, nil }) {
tools[tl.Name] = tl
}
for _, verb := range KeptVerbs {
if _, ok := tools["mesh-broker."+verb]; !ok {
t.Errorf("the verb mesh-broker.%s is not served", verb)
}
}
out, err := tools["mesh-broker.snapshot-kept"].Run(map[string]any{"module": "idx", "local-name": "modules", "form": float64(1),
"fingerprint": "abc", "catch-up": "indexer_take_in", "why": "before form 1", "caller": "person.mallory"})
if err != nil {
t.Fatal(err)
}
if r := out.(*keeper.Request); r.Verb != keeper.VerbSnapshotKept || r.Form != 1 || r.Fingerprint != "abc" || r.CatchUp != "indexer_take_in" ||
r.Caller != stdio.Unproven || r.Dir == "" {
t.Errorf("answered %+v: the request for form 1 before abc, for the caller the runtime proved, with the directory the keeper answered", r)
}
for name, args := range map[string]map[string]any{
"mesh-broker.snapshot-kept": {"module": "idx", "local-name": "modules", "fingerprint": "abc"},
"mesh-broker.restore-kept": {"module": "idx", "local-name": "modules", "fingerprint": "abc", "why": "revert"},
"mesh-broker.discard-aside": {"why": "x"},
} {
if _, err := tools[name].Run(args); err == nil {
t.Errorf("%s with %v was handed to the keeper", name, args)
}
}
if out, err := tools["mesh-broker.restore-kept"].Run(map[string]any{"module": "idx", "local-name": "modules", "fingerprint": "abc",
"from": "/restored/f0-abc", "why": "revert"}); err != nil || out.(*keeper.Request).From != "/restored/f0-abc" {
t.Errorf("restore answered %+v (%v)", out, err)
}
read, err := tools["mesh-broker.aside"].Run(nil)
if err != nil || !strings.Contains(strings.ReplaceAll(strings.TrimSpace(toJSON(read)), " ", ""), `"id":"idx.modules.1"`) {
t.Errorf("aside answered %v (%v)", read, err)
}
}
func toJSON(v any) string {
b, _ := json.Marshal(v)
return string(b)
}