name: Run Unit Tests

on:
  push:
    branches:
      - main
      - master
  pull_request:
    branches:
      - main
      - master

jobs:
  # Unit tier: an in-memory Postgres (PGlite), so there is no service to start and a database
  # suite cannot skip for want of one.
  unit:
    if: "!contains(github.event.head_commit.message, '[skip ci]')"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '22.x'

      - run: npm ci

      # kempo/kempo-files/kempo-ui/kempo-server are peer dependencies, which npm ci does not install.
      # legacy-peer-deps (set repo-wide in .npmrc, needed so npm ci doesn't ERESOLVE on the several
      # peer paths to kempo-server) also makes a bare `npm install <peer-name>` silently no-op
      # instead of actually placing the package. --no-legacy-peer-deps forces a real install;
      # --force is needed alongside it to get past that same ERESOLVE conflict.
      - name: Install the host for the test gate
        run: npm install --no-save --no-legacy-peer-deps --force kempo@latest kempo-files@latest kempo-ui@latest kempo-server@latest

      # kempo-files reaches into kempo for its own imports, and node resolves through the real path
      # rather than this package's node_modules, so the library needs its own peers present too.
      - name: Satisfy kempo-files' own peers
        run: npm install --no-save --no-legacy-peer-deps --force kempo@latest kempo-server@latest kempo-ui@latest
        working-directory: node_modules/kempo-files

      - name: Run unit tests
        run: npm test


  # Integration tier: the whole suite on a real Postgres. An unreachable database fails it.
  integration:
    if: "!contains(github.event.head_commit.message, '[skip ci]')"
    runs-on: ubuntu-latest
    services:
      # The data-layer suite needs a real database; it skips itself when none is reachable, so this
      # is what makes it actually run rather than silently pass.
      postgres:
        image: postgres:16-alpine
        env:
          POSTGRES_USER: kempo
          POSTGRES_PASSWORD: kempo
          POSTGRES_DB: kempo_user_dirs_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      DATABASE_URL: postgresql://kempo:kempo@localhost:5432/kempo_user_dirs_test
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: '22.x'

      - run: npm ci

      # kempo/kempo-files/kempo-ui/kempo-server are peer dependencies, which npm ci does not install.
      # legacy-peer-deps (set repo-wide in .npmrc, needed so npm ci doesn't ERESOLVE on the several
      # peer paths to kempo-server) also makes a bare `npm install <peer-name>` silently no-op
      # instead of actually placing the package. --no-legacy-peer-deps forces a real install;
      # --force is needed alongside it to get past that same ERESOLVE conflict.
      - name: Install the host for the test gate
        run: npm install --no-save --no-legacy-peer-deps --force kempo@latest kempo-files@latest kempo-ui@latest kempo-server@latest

      # kempo-files reaches into kempo for its own imports, and node resolves through the real path
      # rather than this package's node_modules, so the library needs its own peers present too.
      - name: Satisfy kempo-files' own peers
        run: npm install --no-save --no-legacy-peer-deps --force kempo@latest kempo-server@latest kempo-ui@latest
        working-directory: node_modules/kempo-files

      - name: Apply database schema
        run: npx drizzle-kit push --force

      - name: Run integration tests
        run: npm run test:integration


  # The same suite against sibling checkouts of the host and the library. This extension resolves
  # imports out of both, so it breaks whenever either moves something it reaches into — this job is
  # what surfaces that before a release, and it is advisory rather than blocking.
  siblings:
    if: "!contains(github.event.head_commit.message, '[skip ci]')"
    runs-on: ubuntu-latest
    continue-on-error: true
    services:
      postgres:
        image: postgres:16-alpine
        env:
          POSTGRES_USER: kempo
          POSTGRES_PASSWORD: kempo
          POSTGRES_DB: kempo_user_dirs_test
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    env:
      DATABASE_URL: postgresql://kempo:kempo@localhost:5432/kempo_user_dirs_test
    steps:
      - uses: actions/checkout@v4
        with:
          path: kempo-user-dirs

      - uses: actions/checkout@v4
        with:
          repository: dustinpoissant/kempo
          path: kempo

      - uses: actions/checkout@v4
        with:
          repository: dustinpoissant/kempo-files
          path: kempo-files

      - uses: actions/checkout@v4
        with:
          repository: dustinpoissant/kempo-server
          path: kempo-server

      # kempo's own link:local script links kempo-server and kempo-ui as siblings, so this checkout
      # has to exist even though nothing here imports kempo-ui server-side.
      - uses: actions/checkout@v4
        with:
          repository: dustinpoissant/kempo-ui
          path: kempo-ui

      - uses: actions/setup-node@v4
        with:
          node-version: '22.x'

      - name: Install host
        run: npm ci && npm run link:local
        working-directory: kempo

      - name: Install the library
        run: npm ci && npm run link:local
        working-directory: kempo-files

      - run: npm ci
        working-directory: kempo-user-dirs

      - name: Link sibling checkouts
        run: npm run link:local
        working-directory: kempo-user-dirs

      - name: Apply database schema
        run: npx drizzle-kit push --force
        working-directory: kempo-user-dirs

      - name: Run unit tests
        run: npm test
        working-directory: kempo-user-dirs

      - name: Run integration tests
        run: npm run test:integration
        working-directory: kempo-user-dirs
