# It runs all available Cypress tests and sends test data to Cypress Dashboard.
# It executes on every Monday at 01 a.m, and on demand by the user.
# The tests run with a fail-fast strategy, once one fails, the others will not run.
# If a test fails, it creates a set of screenshots showing the errors.
#
# Matrix:
# - Browser: chrome.
# - Test folder: All tests available on the `/tests` folder.
#
# Jobs:
# - Set up the Cypress Environment: `setup-cypress`.
# - Runs all tests on the matrix: `run-all-tests-matrix`.

name: Run Cypress tests with npm packages

on:
  schedule:
    # runs every Monday at 1 a.m
    - cron: "0 1 * * 1"
      filters:
        branches:
          only:
            - stable
  workflow_dispatch:
    inputs:
      # Sends data to Cy
      cypress-record:
        description: Send test data to Cypress Dashboard? Write 'Yes' to send data.
        required: true
        default: "No"

jobs:
  setup-cypress:
    # Setup Cypress environment without cache.
    name: Setup Cypress environment without cache
    runs-on: ubuntu-latest
    steps:
      # 01. Checkout the repository.
      - name: Checkout
        uses: actions/checkout@v4

      # 02. Install a specific version of Node using.
      - name: Use Node.js
        uses: actions/setup-node@v6
        with:
          node-version: ${{ vars.NODE_VERSION }}

      # 03. Install dependencies and verify Cypress
      - name: Install dependencies and verify Cypress
        env:
          # make sure every Cypress install prints minimal information
          CI: 1
        # print Cypress and OS info
        # This next command should use "npm ci" instead of "npm install"
        run: |
          npm ci
          npx cypress verify
          npx cypress info
          npx cypress version
          npx cypress version --component package
          npx cypress version --component binary
          npx cypress version --component electron
          npx cypress version --component node

  run-all-tests-matrix:
    # Runs all tests.
    runs-on: ubuntu-latest
    needs: setup-cypress
    strategy:
      fail-fast: true
      matrix:
        # Define values for browsers from
        browser: ["chrome"]
        # browser: ['chrome', 'edge', 'firefox', 'chromium']
        type: ["all"]
        # type: ['smoke','e2e', 'ui', 'validation']
        # env: ['local', 'public']
    name: Run ${{ matrix.type }} tests on ${{ matrix.browser }}
    steps:
      # 01. Checkout the repository.
      - name: Checkout
        uses: actions/checkout@v4

      # 02. Install a specific version of Node.
      - name: Use Node.js
        uses: actions/setup-node@v6
        with:
          node-version: ${{ vars.NODE_VERSION }}

      # 03a. Decide whether to send data to Cypress Dashboard, or not, for workflow_dispatch and schedule.
      - name: Decide if we send data to Cypress 2
        if: ${{ github.event.inputs.cypress-record == 'Yes' || github.event_name == 'schedule' }}
        run: |
          echo "CY_RECORD_KEY=${{ secrets.CYPRESS_RECORD_KEY }}" >> $GITHUB_ENV
          echo "CY_RECORD_FLAG=-- --record --key " >> $GITHUB_ENV

      # 03a. Decide whether to send data to Cypress Dashboard, or not, for workflow_dispatch.
      - name: Decide if we send data to Cypress 1
        # We don't want to send the tests to Dashboard through workflow dispatch
        if: ${{ github.event.inputs.cypress-record != 'Yes' }}
        # we set the environment variables dynamically to empty in order to avoid
        # recording the test execution to Cypress Dashboard.
        run: |
          echo "CY_RECORD_KEY=" >> $GITHUB_ENV
          echo "CY_RECORD_FLAG=" >> $GITHUB_ENV

      # 04. Run the tests following the initial matrix: by browser and test type
      - name: Run tests by browser
        uses: cypress-io/github-action@v2
        timeout-minutes: 10
        with:
          # 'build' starts the default demo
          build: npm run build
          # 'test:ci' runs tests over Docker image for build context
          # we also send the Cypress Dashboard record key dynamically
          command: npm run test:ci ${{ env.CY_RECORD_FLAG }} ${{ env.CY_RECORD_KEY }}
          record: true
          parallel: true
          group: "${{ matrix.type }} tests on ${{ matrix.browser }}"
          browser: ${{ matrix.browser }}
          config: "video: true"
          spec: |
            cypress/tests/**/*.js
        env:
          # https://github.com/wiris/html-integrations/settings/secrets/actions
          CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }}
          GITHUB_TOKEN: ${{ secrets.GH_CICD_TOKEN }}

      # 05a. Save videos and screenshots as test artifacts
      # https://github.com/actions/upload-artifact
      - name: Upload screenshots
        uses: actions/upload-artifact@master
        # there might be no screenshots created when:
        # - there are no test failures
        # so only upload screenshots if previous step has failed
        if: failure()
        with:
          name: screenshots-${{ matrix.type }}-${{ matrix.browser }}
          path: cypress/screenshots

      # 05b. Upload videos, since they are always be generated.
      - name: Upload videos for all tests
        uses: actions/upload-artifact@master
        with:
          name: videos-${{ matrix.type }}-${{ matrix.browser }}
          path: cypress/videos
