name: Rust

# Contains checks:
# - That the code compiles
# - That the code complies with formatting
# - Lints (using clippy) to find errors
# - That crates that are published are publish-able (works most of the time)
# - Testing 
#   - Standard Rust integration and unit tests
#   - Fuzz tests (parser and checker)
#   - WASM edition works tests

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

env:
  CARGO_TERM_COLOR: always
  CACHE_PATHS: |
    ~/.cargo/bin/
    ~/.cargo/registry/index/
    ~/.cargo/registry/cache/
    ~/.cargo/git/db/
    target/
    checker/fuzz/target
    parser/fuzz/target

jobs:
  validity:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_PATHS }}
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Check source is valid
        run: cargo check --workspace

      - name: Check binary
        run: cargo check --bin ezno
      
  formating:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - name: Check Rust formatting with rustfmt
        run: cargo fmt --all --check

      - uses: brndnmtthws/rust-action-cargo-binstall@v1
        with:
          packages: taplo-cli

      - name: Check TOML formatting with taplo
        run: taplo fmt --check **/*/Cargo.toml

  tests:
    needs: validity
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_PATHS }}
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - uses: dorny/paths-filter@v3
        id: changes
        with:
          filters: |
            parser:
              - 'parser/**'
            checker:
              - 'checker/**'

      - name: Run parser tests
        if: steps.changes.outputs.parser == 'true' || github.ref_name == 'main'
        run: |
          cargo test
          
          curl https://esm.sh/v128/react-dom@18.2.0/es2022/react-dom.mjs > react.js
          cargo run -p ezno-parser --example parse react.js
        working-directory: parser

      - name: Run checker specification
        if: (steps.changes.outputs.checker == 'true' && github.event_name != 'pull_request') || github.ref_name == 'main'
        run: cargo test
        working-directory: checker/specification
      
      - name: Run checker specification (w/ staging)
        if: steps.changes.outputs.checker == 'true' && github.event_name == 'pull_request'
        run: cargo test -F staging
        working-directory: checker/specification
        env:
          EZNO_DEBUG: 1

      - name: Run checker tests
        if: steps.changes.outputs.checker == 'true' || github.ref_name == 'main'
        run: |
          # Test checker with the parser features
          cargo test -F ezno-parser 
        working-directory: checker

      - name: Run base tests
        run: cargo test

  extras:
    runs-on: ubuntu-latest
    needs: validity
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_PATHS }}
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      
      - uses: dorny/paths-filter@v3
        id: changes
        with:
          filters: |
            src:
              - 'src/**'
            parser:
              - 'parser/**'
            checker:
              - 'checker/**'

      - uses: brndnmtthws/rust-action-cargo-binstall@v1
        if: steps.changes.outputs.src == 'true' || github.ref_name == 'main'
        with:
          packages: wasm-pack@0.13.0
      - uses: denoland/setup-deno@v1
        if: steps.changes.outputs.src == 'true' || github.ref_name == 'main'
        with:
          deno-version: v1.x
      - uses: actions/setup-node@v4
        if: steps.changes.outputs.src == 'true' || github.ref_name == 'main'
        with:
          node-version: 18
      
      - name: Check parser without extras
        if: steps.changes.outputs.parser == 'true'
        # TODO want `continue-on-error: true` but doesn't report error
        run: 
          cargo check -p ezno-parser --no-default-features

      - name: Check parser generator
        if: steps.changes.outputs.parser == 'true'
        # TODO want `continue-on-error: true` but doesn't report error
        run: 
          cargo test -p ezno-ast-generator

      - name: Check checker without default features
        if: steps.changes.outputs.checker == 'true'
        # TODO want `continue-on-error: true` but doesn't report error
        run: 
          cargo check -p ezno-checker --no-default-features

      - name: Build and test WASM
        if: steps.changes.outputs.src == 'true' || github.ref_name == 'main'
        run: |
          # TODO `cargo check --target wasm32-unknown-unknown --lib` might be good enough

          rustup target add wasm32-unknown-unknown
          npm ci
          npm run build
          npm run run-tests

          node ./dist/cli.cjs info
          deno run -A ./dist/cli.mjs info

          npx -p typescript tsc --strict --pretty ./build/ezno_lib.d.ts
          echo "debug checked with TSC"
          cargo run -p ezno-parser --example parse ./build/ezno_lib.d.ts --type-definition-module
          
          # TODO temp as the types generated can be a bit unpredicatible
          if ${{ contains(fromJSON('["main", "ast-typegen-direct"]'), github.ref_name ) }}; then
            npm run build-release
            npx -p typescript tsc --strict --pretty ./build/ezno_lib.d.ts
          fi
        working-directory: src/js-cli-and-library
        shell: bash

      - uses: actions/upload-artifact@v4
        if: steps.changes.outputs.src == 'true' || github.ref_name == 'main'
        with:
          name: wasm-build
          path: src/js-cli-and-library/dist
          retention-days: 3

  fuzzing_parser:
    needs: validity
    runs-on: ubuntu-latest
    timeout-minutes: 15
    continue-on-error: true
    strategy:
      matrix:
        fuzz-target: [module_roundtrip_naive, module_roundtrip_structured]

    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_PATHS }}
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          
      - uses: dorny/paths-filter@v3
        id: changes
        with:
          filters: |
            parser:
              - 'parser/**'

      - name: Install latest nightly and set it as default
        if: steps.changes.outputs.parser == 'true'
        run: |
          rustup install nightly
          rustup default nightly

      # Current `cargo-fuzz` is broken: https://github.com/kaleidawave/ezno/pull/158#issuecomment-2171431210
      # However `cargo install --git ...` does work below
      # - uses: brndnmtthws/rust-action-cargo-binstall@v1
      #   if: steps.changes.outputs.parser == 'true'
      #   with:
      #     packages: cargo-fuzz

      - name: Install cargo-fuzz
        if: steps.changes.outputs.parser == 'true'
        run: cargo install --git https://github.com/rust-fuzz/cargo-fuzz.git
      
      - name: Run fuzzing
        env:
          SHORT_CIRCUIT: true
        if: steps.changes.outputs.parser == 'true'
        run: |
          if ${{ env.SHORT_CIRCUIT }}; then
            CARGO_TARGET_DIR=../../target cargo fuzz run -s none ${{ matrix.fuzz-target }} -- -timeout=10 -use_value_profile=1 -max_total_time=120
          else
            CARGO_TARGET_DIR=../../target cargo fuzz run -s none ${{ matrix.fuzz-target }} -- -timeout=10 -use_value_profile=1 -max_total_time=300 -fork=1 -ignore_crashes=1
            
            if test -d fuzz/artifacts; then 
              find fuzz/artifacts -type f -print -exec xxd {} \; -exec cargo fuzz fmt -s none module_roundtrip_structured {} \;; false; 
            fi
          fi
        working-directory: parser/fuzz

  fuzzing_checker:
    needs: validity
    runs-on: ubuntu-latest
    timeout-minutes: 15
    continue-on-error: true
    strategy:
      matrix:
        fuzz-target: [check_project_naive]

    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_PATHS }}
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - uses: dorny/paths-filter@v3
        id: changes
        with:
          filters: |
            checker:
              - 'checker/**'

      - name: Install latest nightly and set it as default
        if: steps.changes.outputs.checker == 'true'
        run: |
          rustup install nightly
          rustup default nightly

      - name: Install cargo-fuzz
        if: steps.changes.outputs.checker == 'true'
        run: cargo install --git https://github.com/rust-fuzz/cargo-fuzz.git

      - name: Run fuzzing
        env:
          SHORT_CIRCUIT: true
        if: steps.changes.outputs.checker == 'true'
        run: |
          if ${{ env.SHORT_CIRCUIT }}; then
            cargo fuzz run -s none ${{ matrix.fuzz-target }} -- -timeout=10 -use_value_profile=1 -max_total_time=120
          else
            cargo fuzz run -s none ${{ matrix.fuzz-target }} -- -timeout=10 -use_value_profile=1 -max_total_time=300 -fork=1 -ignore_crashes=1
            
            # if test -d fuzz/artifacts; then 
            #   find fuzz/artifacts -type f -print -exec xxd {} \; -exec cargo fuzz fmt -s none module_roundtrip_structured {} \;; false; 
            # fi
          fi

          ls .
          ls target
        working-directory: checker/fuzz
  
  clippy:
    needs: validity
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: actions/cache@v4
        with:
          path: ${{ env.CACHE_PATHS }}
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Lint code with clippy
        run: cargo clippy

  publish-ability:
    # TODO only need to do for `.toml` file changes
    runs-on: ubuntu-latest
    # `publish --dry-run` reports too many false positives and doesn't catch actual errors. So disabling for now
    if: false
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable

      - uses: dorny/paths-filter@v3
        id: changes
        with:
          filters: |
            manifests:
              - '**/*.toml'

      - name: Check that it will publish to crates.io
        if: steps.changes.outputs.manifests == 'true'
        run: |
          cargo metadata --offline --format-version 1 --no-deps | jq -r ".workspace_members[]" | while read -r _n _v pathInfo ; do
            if ! grep -q "publish = false" "${pathInfo:13:-1}/Cargo.toml"; then
              cd ${pathInfo:13:-1}
              cargo publish --no-verify --dry-run
            fi
          done
        shell: bash
