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) } } } // Design 47 H2, H-A-500 (b) and its addenda: every module principal, on an account of its own or carried by a tool runner, // is granted the create, information, pull and acknowledgement of its own takeins durable on the shared events stream. func TestEveryModuleIsGrantedItsTakeInsDurable(t *testing.T) { takeIns := func(node, module string) []string { d := node + "_" + module + TakeInsSuffix return []string{"$JS.API.CONSUMER.CREATE.EVENTS." + d, "$JS.API.CONSUMER.INFO.EVENTS." + d, "$JS.API.CONSUMER.MSG.NEXT.EVENTS." + d, "$JS.ACK.EVENTS." + d + ".>"} } for _, c := range []struct { name string p Principal module string }{ {"a module's own account", Principal{Kind: KindModule, Node: "a", Module: "shop"}, "shop"}, {"a module a tool runner carries", Principal{Kind: KindNodeTools, Node: "a", Module: "node-tools", Carries: []Declared{{Module: "shop"}}}, "shop"}, } { perms, err := PermissionsFor(c.p) if err != nil { t.Fatalf("%s: %v", c.name, err) } for _, g := range takeIns("a", c.module) { if !slices.Contains(perms.Publish, g) { t.Errorf("%s is not granted %s", c.name, g) } } } }