name: CI

on:
  push:
    branches: [main]
  pull_request:

permissions:
  contents: read

jobs:
  rust-quality:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
      - name: Check formatting
        run: cargo fmt --all --check
      - name: Lint
        run: cargo clippy --workspace --all-targets -- -D warnings

  test:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node: [18, 20, 22]
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-unknown-linux-gnu
      - uses: Swatinem/rust-cache@v2
      - uses: actions/setup-node@v6
        with:
          node-version: ${{ matrix.node }}
      - name: Build engine
        run: cargo build --release --locked --target x86_64-unknown-linux-gnu -p slopgate-rs
      - name: Stage binary into vendor/
        run: node scripts/build-npm-packages.mjs --stage linux-x64
      - run: npm run self-test
      - run: npm test
      - name: Check npm package contents
        run: npm pack --dry-run

  workflow-quality:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Lint GitHub Actions workflows
        uses: docker://rhysd/actionlint:1.7.7
        with:
          args: -color
