package grants import ( "os" "path/filepath" "reflect" "slices" "strings" "testing" "time" "github.com/nats-io/nats.go/jetstream" sdkbc "git.novox.be/novox/mesh-sdk/go/buscompose" "git.novox.be/novox/mesh-sdk/go/seatdef" "mesh-grants/internal/buscompose" ) type askCreator struct{ made []string } func (c *askCreator) CreateDeclared(cfg jetstream.StreamConfig) error { c.made = append(c.made, cfg.Name+" "+cfg.Subjects[0]) return nil } func (c *askCreator) AddSubjects(string, []string) error { return nil } func (c *askCreator) RaiseMaxBytes(string, int64) (int64, bool, error) { return 0, false, nil } func (c *askCreator) SourceFilter(string, string, string) (string, bool, error) { return "", false, nil } func (c *askCreator) AllowMsgTTL(string, time.Duration) (bool, error) { return false, nil } // A seat whose queues came with G (mesh-builds) gets its work-queue ask stream from the seat list, as the controller made one for // every seat before G; a seat that accepts nothing gets none, and a stream the bus holds is never made again (lead-47, 2026-11-15). // The stream carries the settings the seat's accepted kinds name, derived by mesh-sdk's SeatQueue, and a seat whose settings it // refuses is said (design 47 H2 ยง777; H-A-362). func TestEachSeatThatAcceptsWorkHasItsAskStream(t *testing.T) { defs := map[string]seatdef.Definition{ "mesh-builds": {Seat: "mesh-builds", Accepts: []seatdef.Message{{Name: "step", MaxAge: "24h", MaxMsgs: 10000, DuplicatesWindow: "2m"}}}, "mesh-wants": {Seat: "mesh-wants", Accepts: []seatdef.Message{{Name: "step"}}}, "git": {Seat: "git"}, "two-ways": {Seat: "two-ways", Accepts: []seatdef.Message{{Name: "a", MaxAge: "1h"}, {Name: "b", MaxAge: "2h"}}}, "node-build": {Seat: "node-build", PulledByKind: true, Accepts: []seatdef.Message{{Name: "check"}}}, } asks, workers, refused := buscompose.AskStreamsOf(defs) // Each seat that accepts work and is not pulled by kind has its one shared worker, SeatQueue's (H-A-468); the seat pulled // by kind has its stream and no shared worker. if len(workers) != 2 || workers[0].Name != "SEAT_MESH_BUILDS_worker" || workers[0].Stream != "SEAT_MESH_BUILDS" || !slices.Equal(workers[0].Filters, []string{"mesh.seat.mesh-builds.accept.>"}) || workers[0].MaxDeliver != 5 || workers[0].AckWaitSeconds != 60 || workers[1].Name != "SEAT_MESH_WANTS_worker" { t.Errorf("workers = %+v", workers) } if len(asks) != 3 || asks[2].Name != "SEAT_NODE_BUILD" { t.Fatalf("ask streams = %+v", asks) } asks = asks[:2] if len(asks) != 2 || asks[0].Name != "SEAT_MESH_BUILDS" || asks[0].Retention != jetstream.WorkQueuePolicy || !slices.Equal(asks[0].Subjects, []string{"mesh.seat.mesh-builds.accept.>"}) || asks[0].MaxAge != 24*time.Hour || asks[0].MaxMsgs != 10000 || asks[0].Duplicates != 2*time.Minute || asks[1].MaxAge != 7*24*time.Hour { t.Fatalf("ask streams = %+v", asks) } if len(refused) != 1 || !strings.Contains(refused[0], "two-ways") { t.Errorf("refused = %v, want the seat whose two kinds name two max-ages", refused) } c := &askCreator{} made, notMade := createAskStreams(c, &creations{}, asks, map[string]Limits{"SEAT_MESH_WANTS": {}}, false) if !slices.Equal(made, []string{"SEAT_MESH_BUILDS"}) || len(notMade) != 0 || !slices.Equal(c.made, []string{"SEAT_MESH_BUILDS mesh.seat.mesh-builds.accept.>"}) { t.Errorf("made %v (not %v), created %v: want only the builds seat's, the wants seat's being held", made, notMade, c.made) } } // The worker the grants part creates for the seat mesh-builds is SeatQueue's, setting by setting (H-A-468 second addendum): // the one the builder's whole test makes from SeatQueue, so the two cannot differ. func TestTheSeatsWorkerIsSeatQueues(t *testing.T) { raw, err := os.ReadFile(filepath.Join("..", "..", "..", "mesh-delivery-contracts", "seats", "mesh-builds", "seat.json")) if err != nil { t.Fatal(err) } def, err := seatdef.Parse(raw) if err != nil { t.Fatal(err) } _, w, ok, err := sdkbc.SeatQueue(def.Seat, def.Accepts) if err != nil || !ok { t.Fatalf("SeatQueue: %v %v", ok, err) } _, workers, refused := buscompose.AskStreamsOf(map[string]seatdef.Definition{def.Seat: def}) if len(workers) != 1 || len(refused) != 0 { t.Fatalf("workers %+v, refused %v", workers, refused) } want := jetstream.ConsumerConfig{Durable: w.Name, FilterSubjects: w.Filters, AckPolicy: jetstream.AckExplicitPolicy, AckWait: time.Duration(w.AckWaitSeconds) * time.Second, MaxDeliver: w.MaxDeliver, MaxAckPending: w.MaxAckPending, Description: w.Why} if got := buscompose.ConsumerConfig(workers[0]); workers[0].Stream != w.Stream || !reflect.DeepEqual(got, want) { t.Errorf("the created worker on %s = %+v, want SeatQueue's %+v on %s", workers[0].Stream, got, want, w.Stream) } }