agent-116 KOOK token即用+自动获取:
- kook/identity.go: Connect 调 GET /user/me 拿 bot 身份(bot_id/bot_username/bot_nickname), 失败容错
- 渠道列表 enrichAccountsWithBot 附 bot 身份; base_url空用默认/receive_mode默认ws(只填token可跑)
agent-117 action/narration 剥出气泡(主人需求: 动作/旁白不进气泡,按位置上/下):
- parseContent.splitBubble(content)->{body,head,tail}: body=纯文本(去标签), head=开头/中间action/narr(气泡上), tail=结尾(气泡下)
- renderContent 只渲染text(去内联灰斜体); chat.tsx BubbleNotes 泡外 *动作/旁白* 灰斜体小字(气泡外依附), SplitChatBubble 先剥splitBubble再splitParagraphs(body), <p>/\n\n/毛玻璃/SourceLabel不破坏
- 注: 非chat mode renderContent 现在丢弃action/narr(可接受)
Channels 配置表单回填(修save后输入框空):
- openConfig 从 data.accounts[0].config 回填 token(掩码提示)/base_url/平台字段/黑白名单/限流, 不再硬清空
全仓 build/vet/test绿+前端build绿
228 lines
6.5 KiB
Go
228 lines
6.5 KiB
Go
package kook
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
|
)
|
|
|
|
func TestParseSignal(t *testing.T) {
|
|
t.Parallel()
|
|
s := []byte(`{"s":1,"d":{"code":0,"session_id":"sess-123"}}`)
|
|
sig, err := parseSignal(s)
|
|
if err != nil {
|
|
t.Fatalf("parseSignal: %v", err)
|
|
}
|
|
if sig.S != signalHello {
|
|
t.Errorf("S = %d, want %d (hello)", sig.S, signalHello)
|
|
}
|
|
if len(sig.D) == 0 {
|
|
t.Error("D should carry the hello payload")
|
|
}
|
|
|
|
s0 := []byte(`{"s":0,"sn":7,"d":{"channel_type":"GROUP"}}`)
|
|
sig0, err := parseSignal(s0)
|
|
if err != nil {
|
|
t.Fatalf("parseSignal event: %v", err)
|
|
}
|
|
if sig0.S != signalEvent || sig0.SN != 7 {
|
|
t.Errorf("event S/SN = %d/%d, want 0/7", sig0.S, sig0.SN)
|
|
}
|
|
|
|
if sig, err := parseSignal([]byte(`{"s":3}`)); err != nil || sig.S != signalPong {
|
|
t.Errorf("pong parse = %+v, %v", sig, err)
|
|
}
|
|
}
|
|
|
|
func TestHeartbeatFrame(t *testing.T) {
|
|
t.Parallel()
|
|
b, err := heartbeatFrame(42)
|
|
if err != nil {
|
|
t.Fatalf("heartbeatFrame: %v", err)
|
|
}
|
|
if string(b) != `{"s":2,"sn":42}` {
|
|
t.Errorf("heartbeat frame = %s, want {\"s\":2,\"sn\":42}", string(b))
|
|
}
|
|
}
|
|
|
|
func TestGatewayURL_FreshNoResume(t *testing.T) {
|
|
t.Parallel()
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != gatewayPath {
|
|
http.Error(w, "unexpected", http.StatusNotFound)
|
|
return
|
|
}
|
|
kookOK(w, map[string]any{"url": "wss://gw.example.com/gateway?compress=0&token=abc"})
|
|
})
|
|
a, _ := newTestAdapter(t, h)
|
|
got, err := a.gatewayURL(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("gatewayURL: %v", err)
|
|
}
|
|
if got != "wss://gw.example.com/gateway?compress=0&token=abc" {
|
|
t.Errorf("fresh url = %q, want unchanged base", got)
|
|
}
|
|
}
|
|
|
|
func TestGatewayURL_ResumeAddsParams(t *testing.T) {
|
|
t.Parallel()
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != gatewayPath {
|
|
http.Error(w, "unexpected", http.StatusNotFound)
|
|
return
|
|
}
|
|
kookOK(w, map[string]any{"url": "wss://gw.example.com/gateway?compress=0&token=abc"})
|
|
})
|
|
a, _ := newTestAdapter(t, h)
|
|
a.setSession("sess-abc", 5)
|
|
assertCanResume(t, a, true)
|
|
|
|
got, err := a.gatewayURL(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("gatewayURL: %v", err)
|
|
}
|
|
u, err := url.Parse(got)
|
|
if err != nil {
|
|
t.Fatalf("parse resume url: %v", err)
|
|
}
|
|
q := u.Query()
|
|
if q.Get("compress") != "0" || q.Get("token") != "abc" {
|
|
t.Errorf("existing query params lost: %q", got)
|
|
}
|
|
if q.Get("resume") != "1" || q.Get("sn") != "5" || q.Get("session_id") != "sess-abc" {
|
|
t.Errorf("resume params = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestGatewayURL_ResetDropsResume(t *testing.T) {
|
|
t.Parallel()
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
kookOK(w, map[string]any{"url": "wss://gw.example.com/gateway?compress=0"})
|
|
})
|
|
a, _ := newTestAdapter(t, h)
|
|
a.setSession("sess-abc", 5)
|
|
a.resetSession()
|
|
assertCanResume(t, a, false)
|
|
got, err := a.gatewayURL(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("gatewayURL: %v", err)
|
|
}
|
|
if strings.Contains(got, "resume=1") {
|
|
t.Errorf("reset session should not emit resume params: %q", got)
|
|
}
|
|
}
|
|
|
|
func assertCanResume(t *testing.T, a *Adapter, want bool) {
|
|
t.Helper()
|
|
if got := a.canResume(); got != want {
|
|
t.Fatalf("canResume = %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// TestConnect_GatewayDeliversEvent is an end-to-end smoke of the gateway path: an
|
|
// httptest server serves the REST gateway lookup, upgrades a WebSocket, sends a
|
|
// hello then an s=0 event, and the adapter dispatches the normalized message to
|
|
// the push handler.
|
|
func TestConnect_GatewayDeliversEvent(t *testing.T) {
|
|
var (
|
|
mu sync.Mutex
|
|
delivered []adapter.UnifiedMessage
|
|
)
|
|
got := make(chan adapter.UnifiedMessage, 1)
|
|
|
|
var srv *httptest.Server
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.URL.Path {
|
|
case gatewayPath:
|
|
wsURL := "ws://" + strings.TrimPrefix(srv.URL, "http://") + "/ws"
|
|
kookOK(w, map[string]any{"url": wsURL})
|
|
case userMePath:
|
|
// Connect resolves the bot identity via user/me (best-effort).
|
|
kookOK(w, map[string]any{"id": "bot-1", "username": "bot", "nickname": "Bot"})
|
|
case "/ws":
|
|
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
t.Errorf("ws upgrade: %v", err)
|
|
return
|
|
}
|
|
defer conn.Close()
|
|
_ = conn.WriteJSON(map[string]any{"s": 1, "d": map[string]any{"code": 0, "session_id": "sess-123"}})
|
|
_ = conn.WriteJSON(map[string]any{"s": 0, "sn": 7, "d": map[string]any{
|
|
"channel_type": "GROUP", "type": 1, "target_id": "chan-8", "author_id": "u-8",
|
|
"content": "gateway hi", "msg_id": "m-8", "msg_timestamp": 1700000000000, "nonce": "",
|
|
"extra": map[string]any{
|
|
"guild_id": "g-8", "channel_name": "test",
|
|
"author": map[string]any{"id": "u-8", "username": "u", "nickname": "Nick"},
|
|
},
|
|
}})
|
|
// Hold the connection and consume client frames (heartbeat/close).
|
|
for {
|
|
if _, _, err := conn.ReadMessage(); err != nil {
|
|
return
|
|
}
|
|
}
|
|
default:
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
srv = httptest.NewServer(h)
|
|
defer srv.Close()
|
|
|
|
a := New(
|
|
Config{BotToken: "testtoken"},
|
|
WithBaseURL(srv.URL),
|
|
WithHTTPClient(srv.Client()),
|
|
WithLogger(discardLogger()),
|
|
)
|
|
a.SetMessageHandler(func(ctx context.Context, msg adapter.UnifiedMessage) {
|
|
mu.Lock()
|
|
delivered = append(delivered, msg)
|
|
mu.Unlock()
|
|
select {
|
|
case got <- msg:
|
|
default:
|
|
}
|
|
})
|
|
|
|
if err := a.Connect(context.Background()); err != nil {
|
|
t.Fatalf("Connect: %v", err)
|
|
}
|
|
defer func() { _ = a.Disconnect(context.Background()) }()
|
|
|
|
select {
|
|
case msg := <-got:
|
|
if msg.Platform != "kook" {
|
|
t.Errorf("Platform = %q, want kook", msg.Platform)
|
|
}
|
|
if msg.ChannelID != "chan-8" || msg.GroupID != "g-8" {
|
|
t.Errorf("ChannelID/GroupID = %q/%q, want chan-8/g-8", msg.ChannelID, msg.GroupID)
|
|
}
|
|
if msg.SenderName != "Nick" || msg.SenderID != "u-8" {
|
|
t.Errorf("Sender = %q/%q, want Nick/u-8", msg.SenderName, msg.SenderID)
|
|
}
|
|
if msg.Content != "gateway hi" || msg.ContentType != adapter.ContentText {
|
|
t.Errorf("Content = %q/%q, want gateway hi/text", msg.Content, msg.ContentType)
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("timed out waiting for gateway event")
|
|
}
|
|
|
|
if sn := a.currentSN(); sn != 7 {
|
|
t.Errorf("currentSN = %d, want 7", sn)
|
|
}
|
|
if sid := a.currentSession(); sid != "sess-123" {
|
|
t.Errorf("currentSession = %q, want sess-123", sid)
|
|
}
|
|
// Connect fetched the bot identity via user/me (best-effort).
|
|
if id, _, nickname := a.BotIdentity(); id != "bot-1" || nickname != "Bot" {
|
|
t.Errorf("BotIdentity = %q/%q, want bot-1/Bot", id, nickname)
|
|
}
|
|
}
|