- plugins/channels/kook: config/adapter/gateway/websocket/send/message/errors — 实现 PlatformAdapter 13方法 - WS 有状态网关: 取gateway地址→连WS→信令0/1/2/3/4/6 + 30s心跳/6s超时 + resume(拼接sn/session_id) + 指数退避重连 - 收事件: 信令0 → UnifiedMessage(word注 channel_type/type/target_id/extra.author→字段) - 发消息: /message/create(频道) + /direct-message/create(私聊,修正正确端点,非user-chat) + Bearer鉴权 - 注册 channels.go + CHANNELS_JSON 多账号 + channelbridge.configured 加 kook(bot_token非空才连) - 框架增强: internal/channel/adapter/wsutil(连接管理/心跳/重连/resume骨架,复用gorilla,KOOK用上) + httpclient(Bearer+envelope归一,KOOK用) - 全仓 go build/vet/test 绿, 零新依赖(复用餐gorilla), 未动telegram/qq/weixin/webhook契约 - §4.6 隔离/黑白名单/第三方边界未破坏 注: KOOK WS/WH 双模式待补(ReceiveMode), 实测需主人提供 CHANNEL_KOOK_TOKEN
116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package kook
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
|
)
|
|
|
|
func TestName(t *testing.T) {
|
|
t.Parallel()
|
|
a := New(Config{BotToken: "x"})
|
|
if got := a.Name(); got != "kook" {
|
|
t.Fatalf("Name() = %q, want kook", got)
|
|
}
|
|
}
|
|
|
|
func TestConfig(t *testing.T) {
|
|
t.Parallel()
|
|
a := New(Config{BotToken: "abc123", BaseURL: "https://example.com/api/v3"})
|
|
got := a.Config()
|
|
if got["bot_token"] != "abc123" {
|
|
t.Fatalf("bot_token = %v, want abc123", got["bot_token"])
|
|
}
|
|
if got["base_url"] != "https://example.com/api/v3" {
|
|
t.Fatalf("base_url = %v, want https://example.com/api/v3", got["base_url"])
|
|
}
|
|
var round Config
|
|
if err := adapter.DecodeConfig(got, &round); err != nil {
|
|
t.Fatalf("DecodeConfig: %v", err)
|
|
}
|
|
if round.BotToken != "abc123" || round.BaseURL != "https://example.com/api/v3" {
|
|
t.Fatalf("round trip mismatch: %+v", round)
|
|
}
|
|
}
|
|
|
|
func TestValidate(t *testing.T) {
|
|
t.Parallel()
|
|
if got := (Config{BotToken: "t"}).Validate(); got != nil {
|
|
t.Fatalf("Validate with token = %v, want nil", got)
|
|
}
|
|
if err := (Config{}).Validate(); !errors.Is(err, ErrMissingToken) {
|
|
t.Fatalf("Validate blank = %v, want ErrMissingToken", err)
|
|
}
|
|
if err := (Config{BotToken: " "}).Validate(); !errors.Is(err, ErrMissingToken) {
|
|
t.Fatalf("Validate whitespace = %v, want ErrMissingToken", err)
|
|
}
|
|
}
|
|
|
|
func TestCapabilities(t *testing.T) {
|
|
t.Parallel()
|
|
a := New(Config{BotToken: "x"})
|
|
caps := a.Capabilities()
|
|
if !caps.Proactive {
|
|
t.Error("Proactive should be true for kook")
|
|
}
|
|
if !caps.Markdown {
|
|
t.Error("kook should support kmarkdown")
|
|
}
|
|
if caps.Image || caps.Voice || caps.Reaction || caps.TypingHint {
|
|
t.Errorf("expected Image/Voice/Reaction/TypingHint unsupported, got %+v", caps)
|
|
}
|
|
if caps.MaxMessageLength != maxMessageLen {
|
|
t.Errorf("MaxMessageLength = %d, want %d", caps.MaxMessageLength, maxMessageLen)
|
|
}
|
|
if caps.RecommendBurstMax <= 0 {
|
|
t.Errorf("RecommendBurstMax should be positive, got %d", caps.RecommendBurstMax)
|
|
}
|
|
}
|
|
|
|
func TestListChannels_Empty(t *testing.T) {
|
|
t.Parallel()
|
|
a := New(Config{BotToken: "x"})
|
|
got, err := a.ListChannels(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("ListChannels: %v", err)
|
|
}
|
|
if got == nil || len(got) != 0 {
|
|
t.Fatalf("expected empty non-nil slice, got %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestConnect_MissingToken(t *testing.T) {
|
|
t.Parallel()
|
|
a := New(Config{})
|
|
err := a.Connect(context.Background())
|
|
if !errors.Is(err, ErrMissingToken) {
|
|
t.Fatalf("err = %v, want ErrMissingToken", err)
|
|
}
|
|
if a.IsConnected() {
|
|
t.Fatal("should not be connected")
|
|
}
|
|
}
|
|
|
|
func TestHealthCheck_NotConnected(t *testing.T) {
|
|
t.Parallel()
|
|
a := New(Config{BotToken: "x"})
|
|
err := a.HealthCheck(context.Background())
|
|
if !errors.Is(err, adapter.ErrNotConnected) {
|
|
t.Fatalf("err = %v, want ErrNotConnected", err)
|
|
}
|
|
}
|
|
|
|
func TestSendMessage_NotConnected(t *testing.T) {
|
|
t.Parallel()
|
|
a := New(Config{BotToken: "x"})
|
|
err := a.SendMessage(context.Background(), adapter.UnifiedResponse{})
|
|
if !errors.Is(err, adapter.ErrNotConnected) {
|
|
t.Fatalf("err = %v, want ErrNotConnected", err)
|
|
}
|
|
if err := a.SendProactive(context.Background(), adapter.UnifiedMessage{}); !errors.Is(err, adapter.ErrNotConnected) {
|
|
t.Fatalf("SendProactive err = %v, want ErrNotConnected", err)
|
|
}
|
|
}
|