##
# Run the Linux E2E flow locally on macOS via Docker.
#
# This mirrors the `e2e-linux` job in `.github/workflows/e2e.yml` so any
# fix that turns the local run green is what CI will run too:
#   - same image: ghcr.io/tinyhumansai/openhuman_ci:latest
#   - same Xvfb + dbus bootstrap (via e2e/docker-entrypoint.sh)
#   - same runner: app/scripts/e2e-run-session.sh (Appium chromium driver
#     attached to CEF on :19222), NOT the retired tauri-driver path.
#
# Usage:
#   # Build the E2E app once (slow — produces the CEF bundle):
#   docker compose -f e2e/docker-compose.yml run --rm e2e \
#     bash -lc "pnpm install --frozen-lockfile && pnpm --filter openhuman-app test:e2e:build"
#
#   # Then run a spec (smoke / mega-flow / etc):
#   docker compose -f e2e/docker-compose.yml run --rm e2e \
#     bash -lc "bash app/scripts/e2e-run-session.sh test/e2e/specs/mega-flow.spec.ts mega-flow"
#
#   # Or run the full suite:
#   docker compose -f e2e/docker-compose.yml run --rm e2e \
#     bash -lc "bash app/scripts/e2e-run-session.sh"
#
# Notes:
#   - The repo is bind-mounted at /workspace so builds + edits round-trip.
#   - Rust target/ and pnpm/npm caches live in named volumes so warm
#     re-runs are fast.
#   - Appium 3 + appium-chromium-driver are installed on first entry by
#     the entrypoint (cached into the npm volume after that).
#
services:
  e2e:
    image: ${OPENHUMAN_CI_IMAGE:-ghcr.io/tinyhumansai/openhuman_ci:latest}
    # Use the bind-mounted local bootstrap (Xvfb + dbus + Appium + pnpm).
    # The image's baked-in /docker-entrypoint.sh is the CI version; the
    # local wrapper additionally lazy-installs Appium and JS deps so a
    # fresh checkout becomes runnable with one command.
    entrypoint: ["/workspace/e2e/docker-local-bootstrap.sh"]
    # Default to an interactive shell so the user can `docker compose run`
    # without an explicit command. CI-style one-shot invocations override
    # this on the command line.
    command: ["bash", "-l"]
    working_dir: /workspace
    volumes:
      - ../:/workspace
      - e2e-cargo-registry:/usr/local/cargo/registry
      - e2e-cargo-git:/usr/local/cargo/git
      - e2e-rust-target:/workspace/target
      - e2e-tauri-target:/workspace/app/src-tauri/target
      - e2e-pnpm-store:/root/.local/share/pnpm/store
      - e2e-npm-global:/usr/lib/node_modules
      # pnpm falls back to a project-local store (./.pnpm-store) when the
      # global store and the destination node_modules sit on different
      # devices — which is exactly our case (named volume vs bind mount).
      # The bind-mount filesystem is slow + flaky for the millions of
      # tiny copies pnpm performs (ENOENT mid-copy), so park the
      # project-local store on its own named volume too.
      - e2e-pnpm-project-store:/workspace/.pnpm-store
      # Isolate node_modules per-platform: the host (macOS arm64) and the
      # container (Linux x64) install different native binaries (rolldown,
      # esbuild, sharp, …). Sharing node_modules over the bind mount means
      # one platform's binaries clobber the other and the build fails with
      # `Cannot find module '@rolldown/binding-linux-x64-gnu'` (or the
      # darwin equivalent when running on the host). Named volumes keep
      # each platform's tree separate.
      - e2e-node-modules:/workspace/node_modules
      - e2e-app-node-modules:/workspace/app/node_modules
      # CEF binary download cache — cef-dll-sys writes to ~/Library/Caches/tauri-cef
      # inside the container. Without a named volume this is lost between runs and
      # every new container re-downloads the ~400 MB CEF archive (which also fails
      # when the container has no outbound DNS / TLS access to the CDN).
      - e2e-cef-cache:/root/Library/Caches/tauri-cef
    environment:
      - DISPLAY=:99
      - CI=true
      # Appium chromium driver downloads a chromedriver matching CEF's
      # Chromium version; cache it next to the repo so re-runs are fast.
      - WDIO_CHROMEDRIVER_CACHE=/workspace/.cache/chromedriver
    # CEF spawns a lot of helper processes; the default IPC namespace
    # sometimes runs out of shm. Bump it to match what GitHub-hosted
    # ubuntu-22.04 runners give CEF.
    shm_size: 2gb

  # Rust inference provider E2E tests (wiremock-based, no live LLM needed).
  # Run: docker compose -f e2e/docker-compose.yml run --rm inference-e2e
  inference-e2e:
    image: ${OPENHUMAN_CI_IMAGE:-ghcr.io/tinyhumansai/openhuman_ci:latest}
    working_dir: /workspace
    volumes:
      - ../:/workspace
      - e2e-cargo-registry:/usr/local/cargo/registry
      - e2e-cargo-git:/usr/local/cargo/git
      - e2e-rust-target:/workspace/target
    entrypoint: ["bash", "-lc"]
    command: ["./scripts/test-rust-inference-e2e.sh"]

volumes:
  e2e-cargo-registry:
  e2e-cargo-git:
  e2e-rust-target:
  e2e-tauri-target:
  e2e-pnpm-store:
  e2e-npm-global:
  e2e-node-modules:
  e2e-app-node-modules:
  e2e-pnpm-project-store:
  e2e-cef-cache:
