Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
104 lines
4.1 KiB
Go
104 lines
4.1 KiB
Go
package signalbot
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
var self = Account{Number: "+33600000000", ACI: "bot-aci"}
|
|
|
|
const directEnvelope = `{"envelope":{"source":"+33600000001","sourceNumber":"+33600000001","sourceUuid":"u-1","sourceName":"Alice","sourceDevice":1,"timestamp":1763640000000,"dataMessage":{"timestamp":1763640000000,"message":"hello","expiresInSeconds":0,"viewOnce":false}},"account":"+33600000000"}`
|
|
|
|
const groupEnvelope = `{"envelope":{"source":"+33600000001","sourceNumber":"+33600000001","sourceUuid":"u-1","sourceName":"Alice","sourceDevice":1,"timestamp":1763640000000,"dataMessage":{"timestamp":1763640000000,"message":"hi group","groupInfo":{"groupId":"Z3JvdXAtaW50ZXJuYWw=","type":"DELIVER"}}},"account":"+33600000000"}`
|
|
|
|
func TestParseDirectMessage(t *testing.T) {
|
|
m, ok := parseEnvelope([]byte(directEnvelope), self)
|
|
if !ok {
|
|
t.Fatal("ok = false")
|
|
}
|
|
if m.From != "+33600000001" || m.GroupID != "" || m.Text != "hello" {
|
|
t.Fatalf("got %+v", m)
|
|
}
|
|
if !m.Timestamp.Equal(time.UnixMilli(1763640000000)) {
|
|
t.Fatalf("Timestamp = %v", m.Timestamp)
|
|
}
|
|
}
|
|
|
|
func TestParseGroupMessage(t *testing.T) {
|
|
m, ok := parseEnvelope([]byte(groupEnvelope), self)
|
|
want := "group." + base64.StdEncoding.EncodeToString([]byte("Z3JvdXAtaW50ZXJuYWw="))
|
|
if !ok || m.GroupID != want || m.Text != "hi group" {
|
|
t.Fatalf("got %+v, %v; want GroupID %s", m, ok, want)
|
|
}
|
|
}
|
|
|
|
func TestParseFromUUIDWhenNoNumber(t *testing.T) {
|
|
env := `{"envelope":{"sourceNumber":null,"sourceUuid":"u-1","timestamp":1,"dataMessage":{"message":"x"}},"account":"+33600000000"}`
|
|
m, ok := parseEnvelope([]byte(env), self)
|
|
if !ok || m.From != "u-1" {
|
|
t.Fatalf("got %+v, %v", m, ok)
|
|
}
|
|
}
|
|
|
|
func TestParseIgnored(t *testing.T) {
|
|
cases := map[string]string{
|
|
"receipt": `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"receiptMessage":{"when":1,"isDelivery":true,"timestamps":[1]}},"account":"+33600000000"}`,
|
|
"typing": `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"typingMessage":{"action":"STARTED","timestamp":1}},"account":"+33600000000"}`,
|
|
"sync": `{"envelope":{"sourceNumber":"+33600000000","timestamp":1,"syncMessage":{"sentMessage":{"message":"from phone","destination":"+33600000001"}}},"account":"+33600000000"}`,
|
|
"empty": `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"dataMessage":{"message":""}},"account":"+33600000000"}`,
|
|
"reaction": `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"dataMessage":{"message":null,"reaction":{"emoji":"👍","targetSentTimestamp":1}}},"account":"+33600000000"}`,
|
|
"invalid": `{not json`,
|
|
}
|
|
for name, env := range cases {
|
|
if m, ok := parseEnvelope([]byte(env), self); ok {
|
|
t.Errorf("%s: ok = true, msg %+v", name, m)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseIgnoresSelf(t *testing.T) {
|
|
byNumber := `{"envelope":{"sourceNumber":"+33600000000","sourceUuid":"bot-aci","timestamp":1,"dataMessage":{"message":"echo"}},"account":"+33600000000"}`
|
|
byACI := `{"envelope":{"sourceNumber":"","sourceUuid":"bot-aci","timestamp":1,"dataMessage":{"message":"echo"}},"account":"+33600000000"}`
|
|
for _, env := range []string{byNumber, byACI} {
|
|
if _, ok := parseEnvelope([]byte(env), self); ok {
|
|
t.Errorf("own message accepted: %s", env)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseUnicode(t *testing.T) {
|
|
env := `{"envelope":{"sourceNumber":"+33600000001","timestamp":1,"dataMessage":{"message":"é 🎬\nx"}},"account":"+33600000000"}`
|
|
m, ok := parseEnvelope([]byte(env), self)
|
|
if !ok || m.Text != "é 🎬\nx" {
|
|
t.Fatalf("got %q, %v", m.Text, ok)
|
|
}
|
|
}
|
|
|
|
func TestReplyTargets(t *testing.T) {
|
|
f := newFakeAPI(t)
|
|
f.accounts(`[{"number":"+33600000000"}]`)
|
|
got := f.captureSend(t, 201, ``)
|
|
c := mustNew(t, f.URL)
|
|
|
|
for _, env := range []string{groupEnvelope, directEnvelope} {
|
|
m, ok := parseEnvelope([]byte(env), self)
|
|
if !ok {
|
|
t.Fatal("parse failed")
|
|
}
|
|
m.client = c
|
|
if err := m.Reply(context.Background(), "ack"); err != nil {
|
|
t.Fatalf("Reply: %v", err)
|
|
}
|
|
want := m.GroupID
|
|
if want == "" {
|
|
want = m.From
|
|
}
|
|
rcpt := (*got)["recipients"].([]any)
|
|
if len(rcpt) != 1 || rcpt[0] != want {
|
|
t.Errorf("recipients = %v, want [%s]", rcpt, want)
|
|
}
|
|
}
|
|
}
|