The bridge's whole test runs the program against an embedded bus as the node does: a start, a merge, an admitted push to an unprotected branch and a restart after a move each give one head-moved naming the head read through the seat git's branch and the head held before (start true at a start); a head heard twice, a branch not watched and a restart at the head held give none, and the bridge asks the seat git nothing but branch. Its bus permissions are composed for the manifest as it now stands. The pull, open pull request, group and unprotected-branch tests go; the owed test performs the gate's statuses once, after a restart and again by hand. bridge_heads answers each key of the state heads with the head the bridge holds, read-only. Builds: design 47 H §739 (how checked), §774; lead answers H-A-48, H-A-85 (1) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
226 lines
9.3 KiB
Go
226 lines
9.3 KiB
Go
package manifest
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The controller's manifest reader is mesh-controller internal/catalogue (Manifest, manifest.go: Consumes :521,
|
|
// Invokes :590, Streams :613, Reads :659; StreamDeclaration, streams.go), an internal package of another repository.
|
|
// These tests decode the manifest strictly into the fields those types name (DisallowUnknownFields) and hold the
|
|
// rules of issue 1235 (ADR 0437 §4). The merge gate's module check proves the controller takes the manifest.
|
|
|
|
type stream struct {
|
|
Name string `json:"name"`
|
|
Events []string `json:"events"`
|
|
}
|
|
|
|
type state struct {
|
|
Name string `json:"name"`
|
|
History int `json:"history"`
|
|
}
|
|
|
|
// kept is an entry of requires in its long form: kept data on the bus (novox/hq ADR 0595).
|
|
type kept struct {
|
|
Name string `json:"name"`
|
|
Storage string `json:"storage"`
|
|
Template string `json:"template"`
|
|
Options map[string]any `json:"options"`
|
|
}
|
|
|
|
type artifact struct {
|
|
Name string `json:"name"`
|
|
Kind string `json:"kind"`
|
|
Language string `json:"language"`
|
|
System string `json:"system"`
|
|
From string `json:"from"`
|
|
Binary string `json:"binary"`
|
|
Loads []string `json:"loads"`
|
|
Paths []string `json:"paths"`
|
|
Series string `json:"series"`
|
|
}
|
|
|
|
type manifest struct {
|
|
Module string `json:"module"`
|
|
Version string `json:"version"`
|
|
Slug string `json:"slug"`
|
|
Consumes []string `json:"consumes"`
|
|
Streams []stream `json:"streams"`
|
|
State []state `json:"state"`
|
|
Requires []kept `json:"requires"`
|
|
Reads []string `json:"reads"`
|
|
Tools []string `json:"tools"`
|
|
Invokes []string `json:"invokes"`
|
|
OwnSecrets map[string]string `json:"own-secrets"`
|
|
SecretsOwner string `json:"secrets-owner"`
|
|
Resources []map[string]any `json:"resources"`
|
|
Build struct {
|
|
Artifacts []artifact `json:"artifacts"`
|
|
} `json:"build"`
|
|
}
|
|
|
|
func load(t *testing.T) (manifest, map[string]json.RawMessage) {
|
|
t.Helper()
|
|
raw, err := os.ReadFile("module.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var m manifest
|
|
dec := json.NewDecoder(bytes.NewReader(raw))
|
|
dec.DisallowUnknownFields()
|
|
if err := dec.Decode(&m); err != nil {
|
|
t.Fatalf("the controller's reader refuses a key this manifest has: %v", err)
|
|
}
|
|
var top map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &top); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m, top
|
|
}
|
|
|
|
func sorted(s []string) []string {
|
|
s = slices.Clone(s)
|
|
slices.Sort(s)
|
|
return s
|
|
}
|
|
|
|
func TestManifestDeclarations(t *testing.T) { // 1235/AC1
|
|
m, top := load(t)
|
|
if m.Module != "mesh-delivery-forge-bridge" {
|
|
t.Errorf("module = %q", m.Module)
|
|
}
|
|
if !reflect.DeepEqual(m.Streams, []stream{{Name: "forge", Events: []string{"forge.**", "owed.ended"}}}) {
|
|
t.Errorf("streams = %+v", m.Streams)
|
|
}
|
|
// What the gate owes the forge is its seat's event owed, heard on its own stream (design 47 *What the gate owes the
|
|
// forge*), never read from its state. The state performed's binding is asked again on the seat mesh-broker's recreated
|
|
// and dropped, and asked of its show (design 47 G §577). Every move of a watched branch is heard on branch-moved, and
|
|
// the watched branches from the registrations, before the first the gate's source (design 47 G §4, §507; H §739). No
|
|
// pull request event, no checker and no finding is heard (design 47 H §764, H-A-48).
|
|
if want := []string{"git.branch-moved", "mesh-broker.dropped", "mesh-broker.recreated", "mesh-catalog.registration-changed", "mesh-delivery.owed", "mesh-delivery.source-moved"}; !reflect.DeepEqual(sorted(m.Consumes), want) {
|
|
t.Errorf("consumes = %v, want %v", m.Consumes, want)
|
|
}
|
|
// The bridge reads no state of another module's.
|
|
if len(m.Reads) != 0 {
|
|
t.Errorf("reads = %v, want none", m.Reads)
|
|
}
|
|
// No pull, tree, file, open pull requests, merge or group is asked (design 47 H §739, §764).
|
|
if want := []string{"seat:git.branch", "seat:git.comment", "seat:git.note", "seat:git.status", "seat:git.tags", "seat:git.view",
|
|
"seat:mesh-broker.show", "seat:mesh-catalog.registrations", "seat:mesh-delivery.source", "seat:mesh-hand-acts.hand-act"}; !reflect.DeepEqual(sorted(m.Invokes), want) {
|
|
t.Errorf("invokes = %v, want %v", m.Invokes, want)
|
|
}
|
|
if b, ok := m.OwnSecrets["broker"]; !ok || len(m.OwnSecrets) != 1 || b != "${dir:state}/broker" {
|
|
t.Errorf("own-secrets = %v, want exactly broker", m.OwnSecrets)
|
|
}
|
|
if m.SecretsOwner != "mesh-delivery-forge-bridge" {
|
|
t.Errorf("secrets-owner = %q", m.SecretsOwner)
|
|
}
|
|
// The head held for each watched branch (design 47 H §739), and the asks about what is owed. What it performed of what is
|
|
// owed is kept data in the new form, one key per act, kept 7 days as the owner's stream owed is; its size is the
|
|
// template's own (design 47 *What the gate owes the forge*, ADR 0595 §3). No inputs, end-asks or findings (H §764, H-A-48).
|
|
if !reflect.DeepEqual(m.State, []state{{Name: "heads", History: 1}, {Name: "owed-asks", History: 1}}) {
|
|
t.Errorf("state = %+v, want the buckets heads and owed-asks", m.State)
|
|
}
|
|
if want := []kept{{Name: "performed", Storage: "key-value", Template: "state", Options: map[string]any{"history": float64(1), "ttl-seconds": float64(604800)}}}; !reflect.DeepEqual(m.Requires, want) {
|
|
t.Errorf("requires = %+v, want performed alone as %+v", m.Requires, want)
|
|
}
|
|
// bridge_tags reads a package's tags for the builder's floor (design 51); bridge_heads reads the state heads (design 47
|
|
// H-A-85); the owed tools ask the bridge through owed-asks.
|
|
if want := []string{"bridge_heads", "bridge_owed", "bridge_owed_again", "bridge_owed_drop", "bridge_tags"}; !reflect.DeepEqual(sorted(m.Tools), want) {
|
|
t.Errorf("tools = %v, want %v", m.Tools, want)
|
|
}
|
|
// Design 47 H §764 (B3 AC1): the earlier model's names are in no file of the bridge but tests asserting their absence.
|
|
retired := []string{"pushed", "forge.group.merged", "bridge_groups", "bridge_group_end_again", "bridge_merged_message",
|
|
"OwedActMerge", "mesh-delivery-checker", "seat:git.merge-pull", "branch-unprotected", "ForgeMerged", "ForgePushed"}
|
|
_ = filepath.WalkDir(".", func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil || d.IsDir() || strings.HasSuffix(p, "_test.go") || (!strings.HasSuffix(p, ".go") && !strings.HasSuffix(p, ".json")) {
|
|
return err
|
|
}
|
|
data, _ := os.ReadFile(p)
|
|
for _, name := range retired {
|
|
if bytes.Contains(data, []byte(name)) {
|
|
t.Errorf("%s names %q, which design 47 H §764 retires", p, name)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
// No other state, no seat, no event (ADR 0437 §4; ADR 0521: no check.status); its kept data on the bus is the one entry of requires above.
|
|
for _, k := range []string{"logs", "emits", "claims", "provides", "uses", "defines-seats", "seat", "seats"} {
|
|
if _, ok := top[k]; ok {
|
|
t.Errorf("the manifest declares %q: the bridge has none", k)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestManifestProcess(t *testing.T) { // 1235/AC1
|
|
m, _ := load(t)
|
|
var procs []map[string]any
|
|
for _, r := range m.Resources {
|
|
if r["type"] == "process" {
|
|
procs = append(procs, r)
|
|
}
|
|
if r["type"] == "tool" || r["type"] == "seat" {
|
|
t.Errorf("resource %v: the bridge serves no tool and holds no seat", r)
|
|
}
|
|
}
|
|
if len(procs) != 1 {
|
|
t.Fatalf("%d process resources, want one", len(procs))
|
|
}
|
|
p := procs[0]
|
|
if p["artifact"] != "program" || !reflect.DeepEqual(p["run"], []any{"./mesh-delivery-forge-bridge"}) {
|
|
t.Errorf("process = %v", p)
|
|
}
|
|
env, _ := p["env"].(map[string]any)
|
|
if len(env) != 1 || env["MESH_BROKER_FILE"] != "${dir:state}/broker" {
|
|
t.Errorf("env = %v", p["env"])
|
|
}
|
|
if _, bus := p["bus"]; bus {
|
|
t.Errorf("the process declares a bus: it runs on its own broker credential")
|
|
}
|
|
// Issue 1966's bootstrap: the package artifact forgemsg is out of the manifest until the new builder runs; the follow-up
|
|
// puts it back with its assertion (ADR 0573 §1: three artifacts, the third the package forgemsg).
|
|
if len(m.Build.Artifacts) != 2 || m.Build.Artifacts[1].Name != "tools" || m.Build.Artifacts[1].From != "cmd/mesh-delivery-forge-bridge-tools" {
|
|
t.Fatalf("artifacts = %+v, want the program and the tools bundle", m.Build.Artifacts)
|
|
}
|
|
a := m.Build.Artifacts[0]
|
|
if st, err := os.Stat(a.From); err != nil || !st.IsDir() || a.Binary != "mesh-delivery-forge-bridge" || !strings.HasSuffix(a.From, a.Binary) {
|
|
t.Errorf("artifact = %+v", a)
|
|
}
|
|
}
|
|
|
|
func TestServesNoRequestSubjectAndHoldsNoTicker(t *testing.T) { // 1235/AC6 (ADR 0437 §8)
|
|
forbidden := []string{"Subscribe(", "QueueSubscribe", "ChanSubscribe", "time.NewTicker", "time.Tick", "time.NewTimer", "time.AfterFunc", "time.After(", "time.Sleep"}
|
|
var files int
|
|
for _, root := range []string{"cmd", "internal"} {
|
|
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
|
|
return err
|
|
}
|
|
files++
|
|
src, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, f := range forbidden {
|
|
if strings.Contains(string(src), f) {
|
|
t.Errorf("%s holds %s: the bridge serves no request subject and holds no timer", path, f)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if files < 2 {
|
|
t.Fatalf("read %d source files, want the program's", files)
|
|
}
|
|
}
|