name: "[CI] Test"

on:
    push: # Only run on merges into main that modify certain files
        branches:
            - main
        paths:
            - core/**
            - .github/workflows/test.yml

    pull_request: # Only run on merges into main that modify certain files
        branches:
            - main
        paths:
            - core/**
            - .github/workflows/test.yml
    workflow_dispatch:

jobs:
    BuildAndTest:
        runs-on: ubuntu-latest
        env:
            MINICONDA_PYTHON_VERSION: py38
            MINICONDA_VERSION: 4.11.0
        steps:
            - name: Checkout
              uses: actions/checkout@v5
              with:
                  fetch-depth: 3

            # display a git log: when you run CI on PRs, github automatically
            # merges the PR into main and run the CI on that commit. The idea
            # here is to show enough of git log to understand what is the
            # actual commit (in the PR) that we are using. See also
            # 'fetch-depth: 3' above.
            - name: git log
              run: git log --graph -3

            - name: Install node
              uses: actions/setup-node@v5
              with:
                  node-version: 20.x

            - name: Cache node modules
              uses: actions/cache@v4
              env:
                  cache-name: cache-node-modules
              with:
                  # npm cache files are stored in `~/.npm` on Linux/macOS
                  path: ~/.npm
                  key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
                  restore-keys: |
                      ${{ runner.os }}-build-${{ env.cache-name }}-
                      ${{ runner.os }}-build-
                      ${{ runner.os }}-

            - name: setup Miniconda
              uses: conda-incubator/setup-miniconda@v3

            - name: Create and activate virtual environment
              run: |
                  python3 -m venv test_venv
                  source test_venv/bin/activate
                  echo PATH=$PATH >> $GITHUB_ENV
                  echo VIRTUAL_ENV=$VIRTUAL_ENV >> $GITHUB_ENV

            - name: Setup dependencies in virtual environment
              run: |
                  make setup

            - name: Build
              run: make build # Integration tests run in the build step.

            - uses: actions/upload-artifact@v4
              with:
                  name: pyscript
                  path: |
                      core/dist/
                  if-no-files-found: error
                  retention-days: 7
