# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [18.x, 23.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm ci
    - name: Prettier Format Check
      run: npx prettier --check "{src,tests}/**/*.{ts,tsx,json}"
    - run: npm run lint
    - run: npm run build
    - run: npm test

    - name: Typecheck E2E Specs
      run: npx tsc -p tests/tsconfig.json --noEmit
    
    - name: Get Playwright version
      id: playwright-version
      run: echo "version=$(node -p "require('@playwright/test/package.json').version")" >> $GITHUB_OUTPUT

    - name: Cache Playwright Browsers
      id: cache-playwright
      uses: actions/cache@v4
      with:
        path: ~/.cache/ms-playwright
        key: playwright-${{ runner.os }}-${{ steps.playwright-version.outputs.version }}

    - name: Install Playwright System Dependencies
      run: npx playwright install-deps chromium

    - name: Install Playwright Browsers (If Not Cached)
      if: steps.cache-playwright.outputs.cache-hit != 'true'
      run: npx playwright install chromium

    - name: Run E2E Tests
      env:
        PLAYWRIGHT_HTML_REPORT: playwright-report/e2e
      run: npm run test:e2e

    - name: Run Visual Tests
      env:
        PLAYWRIGHT_HTML_REPORT: playwright-report/visual
      run: npm run test:visual

    - name: Upload Playwright report
      if: always()
      uses: actions/upload-artifact@v4
      with:
        name: playwright-report-${{ matrix.node-version }}
        path: playwright-report/
        retention-days: 30
