backthink 决策+路由(P2)就绪但无投递 sink(ActiveClient/IMAdapter 无实现)。补齐: - gateway/proactive.go: hub.SendProactive 向 owner/admin 连接广播 proactive_message WS事件, 内容脱敏(去RawData/media/token),返回接收数,count=0→IM兜底;Server.SendProactive 委托 - channelbridge/proactive.go: Bridge.SendProactive 解析 owner 账户(msg.Platform→匹配白名单账号, 否则首个已配置主人频道),ChannelType=Direct+ChannelID/SenderID=owner id,走 adapter.SendProactive - backthink/sinks.go: SetActiveSink/SetIMSink 晚注入(New后重建pusher,currentPusher 并发安全) - wire.go: 在 gw/bridge 建好后 bt.SetActiveSink(gw)/SetIMSink(bridge) - 投递链: think→parseProactive→ShouldPush→currentPusher.Push→gate→active(active_client)/IM(im_bridge) →record(route+送达数) - 测试: sinks/gateway proactive/channelbridge proactive; 后端 build/vet/test 全绿 - 注: -race 本机 TSan 不可用(环境); 多平台IM按msg.Platform匹配 owner 账号(单sink诚实); weixin 24h窗口按 ErrProactiveWindow 记录为 im_bridge 错误不静默
103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package backthink
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
|
)
|
|
|
|
// TestBackthink_SetActiveSink_RebuildsPusher verifies the sink setters rebuild the
|
|
// pusher so a later push routes to the injected active client instead of the
|
|
// default nil-sink RouteNone pusher built at New.
|
|
func TestBackthink_SetActiveSink_RebuildsPusher(t *testing.T) {
|
|
db := newTestDB(t)
|
|
bt, err := New(Config{}, Deps{
|
|
Thinker: &mockThinker{text: "[我决定:push]\n[渠道:web]\nhi"},
|
|
State: &mockState{},
|
|
Gate: &mockGate{allow: true},
|
|
DB: db,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("new: %v", err)
|
|
}
|
|
|
|
active := &mockActive{n: 2}
|
|
im := &mockIMAdapter{}
|
|
bt.SetActiveSink(active)
|
|
bt.SetIMSink(im)
|
|
|
|
res := bt.pusher.Push(context.Background(), PushRequest{
|
|
Message: adapter.UnifiedMessage{Content: "hi"},
|
|
ChannelHint: "web",
|
|
})
|
|
if !res.Pushed || res.Route != RouteActiveClient {
|
|
t.Fatalf("push = %+v, want active_client pushed", res)
|
|
}
|
|
if active.calls != 1 {
|
|
t.Fatalf("active calls = %d, want 1", active.calls)
|
|
}
|
|
if im.count() != 0 {
|
|
t.Fatalf("im calls = %d, want 0 (active preferred)", im.count())
|
|
}
|
|
}
|
|
|
|
// TestBackthink_SetIMSink_Fallback verifies a wired IM sink receives the push when
|
|
// no active client is online (so the owner is still reached).
|
|
func TestBackthink_SetIMSink_Fallback(t *testing.T) {
|
|
db := newTestDB(t)
|
|
bt, err := New(Config{}, Deps{
|
|
Thinker: &mockThinker{text: "[我决定:push]\nhi"},
|
|
State: &mockState{},
|
|
Gate: &mockGate{allow: true},
|
|
DB: db,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("new: %v", err)
|
|
}
|
|
|
|
active := &mockActive{n: 0} // no active client online
|
|
im := &mockIMAdapter{}
|
|
bt.SetActiveSink(active)
|
|
bt.SetIMSink(im)
|
|
|
|
res := bt.pusher.Push(context.Background(), PushRequest{
|
|
Message: adapter.UnifiedMessage{Content: "hi"},
|
|
})
|
|
if !res.Pushed || res.Route != RouteIMBridge {
|
|
t.Fatalf("push = %+v, want im_bridge pushed", res)
|
|
}
|
|
if im.count() != 1 {
|
|
t.Fatalf("im calls = %d, want 1", im.count())
|
|
}
|
|
}
|
|
|
|
// TestBackthink_SinkSetters_ConcurrentSafe verifies the setter and the read of the
|
|
// current pusher do not race under the lifecycle lock, so an in-flight pass never
|
|
// touches a half-rebuilt pusher.
|
|
func TestBackthink_SinkSetters_ConcurrentSafe(t *testing.T) {
|
|
db := newTestDB(t)
|
|
bt, err := New(Config{}, Deps{
|
|
Thinker: &mockThinker{},
|
|
State: &mockState{},
|
|
Gate: &mockGate{allow: true},
|
|
DB: db,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("new: %v", err)
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
for i := 0; i < 200; i++ {
|
|
bt.SetActiveSink(&mockActive{n: 1})
|
|
bt.SetIMSink(&mockIMAdapter{})
|
|
}
|
|
}()
|
|
for i := 0; i < 200; i++ {
|
|
_ = bt.currentPusher().Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{Content: "x"}})
|
|
}
|
|
<-done
|
|
}
|