The state performed is a requires entry, so the bridge reaches it by mesh-sdk's process.Binding: the seat mesh-broker's show asked at the start as a kept call and again on its recreated and dropped, the bucket opened by the name show answers. Until it answers, nothing owed is performed and the bridge says it waits, naming the entry. The old name <module>_performed goes. mesh-sdk's Binding is cherry-picked on feat/47-G-fix11-b3c from feat/47-G-fix11-a7 (fa0a107). Builds: design 47 G §577 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
62 lines
2.6 KiB
Go
62 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
|
|
"mesh-delivery-forge-bridge/internal/gitfixture"
|
|
)
|
|
|
|
// openerFake is a bus that opens no bucket and records each name asked.
|
|
type openerFake struct{ asked []string }
|
|
|
|
func (o *openerFake) KeyValue(_ context.Context, bucket string) (jetstream.KeyValue, error) {
|
|
o.asked = append(o.asked, bucket)
|
|
return nil, errors.New("no bucket in a unit test")
|
|
}
|
|
|
|
// performedIsOpenedByItsBinding: design 47 G §577, unit (forge bridge): the state performed is opened by the bus name
|
|
// the seat mesh-broker's show answers for its local name, never by an old name; until show answers no bucket is opened,
|
|
// the use names the entry, and nothing owed is performed.
|
|
func performedIsOpenedByItsBinding(t *testing.T) {
|
|
var shows []string
|
|
answered := false
|
|
show := func(key string, body any) (json.RawMessage, error) {
|
|
b, _ := json.Marshal(body)
|
|
shows = append(shows, key+" "+string(b))
|
|
if !answered {
|
|
return nil, errors.New("nats: no responders available for request")
|
|
}
|
|
return json.RawMessage(`{"state":"created","binding":{"local":"performed","storage":"key-value","bucket":"bound-performed"}}`), nil
|
|
}
|
|
binding := process.NewBinding(brokerSeat, forgeBridgeModule, show, nil, performedLocal)
|
|
bus := &openerFake{}
|
|
kv := &boundBucket{binding: binding, local: performedLocal, js: bus}
|
|
binding.Ask(performedLocal)
|
|
if _, err := kv.Get(context.Background(), "k"); err == nil || !strings.Contains(err.Error(), `"performed"`) || len(bus.asked) != 0 {
|
|
t.Errorf("unbound: %v, opened %v; want an error naming the entry and no bucket opened", err, bus.asked)
|
|
}
|
|
f := &seatFake{}
|
|
o, pub, kept := newOwing(f)
|
|
o.waits = binding.Waiting
|
|
gate := gitfixture.Seat(t, seat.Seat, "events", "owed is said, a merge", "event:owed")[0]
|
|
if _, err := o.heard(context.Background(), seat.Seat, gate); err == nil || len(f.calls) != 0 || len(pub.data) != 0 || len(kept) != 0 {
|
|
t.Errorf("unbound: %v, called %v, said %d; want the act left unperformed and unsaid", err, f.calls, len(pub.data))
|
|
}
|
|
answered = true
|
|
binding.Ask(performedLocal)
|
|
_, _ = kv.Get(context.Background(), "k")
|
|
want := `seat:mesh-broker.show {"local-name":"performed","module":"mesh-delivery-forge-bridge"}`
|
|
if len(bus.asked) != 1 || bus.asked[0] != "bound-performed" || shows[len(shows)-1] != want {
|
|
t.Errorf("bound: opened %v after %v; want bound-performed, asked as %s", bus.asked, shows, want)
|
|
}
|
|
}
|