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) } }