name: Cypress E2E SQL Workbench Test

on: [pull_request, push]

env:
  CI: 1
  # avoid warnings like "tput: No value for $TERM and no -T specified"
  TERM: xterm
  OPENSEARCH_DASHBOARDS_VERSION: '2.x'

jobs:
  tests:
    name: Run Cypress E2E SQL Workbench tests
    strategy:
      fail-fast: false
      matrix:
        os: [ ubuntu-latest ]
        jdk: [ 11 ]
    runs-on: ${{ matrix.os }}

    steps:
      - name: Set up JDK
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.jdk }}

      - name: Checkout Branch
        uses: actions/checkout@v3

      - name: Set env
        run: |
          opensearch_version=$(node -p "require('./opensearch_dashboards.json').opensearchDashboardsVersion")
          plugin_version=$(node -p "require('./opensearch_dashboards.json').version")
          echo "OPENSEARCH_VERSION=$opensearch_version" >> $GITHUB_ENV
          echo "OPENSEARCH_PLUGIN_VERSION=$plugin_version" >> $GITHUB_ENV
        shell: bash

      - name: Download SQL artifact
        uses: suisei-cn/actions-download-file@v1.4.0
        with:
          url: https://aws.oss.sonatype.org/service/local/artifact/maven/redirect?r=snapshots&g=org.opensearch.plugin&a=opensearch-sql-plugin&v=${{ env.OPENSEARCH_PLUGIN_VERSION }}-SNAPSHOT&p=zip
          target: plugin-artifacts/
          filename: sql.zip

      - name: Download OpenSearch
        uses: peternied/download-file@v2
        with:
          url: https://artifacts.opensearch.org/snapshots/core/opensearch/${{ env.OPENSEARCH_VERSION }}-SNAPSHOT/opensearch-min-${{ env.OPENSEARCH_VERSION }}-SNAPSHOT-linux-x64-latest.tar.gz
  
      - name: Extract OpenSearch
        run: |
          tar -xzf opensearch-*.tar.gz
          rm -f opensearch-*.tar.gz
        shell: bash
      
      - name: Install SQL plugin
        run: |
          /bin/bash -c "yes | ./opensearch-${{ env.OPENSEARCH_VERSION }}-SNAPSHOT/bin/opensearch-plugin install file:$(pwd)/plugin-artifacts/sql.zip"
        shell: bash
    
      - name: Run OpenSearch
        run: |
          /bin/bash -c "./opensearch-${{ env.OPENSEARCH_VERSION }}-SNAPSHOT/bin/opensearch &"
          sleep 30
        shell: bash
    
      - name: Check OpenSearch Running on Linux
        if: ${{ runner.os != 'Windows'}}
        run: curl http://localhost:9200/
        shell: bash
      
      - name: Show OpenSearch Logs
        if: always()
        run: cat ./opensearch-${{ env.OPENSEARCH_VERSION }}-SNAPSHOT/logs/opensearch.log
        shell: bash
      
      - name: Checkout OpenSearch Dashboards
        uses: actions/checkout@v2
        with:
          path: OpenSearch-Dashboards
          repository: opensearch-project/OpenSearch-Dashboards
          ref: ${{ env.OPENSEARCH_DASHBOARDS_VERSION }}
          fetch-depth: 0
          filter: |
            cypress
            test

      - name: Checkout Query Workbench in OpenSearch Dashboards Plugins Dir
        uses: actions/checkout@v2
        with:
            path: OpenSearch-Dashboards/plugins/dashboards-query-workbench

      - id: tool-versions
        run: |
          echo "node_version=$(cat .node-version)" >> $GITHUB_OUTPUT
          echo "yarn_version=$(jq -r '.engines.yarn' package.json)" >> $GITHUB_OUTPUT
        working-directory: OpenSearch-Dashboards
        shell: bash

      - uses: actions/setup-node@v1
        with:
          node-version: ${{ steps.tool-versions.outputs.node_version }}
          registry-url: 'https://registry.npmjs.org'

      - name: Setup Opensearch Dashboards
        run: |
          npm uninstall -g yarn
          echo "Installing yarn ${{ steps.tool-versions.outputs.yarn_version }}"
          npm i -g yarn@${{ steps.tool-versions.outputs.yarn_version }}
          yarn cache clean
          yarn add sha.js
        working-directory: OpenSearch-Dashboards
        shell: bash

      - name: Boodstrap Opensearch Dashboards
        run: |
          yarn osd bootstrap --single-version=loose
        working-directory: OpenSearch-Dashboards
      
      - name: Run Opensearch Dashboards with Query Workbench Installed
        run: |
          nohup yarn start --no-base-path --no-watch | tee dashboard.log &
        working-directory: OpenSearch-Dashboards

      - name : Check If OpenSearch Dashboards Is Ready
        if: ${{ runner.os == 'Linux' }}
        run: |
          if timeout 600 grep -q "bundles compiled successfully after" <(tail -n0 -f dashboard.log); then
            echo "OpenSearch Dashboards compiled successfully."
          else
            echo "Timeout for 600 seconds reached. OpenSearch Dashboards did not finish compiling."
            exit 1
          fi
        working-directory: OpenSearch-Dashboards

      - name: Install Cypress
        run: |
          npx cypress install
        shell: bash
        working-directory: OpenSearch-Dashboards/plugins/dashboards-query-workbench

      - name: Get Cypress version
        id: cypress_version
        run: |
          echo "::set-output name=cypress_version::$(cat ./package.json | jq '.dependencies.cypress' | tr -d '"')"
        working-directory: OpenSearch-Dashboards/plugins/dashboards-query-workbench

      - name: Run Cypress tests
        run: |
          yarn cypress:run --browser chrome --headless --spec '.cypress/integration/${{ matrix.testgroups }}/*'
        working-directory: OpenSearch-Dashboards/plugins/dashboards-query-workbench
  
      - name: Capture failure screenshots
        uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: cypress-screenshots-${{ matrix.os }}
          path: OpenSearch-Dashboards/plugins/dashboards-query-workbench/.cypress/screenshots
  
      - name: Capture test video
        uses: actions/upload-artifact@v1
        if: failure()
        with:
          name: cypress-videos-${{ matrix.os }}
          path: OpenSearch-Dashboards/plugins/dashboards-query-workbench/.cypress/videos