name: cli-deno-tests

on:
  workflow_dispatch:
    inputs:
      test_task:
        description: 'Deno test task to run'
        type: choice
        options:
          - test
          - test:local
          - test:e2e-matrix
          - test:p2p-sync
        default: test

permissions:
  contents: read

jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      task_matrix: ${{ steps.select.outputs.task_matrix }}
    steps:
      - name: Select task matrix
        id: select
        shell: bash
        run: |
          set -euo pipefail
          SELECTED_TASK="${{ github.event_name == 'workflow_dispatch' && inputs.test_task || 'test' }}"
          echo "[INFO] Selected task set: $SELECTED_TASK"

          case "$SELECTED_TASK" in
            test)
              TASK_MATRIX='["test:setup-put-cat","test:mirror","test:push-pull","test:sync-two-local","test:sync-locked-remote","test:p2p-host","test:p2p-peers","test:p2p-sync","test:p2p-three-nodes","test:p2p-upload-download","test:e2e-couchdb","test:e2e-matrix"]'
              ;;
            test:local)
              TASK_MATRIX='["test:setup-put-cat","test:mirror"]'
              ;;
            test:e2e-matrix)
              TASK_MATRIX='["test:e2e-matrix"]'
              ;;
            test:p2p-sync)
              TASK_MATRIX='["test:p2p-sync"]'
              ;;
            *)
              echo "[ERROR] Unknown task set: $SELECTED_TASK" >&2
              exit 1
              ;;
          esac

          echo "task_matrix=$TASK_MATRIX" >> "$GITHUB_OUTPUT"

  test:
    needs: prepare
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        task: ${{ fromJson(needs.prepare.outputs.task_matrix) }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '24.x'
          cache: 'npm'

      - name: Setup Deno
        uses: denoland/setup-deno@v2
        with:
          deno-version: v2.x

      - name: Install dependencies
        run: npm ci

      - name: Build CLI
        working-directory: src/apps/cli
        run: npm run build

      - name: Create .test.env
        working-directory: src/apps/cli
        run: |
          cat <<EOF > .test.env
          hostname=http://127.0.0.1:5989/
          dbname=livesync-test-db-ci
          username=admin
          password=testpassword
          minioEndpoint=http://127.0.0.1:9000
          accessKey=minioadmin
          secretKey=minioadmin
          bucketName=livesync-test-bucket-ci
          EOF

      - name: Run Deno tests
        working-directory: src/apps/cli/testdeno
        env:
          LIVESYNC_DOCKER_MODE: native
          LIVESYNC_CLI_RETRY: 3
        run: |
          TASK="${{ matrix.task }}"
          echo "[INFO] Running Deno task: $TASK"
          deno task "$TASK"

      - name: Stop leftover containers
        if: always()
        run: |
          docker stop couchdb-test minio-test relay-test >/dev/null 2>&1 || true
          docker rm couchdb-test minio-test relay-test >/dev/null 2>&1 || true
