# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

# cSpell: ignore vite

# This is heavily inspired by the cypress example configuration found here:
# https://github.com/cypress-io/cypress-realworld-app/blob/develop/.github/workflows/main.yml

---
name: Test Slintpad (cypress)
description: Test Slintpad using cypress

inputs:
  browser:
    description: "The browser to test with"
    required: false
    default: "chrome"
  wasm-binaries:
    description: "The WASM binaries to download and use"
    required: true
    default: wasm
  slintpad-artifact:
    description: "The Slintpad artifacts"
    required: true
    default: slintpad

runs:
  using: composite
  steps:
    - name: Download the WASM binaries
      uses: actions/download-artifact@v3
      with:
        name: ${{ inputs.wasm-binaries }}
    - name: Download the slintpad artifacts
      uses: actions/download-artifact@v3
      with:
        name: ${{ inputs.slintpad-artifact }}
        path: tools/slintpad/dist
    - name: slintpad setup actions
      run: mv ../../../api/wasm-interpreter/pkg wasm-interpreter
      shell: bash
      working-directory: tools/slintpad/dist
    - name: work around assets problem
      run: ln -s . assets
      shell: bash
      working-directory: tools/slintpad/dist/assets

    # Debug filesystem layout
    - name: work around assets problem
      run: |
        ls -alF tools/slintpad/dist
        ls -alF tools/slintpad/dist/assets
        ls -alF tools/slintpad/dist/wasm-interpreter
      shell: bash

    - name: Install NPM dependencies
      run: npm install
      shell: bash
      working-directory: tools/slintpad

    - name: "UI Tests - Chrome"
      uses: cypress-io/github-action@v4
      with:
        browser: ${{ inputs.browser }}
        working-directory: tools/slintpad
        start: npm run start:vite_cypress-preview
        wait-on: 'http://localhost:3001'
        wait-on-timeout: 20
        config-file: cypress.config.ts
      env:
        DEBUG: "cypress:server:args"

    - uses: actions/upload-artifact@v3
      if: failure()
      with:
        name: cypress-screenshots-chrome
        path: tools/slintpad/cypress/screenshots
