AOSSIE-Org / AOSSIE-Org/DebateAI
Build Comprehensive Test Suite for Go Backend (Routes, WebSockets, & Core Functionality)
- Dominant language
- TypeScript
- Stars
- 84
- Forks
- 198
- Avg merge
- 2d 19h
- Merged PRs (30d)
- 30
Description
The backend infrastructure currently relies partially on manual verification. To ensure architectural stability, regression prevention, and type safety across refactors, we require a formal automated testing strategy. This task involves implementing a multi-layered test suite covering strict unit testing for business logic, integration testing for HTTP endpoints, and concurrency testing for WebSocket streams.
### Technical Implementation Plan
#### 1. Test Environment & Infrastructure
- **Isolation Strategy**: Implement specific test configuration loading that overrides production settings. Tests must execute against a dedicated, ephemeral database instance (e.g., Dockerized Postgres or an in-memory variant if applicable) to prevent data pollution.
- **Database Fixtures**: specific utility functions are needed to seed the database with deterministic state before test suites run and perform `defer` cleanup or transaction rollbacks to maintain atomicity.
- **Execution Tooling**: The suite should be fully executable via `go test ./... -v -race` to catch race conditions in concurrent goroutines, particularly within the WebSocket hub.
#### 2. Service Layer Unit Testing
- **Mocking Dependencies**: Adopt interface-based dependency injection for repositories. Use `testify/mock` or `gomock` to generate mocks for database interaction layers, decoupling service logic from the physical database.
- **Table-Driven Tests**: Utilize Go's table-driven testing pattern for `AuthService` and `VotingService` to efficiently cover permutations of input validators, edge cases (boundary values), and error handling branches.
- **Key Logic Coverage**:
- **Elo Algorithm**: Verify mathematical correctness of rating adjustments in `MatchmakingService`.
- **Token Claims**: Validate JWT payload generation and varying expiration scenarios in `AuthService`.
#### 3. HTTP Integration Testing
- **Testing Methodology**: Utilize `httptest.NewRecorder` and `httptest.NewRequest` to simulate incoming HTTP traffic against the `gin` router. Tests must assert against strict HTTP status codes (200, 201, 400, 401, 403, 500) and validate the structural integrity of JSON response bodies.
- **Middleware Verification**: Assert that the Authentication Middleware correctly blocks unauthorized context propagation and returns 401/403 for invalid or expired tokens.
- **Route Coverage**:
- **Auth**: Validate payload validation rules for Login/Register payloads.
- **Debate**: ensure proper state transitions (e.g., creating a debate returns the correct ID and initial status).
- **Profile**: Confirm `PATCH` requests strictly adhere to partial update semantics.
#### 4. WebSocket & Concurrency Testing
- **Server Lifecycle**: Spin up a `httptest.NewServer` to permit real TCP connections for the WebSocket upgrades during tests.
- **Client Simulation**: Use `gorilla/websocket` client dialer to establish connections. Assert the handshake upgrade process (101 Switching Protocols).
- **Event Propagation**:
- **Broadcast Accuracy**: Verify that a message sent by Client A is received by Client B in the same room via Go channels or `WaitGroup` synchronization.
- **Race Detection**: Heavily stress test the `Hub` explicitly to identify deadlocks or race conditions during concurrent `Register` and `Unregister` events.
---
I would genuinely like to work on this issue and like contributing with others also, from the reference of past few issues in this project, it should have a better testing mechanism to validate that backend work flawlessly. I appreciate if you assign me this issue.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.