Files
mesh-delivery/modules/mesh-delivery-forge-bridge/manifest_test.go
T
jschoubben 13d960a120 F1: an owed act failing on a part away stays owed, and a record's acts are performed again
An owed act met by the forge away (401 while the store was replaced) ended
could not be performed and nothing performed it again. Each act is now a kept
call at the module's pace (kept-call-pace-first, kept-call-pace-max) until
owed-away-bound (60 minutes), then ends could not be performed naming every
try. Before each try the bridge reads the pull request through the seat git's
pull: one closed unmerged ends owed.ended not-needed, never performed. The
gate's owed event gains record (the delivery record's source revision), kept
with the act in performed, and bridge_owed_again by record and why performs
again every act of that record that ended could not be performed, written in
the hand-act log first.

Builds: design 47 H2 §807, §775, §819, H-A-432
2026-11-22 01:46:49 +01:00

257 lines
11 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"`
MaxAgeDays int `json:"max-age-days"`
}
type state struct {
Name string `json:"name"`
History int `json:"history"`
MaxMiB int `json:"max-mib"`
}
// setting is an entry of config.
type setting struct {
Type string `json:"type"`
Description string `json:"description"`
Trusted bool `json:"trusted"`
Runtime bool `json:"runtime"`
Minimum float64 `json:"minimum"`
Maximum float64 `json:"maximum"`
Default any `json:"default"`
}
// 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"`
Config map[string]setting `json:"config"`
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
t.Run("the process", manifestProcess) // design 47 H2 §819: one top-level test fewer
m, top := load(t)
if m.Module != "mesh-delivery-forge-bridge" {
t.Errorf("module = %q", m.Module)
}
// The stream forge keeps head-moved 7 days, its definition's keep; its size is the template's default (design 47 H
// §739, H-A-23).
if !reflect.DeepEqual(m.Streams, []stream{{Name: "forge", Events: []string{"forge.**", "owed.ended"}, MaxAgeDays: 7}}) {
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 tree, file, open pull requests, merge or group is asked (design 47 H §739, §764); one pull request's state is read
// before each try of an act (design 47 H2 §807, H-A-432 (2)).
if want := []string{"seat:git.branch", "seat:git.comment", "seat:git.note", "seat:git.pull", "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, each capped at 8 MiB
// (design 47 H2 §812, H-A-411 (2)). What it performed of what is owed is kept data in the new form, one key per act,
// kept 3 days by the bucket's TTL, performed-keep, and capped at 8 MiB, pruned toward it at the bridge's writes (§812,
// H-A-433). No inputs, end-asks or findings (H §764, H-A-48).
if !reflect.DeepEqual(m.State, []state{{Name: "heads", History: 1, MaxMiB: 8}, {Name: "owed-asks", History: 1, MaxMiB: 8}}) {
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(259200), "max-mib": float64(8)}}}; !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; the two prune tools act on performed (§812, H-A-380 (3)).
if want := []string{"bridge_heads", "bridge_owed", "bridge_owed_again", "bridge_owed_drop", "bridge_tags",
"mesh-delivery-forge-bridge_state_keep", "mesh-delivery-forge-bridge_state_prune_now"}; !reflect.DeepEqual(sorted(m.Tools), want) {
t.Errorf("tools = %v, want %v", m.Tools, want)
}
// performed's prune starts past prune-mark and ends at prune-target, settings read at run time (§812).
// Each owed act is a kept call at the module's pace until owed-away-bound (§807, §775).
for key, def := range map[string]any{"prune-mark": float64(70), "prune-target": float64(50), "owed-away-bound": "60m",
"kept-call-pace-first": "1s", "kept-call-pace-max": "30s"} {
if c := m.Config[key]; c.Default != def || !c.Runtime {
t.Errorf("config %s = %+v, want a runtime setting defaulting to %v", key, c, def)
}
}
// 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 manifestProcess(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")
}
if len(m.Build.Artifacts) != 3 || m.Build.Artifacts[1].Name != "tools" || m.Build.Artifacts[1].From != "cmd/mesh-delivery-forge-bridge-tools" {
t.Fatalf("artifacts = %+v, want the program, the tools bundle and the package forgemsg", m.Build.Artifacts)
}
// ADR 0573 §1, H-A-186 (design 47 G §370): the gate, the builder and the indexer import forgemsg, so the module declares
// it as a package and every release publishes it.
if a := m.Build.Artifacts[2]; a.Name != "forgemsg" || a.Kind != "package" || a.Language != "go" || len(a.Paths) != 1 || a.Paths[0] != "forgemsg" || a.Series != "0.1" {
t.Errorf("package artifact = %+v", a)
}
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)
}
}