78 lines
3.1 KiB
Go
78 lines
3.1 KiB
Go
package grants
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
)
|
|
|
|
// A consume names a seat's event by its bare name, and the definition's local name carries the subject's parts as `*`
|
|
// (`catalog-revision.*`): it is the seat's event, on the seat's subject; one the seat does not say is refused, never
|
|
// turned into a module's event (design 47 G A-409).
|
|
func TestConsumedSeatEvents(t *testing.T) {
|
|
seats := map[string]catalogue.SeatDeclaration{
|
|
"mesh-catalog": {Name: "mesh-catalog", Scope: "mesh", Emits: []string{"catalog-revision.*", "manifest-accepted", "owed.*.*", "milestone.*.asked"}},
|
|
"mesh-wants": {Name: "mesh-wants", Scope: "mesh", Emits: []string{"moved"}},
|
|
}
|
|
modules := map[string]catalogue.Manifest{"mesh-wants": {Module: "mesh-wants"}, "some-app": {Module: "some-app"}}
|
|
cases := []struct {
|
|
name string
|
|
consume string
|
|
wantWatch string
|
|
wantModule bool
|
|
wantRefusals int
|
|
}{
|
|
{"bare name of an event with a part", "mesh-catalog.catalog-revision", "catalog-revision", false, 0},
|
|
{"the local name", "mesh-catalog.catalog-revision.*", "catalog-revision.*", false, 0},
|
|
{"an event without parts", "mesh-catalog.manifest-accepted", "manifest-accepted", false, 0},
|
|
{"two parts", "mesh-catalog.owed", "owed", false, 0},
|
|
{"a part between words", "mesh-catalog.milestone.asked", "milestone.asked", false, 0},
|
|
{"a word of the name alone is not the event", "mesh-catalog.milestone", "", false, 1},
|
|
{"an event the seat does not say", "mesh-catalog.no-such-event", "", false, 1},
|
|
{"a word that only shares a prefix", "mesh-catalog.catalog", "", false, 1},
|
|
{"a module of the seat's name says its own", "mesh-wants.something-else", "", true, 0},
|
|
{"a module's event", "some-app.thing", "", true, 0},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
m := catalogue.Manifest{Module: "x", Consumes: []string{c.consume}}
|
|
fromModules, watches := consumedOf(m, seats, nil, modules)
|
|
if got := slices.Contains(fromModules, c.consume); got != c.wantModule {
|
|
t.Fatalf("module consume = %v (%v), want %v", got, fromModules, c.wantModule)
|
|
}
|
|
var watched string
|
|
if len(watches) > 0 {
|
|
watched = strings.Join(watches[0].Emits, ",")
|
|
}
|
|
if watched != c.wantWatch {
|
|
t.Fatalf("watch = %q, want %q", watched, c.wantWatch)
|
|
}
|
|
refused := refusedConsumes(m, seats, modules)
|
|
if len(refused) != c.wantRefusals {
|
|
t.Fatalf("refusals = %v, want %d", refused, c.wantRefusals)
|
|
}
|
|
if len(refused) > 0 && !strings.Contains(refused[0], c.consume) {
|
|
t.Fatalf("refusal %q does not name the consume", refused[0])
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The refusal is in the pass's answer as a gap.
|
|
func TestRecordsSaysARefusedConsume(t *testing.T) {
|
|
m := catalogue.Manifest{Module: "reader", Consumes: []string{"mesh-catalog.no-such-event"}}
|
|
in := Inputs{Nodes: []string{"n1"}, Assigned: map[string][]string{"n1": {"reader"}}, Manifests: map[string]catalogue.Manifest{"reader": m}}
|
|
_, gaps := Records(in)
|
|
found := false
|
|
for _, g := range gaps {
|
|
if strings.Contains(g.Why, "no event no-such-event") {
|
|
found = true
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("gaps %v do not say the refused consume", gaps)
|
|
}
|
|
}
|