name: Playwright Tests
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions:
  contents: write
  pull-requests: write
  actions: read
  

jobs:

  check-rust-changes:
    runs-on: ubuntu-latest
    outputs:
      rust-changed: ${{ steps.filter.outputs.rust }}

    steps:
      - uses: actions/checkout@v4

      - id: filter
        name: Check for Rust changes
        uses: dorny/paths-filter@v3
        with:
          filters: |
            rust:
              - 'src/wasm-lib/**'

  playwright-ubuntu:
    timeout-minutes: 60
    runs-on: ubuntu-latest-8-cores
    needs: check-rust-changes
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version-file: '.nvmrc'
        cache: 'yarn'
    - uses: KittyCAD/action-install-cli@main
    - name: Install dependencies
      run: yarn
    - name: Install Playwright Browsers
      run: yarn playwright install --with-deps
    - name: Download Wasm Cache
      id: download-wasm
      if: needs.check-rust-changes.outputs.rust-changed == 'false'
      uses: dawidd6/action-download-artifact@v3
      continue-on-error: true
      with:
        github_token: ${{secrets.GITHUB_TOKEN}}
        name: wasm-bundle
        workflow: build-and-store-wasm.yml
        branch: main
        path: src/wasm-lib/pkg
    - name: copy wasm blob
      if: needs.check-rust-changes.outputs.rust-changed == 'false'
      run: cp src/wasm-lib/pkg/wasm_lib_bg.wasm public
      continue-on-error: true
    - name: Setup Rust
      uses: dtolnay/rust-toolchain@stable
    - name: Cache Wasm (because rust diff)
      if: needs.check-rust-changes.outputs.rust-changed == 'true'
      uses: Swatinem/rust-cache@v2
      with:
        workspaces: './src/wasm-lib'
    - name: OR Cache Wasm (because wasm cache failed)
      if: steps.download-wasm.outcome == 'failure'
      uses: Swatinem/rust-cache@v2
      with:
        workspaces: './src/wasm-lib'
    - name: Build Wasm (because rust diff)
      if: needs.check-rust-changes.outputs.rust-changed == 'true'
      run: yarn build:wasm
    - name: OR Build Wasm (because wasm cache failed)
      if: steps.download-wasm.outcome == 'failure'
      run: yarn build:wasm
    - name: build web
      run: yarn build:local
    - name: Run ubuntu/chrome snapshots
      run: yarn playwright test --project="Google Chrome" --update-snapshots e2e/playwright/snapshot-tests.spec.ts
      env:
        CI: true
        token: ${{ secrets.KITTYCAD_API_TOKEN_DEV }}
        snapshottoken: ${{ secrets.KITTYCAD_API_TOKEN }}
    - uses: actions/upload-artifact@v3
      if: always()
      with:
        name: playwright-report
        path: playwright-report/
        retention-days: 30
    - name: check for changes
      id: git-check
      run: |
          git add .
          if git status | grep -q "Changes to be committed"
          then
            echo "::set-output name=modified::true"
          else
            echo "::set-output name=modified::false"
          fi
    - name: Commit changes, if any
      if: steps.git-check.outputs.modified == 'true'
      run: |
        git add .
        git config --local user.email "github-actions[bot]@users.noreply.github.com"
        git config --local user.name "github-actions[bot]"
        git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git
        git fetch origin
        echo ${{ github.head_ref }}
        git checkout ${{ github.head_ref }}
        # TODO when webkit works on ubuntu remove the os part of the commit message
        git commit -am "A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)" || true
        git push
        git push origin ${{ github.head_ref }}
    - name: Run ubuntu/chrome flow
      run: yarn playwright test --project="Google Chrome" e2e/playwright/flow-tests.spec.ts
      env:
        CI: true
        token: ${{ secrets.KITTYCAD_API_TOKEN_DEV }}
    - uses: actions/upload-artifact@v3
      if: always()
      with:
        name: playwright-report
        path: playwright-report/
        retention-days: 30

  playwright-macos:
    timeout-minutes: 60
    runs-on: macos-14
    needs: check-rust-changes
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
      with:
        node-version-file: '.nvmrc'
        cache: 'yarn'
    - name: Install dependencies
      run: yarn
    - name: Install Playwright Browsers
      run: yarn playwright install --with-deps
    - name: Download Wasm Cache
      id: download-wasm
      if: needs.check-rust-changes.outputs.rust-changed == 'false'
      uses: dawidd6/action-download-artifact@v3
      continue-on-error: true
      with:
        github_token: ${{secrets.GITHUB_TOKEN}}
        name: wasm-bundle
        workflow: build-and-store-wasm.yml
        branch: main
        path: src/wasm-lib/pkg
    - name: copy wasm blob
      if: needs.check-rust-changes.outputs.rust-changed == 'false'
      run: cp src/wasm-lib/pkg/wasm_lib_bg.wasm public
      continue-on-error: true
    - name: Setup Rust
      uses: dtolnay/rust-toolchain@stable
    - name: Cache Wasm (because rust diff)
      if: needs.check-rust-changes.outputs.rust-changed == 'true'
      uses: Swatinem/rust-cache@v2
      with:
        workspaces: './src/wasm-lib'
    - name: OR Cache Wasm (because wasm cache failed)
      if: steps.download-wasm.outcome == 'failure'
      uses: Swatinem/rust-cache@v2
      with:
        workspaces: './src/wasm-lib'
    - name: Build Wasm (because rust diff)
      if: needs.check-rust-changes.outputs.rust-changed == 'true'
      run: yarn build:wasm
    - name: OR Build Wasm (because wasm cache failed)
      if: steps.download-wasm.outcome == 'failure'
      run: yarn build:wasm
    - name: build web
      run: yarn build:local
    - name: Run macos/safari flow
      # webkit doesn't work on Ubuntu because of the same reason tauri doesn't (webRTC issues)
      # TODO remove this and the matrix and run all tests on ubuntu when this is fixed
      run: yarn playwright test --project="webkit" e2e/playwright/flow-tests.spec.ts
      env:
        CI: true
        token: ${{ secrets.KITTYCAD_API_TOKEN_DEV }}
    - uses: actions/upload-artifact@v3
      if: always()
      with:
        name: playwright-report
        path: playwright-report/
        retention-days: 30
