Files
mesh-sdk/go/catalogue/seats_test.go
T
jschoubben 42ac1a9a0a mesh-sdk's node-engine names are the seat's generated contract, kept deprecated for their importers, with their checks
The seat is defined whole in mesh-contracts (ADR 0613 §3), so mesh-sdk takes its names from there (ADR 0613 §5):
EngineSeat and EngineEvents are nodeengine's, EngineComposeCandidateVerb is nodeengine.VerbComposeCandidate, and
ComposeCandidateAsk, ComposeCandidateAnswer and ModuleComposed are aliases of nodeengine's ComposeCandidateInput,
ComposeCandidateAnswer and Module. The encode and decode checks stay here over those types until the seat has a client
package (ADR 0625 §4). For importers: a module's fingerprint is Fp, and the answer's Input is a JSON object
(map[string]any), not raw bytes. compose-candidate's input is the boolean the engine and the checker read; the seat
set's test takes that correction, and the whole seat's added verbs as da653ce reads them. EngineSeat leaves
testdata/seat-names.txt.

Claude-Session: https://claude.ai/code/session_01JG73KG7AfSdh6FP8VrBqCn
2026-11-04 12:40:03 +01:00

139 lines
5.2 KiB
Go

package catalogue
import (
"encoding/json"
"os"
"reflect"
"slices"
"testing"
)
// Defends novox/hq ADR 0110: a seat is a module assignment from a closed set.
func claimed(claims string) []byte {
return []byte(`{"module":"thing","version":"1","provides":[{"name":"npm-package-registry","scope":"mesh","credential":{"form":"none"}}],"claims":` + claims + `}`)
}
// The holder of a seat answers among several providers.
// The working set is loaded from the store, and an empty load never erases it (novox/hq ADR 0122).
func TestUseSeatsReplacesTheSetButNeverEmptiesIt(t *testing.T) {
before := Seats()
defer UseSeats(DefaultSeats()) // restore for other tests, whatever this leaves it as
// An empty load (store not seeded, or unreadable) leaves the compiled defaults in force.
UseSeats(nil)
if len(Seats()) != len(before) {
t.Fatalf("an empty load changed the set from %d to %d seats", len(before), len(Seats()))
}
// A non-empty load replaces it — this is how a rename in the store reaches the lookups.
UseSeats([]Seat{{Name: "node-firewall", Scope: ScopeNode, Decision: "novox/hq ADR 0122"}})
if _, known := SeatNamed("node-firewall"); !known {
t.Fatal("the loaded set did not replace the working set")
}
if len(Seats()) != 1 {
t.Fatalf("the working set is %d seats, not the one that was loaded", len(Seats()))
}
// **The compiled set is the generated one, and the move changed nothing a reader sees** (design 26, SD-M3, SDK-8):
// every seat the list mesh-sdk compiled before the move held (testdata, written from 5a38a12) is in the set
// generated from mesh-contracts' definitions, entry for entry, its verbs' optional marks and a kind's code
// included. A seat defined whole may add verbs, events and answers; the vault drops accept and move-asks (ADR 0620
// §3); three registry seats are new.
UseSeats(DefaultSeats())
type golden struct {
Seat
Optional []string
ShapeCode []string
}
raw, err := os.ReadFile("testdata/seats-before-move.json")
if err != nil {
t.Fatal(err)
}
var compiled []golden
if err := json.Unmarshal(raw, &compiled); err != nil {
t.Fatal(err)
}
whole := []string{"npm-package-registry", "mesh-broker", "mesh-config", "mesh-vault", "mesh-delivery-checker", "node-engine"}
dropped := map[string]bool{"mesh-vault.accept": true, "mesh-vault.move-asks": true}
// Verbs a seat at the seat list's grain gained in the milestone, each optional until its holders serve it.
addedVerbs := map[string]bool{"node-engine.pull-waits": true}
// A verb whose input the whole definition corrected: compose-candidate's input is the boolean the engine and the
// checker read, where the compiled list said a word (engine_compose_candidate_test.go reads it).
retyped := map[string]bool{"node-engine.compose-candidate": true}
norm := func(v any) any {
b, _ := json.Marshal(v)
var out any
_ = json.Unmarshal(b, &out)
return out
}
var names []string
for _, want := range compiled {
names = append(names, want.Name)
got, ok := SeatNamed(want.Name)
if !ok {
t.Errorf("%s left the generated set", want.Name)
continue
}
g := golden{Seat: got}
for _, v := range got.Serves {
if v.Optional {
g.Optional = append(g.Optional, v.Name)
}
}
for _, r := range got.Receives {
if r.Shape != nil && (r.Shape.Check != nil || r.Shape.Reads != nil) {
g.ShapeCode = append(g.ShapeCode, r.Kind)
}
}
g.Serves = slices.DeleteFunc(slices.Clone(g.Serves), func(v Verb) bool { return addedVerbs[want.Name+"."+v.Name] })
g.Optional = slices.DeleteFunc(g.Optional, func(n string) bool { return addedVerbs[want.Name+"."+n] })
if slices.Contains(whole, want.Name) {
want := &want
byName := map[string]Verb{}
for _, v := range got.Serves {
v.Output = nil
byName[v.Name] = v
}
var kept []Verb
for _, v := range want.Serves {
if dropped[want.Name+"."+v.Name] {
if _, still := byName[v.Name]; still {
t.Errorf("%s still serves %s", want.Name, v.Name)
}
continue
}
kv := byName[v.Name]
if retyped[want.Name+"."+v.Name] {
kv.Input = v.Input
}
kept = append(kept, kv)
}
g.Serves, g.Emits, g.Accepts = kept, want.Emits, want.Accepts
// A verb the whole seat added (new, optional while its holders catch up) is no verb of the compiled list.
g.Optional = slices.DeleteFunc(g.Optional, func(n string) bool {
return !slices.ContainsFunc(want.Serves, func(v Verb) bool { return v.Name == n })
})
if len(g.Optional) == 0 {
g.Optional = nil
}
want.Optional = slices.DeleteFunc(want.Optional, func(n string) bool { return dropped[want.Name+"."+n] })
want.Serves = slices.DeleteFunc(slices.Clone(want.Serves), func(v Verb) bool { return dropped[want.Name+"."+v.Name] })
}
if !reflect.DeepEqual(norm(g), norm(want)) {
gb, _ := json.Marshal(g)
wb, _ := json.Marshal(want)
t.Errorf("%s is not as the compiled list held it:\n got %s\nwant %s", want.Name, gb, wb)
}
}
var added []string
for _, s := range DefaultSeats() {
if !slices.Contains(names, s.Name) {
added = append(added, s.Name)
}
}
if want := []string{"go-package-registry", "container-image-registry", "oci-artifact-registry"}; !slices.Equal(added, want) {
t.Errorf("the generated set adds %v, want %v", added, want)
}
}