name: Tests

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  # Unit and integration tests with Vitest
  unit-tests:
    name: Unit Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Run unit tests
        run: bun run test

      - name: Run integration tests
        run: bun run test:integration

  # E2E tests for web browsers
  e2e-web:
    name: E2E Web (${{ matrix.browser }})
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        browser: [chromium, firefox, webkit]

    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Install Playwright browsers
        run: bunx playwright install --with-deps ${{ matrix.browser }}

      - name: Build application
        run: bun run build

      - name: Run E2E tests
        run: bunx playwright test --project=${{ matrix.browser }}

      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: playwright-report-${{ matrix.browser }}
          path: test-results/
          retention-days: 7

  # Desktop E2E tests (Tauri) - only on main branch
  e2e-desktop:
    name: E2E Desktop
    runs-on: ${{ matrix.os }}
    if: github.ref == 'refs/heads/main'
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Install Playwright browsers
        run: bunx playwright install chromium

      - name: Setup Rust (Ubuntu)
        if: matrix.os == 'ubuntu-latest'
        run: |
          rustup update stable
          rustup default stable

      - name: Setup Rust (macOS/Windows)
        if: matrix.os != 'ubuntu-latest'
        uses: dtolnay/rust-toolchain@stable

      - name: Install Tauri dependencies (Ubuntu)
        if: matrix.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y libgtk-3-dev libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf

      - name: Build Tauri app
        run: bun run tauri:build
        continue-on-error: true

      - name: Run desktop E2E tests
        run: bun run test:e2e:desktop
        continue-on-error: true
        env:
          TAURI: "true"

  # Coverage report
  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Generate coverage
        run: bun run test:coverage
        continue-on-error: true

      - name: Upload coverage
        uses: codecov/codecov-action@v4
        if: always()
        with:
          fail_ci_if_error: false
