Files
mesh-delivery/modules/mesh-grants/internal/grants/grantskept_test.go
T
jschoubben 380d4781e6 mesh-grants: bring a held kept stream to its definition's source filter
The kept stream of the runner's pipeline-ended was made at G's
definition, sourced by a three-part subject; H1 moved the event to
<branch>.<source>, so the stream took nothing in since 08:13 and the
assigner, which hears a kept event from its kept stream, heard no end.
createKept left every held kept stream as it was. It now brings a held
one's source filter from the events stream to the definition's in place
(a stream update, its messages and other settings kept, never made
again) and says the held and the composed filter in the journal and the
pass; the same holds for pipeline-started and wait-held.

Builds: design 47 H H-A-145 (hq design/47-H1-answers-59 0787704b), §746
2026-11-20 15:42:24 +01:00

112 lines
4.4 KiB
Go

package grants
import (
"strings"
"testing"
"github.com/nats-io/nats.go/jetstream"
"mesh-grants/internal/buscompose"
)
// Design 47 G A-364: the grants part's own account carries the right to create each kept event stream of the seat list, such as
// SEAT_git_branch-moved, composed from the same records as every other account.
func TestTheGrantsAccountMayCreateEachKeptEventStream(t *testing.T) {
const stream, subject = "SEAT_git_branch-moved", "mesh.seat.git.event.branch-moved"
for _, c := range []struct {
name string
kept []buscompose.KeptEvent
may bool
}{
{"a kept event of the seat list", []buscompose.KeptEvent{{Seat: "git", Event: "branch-moved", Stream: stream, Filter: subject}}, true},
{"no kept event", nil, false},
} {
t.Run(c.name, func(t *testing.T) {
users, err := buscompose.Users(buscompose.Records{Nodes: []string{"ace"}, Kept: c.kept,
Assigned: map[string][]buscompose.Declared{"ace": {{Module: buscompose.GrantsModule}}}})
if err != nil {
t.Fatal(err)
}
for _, u := range users {
if u.Kind != buscompose.KindModule || u.Module != buscompose.GrantsModule {
continue
}
perms, err := buscompose.PermissionsFor(u)
if err != nil {
t.Fatal(err)
}
if got := buscompose.MayPublish(perms, "$JS.API.STREAM.CREATE."+stream); got != c.may {
t.Errorf("the grants account may create %s: %v, want %v", stream, got, c.may)
}
}
})
}
}
// sourcing is a Creator holding kept streams by their source filter and message count: SourceFilter updates a held one in
// place, so its messages stay; CreateDeclared makes one the bus lacks.
type sourcing struct {
Creator
filters map[string]string // by stream, its source filter from EVENTS
messages map[string]int
made []string
}
func (s *sourcing) SourceFilter(stream, source, filter string) (string, bool, error) {
from, ok := s.filters[stream]
if !ok || source != buscompose.EventsStream || from == filter {
return from, false, nil
}
s.filters[stream] = filter
return from, true, nil
}
func (s *sourcing) CreateDeclared(cfg jetstream.StreamConfig) error {
s.made = append(s.made, cfg.Name)
s.filters[cfg.Name], s.messages[cfg.Name] = cfg.Sources[0].FilterSubject, 0
return nil
}
// H-A-145 (design 47 H §746): a held kept stream whose source filter the definition changed is brought to the definition's
// filter in place, its messages kept and the change said with both filters; one at the definition's filter is untouched; one
// the bus lacks is made.
func TestAKeptStreamFollowsItsDefinitionsSourceFilter(t *testing.T) {
const ended = "mesh.seat.mesh-pipeline-runner.event.pipeline-ended.*.*"
for _, row := range []struct {
name string
held string // the held stream's source filter; "" for a stream the bus lacks
made bool
moved string
filter string
messages, keptMsgs int
}{
{name: "held at the definition's filter: untouched", held: ended, filter: ended, messages: 604, keptMsgs: 604},
{name: "held at an earlier filter: brought to the definition's, messages kept", held: ended + ".*", filter: ended, messages: 604, keptMsgs: 604,
moved: "SEAT_mesh-pipeline-runner_pipeline-ended: source filter " + ended + ".* brought to " + ended + ", its messages kept"},
{name: "not held: made", made: true, filter: ended},
} {
t.Run(row.name, func(t *testing.T) {
k := buscompose.KeptEvent{Seat: "mesh-pipeline-runner", Event: "pipeline-ended", Stream: "SEAT_mesh-pipeline-runner_pipeline-ended", Filter: ended, MaxAgeDays: 90, MaxMiB: 192}
c := &sourcing{filters: map[string]string{}, messages: map[string]int{}}
held := map[string]Limits{}
if row.held != "" {
c.filters[k.Stream], c.messages[k.Stream] = row.held, row.messages
held[k.Stream] = Limits{}
}
made, moved, notMade := createKept(c, &creations{}, []buscompose.KeptEvent{k}, held, false)
if len(notMade) != 0 {
t.Fatalf("not made: %v", notMade)
}
if (len(made) == 1) != row.made || len(c.made) != len(made) {
t.Errorf("made %v (bus %v), want made %v", made, c.made, row.made)
}
if got := strings.Join(moved, "; "); got != row.moved {
t.Errorf("moved %q, want %q", got, row.moved)
}
if c.filters[k.Stream] != row.filter || c.messages[k.Stream] != row.keptMsgs {
t.Errorf("the stream holds filter %q and %d messages, want %q and %d", c.filters[k.Stream], c.messages[k.Stream], row.filter, row.keptMsgs)
}
})
}
}