# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# This workflow runs live/smoke sanity tests

name: smoke-tests

on:
  workflow_dispatch:
  push:
    branches: ["main"]
  pull_request_target:
    branches: ["main"]
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions:
  pull-requests: read
  contents: read
  id-token: write

env:
  NODE_OPTIONS: --max_old_space_size=8192
  #  DEBUG: pw:browser*                       # PlayWright debug messages
  #  ELECTRON_ENABLE_LOGGING: true            # Electron debug messages
  #  DEBUG: typeagent:*                       # TypeAgent debug messages

jobs:
  shell_and_cli:
    #environment: ${{ github.event_name == 'pull_request_target' && 'development-fork' || 'development' }}      # required for federated credentials
    environment: development-fork
    strategy:
      fail-fast: false
      matrix:
        os: ["ubuntu-latest", "windows-latest"]
        version: [22]

    runs-on: ${{ matrix.os }}

    steps:
      # The following two steps (permissions checks) ensure that only users with write access can run this workflow on a PR (except trusted bots)
      # PRs from forks we check the permissions of the user that triggered the workflow (github.triggering_actor)
      # This means that if a user without write access opens a PR from a fork, they cannot run this workflow
      # Users with write access can still run this workflow on a PR from a fork
      # For PRs from the same repo, we allow the workflow to run as normal
      - name: Get User Permission
        if: ${{ github.event_name == 'pull_request_target' && !contains('github-merge-queue[bot],typeagent-bot[bot]', github.triggering_actor) }}
        id: checkAccess
        uses: actions-cool/check-user-permission@v2
        with:
          require: write
          username: ${{ github.triggering_actor }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Check User Permission
        if: ${{ github.event_name == 'pull_request_target' && !contains('github-merge-queue[bot],typeagent-bot[bot]', github.triggering_actor) && steps.checkAccess.outputs.require-result == 'false' }}
        run: |
          echo "${{ github.triggering_actor }} does not have permissions on this repo."
          echo "Current permission level is ${{ steps.checkAccess.outputs.user-permission }}"
          echo "Job originally triggered by ${{ github.actor }}"
          exit 1

      - if: runner.os == 'Linux'
        run: |
          sudo apt install libsecret-1-0

      - name: Setup Git LF
        run: |
          git config --global core.autocrlf false

      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - uses: dorny/paths-filter@v3
        id: filter
        continue-on-error: true
        with:
          filters: |
            ts:
              - "ts/**"
              - ".github/workflows/smoke-tests.yml"

      - uses: pnpm/action-setup@v4
        if: ${{ steps.filter.outputs.ts != 'false' }}
        name: Install pnpm
        with:
          package_json_file: ts/package.json

      - uses: actions/setup-node@v4
        if: ${{ steps.filter.outputs.ts != 'false' }}
        with:
          node-version: ${{ matrix.version }}
          cache: "pnpm"
          cache-dependency-path: ts/pnpm-lock.yaml

      - name: Install dependencies
        if: ${{ steps.filter.outputs.ts != 'false' }}
        working-directory: ts
        run: |
          pnpm install --frozen-lockfile --strict-peer-dependencies

      - name: Install Playwright Browsers
        if: ${{ steps.filter.outputs.ts != 'false' }}
        run: pnpm exec playwright install --with-deps
        working-directory: ts/packages/shell

      - name: Build
        if: ${{ steps.filter.outputs.ts != 'false' }}
        working-directory: ts
        run: |
          npm run build

      - name: Login to Azure
        if: ${{ steps.filter.outputs.ts != 'false' }}
        uses: azure/login@v2.2.0
        with:
          client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_5B0D2D6BA40F4710B45721D2112356DD }}
          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_39BB903136F14B6EAD8F53A8AB78E3AA }}
          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_F36C1F2C4B2C49CA8DD5C52FAB98FA30 }}

      - name: Get Keys
        if: ${{ steps.filter.outputs.ts != 'false' }}
        run: |
          node tools/scripts/getKeys.mjs --vault build-pipeline-kv
        working-directory: ts

      - name: Test CLI - smoke 
        if: ${{ steps.filter.outputs.ts != 'false' }}     
        run: |
          npm run start:dev 'prompt' 'why is the sky blue'
        working-directory: ts/packages/cli

      - name: Shell Tests - full (windows)
        if: ${{ steps.filter.outputs.ts != 'false' &&  runner.os == 'windows'}}
        timeout-minutes: 60
        run: |
          npm run shell:test
        working-directory: ts/packages/shell

      - name: Shell Tests - smoke (linux)
        if: ${{ steps.filter.outputs.ts != 'false' && runner.os == 'Linux' }}
        timeout-minutes: 60
        run: |
          Xvfb :99 -screen 0 1600x1200x24 & export DISPLAY=:99
          npm run shell:smoke
        working-directory: ts/packages/shell

      - name: Live Tests (Linux)
        if: ${{ steps.filter.outputs.ts != 'false' && runner.os == 'Linux' }}
        timeout-minutes: 60
        run: |
          npm run test:live
        working-directory: ts
        continue-on-error: true

      - name: Clean up Keys
        run: |
          node -e "try{require('fs').unlinkSync('./.env');}catch(e){}"
        working-directory: ts
        if: always()
