name: Test CI

on:
  pull_request:
  push:
    branches:
      - main

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always
  CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Set up Node.js
        uses: actions/setup-node@v6
        with:
          node-version: 24
          cache: npm

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

      - name: Cache Rust build artifacts
        uses: Swatinem/rust-cache@v2

      - name: Install npm dependencies
        run: npm ci --ignore-scripts

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

      - name: Check JavaScript formatting
        run: npm run fmt:js:check

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

      - name: Run Oxlint
        run: npm run lint:js

      - name: Run npm tests
        run: npm test --ignore-scripts

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov

      - name: Enforce Rust coverage
        run: |
          mkdir -p target/llvm-cov
          cargo llvm-cov --all-features --fail-uncovered-lines 0 --fail-uncovered-functions 0 --show-missing-lines --lcov --output-path target/llvm-cov/lcov.info

      - name: Upload Rust coverage to Codecov
        if: ${{ env.CODECOV_TOKEN != '' }}
        uses: codecov/codecov-action@v6
        with:
          token: ${{ env.CODECOV_TOKEN }}
          files: target/llvm-cov/lcov.info
          flags: rust
          fail_ci_if_error: true

      - name: Upload JavaScript coverage to Codecov
        if: ${{ env.CODECOV_TOKEN != '' }}
        uses: codecov/codecov-action@v5
        with:
          token: ${{ env.CODECOV_TOKEN }}
          files: coverage/lcov.info
          flags: javascript
          fail_ci_if_error: true
