package broker import ( "slices" "testing" ) // novox/hq ADR 0259 ยง6: the controller asks the operator through the router's seat as any user of it, under // its own name, hears its own warrants, reads its own record, and calls the verbs a warrant chooses. func TestTheControllerAsksUnderItsOwnNameAndCallsTheVerbsAWarrantChooses(t *testing.T) { records := Records{Nodes: []string{"anchor"}, Assigned: map[string][]Declared{"anchor": { {Module: "messenger", Holds: []Seat{operatorChannel()}}, }}} users, err := Users(records) if err != nil { t.Fatal(err) } got := perms(t, users[0]) for _, s := range []string{ "mesh.seat.operator-channel.accept.ask.mesh-controller", "mesh.seat.operator-channel.accept.cancel.mesh-controller", "$JS.API.DIRECT.GET.KV_messenger_asks.$KV.messenger_asks.mesh-controller.c1", "mesh.seat.mesh-delivery.tool.release", "mesh.seat.mesh-delivery.tool.stop", "mesh.seat.node-service-manager.tool.restart.g14", "mesh.seat.mesh-controller.tool.plans", } { if !allowed(got.Publish, s) { t.Errorf("the controller may not publish %s", s) } } for _, s := range []string{ "mesh.seat.operator-channel.accept.ask.mesh-delivery", "mesh.seat.operator-channel.event.decided.mesh-controller", // (A direct get of another asker's record is not refused here: the controller holds the whole // JetStream API, as the only writer of stream definitions.) "mesh.seat.node-service-manager.tool.stop.g14", } { if allowed(got.Publish, s) { t.Errorf("the controller may publish %s", s) } } if !allowed(got.Subscribe, DecidedSubject) || allowed(got.Subscribe, "mesh.seat.operator-channel.event.decided.mesh-delivery") { t.Error("the controller does not hear exactly its own warrants") } // Its events consumer carries them, so a controller that was away hears what was decided meanwhile. if !slices.Contains(ControllerFollows, DecidedSubject) { t.Error("the controller does not follow its warrants") } // Without a holder of the seat it is granted no ask at all. alone, _ := Users(Records{Nodes: []string{"anchor"}, Assigned: map[string][]Declared{}}) if allowed(perms(t, alone[0]).Publish, "mesh.seat.operator-channel.accept.ask.mesh-controller") { t.Error("asked a seat nobody holds") } }