name: Unit Tests

on:
  pull_request:
  push:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

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

jobs:
  npm-build-wasm:
    uses: ./.github/workflows/build-wasm.yml

  npm-test-unit:
    runs-on: ubuntu-latest
    needs: npm-build-wasm
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'npm'

      - run: npm install
      - uses: taiki-e/install-action@d4635f2de61c8b8104d59cd4aede2060638378cc
        with:
          tool: wasm-pack

      - name: Download all artifacts
        uses: actions/download-artifact@v4

      - name: Copy prepared wasm
        run: |
          ls -R prepared-wasm
          cp prepared-wasm/kcl_wasm_lib_bg.wasm public
          mkdir rust/kcl-wasm-lib/pkg
          cp prepared-wasm/kcl_wasm_lib* rust/kcl-wasm-lib/pkg

      - name: Copy prepared ts-rs bindings
        run: |
          ls -R prepared-ts-rs-bindings
          mkdir rust/kcl-lib/bindings
          cp -r prepared-ts-rs-bindings/* rust/kcl-lib/bindings/

      - run: npm run simpleserver:bg
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}

      - name: Install Chromium Browser
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}
        run: npm run playwright install chromium --with-deps

      - name: Run unit tests
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}
        run: xvfb-run -a npm run test:unit
        env:
          VITE_KC_DEV_TOKEN: ${{ secrets.KITTYCAD_API_TOKEN_DEV }}

      - name: Check for changes
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' }}
        id: git-check
        run: |
            git add src/lang/std/artifactMapGraphs
            if git status src/lang/std/artifactMapGraphs | grep -q "Changes to be committed"
            then echo "modified=true" >> $GITHUB_OUTPUT
            else echo "modified=false" >> $GITHUB_OUTPUT
            fi

      - name: Commit changes, if any
        if: ${{ github.event_name != 'release' && github.event_name != 'schedule' && steps.git-check.outputs.modified == 'true' }}
        run: |
          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 "Look at this (photo)Graph *in the voice of Nickelback*" || true
          git push
          git push origin ${{ github.head_ref }}

  npm-test-unit-components:
    runs-on: ubuntu-latest
    needs: npm-build-wasm
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version-file: '.nvmrc'
          cache: 'npm'

      - run: npm install
      - uses: taiki-e/install-action@d4635f2de61c8b8104d59cd4aede2060638378cc
        with:
          tool: wasm-pack

      - name: Download all artifacts
        uses: actions/download-artifact@v4

      - name: Copy prepared wasm
        run: |
          ls -R prepared-wasm
          cp prepared-wasm/kcl_wasm_lib_bg.wasm public
          mkdir rust/kcl-wasm-lib/pkg
          cp prepared-wasm/kcl_wasm_lib* rust/kcl-wasm-lib/pkg

      - name: Copy prepared ts-rs bindings
        run: |
          ls -R prepared-ts-rs-bindings
          mkdir rust/kcl-lib/bindings
          cp -r prepared-ts-rs-bindings/* rust/kcl-lib/bindings/

      - name: Run component tests
        run: npm run test:unit:components
