# Run Unit test without Harnesses
name: unit-ci

on:
  workflow_dispatch:
  push:
    branches:
      - main
      - beta
    paths:
      - 'src/**'
      - 'test/**'
      - 'package.json'
      - 'package-lock.json'
      - 'tsconfig.json'
      - 'vite.config.ts'
      - 'vitest.config*.ts'
      - 'esbuild.config.mjs'
      - 'eslint.config.mjs'
      - 'eslint.config.common.mjs'
      - 'eslint.community.config.mjs'
      - 'update-workspaces.mjs'
      - 'version-bump.mjs'
      - 'utils/release-*.mjs'
      - 'utils/release-*.unit.spec.ts'
      - 'utils/couchdb/**'
      - 'utils/flyio/**'
      - 'utils/setup/**'
      - '.github/workflows/prepare-release.yml'
      - '.github/workflows/finalise-release.yml'
      - '.github/workflows/release.yml'
      - '.github/workflows/unit-ci.yml'
  pull_request:
    paths:
      - 'src/**'
      - 'test/**'
      - 'package.json'
      - 'package-lock.json'
      - 'tsconfig.json'
      - 'vite.config.ts'
      - 'vitest.config*.ts'
      - 'esbuild.config.mjs'
      - 'eslint.config.mjs'
      - 'eslint.config.common.mjs'
      - 'eslint.community.config.mjs'
      - 'update-workspaces.mjs'
      - 'version-bump.mjs'
      - 'utils/release-*.mjs'
      - 'utils/release-*.unit.spec.ts'
      - 'utils/couchdb/**'
      - 'utils/flyio/**'
      - 'utils/setup/**'
      - '.github/workflows/prepare-release.yml'
      - '.github/workflows/finalise-release.yml'
      - '.github/workflows/release.yml'
      - '.github/workflows/unit-ci.yml'

permissions:
  contents: read

jobs:
  setup-tools:
    name: Self-hosted setup tools
    runs-on: ubuntu-latest
    timeout-minutes: 15
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - 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: Run setup-tool contract tests
        run: npm run test:setup-tools

      - name: Create CouchDB test configuration
        run: |
          cat <<EOF > .test.env
          hostname=http://127.0.0.1:5989/
          dbname=livesync-test-db
          username=admin
          password=testpassword
          EOF

      - name: Start CouchDB
        run: npm run test:docker-couchdb:start

      - name: Provision a versioned LiveSync database
        run: npx dotenv-cli -e .test.env -- env database=setup-tools-ci retry_count=1 retry_delay_ms=0 ./utils/couchdb/couchdb-init.sh

      - name: Verify the Commonlib database version
        run: |
          npx dotenv-cli -e .test.env -- bash -lc 'curl --fail --silent --show-error --user "${username}:${password}" "${hostname}/setup-tools-ci/obsydian_livesync_version" | node --input-type=module -e "import { VER } from \"@vrtmrz/livesync-commonlib/compat/common/types\";let input=\"\";process.stdin.on(\"data\",chunk=>input+=chunk).on(\"end\",()=>{const document=JSON.parse(input);if(document.type!==\"versioninfo\"||document.version!==VER)throw new Error(\"Unexpected LiveSync database version\");})"'

      - name: Stop CouchDB
        if: always()
        run: npm run test:docker-couchdb:stop || true

  unit-test:
    name: Unit Tests
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Install dependencies
        run: npm ci

      - name: Run source checks
        run: npm run check

      - name: Run unit tests suite with coverage
        run: npm run test:unit:coverage

      - name: Run real-Obsidian runner contract tests
        run: npm run test:e2e:obsidian:runner

      - name: Upload coverage report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: unit-coverage-report
          path: coverage/**

  integration-test:
    name: Integration Tests
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Detect LiveSync-owned integration tests
        id: integration_tests
        shell: bash
        run: |
          git ls-files -- ':(glob)**/*.integration.spec.ts' ':(glob)**/*.integration.test.ts' > "$RUNNER_TEMP/livesync-integration-tests.txt"
          if [[ -s "$RUNNER_TEMP/livesync-integration-tests.txt" ]]; then
            echo 'present=true' >> "$GITHUB_OUTPUT"
          else
            echo 'present=false' >> "$GITHUB_OUTPUT"
          fi

      - name: Record delegated integration coverage
        if: ${{ steps.integration_tests.outputs.present != 'true' }}
        run: echo 'No LiveSync-owned integration tests are present. Commonlib integration tests run in the Commonlib package CI.' >> "$GITHUB_STEP_SUMMARY"

      - name: Setup Node.js
        if: ${{ steps.integration_tests.outputs.present == 'true' }}
        uses: actions/setup-node@v4
        with:
          node-version: '24.x'
          cache: 'npm'

      - name: Install dependencies
        if: ${{ steps.integration_tests.outputs.present == 'true' }}
        run: npm ci

      - name: Create environment configuration files
        if: ${{ steps.integration_tests.outputs.present == 'true' }}
        run: |
          cat <<EOF > .env
          BUILD_MODE=dev
          PATHS_TEST_INSTALL=
          EOF
          cat <<EOF > .test.env
          hostname=http://127.0.0.1:5989/
          dbname=livesync-test-db2
          username=admin
          password=testpassword
          minioEndpoint=http://127.0.0.1:9000
          accessKey=minioadmin
          secretKey=minioadmin
          bucketName=livesync-test-bucket
          EOF

      - name: Start CouchDB container
        if: ${{ steps.integration_tests.outputs.present == 'true' }}
        run: npm run test:docker-couchdb:start

      - name: Start MinIO container
        if: ${{ steps.integration_tests.outputs.present == 'true' }}
        run: npm run test:docker-s3:start

      - name: Run integration tests
        if: ${{ steps.integration_tests.outputs.present == 'true' }}
        run: npm run test:integration

      - name: Stop containers
        if: ${{ always() && steps.integration_tests.outputs.present == 'true' }}
        run: |
          npm run test:docker-couchdb:stop || true
          npm run test:docker-s3:stop || true
