Skip to content

Add unit tests for SignalR hub methods #889

@nickna

Description

@nickna

Overview

The SignalR hubs lack unit test coverage. There are 16 hub classes with ~3,744 lines of code and no dedicated unit tests.

Scope

High Priority Hubs

  • TaskHub (192 lines) - Task lifecycle management, subscription methods
  • SecureHub (220 lines) - Base authentication hub, connection lifecycle
  • SpendNotificationHub (355 lines) - Spend threshold alerts

Medium Priority Hubs

  • UsageAnalyticsHub (524 lines) - Analytics streaming
  • VirtualKeyManagementHub (425 lines) - Key management operations
  • HealthMonitoringHub (330 lines) - Health status broadcasts

Lower Priority Hubs

  • MetricsHub, WebhookDeliveryHub, SystemNotificationHub
  • ImageGenerationHub, VideoGenerationHub, ContentGenerationHub
  • AcknowledgmentHub, SecurityMonitoringHub

Test Cases Needed

SecureHub (Base Class)

  • OnConnectedAsync adds connection to virtual key group
  • OnConnectedAsync aborts connection without valid virtual key
  • OnDisconnectedAsync removes from virtual key group
  • RequireVirtualKeyId throws HubException when unauthorized
  • GetVirtualKeyId extracts ID from context correctly
  • CanAccessTaskAsync validates resource ownership

TaskHub

  • SubscribeToTask with valid task ID adds to group
  • SubscribeToTask with invalid task ID throws HubException
  • SubscribeToTask with non-existent task throws "Task not found"
  • SubscribeToTask with unauthorized task throws "Unauthorized access"
  • UnsubscribeFromTask removes from group
  • SubscribeToTaskType adds to type-specific group
  • TaskStarted notifies correct groups
  • TaskProgress sends progress to task group
  • TaskCompleted/TaskFailed/TaskCancelled notify correctly

SpendNotificationHub

  • Threshold subscription and notification
  • Spend alert broadcasting
  • Virtual key group notifications

Implementation Notes

  • Create test base class with mock HubCallerContext and Groups
  • Use Moq to mock IHubContext, IAsyncTaskService, ISignalRAuthenticationService
  • Test both success and error scenarios
  • Verify group membership operations

Related Files

  • /Services/ConduitLLM.Gateway/Hubs/
  • /Tests/ConduitLLM.Tests/Gateway/ (add new Hubs/ directory)

References

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions