Skip to content

Commit f57fe97

Browse files
authored
add test for socket (#2674)
# Add Comprehensive Tests for Socket Module ## Overview This PR adds comprehensive test coverage for the `socket` module, ensuring robust testing of WebSocket connection handling, authentication, session management, and data persistence functionality. ## Changes - **New Test File**: `backend/tests/test_socket.py` with 30 test cases - **Test Coverage**: Authentication, session management, thread resumption, user environment loading, and edge cases ## Test Suites ### 1. Authentication Tests (11 tests) - **`TestGetTokenFromCookie`**: Token extraction from HTTP cookies - Valid cookie handling - Missing cookie scenarios - Empty cookie handling - **`TestGetToken`**: Token retrieval wrapper functionality - **`TestAuthenticateConnection`**: User authentication flow - Valid token authentication - Missing token handling - Invalid token scenarios - Exception handling ### 2. Session Management Tests (4 tests) - **`TestRestoreExistingSession`**: Session restoration with new socket IDs - Successful restoration - Session not found scenarios - **`TestCleanSession`**: Session cleanup marking - Existing session cleanup - Non-existent session handling ### 3. Data Persistence Tests (4 tests) - **`TestPersistUserSession`**: Session metadata persistence - Persistence with data layer - Behavior without data layer - Empty metadata handling ### 4. Thread Resumption Tests (8 tests) - **`TestResumeThread`**: Thread restoration functionality - Missing data layer handling - User authorization checks - Thread not found scenarios - Successful thread resumption - JSON string metadata parsing - Empty metadata handling ### 5. User Environment Tests (6 tests) - **`TestLoadUserEnv`**: User environment variable loading - Valid JSON parsing - Required key validation - Missing required keys error handling - Empty JSON handling - Bug documentation for `None` input with required keys ### 6. Edge Cases Tests (9 tests) - **`TestSocketEdgeCases`**: Boundary conditions and error scenarios - `None` session ID handling - Empty metadata persistence - Empty JSON environments - Exception propagation ## Coverage Details - **Total Tests**: 30 - **Async Tests**: 17 (using `pytest.mark.asyncio`) - **Mocking Strategy**: Comprehensive use of `AsyncMock` and `Mock` for external dependencies - **Cross-Platform**: Compatible with Windows and Linux environments ## Key Features - **Proper Mocking**: All external dependencies (data layer, config, authentication) are mocked - **Error Handling**: Tests cover both success and failure scenarios - **Bug Documentation**: Tests document existing bugs in the codebase (e.g., `UnboundLocalError` in `load_user_env`) - **Session Cleanup**: Proper cleanup of `user_sessions` dictionary in tests to avoid side effects ## Testing Approach - Uses `unittest.mock` for patching external dependencies - Employs `pytest-asyncio` for async function testing - Maintains test isolation with proper setup and teardown - Follows existing test patterns from other test files Contribution by Gittensor, learn more at https://gittensor.io/
1 parent 06fc867 commit f57fe97

File tree

1 file changed

+479
-0
lines changed

1 file changed

+479
-0
lines changed

0 commit comments

Comments
 (0)