on:
  push:
    branches:
      - main
  pull_request:
name: Install and Test

env:
  FORCE_COLOR: 1
  TURBO_TELEMETRY_DISABLED: 1
  TURBO_NO_UPDATE_NOTIFIER: 1
jobs:
  lambda-tests:
    runs-on: ubuntu-latest
    name: Lambda integration
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 25
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Install
        run: bun ci
      - name: Cache Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
      - name: Test Lambda IT
        run: |
          bun run testlambda
      - name: IT tests
        timeout-minutes: 10
        run: |
          cd packages/it-tests && bun test src/lambda src/cloudrun --run
      - name: Lambda tests
        timeout-minutes: 10
        run: |
          cd packages/lambda && bunx remotion browser ensure && bun test src/test/integration --run
  nextjs-tests:
    runs-on: ubuntu-latest
    name: Next.js SSR build
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 25
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Install
        run: bun ci
      - name: Cache Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
      - name: Test Lambda IT
        run: |
          bun run build
      - name: IT tests
        timeout-minutes: 10
        run: |
          cd packages/player-example && bun run build-site
  browser-tests:
    runs-on: macos-latest
    name: Browser tests
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 25
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Install
        run: bun ci
      - name: Install deps
        run: cd packages/webcodecs && bunx playwright install --with-deps
      - name: Cache Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
      - name: Test Webcodecs
        run: |
          bun run testwebcodecs
      - name: Install E2E deps
        run: cd packages/example && bunx playwright install --with-deps
      - name: Test Example E2E
        run: |
          bun run teste2e
  webrenderer-tests:
    runs-on: macos-latest
    name: Web renderer tests
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 25
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Install
        run: bun ci
      - name: Install deps
        run: cd packages/web-renderer && bunx playwright install --with-deps && cd ../media && bunx playwright install --with-deps
      - name: Cache Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
      - name: Test web renderer
        run: |
          bun run testwebrenderer
  ssr-tests:
    runs-on: ubuntu-latest
    name: SSR integration
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 16
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Install
        run: bun ci
      - name: Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: "3.11"
      - uses: ruby/setup-ruby@master
        with:
          ruby-version: "3.1"
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: "8.4"
      - run: pip install pylint boto3 pytest
      - name: Cache Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
      - name: Test SSR
        timeout-minutes: 8
        run: |
          bun run testssr
  template-tests-check:
    runs-on: ubuntu-latest
    name: Template tests precheck
    outputs:
      should_run: ${{ steps.check.outputs.should_run }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v6
        with:
          node-version: 25
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Check if template tests are affected
        id: check
        run: |
          set +e
          bunx turbo-ignore @remotion/it-tests --task=testtemplates
          status=$?
          set -e

          if [ "$status" -eq 0 ]; then
            echo "should_run=false" >> "$GITHUB_OUTPUT"
            echo "No relevant changes for testtemplates; skipping template-tests."
          elif [ "$status" -eq 1 ]; then
            echo "should_run=true" >> "$GITHUB_OUTPUT"
            echo "Relevant changes detected for testtemplates; running template-tests."
          else
            echo "turbo-ignore failed with exit code $status"
            exit "$status"
          fi
  template-tests:
    name: Template integration on ${{ matrix.os }}
    needs: template-tests-check
    if: needs.template-tests-check.outputs.should_run == 'true'
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
          - windows-latest
    env:
      BUN_INSTALL_CACHE_DIR: ${{ matrix.os == 'windows-latest' && 'D:\.bun\install\cache' || '' }}
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 25
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Cache Bun dependencies (Windows)
        if: matrix.os == 'windows-latest'
        uses: actions/cache@v5
        with:
          path: D:\.bun\install\cache
          key: ${{ matrix.os }}-bun-${{ hashFiles('**/bun.lock') }}
          restore-keys: |
            ${{ matrix.os }}-bun-
      - name: Install
        run: bun ci
      - name: Cache Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
      - name: Test templates
        timeout-minutes: 20
        run: |
          bun run testtemplates
  lint:
    runs-on: ubuntu-latest
    name: Linting + Formatting
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: 25
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Install
        run: bun ci
        env:
          CI: true
      - name: Cache Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
      - name: Perform stylecheck
        timeout-minutes: 10
        run: |
          bun run stylecheck
  build:
    name: Build Node ${{ matrix.node_version }} on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            node_version: 16
          - os: windows-latest
            node_version: 16
          - os: macos-latest
            node_version: 25
    env:
      BUN_INSTALL_CACHE_DIR: ${{ matrix.os == 'windows-latest' && 'D:\.bun\install\cache' || '' }}
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-node@v6
        with:
          node-version: ${{ matrix.node_version }}
      - uses: oven-sh/setup-bun@v2.1.2
        with:
          bun-version: 1.3.3
      - name: Cache Bun dependencies (Windows)
        if: matrix.os == 'windows-latest'
        uses: actions/cache@v5
        with:
          path: D:\.bun\install\cache
          key: ${{ matrix.os }}-bun-${{ hashFiles('**/bun.lock') }}
          restore-keys: |
            ${{ matrix.os }}-bun-
      - name: Install
        run: bun ci
      - name: Cache Turbo
        uses: rharkor/caching-for-turbo@v2.3.11
      - name: Build & Test
        timeout-minutes: 30
        run: |
          bun run ci
