fix(backthink): 主动推送降频(15min心跳+60min≤1次+推后30min冷却+刚聊过10min冷避) + 情绪约束(分类限频/活跃感知) + 剥离'我决定:push'内部标记
This commit is contained in:
1 parent
03d7050e1e
commit
12031bd13c
14 files changed
+662
-149
No files matched your search
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter"
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/channel/session"
|
||||
)
|
||||
|
||||
func TestPusher_ActiveFirst(t *testing.T) {
|
||||
@@ -126,7 +127,7 @@ func TestPusher_CancelledContext_Denied(t *testing.T) {
|
||||
func TestPusher_RateLimit(t *testing.T) {
|
||||
t.Parallel()
|
||||
active := &mockActive{n: 1}
|
||||
p := newPusher(active, nil, newRateGate(1, time.Minute, discardLog()), discardLog())
|
||||
p := newPusher(active, nil, newRateGate(1, time.Minute, 0, 0, discardLog()), discardLog())
|
||||
|
||||
first := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
|
||||
second := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{}})
|
||||
@@ -145,3 +146,60 @@ func TestPusher_RateLimit(t *testing.T) {
|
||||
t.Fatalf("active calls = %d, want 1 (rate limit caps the sink)", active.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateGate_CooldownAfterSuccess verifies the post-push cooldown: after a
|
||||
// successful delivery a further push is refused until the cooldown elapses,
|
||||
// regardless of the configured window counter.
|
||||
func TestRateGate_CooldownAfterSuccess(t *testing.T) {
|
||||
t.Parallel()
|
||||
t0 := time.Now()
|
||||
g := &rateGate{
|
||||
log: discardLog(),
|
||||
limiter: session.NewRateLimiter(10, time.Hour),
|
||||
cooldown: 30 * time.Minute,
|
||||
kindWindow: 0, // disabled to isolate the cooldown
|
||||
now: func() time.Time { return t0 },
|
||||
lastKind: make(map[PushKind]time.Time),
|
||||
}
|
||||
if !g.Allow(context.Background(), KindCare) {
|
||||
t.Fatal("first push should be allowed")
|
||||
}
|
||||
g.RecordPush(KindCare)
|
||||
if g.Allow(context.Background(), KindReminder) {
|
||||
t.Fatal("push within cooldown should be denied")
|
||||
}
|
||||
g.now = func() time.Time { return t0.Add(31 * time.Minute) }
|
||||
if !g.Allow(context.Background(), KindReminder) {
|
||||
t.Fatal("push after the cooldown has elapsed should be allowed")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRateGate_KindWindowLimitsSameFlavor verifies the per-flavor (问候/提醒/分享/
|
||||
// 关心) limit: the same flavor cannot repeat within the kind window, while a
|
||||
// different flavor and a later same-flavor push (after the window) still pass.
|
||||
func TestRateGate_KindWindowLimitsSameFlavor(t *testing.T) {
|
||||
t.Parallel()
|
||||
t0 := time.Now()
|
||||
g := &rateGate{
|
||||
log: discardLog(),
|
||||
limiter: session.NewRateLimiter(10, time.Hour),
|
||||
cooldown: 0, // disabled to isolate the kind window
|
||||
kindWindow: 2 * time.Hour,
|
||||
now: func() time.Time { return t0 },
|
||||
lastKind: make(map[PushKind]time.Time),
|
||||
}
|
||||
if !g.Allow(context.Background(), KindReminder) {
|
||||
t.Fatal("first reminder should be allowed")
|
||||
}
|
||||
g.RecordPush(KindReminder)
|
||||
if g.Allow(context.Background(), KindReminder) {
|
||||
t.Fatal("same flavor within the kind window should be denied")
|
||||
}
|
||||
if !g.Allow(context.Background(), KindShare) {
|
||||
t.Fatal("a different flavor should be allowed")
|
||||
}
|
||||
g.now = func() time.Time { return t0.Add(3 * time.Hour) }
|
||||
if !g.Allow(context.Background(), KindReminder) {
|
||||
t.Fatal("same flavor after the kind window should be allowed")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user