Files
mesh-catalog/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/keeps_test.go
T
jschoubben cbd4da3a11 GATE §613: deliveries, groups, owed and conditions are template state entries under the same names, taking the buckets they had
merges and source stay in the old state section; pulls and asks wait for the removal of the merge states.

Builds: design 47 G §613, A-8, A-20 (G-17, G-20)

Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
2026-11-11 21:49:54 +01:00

111 lines
3.8 KiB
Go

package main
import (
"encoding/json"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// readers is the gate's own declaration of who reads its states (ADR 0495 clause 3: a read is granted only when the owner
// announced the state readable), kept here and not read from other repositories' manifests (clause 1). Each line names
// the modules of mesh-delivery whose manifest reads the state, as of R4e.
var readers = map[string][]string{
"asks": {"mesh-delivery-teller", "mesh-delivery-view"},
"merges": {"mesh-delivery-forge-bridge"},
"pulls": {"build-agent"}, // in mesh-controller
"owed": {"mesh-delivery-forge-bridge"},
"source": {"mesh-delivery-indexer", "mesh-delivery-checker"},
// ADR 0548 §3, §4: the checker reads the merge turns and claims no check of a repository one names; the gate alone writes
// them (a state is written by its owner's account alone, ADR 0495 §3: announcing it readable grants a read, never a write).
"turns": {"mesh-delivery-checker"},
}
// 1790/S: the states the module announces are the states its manifest declares, no more, no fewer, and the readable
// ones are the states other modules' manifests read.
func TestTheStatesTheModuleAnnouncesAreTheStatesItsManifestDeclares(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
if err != nil {
t.Fatal(err)
}
var m struct {
State []json.RawMessage `json:"state"`
Requires []struct {
Name string `json:"name"`
Storage string `json:"storage"`
Template string `json:"template"`
} `json:"requires"`
}
if err := json.Unmarshal(raw, &m); err != nil {
t.Fatal(err)
}
declared := []string{}
for _, s := range m.State {
var name string
if json.Unmarshal(s, &name) != nil {
var o struct{ Name string }
if err := json.Unmarshal(s, &o); err != nil {
t.Fatal(err)
}
name = o.Name
}
declared = append(declared, name)
}
// The kept data of the template `state` are asked for in `requires`, under their local names (design 47 G §613).
for _, r := range m.Requires {
if r.Storage == "key-value" && r.Template == "state" {
declared = append(declared, r.Name)
}
}
announced := []string{}
for _, s := range Kept {
announced = append(announced, s.Name)
}
slices.Sort(declared)
slices.Sort(announced)
if !slices.Equal(declared, announced) {
t.Errorf("announces %v, the manifest declares %v", announced, declared)
}
for _, s := range Kept {
if want := len(readers[s.Name]) > 0; s.Readable != want {
t.Errorf("state %s readable = %v, but its readers are %v", s.Name, s.Readable, readers[s.Name])
}
}
}
// Design 47 G §613 (A-20): deliveries, groups, owed and conditions are `requires` entries of the template `state` under
// their local names, taking the buckets they already had; merges and source stay in the old `state` section.
func TestTheFourStatesMoveToTheTemplateAndTwoStayOld(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("..", "..", "module.json"))
if err != nil {
t.Fatal(err)
}
var m struct {
State []json.RawMessage `json:"state"`
Requires []struct{ Name, Storage, Template string }
}
if err := json.Unmarshal(raw, &m); err != nil {
t.Fatal(err)
}
moved := map[string]bool{}
for _, r := range m.Requires {
if r.Storage == "key-value" && r.Template == "state" {
moved[r.Name] = true
}
}
old := string(raw[strings.Index(string(raw), `"state": [`):])
old = old[:strings.Index(old, "]")]
for _, n := range []string{"deliveries", "groups", "owed", "conditions"} {
if !moved[n] || strings.Contains(old, `"`+n+`"`) {
t.Errorf("%s: template state %v, old section lists it %v", n, moved[n], strings.Contains(old, `"`+n+`"`))
}
}
for _, n := range []string{"merges", "source"} {
if moved[n] || !strings.Contains(old, `"`+n+`"`) {
t.Errorf("%s stays in the old section", n)
}
}
}