name: CI/CD

on:
  push:
    branches:
      - main
  pull_request:

# Cancel in-progress runs on the same branch/PR
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    timeout-minutes: 30
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup and Build
        uses: ./.github/actions/setup-and-build

      - name: Cache TypeScript compilation
        uses: actions/cache@v4
        with:
          path: |
            .tsbuildinfo
            node_modules/.cache
          key: ${{ runner.os }}-typescript-${{ hashFiles('**/tsconfig.json', 'app/**/*.ts', 'app/**/*.tsx') }}
          restore-keys: |
            ${{ runner.os }}-typescript-

      - name: Run type check
        run: pnpm run typecheck

      - name: Cache ESLint
        uses: actions/cache@v4
        with:
          path: node_modules/.cache/eslint
          key: ${{ runner.os }}-eslint-${{ hashFiles('.eslintrc*', 'app/**/*.ts', 'app/**/*.tsx') }}
          restore-keys: |
            ${{ runner.os }}-eslint-

      - name: Run ESLint
        run: pnpm run lint

      - name: Run tests
        run: pnpm run test

      - name: Upload test coverage
        uses: actions/upload-artifact@v4
        if: always()
        with:
          name: coverage-report
          path: coverage/
          retention-days: 7
