name: Test Docker images

on:
  workflow_call:
    secrets:
      DOCKERHUB_USERNAME:
        required: true
      DOCKERHUB_TOKEN:
        required: true
  workflow_dispatch:

env:
  POETRY_VERSION: "1.8.2"

jobs:
  test-docker:
    runs-on: [self-hosted, linux, ARM64, langflow-ai-arm64-40gb-ephemeral]
    name: Test docker images
    steps:
      - uses: actions/checkout@v6
      - name: Set container runtime
        run: |
          if command -v docker &> /dev/null; then
            echo "CONTAINER_RUNTIME=docker" >> $GITHUB_ENV
          elif command -v podman &> /dev/null; then
            echo "CONTAINER_RUNTIME=podman" >> $GITHUB_ENV
          else
            echo "Error: Neither docker nor podman found"
            exit 1
          fi
          echo "Using container runtime: ${{ env.CONTAINER_RUNTIME }}"
      - name: Docker System Info and Cleanup
        run: |
          echo "=== Docker System Usage Before Cleanup ==="
          ${{ env.CONTAINER_RUNTIME }} system df || true
          if command -v docker &> /dev/null; then
            docker buildx du || true
          fi

          echo "=== Cleaning up Docker System ==="
          ${{ env.CONTAINER_RUNTIME }} system prune -af --volumes || true
          if command -v docker &> /dev/null; then
            docker buildx prune -af || true
          fi

          echo "=== Docker System Usage After Cleanup ==="
          ${{ env.CONTAINER_RUNTIME }} system df || true
          if command -v docker &> /dev/null; then
            docker buildx du || true
          fi
      - name: Login to Docker Hub
        if: github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build image
        run: |
          ${{ env.CONTAINER_RUNTIME }} build -t langflowai/langflow:latest-dev \
            -f docker/build_and_push.Dockerfile \
            .
      - name: Test image
        run: |
          expected_version=$(cat pyproject.toml | grep version | head -n 1 | cut -d '"' -f 2)
          version=$(${{ env.CONTAINER_RUNTIME }} run --rm --entrypoint bash langflowai/langflow:latest-dev -c "python -c 'from langflow.utils.version import get_version_info; print(get_version_info()[\"version\"])'")
          if [ "$expected_version" != "$version" ]; then
              echo "Expected version: $expected_version"
              echo "Actual version: $version"
              exit 1
          fi

      - name: Cleanup after main image
        run: |
          echo "=== Disk usage before cleanup ==="
          df -h /
          ${{ env.CONTAINER_RUNTIME }} rmi langflowai/langflow:latest-dev || true
          ${{ env.CONTAINER_RUNTIME }} system prune -af || true
          if command -v docker &> /dev/null; then
            docker buildx prune -af || true
          fi
          echo "=== Disk usage after cleanup ==="
          df -h /

      - name: Build backend image
        run: |
          ${{ env.CONTAINER_RUNTIME }} build -t langflowai/langflow-backend:latest-dev \
            --build-arg LANGFLOW_IMAGE=langflowai/langflow:latest-dev \
            -f docker/build_and_push_backend.Dockerfile \
            .
      - name: Test backend image
        run: |
          expected_version=$(cat src/backend/base/pyproject.toml | grep version | head -n 1 | cut -d '"' -f 2)
          version=$(${{ env.CONTAINER_RUNTIME }} run --rm --entrypoint bash langflowai/langflow-backend:latest-dev -c "python -c 'from langflow.utils.version import get_version_info; print(get_version_info()[\"version\"])'")
          if [ "$expected_version" != "$version" ]; then
              echo "Expected version: $expected_version"
              echo "Actual version: $version"
              exit 1
          fi

      - name: Cleanup after backend image
        run: |
          echo "=== Disk usage before cleanup ==="
          df -h /
          ${{ env.CONTAINER_RUNTIME }} rmi langflowai/langflow-backend:latest-dev || true
          ${{ env.CONTAINER_RUNTIME }} system prune -af || true
          if command -v docker &> /dev/null; then
            docker buildx prune -af || true
          fi
          echo "=== Disk usage after cleanup ==="
          df -h /

      - name: Build frontend image
        run: |
          ${{ env.CONTAINER_RUNTIME }} build -t langflowai/langflow-frontend:latest-dev \
            -f docker/frontend/build_and_push_frontend.Dockerfile \
            .
