The delivery repository's contracts and SDK live under modules/ as mesh-delivery-contracts and mesh-delivery-sdk, their Go module paths renamed to match the folders (the TypeScript names already did), and every importer in mesh-delivery, mesh-catalog, mesh-host, mesh-tools and mesh-sdk follows in the same group: a design as it should be, one path per module, checked by the group's check building every importer. Builds: design 47 G §427, §88 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
255 lines
12 KiB
Go
255 lines
12 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"log"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshdeliveryclient"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
checker "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdeliverychecker"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
|
|
|
"mesh-delivery-forge-bridge/internal/gitfixture"
|
|
"mesh-delivery-forge-bridge/internal/groups"
|
|
)
|
|
|
|
const (
|
|
headA = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
headB = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
|
|
)
|
|
|
|
type memStrs map[string]string
|
|
|
|
func (m memStrs) Get(_ context.Context, key string) (string, bool, error) {
|
|
r, ok := m[key]
|
|
return r, ok, nil
|
|
}
|
|
func (m memStrs) Put(_ context.Context, key, v string) error { m[key] = v; return nil }
|
|
|
|
type memPerformed map[string][]byte
|
|
|
|
func (m memPerformed) Get(_ context.Context, key string) ([]byte, bool, error) {
|
|
v, ok := m[key]
|
|
return v, ok, nil
|
|
}
|
|
func (m memPerformed) Put(_ context.Context, key string, v []byte) error { m[key] = v; return nil }
|
|
|
|
// seatFake answers each seat verb from answers, by its key; one absent is not answered (an error, as a bound passed).
|
|
type seatFake struct {
|
|
calls []string
|
|
answers map[string]json.RawMessage
|
|
}
|
|
|
|
func (f *seatFake) call(_ context.Context, key string, _ any) (json.RawMessage, error) {
|
|
f.calls = append(f.calls, key)
|
|
if a, ok := f.answers[key]; ok {
|
|
return a, nil
|
|
}
|
|
return nil, errors.New("nats: timeout")
|
|
}
|
|
|
|
func newOwing(f *seatFake) (*owing, *fakePub, memPerformed) {
|
|
pub, kept := &fakePub{}, memPerformed{}
|
|
at := time.Date(2026, 11, 9, 21, 0, 0, 0, time.UTC)
|
|
return &owing{call: f.call, pub: pub, performed: kept, now: func() time.Time { return at }, logger: log.New(&bytes.Buffer{}, "", 0)}, pub, kept
|
|
}
|
|
|
|
// Design 47 *What the gate owes the forge*: an owed act is the seat git's verb under its id, its outcome said as
|
|
// owed.ended on the stream forge before it is kept in performed, an act it cannot read said undecodable; and at its start
|
|
// the bridge performs from the seat's groups every act said and not answered.
|
|
func TestWhatIsOwedIsPerformedSaidAndKept(t *testing.T) {
|
|
t.Run("the names are the manifest's", theNamesAreTheManifests)
|
|
gate := func(name string) []byte { return gitfixture.Seat(t, seat.Seat, "events", name, "event:owed")[0] }
|
|
answer := func(verb, name string) json.RawMessage { return gitfixture.Given(t, name, "verb:"+verb+".answer") }
|
|
for _, c := range []struct {
|
|
name, of string
|
|
data []byte
|
|
answers map[string]json.RawMessage
|
|
pubErr error
|
|
call string
|
|
outcome string
|
|
kept bool
|
|
err string
|
|
}{
|
|
{"a merge performed", seat.Seat, gate("owed is said, a merge"),
|
|
map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers")}, nil,
|
|
"seat:git.merge-pull", forgemsg.OwedPerformed, true, ""},
|
|
{"a status refused", seat.Seat, gate("owed is said, a status"),
|
|
map[string]json.RawMessage{"seat:git.status": answer("status", "status answers an error, never none")}, nil,
|
|
"seat:git.status", forgemsg.OwedRefused, true, ""},
|
|
{"the checker's status not answered", checker.Seat, gitfixture.Seat(t, checker.Seat, "events", "owed is said", "event:owed")[0],
|
|
nil, nil, "seat:git.status", forgemsg.OwedCouldNotBePerformed, true, ""},
|
|
{"an act it cannot read", seat.Seat, []byte(`{`), nil, nil, "", forgemsg.OwedUndecodable, false, ""},
|
|
{"a seat that says no owed act", "mesh-wants", gate("owed is said, a merge"), nil, nil, "", forgemsg.OwedUndecodable, false, ""},
|
|
{"the stream forge refusing the outcome", seat.Seat, gate("owed is said, a merge"),
|
|
map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers")}, errors.New("no responders"),
|
|
"seat:git.merge-pull", "", false, "could not be said"},
|
|
} {
|
|
f := &seatFake{answers: c.answers}
|
|
o, pub, kept := newOwing(f)
|
|
pub.err = c.pubErr
|
|
_, err := o.heard(context.Background(), c.of, c.data)
|
|
if c.err != "" {
|
|
if err == nil || !strings.Contains(err.Error(), c.err) || len(kept) != 0 {
|
|
t.Errorf("%s: %v, kept %d; want an error naming %q and nothing kept", c.name, err, len(kept), c.err)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil || len(pub.data) != 1 || pub.subjects[0] != forgeSubject(forgemsg.OwedEndedEvent) {
|
|
t.Fatalf("%s: %v, said %v; want one owed.ended", c.name, err, pub.subjects)
|
|
}
|
|
e, err := forgemsg.DecodeOwedEnded(pub.data[0])
|
|
if err != nil || e.Outcome != c.outcome || e.Of != c.of {
|
|
t.Errorf("%s: said %+v %v, want %s of %s", c.name, e, err, c.outcome, c.of)
|
|
}
|
|
if (c.call == "") != (len(f.calls) == 0) || (c.call != "" && f.calls[0] != c.call) {
|
|
t.Errorf("%s: called %v, want %q", c.name, f.calls, c.call)
|
|
}
|
|
if v, ok := kept[performedKey(c.of, e.ID)]; ok != c.kept {
|
|
t.Errorf("%s: kept %v (%s), want %v", c.name, ok, v, c.kept)
|
|
} else if ok {
|
|
var p Performed
|
|
if err := json.Unmarshal(v, &p); err != nil || p.Ended.Outcome != c.outcome || !json.Valid(p.Owed) {
|
|
t.Errorf("%s: kept %s, want the outcome and the owed event", c.name, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Design 47 *The switch carries what is owed*: an act the gate switched from its old state is performed only when the old
|
|
// bridge's own bookkeeping does not name its key at its revision or later; one it names is said performed, not done.
|
|
t.Run("a switched act the old bridge performed", func(t *testing.T) {
|
|
var owed map[string]any
|
|
if err := json.Unmarshal(gate("owed is said, a merge"), &owed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
id, err := meshdeliveryclient.SwitchedActID(meshdeliveryclient.SwitchedMerges, "feat~x", 7)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
owed["id"] = id
|
|
data, _ := json.Marshal(owed)
|
|
for _, c := range []struct {
|
|
name string
|
|
kept string // the old bridge's merges.feat~x, "" for none
|
|
called bool
|
|
}{
|
|
{"kept at its revision", "7", false},
|
|
{"kept at a later revision", "9", false},
|
|
{"kept at an earlier revision", "6", true},
|
|
{"not kept", "", true},
|
|
} {
|
|
f := &seatFake{answers: map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers")}}
|
|
o, pub, kept := newOwing(f)
|
|
if c.kept != "" {
|
|
kept["merges.feat~x"] = []byte(c.kept)
|
|
}
|
|
if _, err := o.heard(context.Background(), seat.Seat, data); err != nil || len(pub.data) != 1 {
|
|
t.Fatalf("%s: %v, said %d", c.name, err, len(pub.data))
|
|
}
|
|
e, _ := forgemsg.DecodeOwedEnded(pub.data[0])
|
|
if (len(f.calls) == 1) != c.called || e.Outcome != forgemsg.OwedPerformed || e.ID != id {
|
|
t.Errorf("%s: called %v, said %+v; want called %v and performed", c.name, f.calls, e, c.called)
|
|
}
|
|
if _, ok := kept[performedKey(seat.Seat, id)]; !ok {
|
|
t.Errorf("%s: the outcome is not kept under the act's id", c.name)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("heard from a seat's stream owed", func(t *testing.T) {
|
|
f := &seatFake{answers: map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers")}}
|
|
o, pub, _ := newOwing(f)
|
|
m := &fakeMsg{subject: "mesh.seat.mesh-delivery.event.owed.novox/mesh-sdk.178", data: gate("owed is said, a merge")}
|
|
o.hear(context.Background(), seat.Seat, m)
|
|
again := &fakeMsg{subject: m.subject, data: m.data}
|
|
o.hear(context.Background(), seat.Seat, again)
|
|
if m.acked != 1 || again.acked != 1 || len(f.calls) != 1 || len(pub.data) != 1 {
|
|
t.Errorf("an act said twice: acked %d and %d, called %v, said %d; want performed and said once, both acknowledged",
|
|
m.acked, again.acked, f.calls, len(pub.data))
|
|
}
|
|
pub.err = errors.New("no responders")
|
|
lost := &fakeMsg{subject: m.subject, data: gate("owed is said, a status")}
|
|
o.hear(context.Background(), seat.Seat, lost)
|
|
if lost.acked != 0 || len(lost.nak) != 1 {
|
|
t.Errorf("an outcome not said: acked %d, nak %v; want it heard again", lost.acked, lost.nak)
|
|
}
|
|
})
|
|
|
|
t.Run("asked again, dropped, and its backlog read by hand", func(t *testing.T) {
|
|
groupsAnswer := gitfixture.Seat(t, seat.Seat, "verbs", "groups answers a group with its members and acts", "verb:groups.answer")[0]
|
|
f := &seatFake{answers: map[string]json.RawMessage{"seat:git.merge-pull": answer("merge-pull", "merge-pull answers"),
|
|
"seat:mesh-delivery.groups": groupsAnswer, "seat:git.status": answer("status", "status answers")}}
|
|
o, pub, kept := newOwing(f)
|
|
merge := "merge.feat=47-G.0123456789ab.novox/mesh-sdk#178"
|
|
if _, err := o.heard(context.Background(), seat.Seat, gate("owed is said, a merge")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
backlog := func(context.Context) []groups.Backlog { return []groups.Backlog{{Stream: seat.StreamOwed, Waiting: 2}} }
|
|
for _, c := range []struct {
|
|
name string
|
|
ask groups.OwedAsk
|
|
calls, said int
|
|
outcome, answers string
|
|
}{
|
|
{"again, from performed", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: merge}, 2, 2, forgemsg.OwedPerformed, "performed"},
|
|
// The fixture's status is in the groups only: the bridge never heard it.
|
|
{"again, from the groups", groups.OwedAsk{Kind: groups.OwedAgain, Of: seat.Seat, ID: "status.novox/mesh-sdk#178.aaaaaaaaaaaa.mesh/merge-gate"}, 4, 3, forgemsg.OwedPerformed, "performed"},
|
|
{"dropped", groups.OwedAsk{Kind: groups.OwedDrop, Of: seat.Seat, ID: merge, Why: "merged by hand", By: "lead@shanks"}, 4, 4, forgemsg.OwedDropped, "merged by hand"},
|
|
{"again, an act nobody holds", groups.OwedAsk{Kind: groups.OwedAgain, Of: checker.Seat, ID: "x"}, 4, 4, "", "checker says"},
|
|
{"the backlog", groups.OwedAsk{Kind: groups.OwedBacklog}, 4, 4, "", "backlog"},
|
|
} {
|
|
a := o.answer(context.Background(), c.ask, backlog)
|
|
if !a.Done() || a.Outcome != c.outcome || !strings.Contains(a.Answer, c.answers) || len(f.calls) != c.calls || len(pub.data) != c.said {
|
|
t.Errorf("%s: %+v, called %v, said %d; want %q naming %q, %d calls, %d said", c.name, a, f.calls, len(pub.data), c.outcome, c.answers, c.calls, c.said)
|
|
}
|
|
if c.ask.Kind == groups.OwedBacklog && (len(a.Backlog) != 1 || a.Backlog[0].Waiting != 2) {
|
|
t.Errorf("%s: %+v", c.name, a.Backlog)
|
|
}
|
|
}
|
|
var p Performed
|
|
if err := json.Unmarshal(kept[performedKey(seat.Seat, merge)], &p); err != nil || p.Ended.Outcome != forgemsg.OwedDropped {
|
|
t.Errorf("performed after the drop: %+v %v", p, err)
|
|
}
|
|
})
|
|
|
|
t.Run("caught up from the groups", func(t *testing.T) {
|
|
groups := gitfixture.Seat(t, seat.Seat, "verbs", "groups answers a group with its members and acts", "verb:groups.answer")[0]
|
|
f := &seatFake{answers: map[string]json.RawMessage{"seat:mesh-delivery.groups": groups,
|
|
"seat:git.status": answer("status", "status answers")}}
|
|
o, pub, kept := newOwing(f)
|
|
// The fixture's merge has its outcome recorded; its status is said and unanswered.
|
|
if n, err := o.catchUp(context.Background()); err != nil || n != 1 || len(pub.data) != 1 || f.calls[1] != "seat:git.status" {
|
|
t.Fatalf("catch-up: %d %v, called %v; want the one unanswered status performed", n, err, f.calls)
|
|
}
|
|
if n, err := o.catchUp(context.Background()); err != nil || n != 0 || len(f.calls) != 3 {
|
|
t.Errorf("catch-up again: %d %v, called %v; want nothing performed that performed holds", n, err, f.calls)
|
|
}
|
|
if len(kept) != 1 {
|
|
t.Errorf("kept %v, want the status", kept)
|
|
}
|
|
// The seat answers a group it does not know with no groups, never an error (W11.10): nothing is performed and
|
|
// nothing fails; a groups the seat leaves unanswered is the catch-up's error.
|
|
none := gitfixture.Seat(t, seat.Seat, "verbs", "groups answers a group it does not know with no groups", "verb:groups.answer")[0]
|
|
for _, row := range []struct {
|
|
answer json.RawMessage
|
|
fails bool
|
|
}{{none, false}, {nil, true}} {
|
|
f := &seatFake{answers: map[string]json.RawMessage{}}
|
|
if row.answer != nil {
|
|
f.answers["seat:mesh-delivery.groups"] = row.answer
|
|
}
|
|
o, pub, _ := newOwing(f)
|
|
n, err := o.catchUp(context.Background())
|
|
if (err != nil) != row.fails || n != 0 || len(pub.data) != 0 {
|
|
t.Errorf("catch-up from %s: %d performed, %v; want none performed, failing %v", row.answer, n, err, row.fails)
|
|
}
|
|
}
|
|
})
|
|
}
|