name: CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  CARGO_TERM_COLOR: always

jobs:
  msrv:
    name: MSRV (1.89)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@1.89
        with:
          components: clippy

      - uses: Swatinem/rust-cache@v2

      - name: Build
        run: cargo build

      - name: Run tests
        run: cargo test

  check:
    name: Check (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]

    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

      - uses: Swatinem/rust-cache@v2

      - name: Check formatting
        run: cargo fmt --check

      - name: Lint
        run: cargo clippy -- -D warnings

      - name: Run tests
        run: cargo test

      - name: Build release
        run: cargo build --release

      - name: Lint (async feature)
        run: cargo clippy --features async -- -D warnings

      - name: Test (async feature)
        run: cargo test --features async

      - name: Lint (async-gemini feature)
        run: cargo clippy --features async-gemini -- -D warnings

      - name: Test (async-gemini feature)
        run: cargo test --features async-gemini

      - name: CLI smoke test
        run: echo "hello" | cargo run -- --format txt

  wasm:
    name: WASM Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown
          components: clippy

      - uses: Swatinem/rust-cache@v2

      - name: Check (no default features)
        run: cargo check --lib --target wasm32-unknown-unknown --no-default-features

      - name: Check (async feature)
        run: cargo check --lib --target wasm32-unknown-unknown --no-default-features --features async

      - name: Check (wasm feature)
        run: cargo check --lib --target wasm32-unknown-unknown --no-default-features --features wasm

      - name: Clippy (wasm)
        run: cargo clippy --lib --target wasm32-unknown-unknown --no-default-features --features wasm -- -D warnings

      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

      - name: Run WASM tests
        run: wasm-pack test --node --no-default-features --features wasm
