Files
mesh-delivery/modules/mesh-delivery-forge-bridge/cmd/mesh-delivery-forge-bridge-tools/main_test.go
T
jschoubben 9fe6330d79 B4: the bridge's whole test says head-moved; bridge_heads reads the heads held
The bridge's whole test runs the program against an embedded bus as the node does: a start, a
merge, an admitted push to an unprotected branch and a restart after a move each give one
head-moved naming the head read through the seat git's branch and the head held before (start
true at a start); a head heard twice, a branch not watched and a restart at the head held give
none, and the bridge asks the seat git nothing but branch. Its bus permissions are composed for
the manifest as it now stands. The pull, open pull request, group and unprotected-branch tests
go; the owed test performs the gate's statuses once, after a restart and again by hand.

bridge_heads answers each key of the state heads with the head the bridge holds, read-only.

Builds: design 47 H §739 (how checked), §774; lead answers H-A-48, H-A-85 (1)

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 03:34:07 +01:00

130 lines
4.7 KiB
Go

package main
import (
"encoding/json"
"errors"
"reflect"
"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"
"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{{Of: seat.Seat, ID: "a", Outcome: forgemsg.OwedPerformed, At: at}}
var recorded []string
recordErr := error(nil)
record := func(what, why string) error { recorded = append(recorded, what+": "+why); return recordErr }
tools := map[string]func(map[string]any) (any, error){}
for _, tl := range OwedTools(ask, func() ([]OwedRow, error) { return rows, nil }, record, func() time.Time { return at }) {
tools[tl.Name] = tl.Run
}
out, err := tools[OwedTool](map[string]any{"of": seat.Seat})
if m, _ := out.(map[string]any); err != nil || len(m["performed"].([]OwedRow)) != 1 || len(m["backlog"].([]groups.Backlog)) != 1 {
t.Errorf("bridge_owed: %v %v", out, err)
}
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")
}
})
}