Files
Cyrene_v2/internal/services/backthink/push_test.go
T
Aska 2283383b9e
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(backthink): 自主思考 P2——结构化推送决策+渠道选择(design/20)
- proactive.go(新): parseProactive 从结论解析 [我决定:push|no_push][渠道:...]<正文>;
  兼容大小写/全半角冒号; 无标记回退(无/nothing=不推); 畸形/孤标记 fail-safe 不推
- context.go: guidancePrompt 引导结构化结尾+保留P1温暖引导
- push.go: PushRequest{Message,ChannelHint}; Pusher.Push 路由(auto→active优先/IM兜底,
  web→仅active, im→仅IM, 具体平台→IM+platform打标+缺IM回退默认记detail); buildProactiveMessage
- backthink.go: think() 用 parseProactive→ShouldPush=push&&disturbAllowed(不再被tr.Import卡死)
- decide.go: disturbAllowed(asleep/working/quiet 硬性不打扰门) 取代旧import门(保留旧Decide兼容)
- 测试: parseProactive 6组+think端到端(真实pusher:kook→IM/无标记→active/no_push→不推/睡眠→门拦截)
   +buildProactiveMessage+具体平台无IM回退; 后端 build/vet/test 全绿
- 注: 生产 Active/IM sink 未注入 wire.go backthink.Deps(另一步); 多平台IM受单IMAdapter接口限制
2026-09-07 19:34:51 +08:00

148 lines
4.2 KiB
Go

package backthink
import (
"context"
"testing"
"time"
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
)
func TestPusher_ActiveFirst(t *testing.T) {
t.Parallel()
active := &mockActive{n: 2}
im := &mockIMAdapter{}
p := newPusher(active, im, &mockGate{allow: true}, discardLog())
res := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
if !res.Pushed {
t.Fatalf("Pushed = false, want true")
}
if res.Route != RouteActiveClient {
t.Fatalf("Route = %q, want %q", res.Route, RouteActiveClient)
}
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 should be preferred)", im.count())
}
}
func TestPusher_IMFallback_NoActive(t *testing.T) {
t.Parallel()
active := &mockActive{n: 0} // no active client online
im := &mockIMAdapter{}
p := newPusher(active, im, &mockGate{allow: true}, discardLog())
res := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
if !res.Pushed {
t.Fatalf("Pushed = false, want true")
}
if res.Route != RouteIMBridge {
t.Fatalf("Route = %q, want %q", res.Route, RouteIMBridge)
}
if im.count() != 1 {
t.Fatalf("im calls = %d, want 1", im.count())
}
}
func TestPusher_IMFallback_ActiveError(t *testing.T) {
t.Parallel()
active := &mockActive{err: context.DeadlineExceeded}
im := &mockIMAdapter{}
p := newPusher(active, im, &mockGate{allow: true}, discardLog())
res := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
if !res.Pushed {
t.Fatalf("Pushed = false, want true")
}
if res.Route != RouteIMBridge {
t.Fatalf("Route = %q, want %q", res.Route, RouteIMBridge)
}
if im.count() != 1 {
t.Fatalf("im calls = %d, want 1", im.count())
}
}
func TestPusher_NoTarget_Skipped(t *testing.T) {
t.Parallel()
p := newPusher(nil, nil, &mockGate{allow: true}, discardLog())
res := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
if res.Pushed {
t.Fatalf("Pushed = true, want false")
}
if res.Route != RouteNone {
t.Fatalf("Route = %q, want %q", res.Route, RouteNone)
}
}
func TestPusher_GateDenied(t *testing.T) {
t.Parallel()
active := &mockActive{n: 1}
im := &mockIMAdapter{}
p := newPusher(active, im, &mockGate{allow: false}, discardLog())
res := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
if res.Pushed {
t.Fatalf("Pushed = true, want false")
}
if res.Route != RouteRateLimited {
t.Fatalf("Route = %q, want %q", res.Route, RouteRateLimited)
}
if active.calls != 0 || im.count() != 0 {
t.Fatalf("targets called despite denial: active=%d im=%d", active.calls, im.count())
}
}
func TestPusher_CancelledContext_Denied(t *testing.T) {
t.Parallel()
active := &mockActive{n: 1}
p := newPusher(active, nil, &mockGate{allow: true}, discardLog())
ctx, cancel := context.WithCancel(context.Background())
cancel()
res := p.Push(ctx, PushRequest{Message: adapter.UnifiedMessage{}})
if res.Pushed {
t.Fatalf("Pushed = true, want false")
}
if res.Route != RouteNone {
t.Fatalf("Route = %q, want %q", res.Route, RouteNone)
}
if active.calls != 0 {
t.Fatalf("active calls = %d, want 0", active.calls)
}
}
// TestPusher_RateLimit exercises the production rateGate over session.RateLimiter:
// with a limit of 1 push per window, only the first of three attempts passes.
func TestPusher_RateLimit(t *testing.T) {
t.Parallel()
active := &mockActive{n: 1}
p := newPusher(active, nil, newRateGate(1, time.Minute, discardLog()), discardLog())
first := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
second := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
third := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
if !first.Pushed || first.Route != RouteActiveClient {
t.Fatalf("first push = %+v, want active_client pushed", first)
}
if second.Pushed || second.Route != RouteRateLimited {
t.Fatalf("second push = %+v, want rate_limited not pushed", second)
}
if third.Pushed || third.Route != RouteRateLimited {
t.Fatalf("third push = %+v, want rate_limited not pushed", third)
}
if active.calls != 1 {
t.Fatalf("active calls = %d, want 1 (rate limit caps the sink)", active.calls)
}
}