package backthink import ( "context" "testing" "time" "git.yeij.top/AskaEth/Cyrene_v2/internal/channel/adapter" "git.yeij.top/AskaEth/Cyrene_v2/internal/channel/session" ) func TestPusher_AutoRoutesIM(t *testing.T) { t.Parallel() // The owner mandate: a default (auto) push reaches ONE channel over the IM // bridge (the bridge picks her [渠道:...] choice, then her most recent platform, // then the whitelist default) — not the active web client by default. active := &mockActive{n: 2} // a web client is online but is NOT the default target 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 (single IM target)", im.count()) } if active.calls != 0 { t.Fatalf("active calls = %d, want 0 (web is not the default target)", active.calls) } } func TestPusher_WebHintRoutesActive(t *testing.T) { t.Parallel() active := &mockActive{n: 1} im := &mockIMAdapter{} p := newPusher(active, im, &mockGate{allow: true}, discardLog()) res := p.Push(context.Background(), PushRequest{ChannelHint: channelWeb, 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 (web hint targets only the active client)", im.count()) } } func TestPusher_ConcreteHintRoutesIM_SingleTarget(t *testing.T) { t.Parallel() // A concrete decision channel is honored over the IM bridge, and exactly ONE // target is used (the IM bridge carries it; the active client is not touched). active := &mockActive{n: 2} im := &mockIMAdapter{} p := newPusher(active, im, &mockGate{allow: true}, discardLog()) res := p.Push(context.Background(), PushRequest{ChannelHint: "kook", 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()) } if active.calls != 0 { t.Fatalf("active calls = %d, want 0 (a concrete hint targets only the IM bridge)", active.calls) } if im.last.Platform != "kook" { t.Fatalf("im platform = %q, want kook", im.last.Platform) } } func TestPusher_NoIMSink_DegradesToActive(t *testing.T) { t.Parallel() // With no IM sink wired (backthink IM seam absent) the push degrades to the // active web client so the owner is still reached rather than dropped. active := &mockActive{n: 1} p := newPusher(active, nil, &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) } } 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, 0, 0, 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) } } // 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") } } // TestPusher_EventBypassesGate verifies an event-driven push skips the PushGate: // with a limit of 1 push per window, three consecutive event pushes are ALL // delivered (owner mandate "该发就发"), each marked Bypassed and each advancing // no cooldown/kind window. func TestPusher_EventBypassesGate(t *testing.T) { t.Parallel() active := &mockActive{n: 1} p := newPusher(active, nil, newRateGate(1, time.Minute, 0, 0, discardLog()), discardLog()) req := PushRequest{Message: adapter.UnifiedMessage{Content: "洗衣机洗好了"}, TriggerKind: TriggerEvent} first := p.Push(context.Background(), req) second := p.Push(context.Background(), req) third := p.Push(context.Background(), req) if !first.Pushed || !first.Bypassed { t.Fatalf("first event push = %+v, want delivered + Bypassed", first) } if !second.Pushed || !second.Bypassed { t.Fatalf("second event push = %+v, want delivered + Bypassed (gate bypassed)", second) } if !third.Pushed || !third.Bypassed { t.Fatalf("third event push = %+v, want delivered + Bypassed (gate bypassed)", third) } if active.calls != 3 { t.Fatalf("active calls = %d, want 3 (events are not rate-capped)", active.calls) } } // TestPusher_HeartbeatStillRateLimited verifies a heartbeat-driven push stays // gate-governed: with a limit of 1 push per window, the first passes and the // second is refused as rate_limited, never Bypassed. func TestPusher_HeartbeatStillRateLimited(t *testing.T) { t.Parallel() active := &mockActive{n: 1} p := newPusher(active, nil, newRateGate(1, time.Minute, 0, 0, discardLog()), discardLog()) req := PushRequest{Message: adapter.UnifiedMessage{Content: "想跟你说声晚安"}, TriggerKind: TriggerHeartbeat} first := p.Push(context.Background(), req) second := p.Push(context.Background(), req) if !first.Pushed || first.Bypassed { t.Fatalf("first heartbeat push = %+v, want delivered non-bypassed", first) } if second.Pushed || second.Route != RouteRateLimited || second.Bypassed { t.Fatalf("second heartbeat push = %+v, want rate_limited non-bypassed", second) } if active.calls != 1 { t.Fatalf("active calls = %d, want 1 (heartbeat still gated)", active.calls) } } // TestPusher_EventBypass_DoesNotAdvanceGate verifies an event-driven push does not // consume the heartbeat limit: after an event push, a following heartbeat push // still gets its own full allowance. func TestPusher_EventBypass_DoesNotAdvanceGate(t *testing.T) { t.Parallel() active := &mockActive{n: 1} p := newPusher(active, nil, newRateGate(1, time.Minute, 0, 0, discardLog()), discardLog()) ev := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{Content: "洗衣机洗好了"}, TriggerKind: TriggerEvent}) hb := p.Push(context.Background(), PushRequest{Message: adapter.UnifiedMessage{Content: "想跟你说声晚安"}, TriggerKind: TriggerHeartbeat}) if !ev.Pushed || !ev.Bypassed { t.Fatalf("event push = %+v, want delivered + Bypassed", ev) } if !hb.Pushed || hb.Bypassed { t.Fatalf("heartbeat push after an event = %+v, want delivered non-bypassed (event did not consume the gate)", hb) } if active.calls != 2 { t.Fatalf("active calls = %d, want 2", active.calls) } }