name: CI

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

env:
  CARGO_TERM_COLOR: always

# ensure that the workflow is only triggered once per PR, subsequent pushes to the PR will cancel
# and restart the workflow. See https://docs.github.com/en/actions/using-jobs/using-concurrency
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  fmt:
    name: fmt
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2
      - name: check formatting
        run: cargo fmt -- --check
  clippy:
    name: clippy
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2
      # Plain `cargo clippy` fails the build only on error-level diagnostics
      # (deny-by-default lints, the crate's `#![deny(clippy::let_underscore_must_use)]`,
      # or compile errors) — clippy *warnings* do not fail it. The previous
      # rs-clippy-check action ran with `-D warnings`, so a clippy toolchain
      # bump that introduced new style/pedantic warnings across the existing
      # codebase turned the whole job red. Treat warnings as warnings.
      - name: Run clippy (errors fail the build; warnings are allowed)
        run: cargo clippy --all-features --all-targets
  doc:
    # run docs generation on nightly rather than stable. This enables features like
    # https://doc.rust-lang.org/beta/unstable-book/language-features/doc-cfg.html which allows an
    # API be documented as only available in some specific platforms.
    name: doc
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Install Rust nightly
        uses: dtolnay/rust-toolchain@nightly
      - name: Run cargo doc
        run: cargo doc --no-deps --all-features
        env:
          RUSTDOCFLAGS: --cfg docsrs
  schema:
    name: schema
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2
      - name: Generate schema
        run: cargo run --features dev-bins --bin generate_schema > /tmp/config-schema.json
      - name: Check schema is up-to-date
        run: diff -u crates/fresh-editor/plugins/config-schema.json /tmp/config-schema.json

  check-no-plugins:
    name: check (no plugins)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2
      - name: Check build without plugins feature
        run: cargo check --no-default-features --features runtime --all-targets

  test:
    runs-on: ${{ matrix.os }}
    name: test ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      # if your project needs OpenSSL, uncomment this to fix Windows builds.
      # it's commented out by default as the install command takes 5-10m.
      # - run: echo "VCPKG_ROOT=$env:VCPKG_INSTALLATION_ROOT" | Out-File -FilePath $env:GITHUB_ENV -Append
      #   if: runner.os == 'Windows'
      # - run: vcpkg install openssl:x64-windows-static-md
      #   if: runner.os == 'Windows'
      - uses: actions/checkout@v6
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
      - name: Cache Cargo dependencies
        uses: Swatinem/rust-cache@v2
      - name: Install cargo-nextest
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-nextest
      # Install Vulkan + Xvfb for headless GPU testing (Linux only).
      # openssh-server/client back the remote-mode SSH terminal integration
      # test (tests/remote_ssh_terminal.rs), which spins up a throwaway
      # non-root sshd on localhost; it skips itself if these are absent.
      - name: Install Vulkan and Xvfb
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y mesa-vulkan-drivers libvulkan1 xvfb libxkbcommon-x11-0 openssh-server openssh-client
      # enable this ci template to run regardless of whether the lockfile is checked in or not
      - name: cargo generate-lockfile
        if: hashFiles('Cargo.lock') == ''
        run: cargo generate-lockfile
      - name: Run tests (Linux — headless with Xvfb + lavapipe)
        if: runner.os == 'Linux'
        run: xvfb-run cargo nextest run -j=4 --no-fail-fast --locked --all-features --all-targets
        env:
          LIBGL_ALWAYS_SOFTWARE: "1"
          WGPU_BACKEND: "vulkan"
      - name: Run tests
        if: runner.os != 'Linux'
        run: cargo nextest run -j=4 --no-fail-fast --locked --all-features --all-targets
