Files
mesh-delivery/modules/mesh-delivery-pipeline/internal/pipeline/registrations_test.go
T
2026-11-12 05:53:07 +01:00

123 lines
4.7 KiB
Go

package pipeline
import (
"context"
"errors"
"strings"
"testing"
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
)
// The policy, the build seat by language and the views per watched branch (design 47 G §21, §23, §323, §195).
func TestPolicyAndBuildSeat(t *testing.T) {
reg := catalog.Registration{Module: "m", Branches: []string{"main"}, Policy: catalog.Policy{
Steps: catalog.StepStatuses{Lint: "required", Unit: "required", Integration: "optional", Approval: "required"},
Artifacts: map[string]catalog.StepStatuses{"tools": {Unit: "bypass"}},
}}
for _, c := range []struct{ artifact, step, want string }{
{"program", "unit", "required"},
{"tools", "unit", "bypass"},
{"tools", "integration", "optional"},
{"tools", "approval", "required"},
// A step whose status the policy leaves empty is optional (design 47 G §553), never no step.
{"program", "composition", "optional"},
{"program", "review", "optional"},
} {
if got := Status(reg, c.artifact, c.step); got != c.want {
t.Errorf("Status(%s, %s) = %q, want %q", c.artifact, c.step, got, c.want)
}
}
// An empty approval status asks the operator nothing (A-196): no step.
if got := Status(catalog.Registration{}, "", "approval"); got != "" {
t.Errorf("an empty approval status = %q, want none", got)
}
entries := map[string]catalog.CatalogEntry{
"mesh-build-go": {Module: "mesh-build-go", Acts: []catalog.ActingRule{{Seat: "node-build-go", Step: "build", On: "go"}}},
"mesh-build-node": {Module: "mesh-build-node", Acts: []catalog.ActingRule{{Seat: "node-build-node", Step: "build", On: "typescript"}, {Seat: "node-build-node", Step: "check", On: "go"}}},
"app": {Module: "app", Artifacts: []catalog.Artifact{{Ref: "app:program", Language: "go"}}},
}
for _, c := range []struct{ language, want, err string }{
{"go", "node-build-go", ""},
{"typescript", "node-build-node", ""},
{"rust", "", `"rust"`},
} {
got, err := BuildSeat(entries, c.language)
if got != c.want || (c.err == "") != (err == nil) || (err != nil && !strings.Contains(err.Error(), c.err)) {
t.Errorf("BuildSeat(%s) = %q, %v; want %q, an error naming %s", c.language, got, err, c.want, c.err)
}
}
entries["other"] = catalog.CatalogEntry{Acts: []catalog.ActingRule{{Seat: "node-build-other", Step: "build", On: "go"}}}
if _, err := BuildSeat(entries, "go"); err == nil || !strings.Contains(err.Error(), "node-build-other") {
t.Errorf("two seats naming go: %v, want an error naming both", err)
}
heard := map[string]int{}
opened := 0
v := &Views{Open: func(b string) (BranchView, error) {
opened++
if b == "broken" {
return nil, errors.New("refused")
}
return fakeView{branch: b, heard: heard}, nil
}}
v.Registered(reg)
v.Registered(catalog.Registration{Module: "n", Branches: []string{"main", "feature/x", "broken"}})
if got := v.Branches(); strings.Join(got, ",") != "feature/x,main" || opened != 3 {
t.Fatalf("branches %v opened %d, want feature/x and main, three opens", got, opened)
}
v.Registered(catalog.Registration{Module: "n", Branches: []string{"broken"}})
if opened != 4 {
t.Errorf("a branch whose view was refused is opened again at the next registration: %d opens", opened)
}
if err := v.HearRevision(context.Background(), []byte(`{}`)); err != nil || heard["main"] != 1 || heard["feature/x"] != 1 {
t.Errorf("a revision heard reaches every branch's view: %v %v", err, heard)
}
if h, ok := v.View("main"); !ok || h.Revision != 7 {
t.Errorf("View(main) = %v %v", h, ok)
}
if _, ok := v.View("gone"); ok {
t.Error("a branch not watched has no view")
}
}
type fakeView struct {
branch string
heard map[string]int
}
func (f fakeView) Hear(context.Context, []byte) error { f.heard[f.branch]++; return nil }
func (f fakeView) View() (Half, bool) { return Half{Revision: 7}, true }
type readyFake struct {
fakeView
ch chan struct{}
}
func (r readyFake) Ready() <-chan struct{} { return r.ch }
// A branch's view that holds its first half tells Views once, so the runner asks the steps placed on that branch (§88).
func TestViewsTellOnceWhenABranchHoldsItsFirstHalf(t *testing.T) {
ch := make(chan struct{})
told := make(chan string, 4)
v := &Views{OnReady: func(b string) { told <- b }, Open: func(b string) (BranchView, error) {
return readyFake{fakeView{branch: b, heard: map[string]int{}}, ch}, nil
}}
v.Registered(catalog.Registration{Module: "n", Branches: []string{"main"}})
v.Registered(catalog.Registration{Module: "m", Branches: []string{"main"}})
select {
case b := <-told:
t.Fatalf("told %s before the view held a half", b)
default:
}
close(ch)
if b := <-told; b != "main" {
t.Errorf("told %q, want main", b)
}
if len(told) != 0 {
t.Error("a branch's readiness is told once")
}
}