# Workspace gate: the same clippy + test commands CLAUDE.md §6 requires
# locally, run on every master push and pull request. Until this workflow
# existed, nothing in CI ran the test suite at all — a direct push to
# master (or the web-wasm-rebuild bot commit) was entirely unguarded.
name: Test

on:
  push:
    branches: [master]
    paths:
      - 'solitaire_app/**'
      - 'solitaire_assetgen/**'
      - 'solitaire_core/**'
      - 'solitaire_data/**'
      - 'solitaire_engine/**'
      - 'solitaire_server/src/**'
      - 'solitaire_server/tests/**'
      - 'solitaire_server/migrations/**'
      - 'solitaire_sync/**'
      - 'solitaire_wasm/**'
      - 'solitaire_web/**'
      - 'Cargo.toml'
      - 'Cargo.lock'
      - '.cargo/**'
      - '.sqlx/**'
      - '.gitea/workflows/test.yml'
  pull_request:
  workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest

    # Full debuginfo made the solitaire_engine test-binary link peak past the
    # runner's memory — ld was OOM-killed (signal 9) on runs 447 and 486.
    # line-tables-only keeps file:line in panic backtraces while cutting the
    # link's memory footprint enough to fit the runner.
    #
    # CARGO_BUILD_JOBS=2: with one job per core, cargo links several large
    # test binaries concurrently; as the workspace grew (runs 514/516/519)
    # two+ simultaneous ld processes OOM-killed the runner again even at
    # line-tables-only. Two jobs keeps at most two links in flight — the
    # compile-throughput cost is small next to the cache-warm build.
    env:
      CARGO_PROFILE_DEV_DEBUG: line-tables-only
      CARGO_BUILD_JOBS: '2'

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust 1.95.0
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: 1.95.0
          components: clippy, rustfmt

      - name: Cache cargo build
        uses: Swatinem/rust-cache@v2

      # Native link deps for the Bevy crates (engine/app/web) on a bare
      # ubuntu runner: ALSA + udev for input/audio, X11 + Wayland for winit.
      - name: Install Bevy native dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y --no-install-recommends \
            libasound2-dev libudev-dev pkg-config libx11-dev libxcursor-dev \
            libxrandr-dev libxi-dev libwayland-dev libxkbcommon-dev

      - name: Format check
        run: cargo fmt --check

      # SQLX_OFFLINE uses the checked-in `.sqlx/` query cache (no live DB),
      # same as the web-e2e workflow's server prebuild.
      - name: Clippy (deny warnings)
        env:
          SQLX_OFFLINE: 'true'
        run: cargo clippy --workspace --all-targets -- -D warnings

      - name: Test
        env:
          SQLX_OFFLINE: 'true'
        run: cargo test --workspace
