Files
mesh-sdk/go/process/call_test.go
T
jschoubben 3f8f35a87a S14 kept chunked call: CallChunksKept and CallFileKept pace the ask and carry its id on every chunk
The assigner's compose-candidate ask is chunked, and the one-shot CallFile ended at the
first no responders; the kept call's form now makes the chunked call too.

Builds: design 47 H2 §775, H-A-405 (1), H-A-351
2026-11-22 00:55:25 +01:00

296 lines
14 KiB
Go

package process
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"git.novox.be/novox/mesh-sdk/go/seatdef"
"strings"
"testing"
"time"
"github.com/nats-io/nats.go"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/approval"
"git.novox.be/novox/mesh-sdk/go/buscalls"
"git.novox.be/novox/mesh-sdk/go/busretry"
)
// 1191/AC3: given a module tool key <module>.<tool>, when called, then the subject is
// mesh.mod.<module>.call.<tool>.<token> with the same fallback (mesh.mod.<module>.tool.<tool>).
func TestCallSubjectsForAModuleTool(t *testing.T) {
called, fallback, err := callSubjects("mesh-mail.send", "novox~mesh-mail~send")
if err != nil {
t.Fatal(err)
}
if called != "mesh.mod.mesh-mail.call.send.novox~mesh-mail~send" {
t.Errorf("called: %q", called)
}
if fallback != "mesh.mod.mesh-mail.tool.send" {
t.Errorf("fallback: %q", fallback)
}
}
// 1191/AC1's subject (the seat form): seat:<seat>.<verb> calls mesh.seat.<seat>.call.<verb>.<token>,
// falling back to mesh.seat.<seat>.tool.<verb>.
func TestCallSubjectsForASeatVerb(t *testing.T) {
called, fallback, err := callSubjects("seat:operator-channel.tell", "novox~mesh-mail~send")
if err != nil {
t.Fatal(err)
}
if called != "mesh.seat.operator-channel.call.tell.novox~mesh-mail~send" {
t.Errorf("called: %q", called)
}
if fallback != "mesh.seat.operator-channel.tool.tell" {
t.Errorf("fallback: %q", fallback)
}
}
func TestCallSubjectsRefusesAKeyNamingNeither(t *testing.T) {
for _, key := range []string{"", "nodot", "seat:", "seat:noverb", "."} {
if _, _, err := callSubjects(key, "token"); err == nil {
t.Errorf("callSubjects(%q) was accepted", key)
}
}
}
// 1470/AC1: key mesh-build-go.fetch, node ace, caller ace.mesh-delivery-builder.
func TestCallSubjectsOnANodeForAModuleTool(t *testing.T) {
c := &Conn{account: Account{User: "ace.mesh-delivery-builder"}}
called, fallback, err := callSubjectsOn("mesh-build-go.fetch", "ace", c.callerToken())
if err != nil {
t.Fatal(err)
}
if called != "mesh.mod.mesh-build-go.call.fetch.ace.ace~mesh-delivery-builder" {
t.Errorf("called: %q", called)
}
if fallback != "mesh.mod.mesh-build-go.tool.fetch.ace" {
t.Errorf("fallback: %q", fallback)
}
}
// 1470/AC2: seat:node-files.read on g14.
func TestCallSubjectsOnANodeForASeatVerb(t *testing.T) {
called, fallback, err := callSubjectsOn("seat:node-files.read", "g14", "tok")
if err != nil {
t.Fatal(err)
}
if called != "mesh.seat.node-files.call.read.g14.tok" || fallback != "mesh.seat.node-files.tool.read.g14" {
t.Errorf("called %q, fallback %q", called, fallback)
}
// Issue 774 notes 84-87: the machine named in the key after '@' is the same subject, never a token holding '@'.
called, fallback, err = callSubjects("seat:node-files.read@g14", "tok")
if err != nil || called != "mesh.seat.node-files.call.read.g14.tok" || fallback != "mesh.seat.node-files.tool.read.g14" {
t.Errorf("@g14: called %q, fallback %q, %v", called, fallback, err)
}
if _, _, err := callSubjectsOn("seat:node-files.read@ace", "g14", "tok"); err == nil {
t.Error("a key naming ace was asked of g14")
}
}
// 1470/AC3: a node that is not one token is refused before anything is published (the Conn has no
// connection, so only the name check can answer with this error).
func TestCallOnRefusesANodeThatIsNotOneToken(t *testing.T) {
c := &Conn{}
for _, node := range []string{"", "a.b", "*", ">", "a b", "a\tb", "a@b"} {
if _, err := c.CallOn(node, "mesh-build-go.fetch", nil); err == nil || !strings.Contains(err.Error(), "not a node name") {
t.Errorf("CallOn(%q) = %v, want the refusal of the name", node, err)
}
}
// Issue 774 notes 84-87: Call refuses a malformed key the same way, before anything is published.
for _, key := range []string{"seat:node-service-manager.stop@novox-be.lan", "seat:a.b@x@y", "seat:a.*", "a.b@"} {
if _, err := c.Call(key, nil); err == nil {
t.Errorf("Call(%q) was published", key)
}
}
}
// The review of 2026-10-30 (issue 774): a callee's own answer is delivered, whatever its words, and is never asked
// again during a blackout; only the transport's no responders is.
func TestACallIsAskedAgainOnlyWhenItReachedNoOne(t *testing.T) {
t.Cleanup(busretry.Waits(10*time.Second, time.Millisecond, 2*time.Millisecond))
for _, row := range []struct {
name string
answer []error // per ask; the last repeats
asks int
kept bool // CallKept (novox/hq ADR 0668; issue 1966)
}{
{"a callee answers jetstream not enabled: asked once", []error{answered("jetstream not enabled")}, 1, false},
{"a callee answers no responders in its words: asked once", []error{answered("nats: no responders available for request")}, 1, false},
{"no responders on either subject, then an answer", []error{nats.ErrNoResponders, nats.ErrNoResponders, nil}, 3, false},
{"a timeout may have been delivered: Call asks once", []error{context.DeadlineExceeded, nil}, 1, false},
{"kept: no responders on a bus that answers (a callee restarting), then an answer", []error{nats.ErrNoResponders, nats.ErrNoResponders, nil}, 3, true},
{"kept: a timeout, then an answer", []error{context.DeadlineExceeded, nil}, 2, true},
{"kept: a callee's answer is asked once", []error{answered("nats: timeout")}, 1, true},
} {
asks := 0
request := func(string, []byte) (json.RawMessage, error) {
e := row.answer[min(asks, len(row.answer)-1)]
asks++
return json.RawMessage(`{}`), e
}
var err error
if row.kept {
_, err = callKeptWith(busretry.Kept{Seat: "s", Verb: "v"}, request, "called", "fallback", nil)
} else {
_, err = callWith(request, func() bool { return true }, "called", "fallback", nil)
}
if asks != row.asks {
t.Errorf("%s: asked %d times (%v), want %d", row.name, asks, err, row.asks)
}
}
// Design 47 H2 §776: a holder's answer of the kind away is a transport fault, called again at the pace; a refusal
// of another kind is the caller's answer at once.
for _, row := range []struct {
name string
answers []string
asks int
}{
{"away, then answered", []string{`{"error":"x","error-kind":"away","part":"the forge's store","words":"401"}`, `{"merged":true}`}, 2},
{"refused: answered at once", []string{`{"error":"no","error-kind":"refused"}`}, 1},
} {
asks := 0
request := func(string, []byte) (json.RawMessage, error) {
a := row.answers[min(asks, len(row.answers)-1)]
asks++
return json.RawMessage(a), nil
}
got, err := callKeptWith(busretry.Kept{Seat: "git", Verb: "merge-pull"}, request, "called", "fallback", nil)
if asks != row.asks || err != nil || string(got) != row.answers[len(row.answers)-1] {
t.Errorf("%s: asked %d times, %s, %v", row.name, asks, got, err)
}
}
// Design 47 H2 §775, H-A-351: a kept call carries its ask's id in the header, and a holder in a process reads it with
// AskOf while its handler runs; a call carrying none reads empty.
if m := askMsg("mesh.seat.mesh-builds.call.build", []byte(`{}`), "ask-9"); m.Header.Get(stdio.AskHeader) != "ask-9" {
t.Errorf("the ask's id is not in the header: %v", m.Header)
}
if m := askMsg("s", nil, ""); m.Header.Get(stdio.AskHeader) != "" {
t.Errorf("a call with no ask carries one: %v", m.Header)
}
c := &Conn{}
for _, ask := range []string{"ask-9", ""} {
msg := askMsg("mesh.seat.mesh-builds.tool.build", []byte(`{"x":1}`), ask)
read := "unread"
c.answerCall(msg, "mesh.seat.mesh-builds.tool.build", func(_ string, body json.RawMessage, _ bool) (any, *approval.Held, error) {
read = AskOf(body)
return nil, nil, nil
})
if read != ask || AskOf(json.RawMessage(msg.Data)) != "" {
t.Errorf("the holder read the ask %q, want %q, and none once its handler ended", read, ask)
}
}
// Design 47 H2 §775, H-A-405: a kept chunked call (CallChunksKept, CallFileKept) asks its verb through the kept-call
// form, carries its one ask id on every chunk's acknowledgement, and past its bound ends with the kept call's words.
send := buscalls.SeatChunks{Seat: "node-build-go", Verb: "output", Call: "c1"}
for _, row := range []struct {
name string
answer []error // per ask; the last repeats
bound time.Duration
asks int
chunks int64
unasked bool // ends unanswered past its bound
}{
{"no responders twice, then answered with two chunks", []error{nats.ErrNoResponders, nats.ErrNoResponders, nil}, 0, 3, 2, false},
{"answered at once with three chunks", []error{nil}, 0, 1, 3, false},
{"no responders past its bound", []error{nats.ErrNoResponders}, time.Nanosecond, 2, 0, true},
} {
asks := 0
request := func(string, []byte) (json.RawMessage, error) {
e := row.answer[min(asks, len(row.answer)-1)]
asks++
return json.RawMessage(fmt.Sprintf(`{"chunks":%d}`, row.chunks)), e
}
chunks := make(chan *nats.Msg, row.chunks)
var want bytes.Buffer
for n := int64(1); n <= row.chunks; n++ {
data := []byte(fmt.Sprintf("part %d;", n))
want.Write(data)
body, _ := json.Marshal(buscalls.SeatChunk{N: n, Of: row.chunks, Data: data})
chunks <- &nats.Msg{Subject: send.Chunk(n), Data: body}
}
var acks []string
ack := func(m *nats.Msg) error {
acks = append(acks, m.Subject+" "+m.Header.Get(stdio.AskHeader))
return nil
}
ask := KeptAsk{ID: "ask-7", Bound: row.bound}
asking := func() (json.RawMessage, error) {
return callKeptWith(keptOf("seat:node-build-go.output@ace", ask, nil), request, "called", "fallback", nil)
}
var got bytes.Buffer
_, err := receiveChunks(context.Background(), "seat:node-build-go.output@ace", send, chunks, ask.ID, asking, nil, ack, &got)
var unanswered *busretry.Unanswered
if row.unasked != errors.As(err, &unanswered) || asks != row.asks || (!row.unasked && err != nil) {
t.Errorf("%s: asked %d times (want %d), %v", row.name, asks, row.asks, err)
}
if row.unasked && (unanswered.Ask != "ask-7" || !strings.Contains(err.Error(), "node-build-go.output (ask ask-7) unanswered within its bound")) {
t.Errorf("%s: ended with %v, not the kept call's words", row.name, err)
}
if got.String() != want.String() || int64(len(acks)) != row.chunks {
t.Errorf("%s: received %q and acknowledged %d chunks, want %q and %d", row.name, got.String(), len(acks), want.String(), row.chunks)
}
for n, a := range acks {
if a != send.Ack(int64(n+1))+" ask-7" {
t.Errorf("%s: acknowledgement %d is %q, carrying not the ask's id in its header", row.name, n+1, a)
}
}
}
if _, err := (&Conn{}).CallChunksKept(context.Background(), KeptAsk{}, "seat:node-build-go.output", "c1", nil, nil, &bytes.Buffer{}); err == nil {
t.Error("a kept chunked call naming no ask id was made")
}
}
// A-107: AnswerSeat reads the caller from a call's last token (dots written `~`), "" for anything that is not a
// bus user's name; and it refuses what it cannot serve before subscribing. A-179, A-186: before a handler runs, an
// unproven caller is refused, and every caller while the seat's definition is unread, names no such verb or opens it to
// none; a proven caller of a verb the definition names runs it.
func TestUserOfTokenAndAnswerSeatRefusals(t *testing.T) {
for token, want := range map[string]string{"novox~mesh-delivery-builder": "novox.mesh-delivery-builder", "person~jo": "person.jo",
"plain": "plain", "": "", "a b": "", "a*": "", "~": ""} {
if got := userOfToken(token); got != want {
t.Errorf("userOfToken(%q) = %q, want %q", token, got, want)
}
}
c := &Conn{}
ok := func(string, json.RawMessage) (any, error) { return nil, nil }
defined := func() (seatdef.Definition, bool) {
return seatdef.Definition{Seat: "seat", Verbs: []seatdef.Verb{{Name: "v"}}}, true
}
for name, verbs := range map[string]map[string]SeatHandler{"none": nil, "nil handler": {"v": nil}, "bad verb": {"a b": ok}, "wildcard": {"*": ok}} {
if _, err := c.AnswerSeat("seat", defined, verbs); err == nil {
t.Errorf("AnswerSeat accepted %s", name)
}
}
if _, err := c.AnswerSeat("seat", nil, map[string]SeatHandler{"v": ok}); err == nil {
t.Error("AnswerSeat accepted no definition")
}
ran := 0
run := func(string, json.RawMessage) (any, error) { ran++; return "ran", nil }
for _, row := range []struct {
name, caller string
def func() (seatdef.Definition, bool)
says string
}{
{"an unproven caller", "", defined, ""}, // a read definition admits every caller (the operator, 2026-11-15)
{"the definition unread", "novox.mesh-recovery", func() (seatdef.Definition, bool) { return seatdef.Definition{}, false }, ""},
{"the definition unread, the console's account (A-435)", "novox.node-tools", func() (seatdef.Definition, bool) { return seatdef.Definition{}, false }, "may need approval"},
{"the definition unread, a person (A-435)", "person.jo", func() (seatdef.Definition, bool) { return seatdef.Definition{}, false }, "may need approval"},
{"the definition unread, an unproven caller (A-435)", "", func() (seatdef.Definition, bool) { return seatdef.Definition{}, false }, "may need approval"},
{"a verb the definition does not name", "novox.mesh-recovery", func() (seatdef.Definition, bool) { return seatdef.Definition{Seat: "seat"}, true }, ""},
{"a marked verb with no gate (§677)", "novox.mesh-recovery", func() (seatdef.Definition, bool) {
return seatdef.Definition{Seat: "seat", Verbs: []seatdef.Verb{{Name: "v", NeedsApproval: true}}}, true
}, "through no gate"},
{"a proven caller", "novox.mesh-recovery", defined, ""},
} {
before := ran
_, _, err := c.admitted("seat", "v", row.def, run, nil)(row.caller, nil, false)
if row.says == "" && (err != nil || ran != before+1) || row.says != "" && (err == nil || !strings.Contains(err.Error(), row.says) || ran != before) {
t.Errorf("%s: %v, ran %d times", row.name, err, ran-before)
}
}
}