69 lines
2.9 KiB
Go
69 lines
2.9 KiB
Go
package operatorchannelclient
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
seat "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
|
)
|
|
|
|
// TestTheBenchShapesAreTheChannelsWire: each shape the benches channel and intake carry, as the wire the benches speak
|
|
// (written out here, the contract's own), reads into the contract's type with no field dropped and reads back as the same wire.
|
|
func TestTheBenchShapesAreTheChannelsWire(t *testing.T) {
|
|
sender := `{"identity":"42","display":"Op","authenticated":true}`
|
|
cases := []struct {
|
|
name string
|
|
wire string
|
|
into any
|
|
}{
|
|
{"message", `{"handle":"h1","title":"t","body":"b","buttons":[{"label":"Approve","ticket":"tk"}],"note":"n","urgent":true,"silent":true,"reply":"r","to":"42","secret":true}`, &seat.Message{}},
|
|
{"a send", `{"handle":"h2","title":"t","body":"b"}`, &seat.Message{}},
|
|
{"choice", `{"id":"c1","ticket":"tk","handle":"h1","sender":` + sender + `,"at":"2026-11-05T12:00:00Z"}`, &seat.Choice{}},
|
|
{"link", `{"id":"l1","sender":` + sender + `,"at":"2026-11-05T12:00:00Z"}`, &seat.LinkAsked{}},
|
|
{"failed", `{"handle":"h1","why":"blocked","permanent":true,"at":"2026-11-05T12:00:00Z"}`, &seat.Failed{}},
|
|
{"standing", `{"ready":true,"why":"w","edits-silently":true,"at":"2026-11-05T12:00:00Z"}`, &seat.Standing{}},
|
|
{"code", `{"sender":` + sender + `,"code":"123456","purpose":"approve"}`, &seat.Code{}},
|
|
{"proof answer", `{"accepted":true,"words":"ok"}`, &seat.ProofAnswer{}},
|
|
}
|
|
for _, c := range cases {
|
|
dec := json.NewDecoder(bytes.NewReader([]byte(c.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([]byte(c.wire), &want)
|
|
if !jsonEqual(got, want) {
|
|
t.Errorf("%s: reads back as %s, want %s", c.name, back, c.wire)
|
|
}
|
|
}
|
|
}
|
|
|
|
func jsonEqual(a, b any) bool {
|
|
x, _ := json.Marshal(a)
|
|
y, _ := json.Marshal(b)
|
|
return bytes.Equal(x, y)
|
|
}
|
|
|
|
// TestTheBenchSubjectsAreTheChannelsOwn: the subjects and words, character for character.
|
|
func TestTheBenchSubjectsAreTheChannelsOwn(t *testing.T) {
|
|
for _, c := range [][2]string{
|
|
{ChannelSubject(Show, "telegram"), "mesh.seat.channel.accept.show.telegram"},
|
|
{ChannelSubject(Edit, "desk"), "mesh.seat.channel.accept.edit.desk"},
|
|
{ChannelSubject(Send, "desk"), "mesh.seat.channel.accept.send.desk"},
|
|
{IntakeSubject(Chosen, "telegram"), "mesh.seat.intake.event.choice.telegram"},
|
|
{IntakeSubject(Link, "telegram"), "mesh.seat.intake.event.link.telegram"},
|
|
{IntakeSubject(FailedWhat, "desk"), "mesh.seat.intake.event.failed.desk"},
|
|
{IntakeSubject(StandingWhat, "desk"), "mesh.seat.intake.event.standing.desk"},
|
|
{ProofSubject(CodeProof, "telegram"), "mesh.seat.intake.proof.code.telegram"},
|
|
} {
|
|
if c[0] != c[1] {
|
|
t.Errorf("subject %q, want %q", c[0], c[1])
|
|
}
|
|
}
|
|
}
|