snowluma: SnowLuma (OneBot 11) Go SDK, drop-in compatible with napcat
This commit is contained in:
75
snowluma_test.go
Normal file
75
snowluma_test.go
Normal file
@ -0,0 +1,75 @@
|
||||
package snowluma
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCQCodeRoundTrip(t *testing.T) {
|
||||
segs := []Segment{
|
||||
Text("hello "),
|
||||
At(12345),
|
||||
Text(" ,[bracket]&comma"),
|
||||
Image("http://x/y.png"),
|
||||
}
|
||||
encoded := EncodeCQ(segs)
|
||||
decoded := ParseCQ(encoded)
|
||||
|
||||
if len(decoded) != len(segs) {
|
||||
t.Fatalf("segment count mismatch: got %d want %d", len(decoded), len(segs))
|
||||
}
|
||||
if decoded[0].Type != "text" || decoded[0].Data["text"] != "hello " {
|
||||
t.Errorf("text segment wrong: %+v", decoded[0])
|
||||
}
|
||||
if decoded[1].Type != "at" || decoded[1].Data["qq"] != "12345" {
|
||||
t.Errorf("at segment wrong: %+v", decoded[1])
|
||||
}
|
||||
if decoded[3].Type != "image" || decoded[3].Data["file"] != "http://x/y.png" {
|
||||
t.Errorf("image segment wrong: %+v", decoded[3])
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeMessageArrayAndString(t *testing.T) {
|
||||
arr := json.RawMessage(`[{"type":"text","data":{"text":"hi"}},{"type":"at","data":{"qq":"all"}}]`)
|
||||
segs, err := DecodeMessage(arr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(segs) != 2 || segs[1].Type != "at" || segs[1].Data["qq"] != "all" {
|
||||
t.Fatalf("array decode wrong: %+v", segs)
|
||||
}
|
||||
|
||||
str := json.RawMessage(`"hello [CQ:at,qq=10001] world"`)
|
||||
segs, err = DecodeMessage(str)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(segs) != 3 || segs[1].Type != "at" || segs[1].Data["qq"] != "10001" {
|
||||
t.Fatalf("string decode wrong: %+v", segs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeEventNoticeExtra(t *testing.T) {
|
||||
data := []byte(`{"post_type":"notice","notice_type":"notify","sub_type":"poke","group_id":1,"user_id":2,"target_id":3,"custom_field":"kept"}`)
|
||||
ev, err := decodeEvent(data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ev.Notice == nil || ev.Notice.SubType != "poke" {
|
||||
t.Fatalf("notice decode wrong: %+v", ev.Notice)
|
||||
}
|
||||
if ev.Notice.Extra["custom_field"] != "kept" {
|
||||
t.Fatalf("extra field lost: %+v", ev.Notice.Extra)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntString(t *testing.T) {
|
||||
var a IntString
|
||||
if err := json.Unmarshal([]byte(`123`), &a); err != nil || a != 123 {
|
||||
t.Fatalf("numeric intstring failed: %v %d", err, a)
|
||||
}
|
||||
var b IntString
|
||||
if err := json.Unmarshal([]byte(`"456"`), &b); err != nil || b != 456 {
|
||||
t.Fatalf("string intstring failed: %v %d", err, b)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user