Files
mesh-sdk/go/buscalls/buscalls_test.go
T
jschoubben cedfa71169 S4 ledger-forms: ends-with, ended-days, expires, a reindex naming an instance
Builds: design 47 H2 H-A-634 (3) (4), H-A-685 (1), H-A-690 (1)
2026-11-25 17:02:53 +01:00

55 lines
2.5 KiB
Go

package buscalls
import (
"encoding/json"
"testing"
"time"
)
// A-42: stream.publish is a method of its own, beside read and fill, and its request and answer keep their field names.
func TestStreamPublishIsAMethodWithItsShapes(t *testing.T) {
if StreamPublish != "mesh/stream.publish" || StreamPublish == StreamRead || StreamPublish == StreamFill {
t.Fatalf("method %q", StreamPublish)
}
b, _ := json.Marshal(StreamPublishRequest{Stream: "events", Event: "scrub.requested", Body: json.RawMessage(`{"a":1}`), ID: "x"})
if string(b) != `{"stream":"events","event":"scrub.requested","body":{"a":1},"id":"x"}` {
t.Errorf("request %s", b)
}
a, _ := json.Marshal(StreamPublished{Sequence: 7})
if string(a) != `{"sequence":7}` {
t.Errorf("answer %s", a)
}
}
// Design 47 H2 H-A-634 (4), H-A-690 (1): an instance's state and a change carry `expires` while it has ended, none
// while it has not; a reindex names an instance only when asked to.
func TestAnInstancesExpiresAndAReindexInstanceKeepTheirNames(t *testing.T) {
at := time.Date(2026, 12, 1, 10, 0, 0, 0, time.UTC)
for _, c := range []struct {
name string
v any
want string
}{
{"state ended", InstanceState{Instance: "record/a", State: json.RawMessage(`{}`), Sequence: 3, Expires: &at},
`{"instance":"record/a","state":{},"sequence":3,"expires":"2026-12-01T10:00:00Z"}`},
{"state open", InstanceState{Instance: "record/a", State: json.RawMessage(`{}`), Sequence: 3},
`{"instance":"record/a","state":{},"sequence":3}`},
{"change ended", InstanceChange{Watch: "w", Instance: "i", State: json.RawMessage(`{}`), Expires: &at},
`{"watch":"w","instance":"i","state":{},"sequence":0,"current":false,"expires":"2026-12-01T10:00:00Z"}`},
{"change open", InstanceChange{Watch: "w", Instance: "i", State: json.RawMessage(`{}`)},
`{"watch":"w","instance":"i","state":{},"sequence":0,"current":false}`},
{"reindex one", ReindexRequest{Ledger: "records", Index: "movers", Instance: "mover/x"},
`{"ledger":"records","index":"movers","instance":"mover/x"}`},
{"reindex whole", ReindexRequest{Ledger: "records", Index: "movers"}, `{"ledger":"records","index":"movers"}`},
} {
b, err := json.Marshal(c.v)
if err != nil || string(b) != c.want {
t.Errorf("%s: %s (%v), want %s", c.name, b, err, c.want)
}
}
var back InstanceState
if err := json.Unmarshal([]byte(`{"instance":"i","state":null,"sequence":1,"expires":"2026-12-01T10:00:00Z"}`), &back); err != nil || back.Expires == nil || !back.Expires.Equal(at) {
t.Errorf("expires read back as %+v (%v)", back.Expires, err)
}
}