59 lines
2.1 KiB
Go
59 lines
2.1 KiB
Go
package operatorchannelclient
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
seat "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
|
"git.novox.be/novox/mesh-sdk/go/channel"
|
|
)
|
|
|
|
// TestTheBenchShapesAreTheChannelsWire: each shape the benches channel and intake carry, written by mesh-sdk's channel
|
|
// package, reads into the contract's type with no field dropped and reads back as the same wire.
|
|
func TestTheBenchShapesAreTheChannelsWire(t *testing.T) {
|
|
at := time.Date(2026, 11, 5, 12, 0, 0, 0, time.UTC)
|
|
who := channel.Sender{Identity: "42", Display: "Op", Authenticated: true}
|
|
cases := []struct {
|
|
name string
|
|
sdk any
|
|
into any
|
|
}{
|
|
{"message", channel.Message{Handle: "h1", Title: "t", Body: "b", Buttons: []channel.Button{{Label: "Approve", Ticket: "tk"}},
|
|
Note: "n", Urgent: true, Silent: true, Reply: "r", To: "42", Secret: true}, &seat.Message{}},
|
|
{"a send", channel.Message{Handle: "h2", Title: "t", Body: "b"}, &seat.Message{}},
|
|
{"choice", channel.Choice{ID: "c1", Ticket: "tk", Handle: "h1", Sender: who, At: at}, &seat.Choice{}},
|
|
{"link", channel.LinkAsked{ID: "l1", Sender: who, At: at}, &seat.LinkAsked{}},
|
|
{"failed", channel.Failed{Handle: "h1", Why: "blocked", Permanent: true, At: at}, &seat.Failed{}},
|
|
{"standing", channel.Standing{Ready: true, Why: "w", EditsSilently: true, At: at}, &seat.Standing{}},
|
|
{"code", channel.Code{Sender: who, Code: "123456", Purpose: "approve"}, &seat.Code{}},
|
|
{"proof answer", channel.ProofAnswer{Accepted: true, Words: "ok"}, &seat.ProofAnswer{}},
|
|
}
|
|
for _, c := range cases {
|
|
wire, err := json.Marshal(c.sdk)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dec := json.NewDecoder(bytes.NewReader(wire))
|
|
dec.DisallowUnknownFields()
|
|
if err := dec.Decode(c.into); err != nil {
|
|
t.Errorf("%s: %v", c.name, err)
|
|
continue
|
|
}
|
|
back, _ := json.Marshal(c.into)
|
|
var got, want any
|
|
_ = json.Unmarshal(back, &got)
|
|
_ = json.Unmarshal(wire, &want)
|
|
if !jsonEqual(got, want) {
|
|
t.Errorf("%s: reads back as %s, want %s", c.name, back, wire)
|
|
}
|
|
}
|
|
}
|
|
|
|
func jsonEqual(a, b any) bool {
|
|
x, _ := json.Marshal(a)
|
|
y, _ := json.Marshal(b)
|
|
return bytes.Equal(x, y)
|
|
}
|