38 lines
1.5 KiB
Go
38 lines
1.5 KiB
Go
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateAttentionAckDedupesAndRefuses(t *testing.T) {
|
|
got, err := ValidateAttentionAck(&AttentionAcks{
|
|
Dismissed: []string{"9:Failed", "12:Decision", "9:Failed"},
|
|
ChatSeen: map[string]int64{"ses_a1": 7, "0123abcd-0123-4567-89ab-0123456789ab": 8},
|
|
})
|
|
if err != nil || !reflect.DeepEqual(got.Dismissed, []string{"12:Decision", "9:Failed"}) || len(got.ChatSeen) != 2 {
|
|
t.Fatalf("valid ack = %+v, %v", got, err)
|
|
}
|
|
for _, bad := range []*AttentionAcks{nil, {}, {Dismissed: []string{"-1:Failed"}}, {Dismissed: []string{"1:failed"}}, {ChatSeen: map[string]int64{"ses_x": -1}}, {ChatSeen: map[string]int64{"ses x": 1}}} {
|
|
if _, err := ValidateAttentionAck(bad); err == nil {
|
|
t.Errorf("accepted %+v", bad)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAttentionAcksWireShape(t *testing.T) {
|
|
raw := `{"type":"ack_attention","id":"1","attention_acks":{"dismissed":["3:Failed"],"chat_seen":{"ses_q":5}}}`
|
|
var message Message
|
|
if err := json.Unmarshal([]byte(raw), &message); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if message.Type != AckAttention || message.AttentionAcks == nil || message.AttentionAcks.Dismissed[0] != "3:Failed" || message.AttentionAcks.ChatSeen["ses_q"] != 5 {
|
|
t.Fatalf("decoded = %+v", message)
|
|
}
|
|
out, _ := json.Marshal(Message{Type: AttentionAcksChanged, AttentionAcks: &AttentionAcks{Dismissed: []string{}, ChatSeen: map[string]int64{}}})
|
|
if string(out) == "" || !json.Valid(out) {
|
|
t.Fatal("unencodable")
|
|
}
|
|
}
|