Files
mesh-catalog/modules/nats/cmd/nats-tools/requests_test.go
T
jschoubben 7c6cf18532 Design 47 H1: a held destroy-level ask says its level; an offer with no channel named says so
The router held the bus module's destroy-level asks with the bare words
"no channel can carry it now", so the asker could not tell why. holdWhy
now names the destroy level and that no channel carries a destroy answer
yet. The bus module's tool says "offered, channel not yet named" for an
open record with no tickets, never "offered on :".

Builds: design 47 H H-A-237 (3), H-A-239 (2)
2026-11-21 11:26:19 +01:00

210 lines
9.7 KiB
Go

package main
import (
"errors"
"runtime"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
occ "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"
stdio "git.novox.be/novox/mesh-sdk/go"
"nats-tools/internal/keeper"
)
// A person's acting tools hand their request to the keeper (design 25 § 12.3; CAT-N8): stamped with the caller the
// tool runner proved, whatever name the arguments give; a create's end read back; a drop raises the operator's ask
// first, binding exactly that act, and is not acted on by the tool.
func TestAPersonsRequestIsHandedToTheKeeperAsTheProvedCaller(t *testing.T) {
held := map[string]keeper.Request{}
var raised []oc.Ask
var acts []meshhandactsclient.Request
rs := requestStore{
Put: func(key string, r keeper.Request) error {
if r.Verb == keeper.VerbCreate {
// The keeper's end, as its watch writes it.
r.Done, r.Outcome = time.Unix(1, 0), "created"
}
held[key] = r
return nil
},
Get: func(key string) (*keeper.Request, error) {
r, ok := held[key]
if !ok {
return nil, nil
}
return &r, nil
},
Keys: func() ([]string, error) {
var out []string
for k := range held {
out = append(out, k)
}
return out, nil
},
Delete: func(key string) error { delete(held, key); return nil },
Spent: func() ([]keeper.Acted, error) {
return []keeper.Acted{{Ask: "bus-old", Request: "r0", Verb: "drop"}}, nil
},
Ask: func(a oc.Ask) error { raised = append(raised, a); return nil },
Now: func() time.Time { return time.Date(2026, 11, 4, 12, 0, 0, 0, time.UTC) },
Record: func(q meshhandactsclient.Request) error { acts = append(acts, q); return nil },
// Design 47 H-A-237: the router held the ask (no channel carries its level), as it held every one live.
Standing: func(id string) (*stdio.StateEntry, error) {
return &stdio.StateEntry{Key: keeper.Module + "." + id, Value: []byte(`{"asker":"nats","state":"held","held":"telegram: Destroy needs a code"}`)}, nil
},
}
tools := map[string]stdio.Tool{}
for _, tl := range requestTools(rs) {
tools[tl.Name] = tl
}
out, err := tools["nats_create"].Run(map[string]any{"module": "m", "local": "log", "why": "a test", "caller": "person.mallory"})
if err != nil {
t.Fatal(err)
}
if r := out.(*keeper.Request); r.Caller != stdio.Unproven || r.Outcome != "created" {
t.Errorf("answered %+v: acted for the caller the runtime proved (here none, %q), never person.mallory", r, stdio.Unproven)
}
out, err = tools["nats_drop"].Run(map[string]any{"module": "m", "local": "old", "why": "no manifest asks for it"})
if err != nil {
t.Fatal(err)
}
answer := out.(map[string]any)
r := held[answer["request"].(string)]
if len(raised) != 1 || r.Ask != raised[0].ID || !r.Done.IsZero() {
t.Fatalf("raised %+v, held %+v: the ask raised, the request waiting for it", raised, r)
}
binds, _ := occ.ActDigest(occ.Act{"verb": "drop", "module": "m", "local": "old"})
// Design 47 H-A-237: the ask is at the approve level, naming the entry, the removal and the snapshot, for the default
// sixty minutes; an ask the router held is said held, with its why, never "the operator is asked".
if o, _ := occ.OptionOf(raised[0], "go"); o.Level != occ.Approve || o.Binds != binds || !strings.Contains(raised[0].Explanation, "snapshot") ||
!strings.Contains(raised[0].Explanation, "remove m_old") || raised[0].Expires != occ.FormatTime(rs.Now().Add(time.Hour)) || !r.AskExpires.Equal(rs.Now().Add(time.Hour)) {
t.Errorf("the ask is %+v: its option approves exactly this drop, after a snapshot, for sixty minutes", raised[0])
}
if st := answer["state"].(string); !strings.Contains(st, "held by the router") || !strings.Contains(st, "Destroy needs a code") || strings.Contains(st, "operator is asked") {
t.Errorf("the tool answered %q for an ask the router held", st)
}
if _, err := tools["nats_drop"].Run(map[string]any{"module": "m", "local": "old"}); err == nil || !strings.Contains(err.Error(), "says why") {
t.Errorf("a drop without why: %v", err)
}
if got, _ := tools["nats_drop"].Run(map[string]any{"request": r.ID}); got.(map[string]any)["request"].(*keeper.Request).ID != r.ID ||
!strings.Contains(got.(map[string]any)["router"].(string), "held by the router") {
t.Errorf("reading the request back answered %+v: the request, and the router's standing of its ask beside it", got)
}
// Review round 1 (tools F3): nats_requests reads what waits, what ended and each warrant spent, and drops a waiting
// request by hand, recorded; an ended one is not dropped.
out, err = tools["nats_requests"].Run(map[string]any{})
if err != nil {
t.Fatal(err)
}
list := out.(map[string]any)
if w, e, spent := list["waiting"].([]keeper.Request), list["ended"].([]keeper.Request), list["warrants-spent"].([]keeper.Acted); len(w) != 1 ||
w[0].ID != r.ID || len(e) != 1 || len(spent) != 1 || spent[0].Ask != "bus-old" {
t.Errorf("nats_requests answered %+v: the drop waiting, the create ended, one warrant spent", list)
}
if _, err := tools["nats_requests"].Run(map[string]any{"drop": r.ID}); err == nil || !strings.Contains(err.Error(), "says why") {
t.Errorf("a drop by hand without why: %v", err)
}
for id, r := range held {
if r.Verb == keeper.VerbCreate {
if _, err := tools["nats_requests"].Run(map[string]any{"drop": id, "why": "x"}); err == nil || !strings.Contains(err.Error(), "only a request that waits") {
t.Errorf("dropping an ended request: %v", err)
}
}
}
if _, err := tools["nats_requests"].Run(map[string]any{"drop": r.ID, "why": "nobody will answer"}); err != nil || len(acts) != 1 ||
!strings.Contains(acts[0].What, "dropped the waiting request "+r.ID) {
t.Errorf("dropped by hand (%v), recorded %+v", err, acts)
}
if _, still := held[r.ID]; still {
t.Error("the dropped request is still held")
}
// The ask first: an ask that cannot be raised hands no request to the keeper.
before := len(held)
rs.Ask = func(oc.Ask) error { return errors.New("no responders") }
tools = map[string]stdio.Tool{}
for _, tl := range requestTools(rs) {
tools[tl.Name] = tl
}
if _, err := tools["nats_recreate"].Run(map[string]any{"module": "m", "local": "log", "why": "drifted"}); err == nil || len(held) != before {
t.Errorf("an ask not raised: %v, %d requests held (was %d)", err, len(held), before)
}
// A request's end is heard on the one watch of the state, handed to the call that waits for it.
var handle func(string, []byte)
watches := 0
e := newEnds(func(h func(string, []byte)) error { watches++; handle = h; return nil })
got := make(chan *keeper.Request, 1)
go func() { got <- e.wait("r1", time.Minute) }()
for {
e.mu.Lock()
ready := len(e.waiting["r1"]) == 1 && handle != nil
e.mu.Unlock()
if ready {
break
}
runtime.Gosched()
}
handle("r1", []byte(`{"id":"r1"}`))
handle("r1", []byte(`{"id":"r1","done":"2026-11-04T12:00:01Z","outcome":"created"}`))
if r := <-got; r == nil || r.Outcome != "created" || watches != 1 {
t.Errorf("the end heard %+v on %d watch(es): created, on one", r, watches)
}
if r := e.wait("r2", time.Millisecond); r != nil || watches != 1 {
t.Errorf("an end not written: %+v on %d watch(es)", r, watches)
}
}
// The ask for a recreate or a drop is at the approve level, for the caller's expires_minutes, 5 to 1430, a value outside
// refused naming its bounds (design 47 H-A-237, H-A-239); the tool's answer is the router's standing as one read finds
// it, "the operator is asked" only for an offered ask.
func TestTheAskLastsTheCallersExpiryAndTheToolSaysTheRoutersStanding(t *testing.T) {
now := time.Now()
r := keeper.Request{ID: "r1", Verb: keeper.VerbDrop, Module: "m", Local: "drafts", Why: "unused", Caller: "person.jo", AskExpires: now.Add(1430 * time.Minute)}
a, err := askFor(r)
if err != nil {
t.Fatal(err)
}
if err := occ.Check(a, now); err != nil || occ.Highest(a) != occ.Approve {
t.Errorf("the ask at %s is refused by the asker client (%v), or not at the approve level", occ.Highest(a), err)
}
for _, c := range []struct {
minutes any
want time.Duration
refused bool
}{{nil, time.Hour, false}, {5.0, 5 * time.Minute, false}, {1430.0, 1430 * time.Minute, false}, {4.0, 0, true}, {1431.0, 0, true}, {"60", 0, true}} {
a := map[string]any{}
if c.minutes != nil {
a["expires_minutes"] = c.minutes
}
got, err := askExpiry(a)
if c.refused != (err != nil) || got != c.want || (c.refused && !strings.Contains(err.Error(), "5 to 1430")) {
t.Errorf("expires_minutes %v: %s, %v", c.minutes, got, err)
}
}
for _, c := range []struct {
record string
err error
want string
}{
{"", nil, "not yet taken in by the router"},
{`{"state":"open","tickets":{"a":{"kind":"telegram"},"b":{"kind":"telegram"},"c":{"kind":"desktop"}}}`, nil, "offered on desktop, telegram: the operator is asked"},
{`{"state":"open"}`, nil, "offered, channel not yet named: the operator is asked"},
{`{"state":"held","held":"no channel carries destroy"}`, nil, "held by the router, not shown to the operator: no channel carries destroy"},
{`{"state":"refused","warrant":{"ask":"bus-r1","asker":"nats","outcome":"refused","words":"its words carry a path"}}`, nil, "refused by the router, so nothing will be removed: its words carry a path"},
{"", errors.New("no responders"), "cannot be read now (no responders)"},
} {
rs := requestStore{Standing: func(string) (*stdio.StateEntry, error) {
if c.record == "" {
return nil, c.err
}
return &stdio.StateEntry{Value: []byte(c.record)}, nil
}}
if got := standingOf(rs, "bus-r1"); !strings.Contains(got, c.want) || (!strings.HasPrefix(c.want, "offered") && strings.Contains(got, "operator is asked")) {
t.Errorf("the record %s (%v) is said %q, not %q", c.record, c.err, got, c.want)
}
}
}