Files
insta-recipe/src/tests/README.md
Giancarmine Salucci 9357bd483a fix
2025-12-21 02:03:05 +01:00

3.7 KiB

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:

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:

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

npm test

Specific test file

npm run test:unit -- scheduler.spec

Watch mode (development)

npm run test:unit -- --watch

Coverage report

npm run test:unit -- --coverage

Test Structure

Each test file follows this pattern:

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:

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:

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

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

npm run test:unit -- --reporter=verbose scheduler.spec

Run single test

npm run test:unit -- scheduler.spec -t "should start when enabled"

Debug in browser

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.