The seat generator gives every value, event and answer the wire field form (left out while 1) and a tolerant decoder answering the form it read; each contract carries its forms and a digest of its fields, and the compare fails a definition whose fields changed while its form did not. Every delivery seat is regenerated, and the readers take the form. Builds: design 47 H2 §780; H-A-352
148 lines
6.2 KiB
Go
148 lines
6.2 KiB
Go
package meshdeliveryclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// sameJSON says whether two documents hold the same values (the generated types write their fields in alphabetical
|
|
// order, the old package's in declaration order: the bytes differ, the documents do not).
|
|
func sameJSON(t *testing.T, got []byte, want string) {
|
|
t.Helper()
|
|
var g, w any
|
|
if err := json.Unmarshal(got, &g); err != nil {
|
|
t.Fatalf("%s: %v", got, err)
|
|
}
|
|
if err := json.Unmarshal([]byte(want), &w); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(g, w) {
|
|
t.Errorf("got %s\nwant %s", got, want)
|
|
}
|
|
}
|
|
|
|
// The goldens are the output of go/delivery's Encode* before it was deleted; each value must encode to the same
|
|
// document and decode back to what it was.
|
|
func TestGateValuesEncodeAsGoDeliveryDid(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name string
|
|
encode func() ([]byte, error)
|
|
decode func([]byte) (any, error)
|
|
value any
|
|
golden string
|
|
}{
|
|
{"source", func() ([]byte, error) {
|
|
return EncodeSource(seat.SourceByBranchValue{Source: 1044, Call: "0123456789abcdef", Heads: map[string]string{"novox/a": "aaaa", "novox/b": "bbbb"}})
|
|
}, func(b []byte) (any, error) { return DecodeSource(b) },
|
|
seat.SourceByBranchValue{V: 1, Source: 1044, Call: "0123456789abcdef", Heads: map[string]string{"novox/a": "aaaa", "novox/b": "bbbb"}},
|
|
`{"v":1,"source":1044,"call":"0123456789abcdef","heads":{"novox/a":"aaaa","novox/b":"bbbb"}}`},
|
|
{"owed", func() ([]byte, error) {
|
|
return EncodeOwed(seat.OwedByRepositoryNumberHeadActValue{Act: OwedStatus, Context: StatusContextCheck, Target: "t", State: "pending"})
|
|
}, func(b []byte) (any, error) { return DecodeOwed(b) },
|
|
seat.OwedByRepositoryNumberHeadActValue{V: 1, Act: OwedStatus, Context: StatusContextCheck, Target: "t", State: "pending"},
|
|
`{"v":1,"act":"status","context":"mesh/check","target":"t","state":"pending"}`},
|
|
{"answer", func() ([]byte, error) { return EncodeAnswer(Answer{Option: "merge", Warrant: "w", Words: "ok"}) },
|
|
func(b []byte) (any, error) { return DecodeAnswer(b) }, Answer{V: 1, Option: "merge", Warrant: "w", Words: "ok"},
|
|
`{"v":1,"option":"merge","warrant":"w","words":"ok"}`},
|
|
} {
|
|
raw, err := c.encode()
|
|
if err != nil {
|
|
t.Fatalf("%s: %v", c.name, err)
|
|
}
|
|
sameJSON(t, raw, c.golden)
|
|
back, err := c.decode([]byte(c.golden))
|
|
if err != nil || !reflect.DeepEqual(back, c.value) {
|
|
t.Errorf("%s decoded %+v, %v; want %+v", c.name, back, err, c.value)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGateKeysAsGoDeliveryHadThem(t *testing.T) {
|
|
if SourceKey("beta") != seat.SourceByBranchKey("beta") || SourceKey("beta") != "beta" {
|
|
t.Error("the source's key is the seat's, one per watched branch")
|
|
}
|
|
// A switched act's id carries its state, key and revision, and reads back; any other id is not one.
|
|
for _, c := range []struct {
|
|
state, key string
|
|
rev uint64
|
|
id string
|
|
}{
|
|
{SwitchedOwed, "novox.app.7.0123abcd.status=mesh/delivery", 4, "switched.owed.4.novox.app.7.0123abcd.status=mesh/delivery"},
|
|
{SwitchedMerges, "release-g", 12, "switched.merges.12.release-g"},
|
|
} {
|
|
id, err := SwitchedActID(c.state, c.key, c.rev)
|
|
if err != nil || id != c.id {
|
|
t.Errorf("SwitchedActID(%q, %q, %d) = %q, %v; want %q", c.state, c.key, c.rev, id, err, c.id)
|
|
}
|
|
if s, k, r, ok := ParseSwitchedActID(id); !ok || s != c.state || k != c.key || r != c.rev {
|
|
t.Errorf("ParseSwitchedActID(%q) = %q %q %d %v", id, s, k, r, ok)
|
|
}
|
|
}
|
|
for _, bad := range []struct {
|
|
state, key string
|
|
rev uint64
|
|
}{{"turns", "k", 1}, {SwitchedOwed, "", 1}, {SwitchedOwed, "a@b", 1}, {SwitchedMerges, "k", 0}} {
|
|
if id, err := SwitchedActID(bad.state, bad.key, bad.rev); err == nil {
|
|
t.Errorf("SwitchedActID(%+v) = %q, no error", bad, id)
|
|
}
|
|
}
|
|
for _, other := range []string{"novox.app.7.0123abcd.view@1700000000", "switched.turns.1.k", "switched.owed.x.k",
|
|
"switched.owed.0.k", "switched.owed.3", "switched.owed.3.k@1"} {
|
|
if _, _, _, ok := ParseSwitchedActID(other); ok {
|
|
t.Errorf("%q reads as a switched act's id", other)
|
|
}
|
|
}
|
|
// A switched act's id carries its state, key and revision, and reads back; any other id is not one.
|
|
for _, c := range []struct {
|
|
state, key string
|
|
rev uint64
|
|
id string
|
|
}{
|
|
{SwitchedOwed, "novox.app.7.0123abcd.status=mesh/delivery", 4, "switched.owed.4.novox.app.7.0123abcd.status=mesh/delivery"},
|
|
{SwitchedMerges, "release-g", 12, "switched.merges.12.release-g"},
|
|
} {
|
|
id, err := SwitchedActID(c.state, c.key, c.rev)
|
|
if err != nil || id != c.id {
|
|
t.Errorf("SwitchedActID(%q, %q, %d) = %q, %v; want %q", c.state, c.key, c.rev, id, err, c.id)
|
|
}
|
|
if s, k, r, ok := ParseSwitchedActID(id); !ok || s != c.state || k != c.key || r != c.rev {
|
|
t.Errorf("ParseSwitchedActID(%q) = %q %q %d %v", id, s, k, r, ok)
|
|
}
|
|
}
|
|
for _, bad := range []struct {
|
|
state, key string
|
|
rev uint64
|
|
}{{"turns", "k", 1}, {SwitchedOwed, "", 1}, {SwitchedOwed, "a@b", 1}, {SwitchedMerges, "k", 0}} {
|
|
if id, err := SwitchedActID(bad.state, bad.key, bad.rev); err == nil {
|
|
t.Errorf("SwitchedActID(%+v) = %q, no error", bad, id)
|
|
}
|
|
}
|
|
for _, other := range []string{"novox.app.7.0123abcd.view@1700000000", "switched.turns.1.k", "switched.owed.x.k",
|
|
"switched.owed.0.k", "switched.owed.3", "switched.owed.3.k@1"} {
|
|
if _, _, _, ok := ParseSwitchedActID(other); ok {
|
|
t.Errorf("%q reads as a switched act's id", other)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGateValuesRefuseWhatTheirRulesRefuse(t *testing.T) {
|
|
if _, err := EncodeSource(seat.SourceByBranchValue{Source: 1, Call: "0123456789abcdef"}); err == nil {
|
|
t.Error("an empty source encodes")
|
|
}
|
|
if _, err := EncodeAnswer(Answer{Option: "merge"}); err == nil {
|
|
t.Error("an answer with no warrant encodes")
|
|
}
|
|
if _, err := DecodeSource([]byte(`{"v":2,"source":1,"call":"c","heads":{"a":"b"}}`)); !errors.Is(err, keys.ErrUnknownVersion) {
|
|
t.Errorf("another version: %v", err)
|
|
}
|
|
// design 47 H2 §780: a reader leaves a field it does not know unread.
|
|
if s, err := DecodeSource([]byte(`{"v":1,"source":1,"call":"0123456789abcdef","heads":{"a":"b"},"extra":1}`)); err != nil || s.Source != 1 {
|
|
t.Errorf("an unknown field is not read past: %+v %v", s, err)
|
|
}
|
|
}
|