For every watched branch the registrations name, one source revision at each registered repository's head read through git.branch, start true, a call of its own, source-moved said and no record opened; recorded in the hand-act log first, the counts answered. It refuses to run while a key of the earlier form is held, naming it (the source bucket is dropped first). Builds: design 47 H §768; H-A-15, H-A-47 (the seed opens no record; the start pass writes through its path), H-A-76 (Q-G2: numbers from 1, refuses an earlier-form key) Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
358 lines
16 KiB
Go
358 lines
16 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
gitseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
|
|
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
)
|
|
|
|
type branchSource struct{ memSource }
|
|
|
|
func (b *branchSource) Branches() ([]string, error) {
|
|
var out []string
|
|
for k := range b.vals {
|
|
out = append(out, k)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Design 47 G §83, §84: gate_source answers every branch's source revision whole; gate_source_again says source-moved once
|
|
// more for one branch with a message id of its own, only after the hand-act log took it.
|
|
func TestTheSourceToolsReadEveryBranchAndSayItAgainAfterTheLog(t *testing.T) {
|
|
src := &branchSource{}
|
|
for _, b := range []string{"main", "feat"} {
|
|
_, _ = src.Set(b, func(*Source) (Source, bool) {
|
|
return Source{Source: 3, Call: "00000000000000c1", Heads: map[string]string{"novox/app": "c1"}}, true
|
|
})
|
|
}
|
|
type said struct{ subject, id string }
|
|
var events []said
|
|
now := time.Date(2026, 11, 12, 4, 0, 0, 0, time.UTC)
|
|
h := &Holder{Now: func() time.Time { return now }, Source: src,
|
|
SaySeat: func(subject string, body any, id string) error {
|
|
events = append(events, said{subject, id})
|
|
return nil
|
|
}}
|
|
all := h.SourceAll()["branches"].([]seat.SourceAnswer)
|
|
if len(all) != 2 {
|
|
t.Fatalf("gate_source answered %+v", all)
|
|
}
|
|
acts := &handActsFake{refuse: errors.New("log down")}
|
|
if _, err := h.SourceAgain("main", "a part missed it", "me", acts); err == nil || len(events) != 0 {
|
|
t.Fatalf("said without the log: %v %v", err, events)
|
|
}
|
|
acts.refuse = nil
|
|
if _, err := h.SourceAgain("main", "", "me", acts); err == nil {
|
|
t.Fatal("said with no reason")
|
|
}
|
|
if _, err := h.SourceAgain("nothing", "x", "me", acts); err == nil {
|
|
t.Fatal("said for a branch with no source")
|
|
}
|
|
if _, err := h.SourceAgain("main", "a part missed it", "me", acts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(acts.took) != 1 || acts.took[0].Cause != "gate_source_again" || len(events) != 1 ||
|
|
!strings.HasSuffix(events[0].subject, ".event.source-moved.main.3") || !strings.Contains(events[0].id, ".again.") {
|
|
t.Fatalf("log %+v events %+v", acts.took, events)
|
|
}
|
|
}
|
|
|
|
// Design 47 G §428, §507: no part's code holds a branch's name; the trunk of each repository a source revision names is the
|
|
// branch whose key was read, and delivery_status says every branch's source, not one fixed branch's.
|
|
func TestTheSourceAnswerNamesTheBranchItWasAskedAndStatusSaysEveryBranch(t *testing.T) {
|
|
src := &branchSource{}
|
|
for _, b := range []string{"release", "feat"} {
|
|
_, _ = src.Set(b, func(*Source) (Source, bool) {
|
|
return Source{Source: 3, Call: "00000000000000c1", Heads: map[string]string{"novox/app": "c1"}}, true
|
|
})
|
|
}
|
|
h := &Holder{Now: time.Now, Source: src}
|
|
got := h.SourceRevision("release")
|
|
if len(got.Repositories) != 1 || got.Repositories[0].Trunk != "release" {
|
|
t.Fatalf("the trunk is the asked branch: %+v", got.Repositories)
|
|
}
|
|
if all, ok := h.SourceAll()["branches"].([]seat.SourceAnswer); !ok || len(all) != 2 {
|
|
t.Fatalf("status says every branch's source: %+v", h.SourceAll())
|
|
}
|
|
}
|
|
|
|
type fakeCatalog struct {
|
|
regs []meshcatalog.Registration
|
|
err error
|
|
}
|
|
|
|
func (f fakeCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) {
|
|
return meshcatalog.RegistrationsAnswer{Registrations: f.regs}, f.err
|
|
}
|
|
|
|
// Design 47 G A-401: a watched branch starts from the forge as a clone does: each registered repository at the branch's head,
|
|
// the group empty, source-moved said; a repository whose head cannot be read is left out; a key held is never rewritten.
|
|
func TestAWatchedBranchStartsFromTheForge(t *testing.T) {
|
|
regs := []meshcatalog.Registration{
|
|
{Module: "car-hunter", Origin: "o/car-hunter", Branches: []string{"main"}},
|
|
{Module: "mesh-a", Origin: "o/mesh", Branches: []string{"main"}},
|
|
{Module: "mesh-b", Origin: "o/mesh", Branches: []string{"main"}}, // the same repository twice: once
|
|
{Module: "f1", Origin: "o/f1-circuits", Branches: []string{"master"}},
|
|
{Module: "l2p", Origin: "o/link2pay", Branches: []string{"nox-mesh"}},
|
|
{Module: "ph", Origin: "o/photos", Branches: []string{"nox-mesh", "main"}},
|
|
}
|
|
forge := &fakeForge{heads: map[string]string{
|
|
"o/car-hunter@main": "c1", "o/mesh@main": "c2", "o/f1-circuits@master": "c3", "o/link2pay@nox-mesh": "c4",
|
|
"o/photos@main": "c6", // o/photos@nox-mesh cannot be read
|
|
}}
|
|
for _, c := range []struct {
|
|
name string
|
|
held map[string]Source
|
|
catalog fakeCatalog
|
|
only string
|
|
started []string
|
|
heads map[string]map[string]string
|
|
events int
|
|
wantErr bool
|
|
}{
|
|
{name: "every branch no key holds", catalog: fakeCatalog{regs: regs},
|
|
started: []string{"main", "master", "nox-mesh"}, events: 3,
|
|
heads: map[string]map[string]string{"main": {"o/car-hunter": "c1", "o/mesh": "c2", "o/photos": "c6"},
|
|
"master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}},
|
|
{name: "a held key lacking repositories gets them, its heads unchanged", catalog: fakeCatalog{regs: regs},
|
|
held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old"}}},
|
|
started: []string{"main", "master", "nox-mesh"}, events: 3,
|
|
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1", "o/photos": "c6"}, "master": {"o/f1-circuits": "c3"}, "nox-mesh": {"o/link2pay": "c4"}}},
|
|
{name: "a held key lacking one repository", catalog: fakeCatalog{regs: regs[:2]},
|
|
held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old"}}},
|
|
started: []string{"main"}, events: 1,
|
|
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "c1"}}},
|
|
{name: "a held key naming all: nothing written or said", catalog: fakeCatalog{regs: regs[:2]},
|
|
held: map[string]Source{"main": {Source: 4, Call: "00000000000000aa", Heads: map[string]string{"o/mesh": "old", "o/car-hunter": "older"}}},
|
|
events: 0,
|
|
heads: map[string]map[string]string{"main": {"o/mesh": "old", "o/car-hunter": "older"}}},
|
|
{name: "by hand, one branch", catalog: fakeCatalog{regs: regs}, only: "master",
|
|
started: []string{"master"}, events: 1, heads: map[string]map[string]string{"master": {"o/f1-circuits": "c3"}}},
|
|
{name: "registrations unreadable", catalog: fakeCatalog{err: errors.New("away")}, wantErr: true},
|
|
{name: "no registration names the branch", catalog: fakeCatalog{regs: regs}, only: "nope", wantErr: true},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
src := &branchSource{memSource{vals: map[string]Source{}}}
|
|
for b, s := range c.held {
|
|
src.vals[b] = s
|
|
}
|
|
events := 0
|
|
var logged []string
|
|
h := &Holder{Now: time.Now, Source: src, Forge: forge, Catalog: c.catalog,
|
|
Logf: func(f string, a ...any) { logged = append(logged, fmt.Sprintf(f, a...)) },
|
|
SaySeat: func(subject string, body any, id string) error {
|
|
events++
|
|
ev := body.(seat.SourceMovedEvent)
|
|
if held, had := c.held[ev.Branch]; ev.Start == had || ev.Source != held.Source+1 || len(ev.Call) != 16 || len(ev.Heads) == 0 {
|
|
t.Errorf("a start's source-moved: %+v (start only for a branch's first revision, numbered above its newest)", ev)
|
|
}
|
|
return nil
|
|
}}
|
|
started, _, err := h.startSources(c.only)
|
|
if (err != nil) != c.wantErr {
|
|
t.Fatalf("err %v", err)
|
|
}
|
|
if c.wantErr {
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(started, c.started) || events != c.events {
|
|
t.Fatalf("started %v (%d events), want %v (%d)", started, events, c.started, c.events)
|
|
}
|
|
for b, want := range c.heads {
|
|
if got := src.vals[b].Heads; !reflect.DeepEqual(got, want) {
|
|
t.Errorf("source.%s heads %v, want %v", b, got, want)
|
|
}
|
|
}
|
|
for b, h := range c.held {
|
|
if c.events == 0 && !reflect.DeepEqual(src.vals[b], h) {
|
|
t.Errorf("source.%s naming every repository is rewritten: %+v", b, src.vals[b])
|
|
}
|
|
}
|
|
if c.only == "" && c.held == nil && !strings.Contains(strings.Join(logged, "\n"), "o/photos") {
|
|
t.Errorf("the repository left out was not said: %v", logged)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Design 47 G A-401: gate_source_start is recorded in the hand-act log first, and starts nothing when the log refuses it.
|
|
func TestSourceStartByHandIsRecordedFirst(t *testing.T) {
|
|
src := &branchSource{memSource{vals: map[string]Source{}}}
|
|
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{heads: map[string]string{"o/f1@master": "c3"}},
|
|
Catalog: fakeCatalog{regs: []meshcatalog.Registration{{Module: "f1", Origin: "o/f1", Branches: []string{"master"}}}}}
|
|
acts := &handActsFake{refuse: errors.New("log down")}
|
|
if _, err := h.SourceStart("master", "newly watched", "me", acts); err == nil || len(src.vals) != 0 {
|
|
t.Fatalf("started without the log: %v %v", err, src.vals)
|
|
}
|
|
acts.refuse = nil
|
|
if _, err := h.SourceStart("master", "", "me", acts); err == nil {
|
|
t.Fatal("started with no reason")
|
|
}
|
|
if _, err := h.SourceStart("master", "newly watched", "me", acts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(acts.took) != 1 || acts.took[0].Cause != "gate_source_start" || src.vals["master"].Heads["o/f1"] != "c3" {
|
|
t.Fatalf("log %+v source %+v", acts.took, src.vals)
|
|
}
|
|
}
|
|
|
|
// Design 47 G A-401: the registration-changed branch of take and the start pass both write the new branch's key and say
|
|
// source-moved; removing either leaves the branch unstarted.
|
|
func TestRegistrationChangedAndTheStartPassStartABranch(t *testing.T) {
|
|
regs := []meshcatalog.Registration{{Module: "f1", Origin: "o/f1", Branches: []string{"master"}}}
|
|
for _, c := range []struct {
|
|
name string
|
|
run func(h *Holder) error
|
|
}{
|
|
{"registration-changed", func(h *Holder) error {
|
|
return take(h, stdio.Envelope{Key: registrationChangedEvent, Body: json.RawMessage(`{}`)})
|
|
}},
|
|
{"the start pass", func(h *Holder) error { startSourcesAtStart(h); return nil }},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
if registrationChangedEvent == "" {
|
|
t.Fatal("the manifest does not consume registration-changed")
|
|
}
|
|
src := &branchSource{memSource{vals: map[string]Source{}}}
|
|
var subjects []string
|
|
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{heads: map[string]string{"o/f1@master": "c3"}},
|
|
Catalog: fakeCatalog{regs: regs}, Logf: func(string, ...any) {},
|
|
SaySeat: func(subject string, body any, id string) error { subjects = append(subjects, subject); return nil }}
|
|
if err := c.run(h); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if src.vals["master"].Heads["o/f1"] != "c3" || len(subjects) != 1 || !strings.HasSuffix(subjects[0], ".event.source-moved.master.1") {
|
|
t.Fatalf("source %+v events %v", src.vals, subjects)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Design 47 G A-442: the start pass runs once; it asks again, doubling its wait, only while the registrations cannot be read,
|
|
// and a repository left out is said, not waited for.
|
|
func TestTheStartAsksAgainOnlyWhileTheRegistrationsCannotBeRead(t *testing.T) {
|
|
regs := []meshcatalog.Registration{{Module: "f1", Origin: "o/f1", Branches: []string{"master"}}}
|
|
for _, c := range []struct {
|
|
name string
|
|
unreadFor int // passes the registrations cannot be read
|
|
forgeFail bool // the forge cannot name the head
|
|
waits []time.Duration
|
|
started bool
|
|
}{
|
|
{"registrations unread twice", 2, false, []time.Duration{2 * time.Second, 4 * time.Second}, true},
|
|
{"a repository left out is not waited for", 0, true, nil, false},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var waits []time.Duration
|
|
old := startSleep
|
|
startSleep = func(d time.Duration) { waits = append(waits, d) }
|
|
defer func() { startSleep = old }()
|
|
src := &branchSource{memSource{vals: map[string]Source{}}}
|
|
forge := &fakeForge{fail: c.forgeFail, heads: map[string]string{"o/f1@master": "c3"}}
|
|
cat := &flakyCatalog{fakeCatalog: fakeCatalog{regs: regs}, failFor: c.unreadFor}
|
|
var said []string
|
|
h := &Holder{Now: time.Now, Source: src, Forge: forge, Catalog: cat,
|
|
Logf: func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }}
|
|
startSourcesAtStart(h)
|
|
if !reflect.DeepEqual(waits, c.waits) || (src.vals["master"].Heads["o/f1"] == "c3") != c.started {
|
|
t.Fatalf("waits %v source %+v", waits, src.vals)
|
|
}
|
|
if c.forgeFail && !strings.Contains(strings.Join(said, "\n"), "o/f1@master") {
|
|
t.Fatalf("the left-out repository is not said: %v", said)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// flakyCatalog cannot read the registrations for its first failFor asks.
|
|
type flakyCatalog struct {
|
|
fakeCatalog
|
|
failFor int
|
|
}
|
|
|
|
func (f *flakyCatalog) Registrations() (meshcatalog.RegistrationsAnswer, error) {
|
|
if f.failFor > 0 {
|
|
f.failFor--
|
|
return meshcatalog.RegistrationsAnswer{}, errors.New("not served yet")
|
|
}
|
|
return f.fakeCatalog.Registrations()
|
|
}
|
|
|
|
// fakeForge is the seat git's reads: a branch's head and whether one commit is in another.
|
|
type fakeForge struct {
|
|
heads map[string]string // "<repository>@<branch>" -> head
|
|
// contains says whether commit is in `in`; nil means every commit is in every other (a straight line forward)
|
|
contains func(commit, in string) bool
|
|
fail bool
|
|
}
|
|
|
|
func (f *fakeForge) Branch(repository, branch string) (gitseat.BranchAnswer, error) {
|
|
if f.fail {
|
|
return gitseat.BranchAnswer{}, errors.New("the forge is away")
|
|
}
|
|
head, ok := f.heads[repository+"@"+branch]
|
|
if !ok {
|
|
return gitseat.BranchAnswer{Error: "no such branch"}, nil
|
|
}
|
|
return gitseat.BranchAnswer{Branch: branch, Repository: repository, Head: head}, nil
|
|
}
|
|
func (f *fakeForge) Contains(repository, commit, in string) (bool, error) {
|
|
if f.fail {
|
|
return false, errors.New("the forge is away")
|
|
}
|
|
if f.contains == nil {
|
|
return true, nil
|
|
}
|
|
return f.contains(commit, in), nil
|
|
}
|
|
|
|
// GATE-8/AC14 (design 47 H §768; H-A-47, H-A-76): gate_source_seed writes one revision per watched branch at each registered
|
|
// repository's head, numbered from 1, start true, a call of its own, says source-moved, opens no record, answers the counts,
|
|
// and is recorded in the hand-act log first; it refuses to run while a key of the earlier form is held, naming it.
|
|
func TestTheSeedWritesEveryWatchedBranchsFirstRevision(t *testing.T) {
|
|
regs := []meshcatalog.Registration{{Module: "a", Origin: "o/a", Branches: []string{"main"}}, {Module: "b", Origin: "o/b", Branches: []string{"main"}},
|
|
{Module: "c", Origin: "o/c", Branches: []string{"master"}}}
|
|
forge := &fakeForge{heads: map[string]string{"o/a@main": "ca", "o/b@main": "cb", "o/c@master": "cc"}}
|
|
setup := func() (*Holder, *branchSource, *[]seat.SourceMovedEvent) {
|
|
src := &branchSource{memSource{vals: map[string]Source{}}}
|
|
var moved []seat.SourceMovedEvent
|
|
h := &Holder{Now: time.Now, Source: src, Forge: forge, Catalog: fakeCatalog{regs: regs}, Logf: func(string, ...any) {},
|
|
SaySeat: func(subject string, body any, id string) error {
|
|
moved = append(moved, body.(seat.SourceMovedEvent))
|
|
return nil
|
|
}}
|
|
return h, src, &moved
|
|
}
|
|
h, src, moved := setup()
|
|
acts := &handActsFake{refuse: errors.New("log down")}
|
|
if _, err := h.SourceSeed("clean state", "lead@shanks", acts); err == nil || len(src.vals) != 0 {
|
|
t.Fatalf("seeded without the log: %v %+v", err, src.vals)
|
|
}
|
|
acts.refuse = nil
|
|
out, err := h.SourceSeed("clean state", "lead@shanks", acts)
|
|
if err != nil || out["branches"] != 2 || out["repositories"] != 3 || len(acts.took) != 1 || acts.took[0].Cause != "gate_source_seed" {
|
|
t.Fatalf("seed %+v %v, log %+v", out, err, acts.took)
|
|
}
|
|
if got := src.vals["main"]; got.Source != 1 || len(got.Call) != 16 || got.Heads["o/a"] != "ca" || got.Heads["o/b"] != "cb" {
|
|
t.Fatalf("source.main %+v", got)
|
|
}
|
|
if len(*moved) != 2 || !(*moved)[0].Start || !(*moved)[1].Start || (*moved)[0].Source != 1 || len((*moved)[0].Heads) != 2 {
|
|
t.Fatalf("source-moved %+v", *moved)
|
|
}
|
|
h, src, moved = setup()
|
|
src.unreadable = map[string]bool{"master": true}
|
|
if _, err := h.SourceSeed("clean state", "lead@shanks", &handActsFake{}); err == nil || !strings.Contains(err.Error(), "source.master") ||
|
|
len(src.vals) != 0 || len(*moved) != 0 {
|
|
t.Fatalf("seeded over a key of the earlier form: %v %+v", err, src.vals)
|
|
}
|
|
}
|