Files
mesh-delivery/modules/mesh-grants/internal/buscompose/twins_test.go
T
jschoubben 4ca6e478a6 mesh-grants: the membership carries the tool runner's twin spec; no twin composed
The grants part stays the one composer of filters while the tool runner makes its
own `_tools` twin: each heard entry, EVENTS included, carries the twin's filters
(the again filter named for it), ack wait, max deliver, max ack pending and
deliver policy. The pass composes no `_tools` consumer any longer, so it makes no
twin nothing reads; the reader grants add the create and delete of the module's
own twin by name. A membership of a module no longer assigned on its node is
withdrawn by a whole pass, so the runner there removes that module's twins. The
part's own hearing loop leaves EVENTS to the hub, now that the membership names it.

Builds: design 47 H2 H-A-472 (fifth, sixth, seventh, eighth and ninth addenda)
2026-11-22 19:30:19 +01:00

78 lines
2.9 KiB
Go

package buscompose
import (
"slices"
"testing"
"git.novox.be/novox/mesh-sdk/go/membership"
)
// Design 47 H2, H-A-472 fifth to seventh addenda: the grants part composes no tool runner's twin; it writes the twin's whole
// spec in the membership's heard entries, EVENTS like any stream, and grants the module's reader the create and delete of
// its own twin by name.
func TestTheRunnersTwinIsTheMembershipsSpec(t *testing.T) {
c := Consumer{Name: "ace_mod", Stream: "EVENTS", Filters: []string{"mesh.mod.x.event.y", AgainFilter("ace_mod")}, AckWaitSeconds: 30,
MaxDeliver: 5, FromNow: true}
for _, row := range []struct {
name string
hasProgram bool
want []string
}{
{"a module with a program of its own: the program's alone", true, []string{"ace_mod"}},
{"a module the tool runner carries: none", false, nil},
} {
var got []string
for _, tc := range twinsOf(c, row.hasProgram) {
got = append(got, tc.Name)
}
if !slices.Equal(got, row.want) {
t.Errorf("%s: consumers composed = %v, want %v", row.name, got, row.want)
}
}
d := Declared{Module: "mod", Consumes: []string{"x.happened"},
Hears: []Heard{{Stream: "feed_one", Emitter: "feed", Filters: []string{"mesh.stream.feed.event.a"}},
{Stream: "kept_one", Emitter: "keeper", Filters: []string{"mesh.stream.keeper.event.k"}, LastPerSubject: true}}}
hears := HeardIssuedFor("ace", d)
byStream := map[string]membership.HeardIssued{}
for _, h := range hears {
byStream[h.Stream] = h
if !h.HasSpec() || h.Consumer != "ace_mod" || h.AckWaitSeconds != 30 || h.MaxDeliver != 5 {
t.Errorf("heard entry %+v: want the twin's spec under the module's consumer ace_mod", h)
}
}
for _, row := range []struct {
stream, policy string
filters []string
}{
{"EVENTS", membership.DeliverFromNow, []string{"mesh.mod.x.event.happened", AgainFilter("ace_mod_tools")}},
{"feed_one", membership.DeliverFromNow, []string{"mesh.stream.feed.event.a"}},
{"kept_one", membership.DeliverLastPerSubject, []string{"mesh.stream.keeper.event.k"}},
} {
h, ok := byStream[row.stream]
if !ok {
t.Errorf("no heard entry for %s in %+v", row.stream, hears)
continue
}
if h.DeliverPolicy != row.policy || !slices.Equal(h.Filters, row.filters) {
t.Errorf("%s: policy %q filters %v, want %q %v", row.stream, h.DeliverPolicy, h.Filters, row.policy, row.filters)
}
}
grants := readerGrants("EVENTS", "ace_mod")
for _, row := range []struct {
subject string
want bool
}{
{"$JS.API.CONSUMER.CREATE.EVENTS.ace_mod_tools", true},
{"$JS.API.CONSUMER.DELETE.EVENTS.ace_mod_tools", true},
{"$JS.API.CONSUMER.INFO.EVENTS.ace_mod_tools", true},
{"$JS.API.CONSUMER.CREATE.EVENTS.ace_mod", false},
{"$JS.API.CONSUMER.DELETE.EVENTS.ace_mod", false},
} {
if slices.Contains(grants, row.subject) != row.want {
t.Errorf("reader grants %s: %v, want %v (%v)", row.subject, !row.want, row.want, grants)
}
}
}