# This script handles the different ways of running Cypress across
# environments.
#
# Ideally Cypress would be run the same way everywhere, but there is
# a bug where the snapshots are generated at different sizes between
# headless and headed - https://github.com/palmerhq/cypress-image-snapshot/issues/67.

# Use the first argument as the switch "mode"
# If no argument is passed, then it defaults to "ci" mode.
case ${1} in
    "local")
        # Run Cypress locally with the headed browser (GUI)
        CYPRESS_local=true ./node_modules/.bin/cypress open
        ;;
    "local-update-snapshots")
        # Update snapshots for the local Cypress GUI version.
        # These are typically larger (see bug comment above) than the
        # versions generated and used by Travis (CI) and Docker.
        CYPRESS_local=true CYPRESS_updateSnapshots=true ./node_modules/.bin/cypress run --browser chrome
        ;;
    "docker")
        # Run Cypress in a headless docker image.
        # This environment is closer to the Travis (CI) environment.
        docker build -t videocontext-cypress-tests .
        # Run with:
        #   --shm-size: Increased shared memory size, otherwise the tests
        #               sometimes hang.
        #   -v: Mounts the host snapshot directory, so any generated snapshots
        #       will be created/modified in the user's filesystem ready to be committed.
        docker run -v `pwd`/test/cypress/snapshots:/cypress/test/cypress/snapshots --shm-size=256m videocontext-cypress-tests
        ;;
    "docker-update-snapshots")
        # Generate new snapshots using the headless docker image.
        # These snapshots should be identical to those generated by Travis (CI)
        docker build -t videocontext-cypress-tests .
        # Run with:
        #   --shm-size: Increased shared memory size, otherwise the tests
        #               sometimes hang.
        #   -v: Mounts the host snapshot directory, so any generated snapshots
        #       will be created/modified in the user's filesystem ready to be committed.
        docker run -v `pwd`/test/cypress/snapshots:/cypress/test/cypress/snapshots -e "CYPRESS_updateSnapshots=true" --shm-size=256m videocontext-cypress-tests
        ;;
    *)
        # Perform the command `cypress run` record the run if `CYPRESS_RECORD_KEY`
        # is set this way we'll only CI will record runs and no non-robots require
        # the record key.
        if [ -z "$CYPRESS_RECORD_KEY" ]
        then
            ./node_modules/.bin/cypress run --browser chrome
        else
            # use chrome on CI so that WebGL works
            ./node_modules/.bin/cypress run --record --browser chrome
        fi
        ;;
esac
