name: CI

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

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install jq 1.8.1
        run: |
          curl -Lo jq https://github.com/jqlang/jq/releases/download/jq-1.8.1/jq-linux-amd64
          chmod +x jq
          sudo mv jq /usr/local/bin/jq
          jq --version
          echo "=== Testing jq functionality ==="
          echo '{"test":true}' | jq '.'
          echo "=== Testing invalid query (expect syntax error) ==="
          echo '{"test":true}' | jq '.invalid[' 2>&1 || true
          echo "=== Checking library dependencies ==="
          ldd /usr/local/bin/jq || true

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

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

      - name: Run tests
        run: cargo test --all-features -- --test-threads=1

  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install jq 1.8.1
        run: |
          curl -Lo jq https://github.com/jqlang/jq/releases/download/jq-1.8.1/jq-linux-amd64
          chmod +x jq
          sudo mv jq /usr/local/bin/jq
          jq --version
          echo "=== Testing jq functionality ==="
          echo '{"test":true}' | jq '.'
          echo "=== Testing invalid query (expect syntax error) ==="
          echo '{"test":true}' | jq '.invalid[' 2>&1 || true
          echo "=== Checking library dependencies ==="
          ldd /usr/local/bin/jq || true

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

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

      - name: Install cargo-tarpaulin
        run: cargo install cargo-tarpaulin

      - name: Generate coverage report
        run: cargo tarpaulin --out xml --out html --output-dir coverage -- --test-threads=1

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v5
        with:
          files: coverage/cobertura.xml
          token: ${{ secrets.CODECOV_TOKEN }}
          fail_ci_if_error: true

      - name: Upload coverage artifact
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage/

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

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

      - name: Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings
