name: Tests

on:
  push:
    branches: [master, main, dev]
  pull_request:
    branches: [master, main]

jobs:
  unit-tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '16'
          cache: 'npm'
      
      - name: Install dependencies
        run: npm ci --ignore-scripts
      
      - name: Run unit tests (parallel)
        run: npm run test:unit:ci

  integration-tests:
    name: Integration Tests
    runs-on: ubuntu-latest
    needs: unit-tests
    
    services:
      mongodb:
        image: mongo:5.0
        ports:
          - 27017:27017
        options: >-
          --health-cmd "mongo --eval 'db.runCommand(\"ping\").ok'"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '16'
          cache: 'npm'
      
      - name: Install dependencies
        run: npm ci --ignore-scripts
      
      - name: Run integration tests (serial)
        run: npm run test:integration:ci

  stress-tests:
    name: Stress Tests (Concurrent Writes)
    runs-on: ubuntu-latest
    needs: unit-tests
    continue-on-error: true
    timeout-minutes: 10
    
    services:
      mongodb:
        image: mongo:5.0
        ports:
          - 27017:27017
        options: >-
          --health-cmd "mongo --eval 'db.runCommand(\"ping\").ok'"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '16'
          cache: 'npm'
      
      - name: Install dependencies
        run: npm ci --ignore-scripts
      
      - name: Run stress tests
        run: npm run test:stress:ci

  full-test-suite:
    name: Full Test Suite with Coverage
    runs-on: ubuntu-latest
    needs: [unit-tests, integration-tests]
    
    services:
      mongodb:
        image: mongo:5.0
        ports:
          - 27017:27017
        options: >-
          --health-cmd "mongo --eval 'db.runCommand(\"ping\").ok'"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '16'
          cache: 'npm'
      
      - name: Install dependencies
        run: npm ci --ignore-scripts
      
      - name: Run full test suite with coverage
        run: npm run test-ci
        env:
          CUSTOMCONNSTR_mongo: mongodb://127.0.0.1:27017/nightscout_test
          API_SECRET: testingsecret123
          HOSTNAME: localhost
          INSECURE_USE_HTTP: true
          PORT: 1337
          NODE_ENV: production
          CI: true
          MONGO_POOL_SIZE: 5
      
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage/lcov.info
          fail_ci_if_error: false
