name: 'test:cypress'

on:
    pull_request:
        branches: [main, release, milestone**, test-cypress]
        paths:
            - 'cypress/**'
            - 'packages/**'
            - '!packages/**/__test__/**'
            - '!packages/**/*.test.[tj]sx?'
            - '!packages/**/*.md'
    push:
        branches: [main, release, milestone**, test-cypress]
        paths:
            - 'cypress/**'
            - 'packages/**'
            - '!packages/**/__test__/**'
            - '!packages/**/*.test.[tj]sx?'
            - '!packages/**/*.md'
            - '.github/**/cypress.yml'

jobs:
    install:
        runs-on: ubuntu-latest
        container:
            # https://github.com/cypress-io/cypress-docker-images/tree/master/browsers
            image: cypress/browsers:node14.17.6-chrome100-ff98
        if: ${{ github.repository_owner == 'DouyinFE' }}
        steps:
            - name: Checkout
              uses: actions/checkout@v3
            - name: Install global packages
              run: npm i -g lerna yarn
            - name: Build storybook
              run: |
                  yarn bootstrap
                  yarn build:lib
                  yarn build-storybook
            - name: Save build folder
              uses: actions/upload-artifact@v2
              with:
                  name: storybook-static
                  if-no-files-found: error
                  path: storybook-static
            - name: Cypress install
              uses: cypress-io/github-action@v2
              with:
                  # Disable running of tests within install job
                  runTests: false
    chrome-tests:
        runs-on: ubuntu-latest
        container:
            image: cypress/browsers:node14.17.6-chrome100-ff98
        needs: install
        steps:
            - name: Checkout
              uses: actions/checkout@v3
            - name: Download the build folders
              uses: actions/download-artifact@v2
              with:
                  name: storybook-static
                  path: storybook-static
            - name: 'Cypress Tests - Chrome'
              uses: cypress-io/github-action@v2
              with:
                  # we have already installed all dependencies above
                  install: true
                  start: npx http-server -p 6006 storybook-static
                  wait-on: 'http://localhost:6006'
                  wait-on-timeout: 120
                  browser: chrome
                  record: true
                  parallel: true
                  group: 'Cypress - Chrome'
                  spec: cypress/integration/*
              env:
                  CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
                  # Recommended: pass the GitHub token lets this action correctly
                  # determine the unique run id necessary to re-run the checks
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    firefox-tests:
        runs-on: ubuntu-latest
        container:
            image: cypress/browsers:node14.17.6-chrome100-ff98
            options: --user 1001
        needs: install
        steps:
            - name: Checkout
              uses: actions/checkout@v3
            - name: Download the build folders
              uses: actions/download-artifact@v2
              with:
                  name: storybook-static
                  path: storybook-static
            - name: 'Cypress Tests - Firefox'
              uses: cypress-io/github-action@v2
              with:
                  # we have already installed all dependencies above
                  install: true
                  start: npx http-server -p 6006 storybook-static
                  wait-on: 'http://localhost:6006'
                  wait-on-timeout: 120
                  browser: firefox
                  record: true
                  parallel: true
                  group: 'Cypress - Firefox'
                  spec: cypress/integration/*
              env:
                  CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
                  # Recommended: pass the GitHub token lets this action correctly
                  # determine the unique run id necessary to re-run the checks
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
