name: Smoke (Phase 10.10)

on:
  push:
    paths:
      - 'src/**'
      - 'examples/**'
      - 'tools/**'
      - 'Cargo.toml'
      - 'Cargo.lock'
      - 'docs/**'
      - '.github/workflows/smoke.yml'
  pull_request:
    paths:
      - 'src/**'
      - 'examples/**'
      - 'tools/**'
      - 'Cargo.toml'
      - 'Cargo.lock'
      - 'docs/**'

jobs:
  smoke:
    runs-on: ubuntu-latest
    env:
      CARGO_TERM_COLOR: always
      # Disable external plugins to keep CI deterministic
      NYASH_DISABLE_PLUGINS: '1'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust (stable)
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry and build
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Run smoke script
        run: bash tools/smoke_phase_10_10.sh

  jit-direct-smoke:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        profile: [debug, release]
    env:
      CARGO_TERM_COLOR: always
      # Disable external plugins to keep CI deterministic
      NYASH_DISABLE_PLUGINS: '1'
      # Ensure lowering runs
      NYASH_JIT_THRESHOLD: '1'
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust (stable)
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry and build
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Build (${{ matrix.profile }}, cranelift-jit)
        run: |
          if [ "${{ matrix.profile }}" = "release" ]; then
            cargo build --release --features cranelift-jit
          else
            cargo build --features cranelift-jit
          fi

      - name: JIT-direct smoke: mir-branch-ret
        run: |
          BIN=./target/${{ matrix.profile }}/nyash
          timeout 15s "$BIN" --jit-direct apps/tests/mir-branch-ret/main.nyash

      - name: JIT-direct smoke: mir-phi-min
        run: |
          BIN=./target/${{ matrix.profile }}/nyash
          timeout 15s "$BIN" --jit-direct apps/tests/mir-phi-min/main.nyash

      - name: JIT-direct smoke: mir-branch-multi
        run: |
          BIN=./target/${{ matrix.profile }}/nyash
          timeout 15s "$BIN" --jit-direct apps/tests/mir-branch-multi/main.nyash

  smoke-compile-events:
    runs-on: ubuntu-latest
    env:
      CARGO_TERM_COLOR: always
      NYASH_DISABLE_PLUGINS: '1'
      NYASH_JIT_EVENTS_COMPILE: '1'
      NYASH_JIT_HOSTCALL: '1'
      NYASH_JIT_EVENTS_PATH: events.jsonl
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust (stable)
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry and build
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: |
            ${{ runner.os }}-cargo-

      - name: Build
        run: cargo build --release -j2 --features cranelift-jit

      - name: Run HH example (compile events)
        run: ./target/release/nyash --backend vm examples/jit_map_get_param_hh.nyash

      - name: Verify events contain phase:lower
        run: |
          test -f events.jsonl
          grep -q '"phase":"lower"' events.jsonl
