Files
mesh-catalog/modules/mesh-delivery-gate/cmd/mesh-delivery-gate/sourcefollow_test.go
T
2026-11-17 09:03:29 +01:00

194 lines
10 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
meshcatalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// Design 47 G A-464, A-464b: the forge bridge's merged message moves the head of its repository in the source of the branch it
// landed on and says source-moved; a merge naming no delivery group is said not made by one; a branch no source key holds, a
// repository the key does not name, and a head the key holds already move nothing.
func TestAMergedMessageMovesTheSourceOfItsBranchWhoeverMadeTheMerge(t *testing.T) {
old, merge := strings.Repeat("a", 40), strings.Repeat("b", 40)
body := func(repository, base, group string) []byte {
f := forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: repository, Base: base, Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)}
data, err := forgemsg.EncodeForge(f)
if err != nil {
t.Fatal(err)
}
return data
}
for _, c := range []struct {
name, repository, base string
moved bool
}{
{"watched branch, named repository", "novox/mesh-contracts", "main", true},
{"a branch no source key holds", "novox/mesh-contracts", "other", false},
{"a repository the key does not name", "novox/other", "main", false},
} {
t.Run(c.name, func(t *testing.T) {
src := &memSource{vals: map[string]Source{"main": {Group: "g", Heads: map[string]string{"novox/mesh-contracts": old}}}}
var said []string
var logged []string
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{},
Logf: func(f string, a ...any) { logged = append(logged, f) },
SaySeat: func(subject string, _ any, id string) error { said = append(said, id); return nil }}
if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil {
t.Fatal(err)
}
got := src.vals["main"]
if c.moved != (got.Heads["novox/mesh-contracts"] == merge) || c.moved != (len(said) == 1) || (c.moved && got.Group != "") {
t.Fatalf("source %+v said %v: moved wanted %v", got, said, c.moved)
}
if c.moved && !strings.Contains(strings.Join(logged, "\n"), "not made by a delivery group of the gate") {
t.Errorf("a merge no group made is not said so: %v", logged)
}
// Heard again, the key names the head already: nothing is moved or said twice.
if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil || len(said) > 1 {
t.Errorf("heard twice: %v, said %v", err, said)
}
})
}
}
// Design 47 G A-464: at its start the gate compares each head with the forge's branch head and moves every one behind it; a head
// the forge cannot read is said and left; gate_source_follow does it for one repository after the hand-act log took it.
func TestTheStartAndTheToolMoveAHeadBehindTheForge(t *testing.T) {
old, now := strings.Repeat("a", 40), strings.Repeat("c", 40)
mk := func() (*Holder, *branchSource, *[]string) {
src := &branchSource{memSource{vals: map[string]Source{"main": {Heads: map[string]string{"o/f1": old, "o/f2": old, "o/f3": now}}}}}
var said []string
return &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {},
Forge: &fakeForge{heads: map[string]string{"o/f1@main": now, "o/f3@main": now}}, // o/f2 cannot be read
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}, src, &said
}
h, src, said := mk()
fw := &fakeFindings{}
h.Findings = fw
forge := h.Forge.(*fakeForge)
var pauses []time.Duration
var heldWhileWaiting int
prevSleep := startSleep
startSleep = func(d time.Duration) {
pauses = append(pauses, d)
heldWhileWaiting = len(fw.held) // the finding stands while a head is uncompared
forge.heads["o/f2@main"] = now // the seat git answers by the second pass
}
defer func() { startSleep = prevSleep }()
moved, err := h.FollowAtStart()
if err != nil || len(moved) != 2 || src.vals["main"].Heads["o/f1"] != now || src.vals["main"].Heads["o/f2"] != now || len(*said) != 2 {
t.Fatalf("start: moved %v err %v heads %v said %v: want o/f1 and, after the second pass, o/f2 moved", moved, err, src.vals["main"].Heads, *said)
}
if len(pauses) != 1 || heldWhileWaiting != 1 || len(fw.held) != 0 {
t.Fatalf("pauses %v, finding held while waiting %d and after %d: want one pause, the finding raised while a head stood uncompared and cleared after", pauses, heldWhileWaiting, len(fw.held))
}
h, src, said = mk()
acts := &handActsFake{refuse: errors.New("log down")}
if _, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts); err == nil || src.vals["main"].Heads["o/f1"] != old {
t.Fatalf("moved without the log: %v", err)
}
acts.refuse = nil
if _, err := h.SourceFollow("main", "o/f1", "", "me", acts); err == nil {
t.Fatal("moved with no reason")
}
out, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts)
if err != nil || out["moved"] != true || len(acts.took) != 1 || acts.took[0].Cause != "gate_source_follow" || src.vals["main"].Heads["o/f1"] != now {
t.Fatalf("tool: %v %v log %+v heads %v", out, err, acts.took, src.vals["main"].Heads)
}
}
// Design 47 G A-464: the gate takes the forge bridge's merged event of any repository, by the manifest's own pattern.
func TestTheGateTakesTheForgeBridgesMergedEventOfAnyRepository(t *testing.T) {
merge := strings.Repeat("b", 40)
data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-contracts", Base: "main", Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)})
if err != nil {
t.Fatal(err)
}
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/mesh-contracts": strings.Repeat("a", 40)}}}}
h := &Holder{Now: time.Now, Source: src, Forge: &fakeForge{}, Logf: func(string, ...any) {}}
key := strings.Replace(strings.Replace(forgeMergedEvent, "*", "novox", 1), "*", "mesh-contracts", 1)
if err := take(h, stdio.Envelope{Key: key, Body: data}); err != nil || src.vals["main"].Heads["novox/mesh-contracts"] != merge {
t.Fatalf("event %q: %v, source %+v", key, err, src.vals["main"])
}
}
// Design 47 G A-464c: a head moves only forward and only when it changes. A merged message for a commit that does not contain
// the held head (replayed, late, out of order) moves nothing and is said; a second message for the same head writes no
// revision; and a take-in.taken for a head a merged already moved writes none and says no source-moved.
func TestAHeadMovesOnlyForwardAndOnlyWhenItChanges(t *testing.T) {
oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40)
msg := func(merge string) []byte {
data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/app", Base: "main", Merge: merge, Head: merge, Number: 3, Root: strings.Repeat("d", 40)})
if err != nil {
t.Fatal(err)
}
return data
}
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}}
var said, logged []string
// newC contains oldC, not the other way round.
forge := &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }}
h := &Holder{Now: time.Now, Source: src, Forge: forge, Logf: func(f string, a ...any) { logged = append(logged, f) },
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}
if err := h.ForgeMerged(msg(oldC)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 || len(logged) == 0 {
t.Fatalf("a replayed old merge: %v, source %+v, revisions %d, said %v, logged %v: want nothing moved and a log line", err, src.vals["main"], src.rev, said, logged)
}
if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 0 || len(said) != 0 {
t.Fatalf("a second message for the same head wrote revision %d, said %v", src.rev, said)
}
// A take-in.taken naming the head a merged moved already writes no revision and says nothing.
body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` +
`"merges":[{"repository":"novox/app","number":3,"merge":"` + newC + `"}],"entries":[]}`)
if err := h.moveSource(mustTaken(t, body)); err != nil || src.rev != 0 || len(said) != 0 {
t.Fatalf("take-in for a head already moved: %v, revision %d, said %v", err, src.rev, said)
}
// A real forward move still writes one revision.
src.vals["main"] = Source{Heads: map[string]string{"novox/app": oldC}}
if err := h.ForgeMerged(msg(newC)); err != nil || src.rev != 1 || len(said) != 1 {
t.Fatalf("a forward move: %v, revision %d, said %v", err, src.rev, said)
}
}
func mustTaken(t *testing.T, body []byte) meshcatalog.TakeInTaken {
t.Helper()
var v meshcatalog.TakeInTaken
if err := json.Unmarshal(body, &v); err != nil {
t.Fatal(err)
}
return v
}
// Design 47 G A-464d: a take-in.taken arriving after a later merge moved the head leaves the head where it is.
func TestATakeInTakenAfterALaterMergeLeavesTheHead(t *testing.T) {
oldC, newC := strings.Repeat("a", 40), strings.Repeat("b", 40)
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/app": newC}}}}
var said []string
h := &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {},
Forge: &fakeForge{contains: func(commit, in string) bool { return commit == in || (commit == oldC && in == newC) }},
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}
body := []byte(`{"v":1,"kind":"taken","key":"g.feat","what":"group","group":"feat","branch":"main","source":7,` +
`"merges":[{"repository":"novox/app","number":3,"merge":"` + oldC + `"}],"entries":[]}`)
if err := h.moveSource(mustTaken(t, body)); err != nil || src.vals["main"].Heads["novox/app"] != newC || src.rev != 0 || len(said) != 0 {
t.Fatalf("take-in of an older merge: %v, source %+v, revision %d, said %v: want the head left at the later merge", err, src.vals["main"], src.rev, said)
}
}
// fakeFindings is the gate's finding writer in memory.
type fakeFindings struct{ held []meshconditions.Finding }
func (f *fakeFindings) Write(_ context.Context, x meshconditions.Finding) error {
f.held = append(f.held, x)
return nil
}
func (f *fakeFindings) Clear(_ context.Context, _, _, _, _ string) error { f.held = nil; return nil }
func (f *fakeFindings) Held() []meshconditions.Finding { return f.held }