name: CI

on:
  push:
    branches: [master]
  pull_request:

concurrency:
  group: ci-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  lint:
    name: Lint (ubuntu-latest)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@1.91.0
        with:
          components: clippy, rustfmt
      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
      - name: Rustfmt
        run: cargo fmt --all -- --check
      - name: Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  coverage:
    name: Coverage (ubuntu-latest)
    if: github.event_name == 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@1.91.0
        with:
          components: llvm-tools-preview
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
      - name: Install tmux
        run: sudo apt-get update && sudo apt-get install -y tmux
      - name: Start persistent tmux server
        run: |
          # Create a dummy session to keep the server running
          tmux new-session -d -s keepalive 'sleep 3600'
      - name: Run coverage
        env:
          TENEX_STATE_PATH: /tmp/tenex-ci-state.json
        run: cargo llvm-cov --all-targets --all-features --profile coverage --fail-under-lines 90 --fail-under-functions 90 -- --test-threads=1

  test:
    name: Test (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    env:
      CARGO_INCREMENTAL: 1
      CARGO_PROFILE_DEV_CODEGEN_UNITS: 16
      CARGO_PROFILE_TEST_CODEGEN_UNITS: 16
      CARGO_PROFILE_DEV_DEBUG: 0
      CARGO_PROFILE_TEST_DEBUG: 0
      CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 16
      CARGO_PROFILE_RELEASE_LTO: false
      CARGO_PROFILE_RELEASE_DEBUG: 0
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@1.91.0
      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
      - name: Install tmux (Ubuntu)
        if: runner.os == 'Linux'
        run: sudo apt-get update && sudo apt-get install -y tmux
      - name: Start persistent tmux server
        if: runner.os == 'Linux'
        run: |
          # Create a dummy session to keep the server running
          tmux new-session -d -s keepalive 'sleep 3600'
      - name: Run tests
        run: cargo test --all-targets --all-features -- --test-threads=1
      - name: Build tenex (release)
        if: runner.os == 'Linux' && github.event_name == 'push'
        run: cargo build --release --locked
      - name: Upload tenex artifact (Linux)
        if: runner.os == 'Linux' && github.event_name == 'push'
        uses: actions/upload-artifact@v4
        with:
          name: tenex-${{ matrix.os }}
          path: target/release/tenex
