A comprehensive testing strategy for the Bardic Inspiration FoundryVTT module using Vitest.
This test suite provides unit, integration, and end-to-end tests for the YouTube DJ module's core functionality, with a focus on multi-user session management, state synchronization, and the complex interaction patterns that occur in real-world usage.
tests/
├── setup/
│ └── test-setup.ts # Test configuration and FoundryVTT mocks
├── unit/ # Unit tests for individual services
│ ├── SessionStore.test.ts # State management tests
│ ├── SessionManager.test.ts # DJ roles and member management
│ └── SocketManager.test.ts # Message handling and broadcasting
├── integration/ # Tests for service interactions
│ └── MultiUserSession.test.ts # Multi-user scenarios
├── e2e/ # End-to-end scenario tests
│ └── GhostUserScenario.test.ts # Complete "ghost user" bug scenario
└── README.md # This file
npm install# Run all tests once
npm run test
# Run tests in watch mode (re-run on changes)
npm run test:watch
# Run tests with coverage report
npm run test:coverage
# Run tests with UI (browser interface)
npm run test:uiSessionStore.test.ts
- Singleton pattern behavior
- State updates and change notifications
- DJ role identification
- Session activity detection
- State recovery after disconnection
- Member cleanup and duplicate handling
SessionManager.test.ts
- DJ role claiming, releasing, and handoff
- GM override functionality
- DJ request approval/denial workflow
- Session member addition and removal
- User join/leave event processing
- Activity tracking and heartbeat handling
SocketManager.test.ts
- Message handler registration and execution
- Socket connection monitoring
- Message validation and filtering
- State synchronization between users
- Fallback communication when disconnected
MultiUserSession.test.ts
- Multiple users joining sessions simultaneously
- DJ handoff between different users
- Session state recovery scenarios
- Heartbeat system and inactive user cleanup
- Complete socket message flows
- State synchronization edge cases
GhostUserScenario.test.ts
- Complete "ghost user" bug reproduction and fix verification
- User disconnect without proper session leave
- Heartbeat-based cleanup of inactive users
- Session state recovery on reconnection
- Multi-user scenario with cleanup and reconnection
- State consistency across all service interactions
The test suite uses comprehensive mocks for FoundryVTT-specific functionality:
- Game Context: User management, socket communication, settings
- UI Notifications: Success/error message display
- YouTube Player API: Video playback control and state
- DOM Operations: Element manipulation and event handling
- Hooks System: FoundryVTT's event system
TestUtils provides helper functions for:
- Resetting all mocks between tests
- Setting up different user contexts (regular user, GM)
- Creating test data (session state, queue items, player state)
- Simulating network conditions
- Waiting for async operations
-
Session Join/Leave Flow
- Users joining existing sessions
- DJ role claiming and management
- Proper USER_JOIN message broadcasting
-
Multi-User State Synchronization
- State changes propagating to all users
- Conflict resolution between simultaneous operations
- Network disconnect/reconnect scenarios
-
Ghost User Bug (Fixed)
- User disconnects without leaving session
- Heartbeat system removes inactive user from persistent state
- User reconnects and session state is properly reset
- User must rejoin session to appear in member list
-
DJ Management
- Role claiming, releasing, and handoff
- Request approval workflow
- GM override capabilities
-
Heartbeat and Cleanup
- Inactive user detection and removal
- Grace period for newly joined users
- Activity tracking and state updates
- Catches regression bugs before they reach production
- Validates complex multi-user interaction scenarios
- Tests edge cases that are difficult to reproduce manually
- Enables safe refactoring of complex systems
- Provides fast feedback during development
- Documents expected behavior through test cases
- Tests real-world usage patterns
- Validates fixes for previously encountered bugs
- Ensures consistent behavior across different user contexts
- Enforces good separation of concerns
- Validates service layer architecture
- Tests error handling and edge cases
- Add unit tests for individual service methods
- Add integration tests for multi-service interactions
- Add E2E tests for complete user scenarios
- Write a failing test that reproduces the bug
- Implement the fix
- Verify the test passes
- Consider edge cases and additional scenarios
describe('FeatureName', () => {
let service: ServiceClass;
beforeEach(() => {
TestUtils.resetMocks();
// Setup test environment
});
describe('Specific Functionality', () => {
it('should handle expected case correctly', async () => {
// Arrange
// Act
// Assert
});
it('should handle edge case properly', async () => {
// Test edge case
});
});
});The test suite aims for:
- >90% line coverage for core services
- 100% coverage for critical paths (DJ role management, session state)
- Complete scenario coverage for user-reported bugs
- Integration test coverage for all service interactions
Tests are designed to run in CI environments:
- Fast execution (all tests complete in <10 seconds)
- No external dependencies required
- Deterministic results with proper mock isolation
- Clear failure messages for debugging
Planned additions to the test suite:
- Performance tests for large session scenarios
- Browser-based widget testing with Playwright
- Queue management comprehensive testing
- YouTube API integration testing with mock player
- Network latency simulation for real-world conditions