name: Backend Integration Tests with MariaDB

on:
  pull_request:
    types: [opened, synchronize]
  push:
    branches:
      - master

permissions:
  contents: read

jobs:
  backend-integration:
    if: "(github.event_name == 'pull_request' && !contains(github.event.pull_request.labels.*.name, 'ops') && !contains(github.head_ref, 'ops/')) || github.event_name == 'push'"
    strategy:
      matrix:
        node: ["24.13.0"]
      fail-fast: false
    runs-on: mempool-ci

    name: Backend Integration Tests - node ${{ matrix.node }}
    
    steps:
      - name: Checkout
        uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 # v3
        with:
          path: ${{ matrix.node }}/integration

      - name: Setup Node
        uses: actions/setup-node@3235b876344d2a9aa001b8d1453c930bba69e610 # v3
        with:
          node-version: ${{ matrix.node }}
          registry-url: "https://registry.npmjs.org"
          cache: 'npm'
          cache-dependency-path: '${{ matrix.node }}/integration/backend/package-lock.json'

      - name: Cache node modules
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
        with:
          path: ${{ matrix.node }}/integration/backend/node_modules
          key: ${{ runner.os }}-backend-integration-node-${{ matrix.node }}-${{ hashFiles('${{ matrix.node }}/integration/backend/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-backend-integration-node-${{ matrix.node }}-
            ${{ runner.os }}-backend-integration-

      - name: Read rust-toolchain file from repository
        id: gettoolchain
        run: echo "::set-output name=toolchain::$(cat ./rust/gbt/rust-toolchain)"
        working-directory: ${{ matrix.node }}/integration

      - name: Cache Rust dependencies
        uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            ${{ matrix.node }}/integration/rust/gbt/target/
          key: ${{ runner.os }}-cargo-integration-${{ hashFiles('${{ matrix.node }}/integration/rust/gbt/**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-integration-
            ${{ runner.os }}-cargo-

      - name: Install ${{ steps.gettoolchain.outputs.toolchain }} Rust toolchain
        uses: dtolnay/rust-toolchain@f7ccc83f9ed1e5b9c81d8a67d7ad1a747e22a561
        with:
          toolchain: ${{ steps.gettoolchain.outputs.toolchain }}

      - name: Install dependencies
        run: bash meta/scripts/check-install-scripts.sh && npm ci
        working-directory: ${{ matrix.node }}/integration/backend

      - name: Build backend
        run: npm run build
        working-directory: ${{ matrix.node }}/integration/backend

      - name: Verify config file exists
        run: |
          ls -la mempool-config.test.json
          echo "Current directory: ${PWD}"
          echo "Config file will be: ${{ github.workspace }}/${{ matrix.node }}/integration/backend/mempool-config.test.json"
          test -f mempool-config.test.json || (echo "ERROR: Config file not found!" && exit 1)
        working-directory: ${{ matrix.node }}/integration/backend

      - name: Run integration tests (DB auto-starts via Jest)
        run: |
          echo "MEMPOOL_CONFIG_FILE=$MEMPOOL_CONFIG_FILE"
          npm run test:integration
        working-directory: ${{ matrix.node }}/integration/backend
        env:
          MEMPOOL_CONFIG_FILE: ${{ github.workspace }}/${{ matrix.node }}/integration/backend/mempool-config.test.json

      - name: Start MariaDB for server test
        run: docker compose -f docker-compose.test.yml up -d
        working-directory: ${{ matrix.node }}/integration/backend

      - name: Wait for MariaDB
        run: |
          echo "Waiting for MariaDB to be ready..."
          for i in {1..30}; do
            if docker compose -f docker-compose.test.yml exec -T db-test mysqladmin ping -h localhost -u mempool_test -pmempool_test --silent 2>/dev/null; then
              echo "MariaDB is ready!"
              break
            fi
            echo "Attempt $i/30..."
            sleep 2
          done
          sleep 3
        working-directory: ${{ matrix.node }}/integration/backend

      - name: Start backend server and verify connectivity
        run: |
          # Start server in background
          node dist/index.js &
          SERVER_PID=$!
          
          # Wait for server to start
          echo "Waiting for server to start..."
          sleep 10
          
          # Check if server is still running
          if ps -p $SERVER_PID > /dev/null 2>&1; then
            echo "Server started successfully and connected to database!"
            kill $SERVER_PID 2>/dev/null || true
            wait $SERVER_PID 2>/dev/null || true
            exit 0
          else
            echo "Server failed to start or exited prematurely"
            exit 1
          fi
        working-directory: ${{ matrix.node }}/integration/backend
        env:
          MEMPOOL_CONFIG_FILE: ${{ github.workspace }}/${{ matrix.node }}/integration/backend/mempool-config.test.json

      - name: Cleanup containers
        if: always()
        run: docker compose -f docker-compose.test.yml down -v
        working-directory: ${{ matrix.node }}/integration/backend

      - name: Display logs on failure
        if: failure()
        run: |
          echo "=== MariaDB logs ==="
          docker compose -f docker-compose.test.yml logs db-test || true
        working-directory: ${{ matrix.node }}/integration/backend

