name: Test Coverage

on:
  push:
    branches: [main, dev]
    paths:
      - '**/*.js'
      - 'package.json'
      - 'jest.config.js'
  pull_request:
    branches: [main, dev]
    paths:
      - '**/*.js'
      - 'package.json'
      - 'jest.config.js'

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [18.x, 22.x]

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}

      - name: Install dependencies
        run: npm install

      - name: Run linter
        run: npm run lint

      - name: Run tests with coverage
        run: npm test

      - name: Upload coverage reports
        if: matrix.node-version == '18.x'
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage/lcov.info
          flags: unittests
          name: codecov-umbrella
          fail_ci_if_error: false

      - name: Comment PR with coverage
        if: github.event_name == 'pull_request' && matrix.node-version == '18.x'
        uses: romeovs/lcov-reporter-action@v0.3.1
        with:
          lcov-file: ./coverage/lcov.info
          github-token: ${{ secrets.GITHUB_TOKEN }}
          delete-old-comments: true
