feat: add DingTalk Stream text ingress
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/open-dingtalk/dingtalk-stream-sdk-go/chatbot"
|
||||
)
|
||||
|
||||
func TestStreamConfigValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
cfg StreamConfig
|
||||
wantErr bool
|
||||
wantOn bool
|
||||
errSubstr string
|
||||
}{
|
||||
{
|
||||
name: "all empty is disabled without error",
|
||||
cfg: StreamConfig{},
|
||||
wantErr: false,
|
||||
wantOn: false,
|
||||
},
|
||||
{
|
||||
name: "whitespace only is disabled without error",
|
||||
cfg: StreamConfig{ClientID: " ", ClientSecret: "\t", TenantKey: " "},
|
||||
wantErr: false,
|
||||
wantOn: false,
|
||||
},
|
||||
{
|
||||
name: "client id only is an error",
|
||||
cfg: StreamConfig{ClientID: "id"},
|
||||
wantErr: true,
|
||||
errSubstr: "client secret",
|
||||
},
|
||||
{
|
||||
name: "client secret only is an error",
|
||||
cfg: StreamConfig{ClientSecret: "secret"},
|
||||
wantErr: true,
|
||||
errSubstr: "client id",
|
||||
},
|
||||
{
|
||||
name: "credentials without tenant key is an error",
|
||||
cfg: StreamConfig{ClientID: "id", ClientSecret: "secret"},
|
||||
wantErr: true,
|
||||
errSubstr: "tenant key",
|
||||
},
|
||||
{
|
||||
name: "tenant key without credentials is an error",
|
||||
cfg: StreamConfig{TenantKey: "acme"},
|
||||
wantErr: true,
|
||||
errSubstr: "client id",
|
||||
},
|
||||
{
|
||||
name: "all three present is enabled",
|
||||
cfg: StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"},
|
||||
wantErr: false,
|
||||
wantOn: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
err := tt.cfg.Validate()
|
||||
if tt.wantErr && err == nil {
|
||||
t.Fatalf("Validate() = nil, want error containing %q", tt.errSubstr)
|
||||
}
|
||||
if !tt.wantErr && err != nil {
|
||||
t.Fatalf("Validate() = %v, want nil", err)
|
||||
}
|
||||
if tt.wantErr && tt.errSubstr != "" && !strings.Contains(err.Error(), tt.errSubstr) {
|
||||
t.Fatalf("Validate() error = %q, want substring %q", err.Error(), tt.errSubstr)
|
||||
}
|
||||
if got := tt.cfg.Enabled(); got != tt.wantOn {
|
||||
t.Fatalf("Enabled() = %v, want %v", got, tt.wantOn)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBotCallbackFromSDK(t *testing.T) {
|
||||
t.Parallel()
|
||||
t.Run("nil data returns error", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := BotCallbackFromSDK(nil); err == nil {
|
||||
t.Fatal("BotCallbackFromSDK(nil) = nil error, want error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("maps every field one to one", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
data := &chatbot.BotCallbackDataModel{
|
||||
ConversationType: "2",
|
||||
ConversationId: "cid123",
|
||||
Msgtype: "text",
|
||||
MsgId: "msg-9",
|
||||
SenderNick: "Alice",
|
||||
SenderStaffId: "staff-1",
|
||||
SenderId: "sender-1",
|
||||
IsInAtList: true,
|
||||
SessionWebhook: "https://oapi.dingtalk.com/robot/sendBySession?session=abc",
|
||||
Text: chatbot.BotCallbackDataTextModel{Content: " 你好 "},
|
||||
}
|
||||
cb, err := BotCallbackFromSDK(data)
|
||||
if err != nil {
|
||||
t.Fatalf("BotCallbackFromSDK() error = %v, want nil", err)
|
||||
}
|
||||
want := BotCallback{
|
||||
ConversationType: "2",
|
||||
ConversationID: "cid123",
|
||||
Msgtype: "text",
|
||||
MsgID: "msg-9",
|
||||
SenderNick: "Alice",
|
||||
SenderStaffID: "staff-1",
|
||||
SenderID: "sender-1",
|
||||
IsInAtList: true,
|
||||
SessionWebhook: "https://oapi.dingtalk.com/robot/sendBySession?session=abc",
|
||||
Text: " 你好 ",
|
||||
}
|
||||
if cb != want {
|
||||
t.Fatalf("BotCallbackFromSDK() = %+v, want %+v", cb, want)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("text content is not trimmed by conversion", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
data := &chatbot.BotCallbackDataModel{
|
||||
ConversationType: "1",
|
||||
Msgtype: "text",
|
||||
MsgId: "m",
|
||||
Text: chatbot.BotCallbackDataTextModel{Content: ""},
|
||||
}
|
||||
cb, err := BotCallbackFromSDK(data)
|
||||
if err != nil {
|
||||
t.Fatalf("BotCallbackFromSDK() error = %v", err)
|
||||
}
|
||||
if cb.Text != "" {
|
||||
t.Fatalf("Text = %q, want empty", cb.Text)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func mustTestHandler(t *testing.T) *Handler {
|
||||
t.Helper()
|
||||
return newTestHandler(t, &fakeQA{}, &fakeReply{})
|
||||
}
|
||||
|
||||
func TestNewStreamClientValidation(t *testing.T) {
|
||||
t.Parallel()
|
||||
h := mustTestHandler(t)
|
||||
|
||||
t.Run("nil handler is an error", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"}
|
||||
if _, err := NewStreamClient(cfg, nil); err == nil {
|
||||
t.Fatal("NewStreamClient with nil handler = nil error, want error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid config is an error", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if _, err := NewStreamClient(StreamConfig{}, h); err == nil {
|
||||
t.Fatal("NewStreamClient with empty config = nil error, want error")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("valid config keeps SDK auto reconnect on until Close disables it", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cfg := StreamConfig{ClientID: "id", ClientSecret: "secret", TenantKey: "acme"}
|
||||
sc, err := NewStreamClient(cfg, h)
|
||||
if err != nil {
|
||||
t.Fatalf("NewStreamClient() error = %v, want nil", err)
|
||||
}
|
||||
if sc == nil || sc.inner == nil {
|
||||
t.Fatal("NewStreamClient() returned nil client")
|
||||
}
|
||||
// The SDK default is AutoReconnect=true; keep it so runtime
|
||||
// disconnects reconnect automatically.
|
||||
if !sc.inner.AutoReconnect {
|
||||
t.Fatal("AutoReconnect = false after NewStreamClient, want true (SDK default must be kept)")
|
||||
}
|
||||
// Close must flip AutoReconnect off first so the SDK processLoop
|
||||
// does not reconnect a closed connection.
|
||||
sc.Close()
|
||||
if sc.inner.AutoReconnect {
|
||||
t.Fatal("AutoReconnect = true after Close, want false (Close must stay closed)")
|
||||
}
|
||||
// Closing again must remain a safe no-op.
|
||||
sc.Close()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user