name: test

on:
  push:
  schedule:
    - cron: '0 0 1 * *'
  pull_request:
  workflow_dispatch:
    
env:
  CARGO_TERM_COLOR: always
  CARGO_TARGET_WASM32_WASIP1_RUNNER: "wasmtime run --dir ."

permissions:
  contents: read

jobs:
  test_64_bit:
    name: Test Rust on 64-bit
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rust-src
      - name: Test Rust Native (stable)
        run: |
          cargo clippy --verbose --all-targets --features "std rog_experimental" -- -D clippy::all -A deprecated
          cargo test --verbose
          cargo test --verbose --release
          cargo test --verbose --no-default-features --features "rog_experimental"
      - name: Test Rust Native (nightly)
        run: |
          rustup override set nightly
          cargo test --verbose --features "rog_experimental from_slice"
          cargo test --verbose --all-features
      - name: Check dead links in documentation
        if: matrix.os == 'ubuntu-latest'
        run: |
          cargo install cargo-deadlinks
          cargo doc --no-deps --all-features
          cargo deadlinks --dir target/doc | grep -vE '(help\.html|settings\.html)'
      - name: Audit dependencies (security & license)
        if: matrix.os == 'ubuntu-latest'
        run: |
          cargo install cargo-audit
          cargo install cargo-deny
          cargo audit
          cargo deny check

  test_32_bit_linux:
    name: Test on 32-bit Ubuntu
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rust-src
      - name: Test Rust 32-bit Ubuntu
        uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: libc6-dev-i386 linux-libc-dev libc6-i386 gcc-multilib
          version: 1
      - name: run tests
        run: |
          rustup target add i686-unknown-linux-gnu
          cargo test --target i686-unknown-linux-gnu --verbose 
          cargo test --target i686-unknown-linux-gnu --verbose --no-default-features --features "rog_experimental"

  test_wasm:
    name: Test WASM
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rust-src
      - name: Setup WASM/unknown
        uses: jetli/wasm-pack-action@v0.4.0
      - name: Test WASM/unknown
        run: |
          wasm-pack test --chrome --headless -- --no-default-features --verbose
      - name: Install wasmtime
        uses: jcbhmr/setup-wasmtime@v2
      - name: Test WASM/wasip1
        run: |
          rustup target add wasm32-wasip1
          cargo test --target wasm32-wasip1 --verbose
          cargo test --target wasm32-wasip1 --verbose --no-default-features --features "rog_experimental"
      - name: Setup and check Embedded
        run: |
          rustup target add thumbv7m-none-eabi
          cargo check --target thumbv7m-none-eabi --no-default-features
          rustup override set nightly
          rustup target add thumbv7m-none-eabi
          cargo check --target thumbv7m-none-eabi --no-default-features
          sudo apt-get update && sudo apt-get install qemu-system-arm
      - name: Test Embedded (in nightly)
        timeout-minutes: 3
        run: |
          cd tests/embedded
          cargo run
          cd ../..
          rustup override set stable
