An owed act met by the forge away (401 while the store was replaced) ended could not be performed and nothing performed it again. Each act is now a kept call at the module's pace (kept-call-pace-first, kept-call-pace-max) until owed-away-bound (60 minutes), then ends could not be performed naming every try. Before each try the bridge reads the pull request through the seat git's pull: one closed unmerged ends owed.ended not-needed, never performed. The gate's owed event gains record (the delivery record's source revision), kept with the act in performed, and bridge_owed_again by record and why performs again every act of that record that ended could not be performed, written in the hand-act log first. Builds: design 47 H2 §807, §775, §819, H-A-432
227 lines
8.8 KiB
Go
227 lines
8.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/views"
|
|
|
|
"mesh-delivery-forge-bridge/internal/groups"
|
|
)
|
|
|
|
// memState is a state in memory as bridge_heads reads it.
|
|
type memState struct {
|
|
vals map[string]string
|
|
err error
|
|
}
|
|
|
|
func (m memState) Keys() ([]string, error) {
|
|
if m.err != nil {
|
|
return nil, m.err
|
|
}
|
|
var out []string
|
|
for k := range m.vals {
|
|
out = append(out, k)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (m memState) Get(key string) (*stdio.StateEntry, error) {
|
|
v, ok := m.vals[key]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
return &stdio.StateEntry{Value: []byte(v)}, nil
|
|
}
|
|
|
|
// Design 47 *What the gate owes the forge*: bridge_owed reads what the bridge performed and its consumer's backlog,
|
|
// bridge_owed_again and bridge_owed_drop write an ask and answer what the bridge answered, a drop naming why and
|
|
// recorded as a hand act first.
|
|
func TestTheBridgeTools(t *testing.T) {
|
|
// Design 47 H §739, H-A-48, H-A-85: bridge_heads answers each key of the state heads with its head, and says a state
|
|
// it cannot read, never none.
|
|
t.Run("bridge_heads", func(t *testing.T) {
|
|
const h1 = "1111111111111111111111111111111111111111"
|
|
for _, c := range []struct {
|
|
name string
|
|
state memState
|
|
want map[string]string
|
|
fails bool
|
|
}{
|
|
{"two branches held", memState{vals: map[string]string{"branch.novox.mesh-sdk.main": h1, "branch.novox.mesh-sdk.beta": h1}},
|
|
map[string]string{"branch.novox.mesh-sdk.main": h1, "branch.novox.mesh-sdk.beta": h1}, false},
|
|
{"none held", memState{vals: map[string]string{}}, map[string]string{}, false},
|
|
{"a state that cannot be listed", memState{err: errors.New("no responders")}, nil, true},
|
|
} {
|
|
var run func(map[string]any) (any, error)
|
|
for _, tl := range Tools(nil, c.state) {
|
|
if tl.Name == HeadsTool {
|
|
run = tl.Run
|
|
}
|
|
}
|
|
if run == nil {
|
|
t.Fatalf("no tool %s", HeadsTool)
|
|
}
|
|
out, err := run(map[string]any{})
|
|
if c.fails {
|
|
if err == nil {
|
|
t.Errorf("%s: %v, want an error", c.name, out)
|
|
}
|
|
continue
|
|
}
|
|
if m, _ := out.(map[string]any); err != nil || !reflect.DeepEqual(m["heads"], c.want) {
|
|
t.Errorf("%s: %v %v, want %v", c.name, out, err, c.want)
|
|
}
|
|
}
|
|
})
|
|
t.Run("the owed tools", func(t *testing.T) {
|
|
at := time.Date(2026, 11, 9, 21, 0, 0, 0, time.UTC)
|
|
var asked []groups.OwedAsk
|
|
ask := func(key string, value []byte) ([]byte, error) {
|
|
a, err := groups.DecodeOwedAsk(value)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
asked = append(asked, a)
|
|
a.Answered, a.Outcome, a.Answer = at, forgemsg.OwedPerformed, "said as owed.ended: performed"
|
|
if a.Kind == groups.OwedBacklog {
|
|
a.Backlog = []groups.Backlog{{Stream: seat.StreamOwed, Waiting: 1}}
|
|
}
|
|
return json.Marshal(a)
|
|
}
|
|
rows := []OwedRow{
|
|
{OwedEnded: forgemsg.OwedEnded{Of: seat.Seat, ID: "a", Outcome: forgemsg.OwedPerformed, At: at}, Repository: "novox/mesh-sdk", Number: 178},
|
|
{OwedEnded: forgemsg.OwedEnded{Of: seat.Seat, ID: "b", Outcome: forgemsg.OwedPerformed, At: at}, Repository: "novox/mesh-sdk", Number: 179}}
|
|
var filters []OwedFilter
|
|
performed := func(f OwedFilter) ([]OwedRow, error) {
|
|
filters = append(filters, f)
|
|
var out []OwedRow
|
|
for _, r := range rows {
|
|
if f.holds(r) {
|
|
out = append(out, r)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
var recorded []string
|
|
recordErr := error(nil)
|
|
record := func(what, why, cause string) error {
|
|
recorded = append(recorded, what+": "+why+" ("+cause+")")
|
|
return recordErr
|
|
}
|
|
tools := map[string]func(map[string]any) (any, error){}
|
|
for _, tl := range OwedTools(ask, performed, record, func() time.Time { return at }) {
|
|
tools[tl.Name] = tl.Run
|
|
}
|
|
// bridge_owed reads one act or one pull request's acts, never the whole state (H-A-181).
|
|
for _, c := range []struct {
|
|
name string
|
|
args map[string]any
|
|
ids []string
|
|
}{
|
|
{"one pull request", map[string]any{"repository": "novox/mesh-sdk", "number": float64(179)}, []string{"b"}},
|
|
{"one act", map[string]any{"of": seat.Seat, "id": "a"}, []string{"a"}},
|
|
{"the whole state", map[string]any{"of": seat.Seat}, nil},
|
|
{"a repository with no number", map[string]any{"repository": "novox/mesh-sdk"}, nil},
|
|
} {
|
|
n := len(filters)
|
|
out, err := tools[OwedTool](c.args)
|
|
if c.ids == nil {
|
|
if err == nil || len(filters) != n {
|
|
t.Errorf("bridge_owed of %s: %v %v, read %d; want refused, nothing read", c.name, out, err, len(filters)-n)
|
|
}
|
|
continue
|
|
}
|
|
m, _ := out.(map[string]any)
|
|
got, _ := m["performed"].([]OwedRow)
|
|
var ids []string
|
|
for _, r := range got {
|
|
ids = append(ids, r.ID)
|
|
}
|
|
if err != nil || !reflect.DeepEqual(ids, c.ids) || len(m["backlog"].([]groups.Backlog)) != 1 {
|
|
t.Errorf("bridge_owed of %s: %v %v, want %v", c.name, out, err, c.ids)
|
|
}
|
|
}
|
|
if out, err := tools[OwedAgainTool](map[string]any{"of": seat.Seat, "id": "a"}); err != nil || out.(map[string]any)["outcome"] != forgemsg.OwedPerformed ||
|
|
asked[len(asked)-1].Kind != groups.OwedAgain {
|
|
t.Errorf("bridge_owed_again: %v %v", out, err)
|
|
}
|
|
if _, err := tools[OwedDropTool](map[string]any{"of": seat.Seat, "id": "a"}); err == nil || len(recorded) != 0 {
|
|
t.Errorf("a drop with no why: %v, recorded %v", err, recorded)
|
|
}
|
|
n := len(asked)
|
|
recordErr = errors.New("mesh-hand-acts has no holder that answers")
|
|
if _, err := tools[OwedDropTool](map[string]any{"of": seat.Seat, "id": "a", "why": "merged by hand"}); err == nil || len(asked) != n {
|
|
t.Errorf("a drop whose hand act was not recorded: %v, asked %d", err, len(asked)-n)
|
|
}
|
|
recordErr = nil
|
|
if _, err := tools[OwedDropTool](map[string]any{"of": seat.Seat, "id": "a", "why": "merged by hand"}); err != nil || asked[len(asked)-1].Why != "merged by hand" {
|
|
t.Errorf("bridge_owed_drop: %v %+v", err, asked)
|
|
}
|
|
if _, _, err := groups.NewOwedAsk(groups.OwedAgain, "", "a", "", "", at); err == nil {
|
|
t.Error("an ask naming no seat was taken")
|
|
}
|
|
// Design 47 H2 §807 (H-A-432 (1)): bridge_owed_again by record asks the bridge for every act of one delivery record,
|
|
// written in the hand-act log first; with no why, or with an act named beside it, nothing is asked.
|
|
n, r := len(asked), len(recorded)
|
|
for _, bad := range []map[string]any{{"record": "main.212"}, {"record": "main.212", "of": seat.Seat, "id": "a", "why": "x"}} {
|
|
if _, err := tools[OwedAgainTool](bad); err == nil || len(asked) != n || len(recorded) != r {
|
|
t.Errorf("bridge_owed_again %v: %v, asked %d; want refused, nothing asked", bad, err, len(asked)-n)
|
|
}
|
|
}
|
|
if out, err := tools[OwedAgainTool](map[string]any{"record": "main.212", "why": "the forge was away"}); err != nil ||
|
|
len(asked) != n+1 || asked[n].Record != "main.212" || asked[n].Kind != groups.OwedAgain || asked[n].ID != "" ||
|
|
len(recorded) != r+1 || !strings.HasSuffix(recorded[r], "(owed-again)") {
|
|
t.Errorf("bridge_owed_again by record: %v %v, asked %+v, recorded %v", out, err, asked[n:], recorded[r:])
|
|
}
|
|
})
|
|
// Design 47 H2 §812 (H-A-380 (3), H-A-433): the bridge serves the two prune tools over its state performed, named for
|
|
// its module, each written in the hand-act log before it acts.
|
|
t.Run("the prune tools over performed", func(t *testing.T) {
|
|
p := &prunable{}
|
|
var recorded []views.HandAct
|
|
tools := map[string]stdio.Tool{}
|
|
for _, tool := range PerformedPruneTools(p, handActs(func(a views.HandAct) error { recorded = append(recorded, a); return nil })) {
|
|
tools[tool.Name] = tool
|
|
}
|
|
now, keep := tools["mesh-delivery-forge-bridge_state_prune_now"], tools["mesh-delivery-forge-bridge_state_keep"]
|
|
if len(tools) != 2 || now.Run == nil || keep.Run == nil {
|
|
t.Fatalf("the prune tools are %v, want mesh-delivery-forge-bridge_state_prune_now and _state_keep", tools)
|
|
}
|
|
until := time.Now().Add(time.Hour).UTC().Format(time.RFC3339)
|
|
if _, err := now.Run(map[string]any{"entry": "performed", "why": "full"}); err != nil || p.pruned != 1 {
|
|
t.Errorf("prune now: %v, pruned %d times", err, p.pruned)
|
|
}
|
|
if _, err := keep.Run(map[string]any{"entry": "performed", "key": "mesh-delivery.YQ", "until": until, "why": "read it"}); err != nil || p.kept != "mesh-delivery.YQ" {
|
|
t.Errorf("keep: %v, kept %q", err, p.kept)
|
|
}
|
|
if len(recorded) != 2 {
|
|
t.Errorf("hand acts %+v, want the prune and the keep", recorded)
|
|
}
|
|
})
|
|
}
|
|
|
|
// prunable is a pruned state in memory.
|
|
type prunable struct {
|
|
pruned int
|
|
kept string
|
|
}
|
|
|
|
func (p *prunable) PruneNow() (stdio.Pruned, error) {
|
|
p.pruned++
|
|
return stdio.Pruned{State: "performed"}, nil
|
|
}
|
|
func (p *prunable) Keep(key string, _ time.Time) error { p.kept = key; return nil }
|
|
|
|
// handActs records a hand act through record.
|
|
type handActs func(views.HandAct) error
|
|
|
|
func (h handActs) RecordHandAct(a views.HandAct) error { return h(a) }
|