Files
mesh-catalog/modules/mesh-recovery/internal/recovery/points_test.go
T

126 lines
4.5 KiB
Go

package recovery_test
import (
"strings"
"testing"
"time"
seat "git.novox.be/novox/mesh-contracts/go/seats/meshrecovery"
"mesh-recovery/internal/recovery"
)
func takeAct() recovery.Act {
return recovery.Act{Kind: recovery.KindTake, Module: "app", Kept: "notes", Form: 2, Fingerprint: "fp1", Why: "form change", Caller: "node.engine"}
}
// A point is taken stream by stream under a temporary name, renamed whole, its digests put on the bus before `ended` is
// said under the act's id with the point whole, and the act is written in the hand-act log with the caller the bus proved
// (design 47 G §516, §554, §560, §567).
func TestATakenPointIsListedWithItsDigestsAndEndedUnderItsAct(t *testing.T) {
r := newRig(t)
ans := r.do(takeAct())
if ans.Accepted == "" {
t.Fatalf("answer %+v", ans)
}
ended := r.s.named(seat.EventEnded)
if len(ended) != 1 {
t.Fatalf("events %+v", r.s.events)
}
ev := ended[0].Body.(seat.EndedEvent)
if ev.Act != ans.Accepted || ev.Outcome != "done" || len(ev.Digests) != 2 || ev.Size == 0 || ev.Snapshot != "app/notes/2-fp1" {
t.Errorf("ended %+v", ev)
}
if got := strings.Join(ended[0].Parts, "."); got != "app.notes" {
t.Errorf("parts %q", got)
}
if len(ev.Consumers) != 1 || ev.Consumers[0].Sequence != 7 {
t.Errorf("consumers %+v", ev.Consumers)
}
pts, err := r.e.Points()
if err != nil || len(pts) != 1 || pts[0].Snapshot != ev.Snapshot {
t.Fatalf("points %+v %v", pts, err)
}
// A-230: the point says the bus names it took, one per bus object, as its digests name them.
if len(pts[0].Names) != 2 || len(pts[0].Digests) != 2 || pts[0].Names[0] != pts[0].Digests[0].Name || pts[0].Names[1] != pts[0].Digests[1].Name {
t.Errorf("names %v for digests %+v", pts[0].Names, pts[0].Digests)
}
if _, ok, _ := r.d.Get("app/notes/2-fp1"); !ok {
t.Errorf("no digests on the bus: %v", r.d.m)
}
if len(r.s.handActs) != 1 || !strings.HasPrefix(r.s.handActs[0], "node.engine||take app notes|form change") {
t.Errorf("hand acts %v", r.s.handActs)
}
}
// One act at a time and no queue: a second ask while one runs is answered busy with the running act (§554).
func TestASecondAskWhileOneRunsIsAnsweredBusy(t *testing.T) {
r := newRig(t)
gate := make(chan struct{})
r.e.Dump = nil
r.e.Bus = &gatedBus{fakeBus: r.b, gate: gate}
first := r.e.Accept(takeAct())
second := r.e.Accept(takeAct())
close(gate)
r.e.Wait()
if first.Accepted == "" || second.Busy != first.Accepted || second.Accepted != "" || second.Error != "" {
t.Errorf("first %+v second %+v", first, second)
}
}
// An act whose hand-act log write fails does nothing and answers an error naming the log (§554).
func TestAnActWhoseHandActLogWriteFailsDoesNothing(t *testing.T) {
r := newRig(t)
r.s.handFail = errTest
ans := r.do(takeAct())
if !strings.Contains(ans.Error, "hand-act log") || ans.Accepted != "" || len(r.s.events) != 0 {
t.Errorf("answer %+v events %+v", ans, r.s.events)
}
if pts, _ := r.e.Points(); len(pts) != 0 {
t.Errorf("points %+v", pts)
}
}
// An act that would pass the cap ends failed naming it, before anything is written (§564).
func TestAnActThatWouldPassTheCapEndsFailedBeforeItWritesAnything(t *testing.T) {
r := newRig(t)
r.e.Settings.Cap = 4
r.do(takeAct())
ev := r.s.named(seat.EventEnded)[0].Body.(seat.EndedEvent)
if ev.Outcome != "failed" || !strings.HasPrefix(ev.Error, recovery.ErrCap) {
t.Errorf("ended %+v", ev)
}
if pts, _ := r.e.Points(); len(pts) != 0 || len(r.d.m) != 0 {
t.Errorf("points %+v digests %v", pts, r.d.m)
}
}
// A point older than the keep days is removed and said, the newest of its kept data stays however old, and the run says
// expiry-ended with the count, 0 included (§551, §560).
func TestExpiryRemovesOldPointsButNeverTheNewest(t *testing.T) {
r := newRig(t)
r.t = time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)
r.do(takeAct())
a := takeAct()
a.Fingerprint, a.Form = "fp2", 3
r.t = time.Date(2026, 7, 1, 0, 0, 0, 0, time.UTC)
r.do(a)
r.s.events = nil
r.t = time.Date(2026, 11, 12, 0, 0, 0, 0, time.UTC)
r.do(recovery.Act{Kind: recovery.KindExpire, Why: "by hand", Caller: "tools"})
pts, _ := r.e.Points()
if len(pts) != 1 || pts[0].Fingerprint != "fp2" {
t.Fatalf("points %+v", pts)
}
rem := r.s.named(seat.EventPointRemoved)
if len(rem) != 1 || rem[0].Body.(seat.PointRemovedEvent).Fingerprint != "fp1" {
t.Errorf("removed %+v", rem)
}
if ex := r.s.named(seat.EventExpiryEnded); len(ex) != 1 || ex[0].Body.(seat.ExpiryEndedEvent).Removed != 1 {
t.Errorf("expiry %+v", ex)
}
if _, ok, _ := r.d.Get("app/notes/2-fp1"); ok {
t.Errorf("its digests stay on the bus")
}
}