fix
This commit is contained in:
177
src/tests/README.md
Normal file
177
src/tests/README.md
Normal file
@@ -0,0 +1,177 @@
|
||||
# Scheduler Tests
|
||||
|
||||
This directory contains comprehensive tests for the authentication scheduler service.
|
||||
|
||||
## Test Files
|
||||
|
||||
### `scheduler.spec.ts`
|
||||
Unit tests for the scheduler service covering:
|
||||
- Configuration parsing and defaults
|
||||
- Scheduler lifecycle (start, stop, status)
|
||||
- Environment variable handling
|
||||
- Error conditions
|
||||
|
||||
**Run unit tests:**
|
||||
```bash
|
||||
npm run test:unit -- scheduler.spec
|
||||
```
|
||||
|
||||
### `scheduler.integration.spec.ts`
|
||||
Integration tests covering:
|
||||
- Auth file management
|
||||
- Scheduler timing calculations
|
||||
- Error handling
|
||||
- Path resolution
|
||||
|
||||
**Run integration tests:**
|
||||
```bash
|
||||
npm run test:unit -- scheduler.integration.spec
|
||||
```
|
||||
|
||||
### `fixtures.ts`
|
||||
Test utilities and fixtures:
|
||||
- Mock auth file creation
|
||||
- Environment setup/teardown
|
||||
- Auth file validation
|
||||
- Mock browser context helpers
|
||||
|
||||
## Running Tests
|
||||
|
||||
### All tests
|
||||
```bash
|
||||
npm test
|
||||
```
|
||||
|
||||
### Specific test file
|
||||
```bash
|
||||
npm run test:unit -- scheduler.spec
|
||||
```
|
||||
|
||||
### Watch mode (development)
|
||||
```bash
|
||||
npm run test:unit -- --watch
|
||||
```
|
||||
|
||||
### Coverage report
|
||||
```bash
|
||||
npm run test:unit -- --coverage
|
||||
```
|
||||
|
||||
## Test Structure
|
||||
|
||||
Each test file follows this pattern:
|
||||
|
||||
```typescript
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
|
||||
describe('Feature', () => {
|
||||
beforeEach(() => {
|
||||
// Setup
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Cleanup
|
||||
});
|
||||
|
||||
it('should do something', () => {
|
||||
// Test
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
## Mocking
|
||||
|
||||
### Environment Variables
|
||||
Tests use `setEnv()` helper to manage environment variables:
|
||||
|
||||
```typescript
|
||||
setEnv('AUTH_SCHEDULER_ENABLED', 'true');
|
||||
setEnv('AUTH_SCHEDULER_INTERVAL_HOURS', '12');
|
||||
```
|
||||
|
||||
### Browser Module
|
||||
The `$lib/server/browser` module is mocked to avoid browser initialization in tests:
|
||||
|
||||
```typescript
|
||||
vi.mock('$lib/server/browser', () => ({
|
||||
getBrowser: vi.fn()
|
||||
}));
|
||||
```
|
||||
|
||||
### File System
|
||||
Use `fs` mocks for testing file operations without touching real files.
|
||||
|
||||
## Key Test Scenarios
|
||||
|
||||
### Configuration Tests
|
||||
- Default values when env vars are missing
|
||||
- Custom values from environment
|
||||
- Invalid value handling
|
||||
- Enabled/disabled states
|
||||
|
||||
### Lifecycle Tests
|
||||
- Starting scheduler when enabled
|
||||
- Not starting when disabled
|
||||
- Preventing duplicate starts
|
||||
- Graceful stops
|
||||
- Status reporting
|
||||
|
||||
### Integration Tests
|
||||
- Auth file creation and validation
|
||||
- Path resolution (Docker vs local)
|
||||
- Error handling for missing files
|
||||
- Timing calculations
|
||||
|
||||
## Example Test
|
||||
|
||||
```typescript
|
||||
it('should parse custom interval hours from environment', async () => {
|
||||
setEnv('AUTH_SCHEDULER_ENABLED', 'true');
|
||||
setEnv('AUTH_SCHEDULER_INTERVAL_HOURS', '6');
|
||||
|
||||
const status = getSchedulerStatus();
|
||||
expect(status.config.intervalHours).toBe(6);
|
||||
});
|
||||
```
|
||||
|
||||
## Debugging Tests
|
||||
|
||||
### Print detailed logs
|
||||
```bash
|
||||
npm run test:unit -- --reporter=verbose scheduler.spec
|
||||
```
|
||||
|
||||
### Run single test
|
||||
```bash
|
||||
npm run test:unit -- scheduler.spec -t "should start when enabled"
|
||||
```
|
||||
|
||||
### Debug in browser
|
||||
```bash
|
||||
npm run test:unit -- --inspect-brk scheduler.spec
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
When adding new scheduler features:
|
||||
|
||||
1. Add unit tests in `scheduler.spec.ts`
|
||||
2. Add integration tests if needed in `scheduler.integration.spec.ts`
|
||||
3. Add test fixtures to `fixtures.ts`
|
||||
4. Ensure tests pass: `npm test`
|
||||
5. Check coverage: `npm run test:unit -- --coverage`
|
||||
|
||||
## Known Limitations
|
||||
|
||||
- Browser context operations are not fully tested (requires Playwright browser)
|
||||
- File system operations use real fs (not fully mocked in all tests)
|
||||
- Actual Instagram login flow is not tested (mocked)
|
||||
|
||||
## CI/CD Integration
|
||||
|
||||
These tests run automatically on:
|
||||
- Pull requests
|
||||
- Commits to main branch
|
||||
- Manual workflow dispatch
|
||||
|
||||
See `.github/workflows/test.yml` for CI configuration.
|
||||
Reference in New Issue
Block a user