Files
Cyrene_v2/plugins/channels/kook/helpers_test.go
T
Aska 764d592859
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(channel): KOOK 渠道适配器 + 框架兼容增强(wsutil/httpclient)
- 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
2026-09-07 12:01:35 +08:00

58 lines
1.6 KiB
Go

package kook
import (
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"testing"
"github.com/gorilla/websocket"
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
)
// discardLogger returns a silent, source-tagged logger for tests.
func discardLogger() *slog.Logger {
return logger.WithSource(logger.New(logger.Options{Out: io.Discard}), "plugins/channels/kook")
}
// newTestAdapter wires a KOOK adapter to an httptest server whose handler
// answers the REST API (and optionally upgrades a WebSocket). The server is
// closed on test cleanup.
func newTestAdapter(t *testing.T, h http.Handler) (*Adapter, *httptest.Server) {
t.Helper()
srv := httptest.NewServer(h)
t.Cleanup(srv.Close)
a := New(
Config{BotToken: "testtoken"},
WithBaseURL(srv.URL),
WithHTTPClient(srv.Client()),
WithLogger(discardLogger()),
)
return a, srv
}
// decodeBody unmarshals an incoming JSON request body into a map for assertions.
func decodeBody(r *http.Request) map[string]any {
var m map[string]any
if err := json.NewDecoder(r.Body).Decode(&m); err != nil {
return nil
}
return m
}
// kookOK writes a KOOK success envelope with the given data payload.
func kookOK(w http.ResponseWriter, data any) {
_ = json.NewEncoder(w).Encode(map[string]any{"code": 0, "message": "ok", "data": data})
}
// kookFail writes a KOOK error envelope with the given code/message.
func kookFail(w http.ResponseWriter, code int, msg string) {
_ = json.NewEncoder(w).Encode(map[string]any{"code": code, "message": msg, "data": struct{}{}})
}
// wsUpgrader upgrades an HTTP request to a WebSocket for gateway tests.
var wsUpgrader = websocket.Upgrader{}