name: Demo smoke tests

on:
  push:
    branches: [main, 'release/**'] # Adjust to your repository's branch names.
  workflow_dispatch:
  # Uncomment to also run on the default branch every weekday at 06:00 UTC.
  # schedule:
  #   - cron: '0 6 * * 1-5'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  smoke:
    name: Smoke / ${{ matrix.case.id }}
    runs-on: ubuntu-latest
    timeout-minutes: 30
    strategy:
      fail-fast: false
      max-parallel: 2
      matrix:
        # Generic exploratory checks. Replace these with your acceptance scenarios.
        case:
          - id: startup
            prompt: >-
              Launch the application following .junie/demo.md. Verify that its main
              screen loads and that the primary navigation opens its destinations.
              Report broken screens or errors. If there is no navigation, explicitly
              record that and verify the main screen's primary control instead.
          - id: main-flow
            prompt: >-
              Identify one primary user task from the project documentation and UI.
              State the expected visible result before performing the task, then
              complete it through the UI using disposable sample data and verify
              that result. Report PARTIAL if the task or its expected result is unclear.
    defaults:
      run:
        shell: bash
    env:
      JUNIE_API_KEY: ${{ secrets.JUNIE_API_KEY }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.sha }}
          persist-credentials: false

      - name: Check prerequisites
        run: |
          test -n "$JUNIE_API_KEY" || { echo "::error::Add the JUNIE_API_KEY repository secret."; exit 1; }
          docker info > /dev/null
          if [[ ! -s .junie/demo.md || ! -d .junie/vms ]]; then
            echo "::error::Set up /demo locally and commit .junie/demo.md and .junie/vms/."
            exit 1
          fi
          find .junie/vms -name Dockerfile -type f -print -quit | grep -q .
          mkdir -p demo-output
          git rev-parse HEAD > demo-output/tested-sha.txt

      - name: Install Junie
        # Install the public Junie CLI release.
        run: |
          curl -fsSL --retry 3 https://junie.jetbrains.com/install.sh \
            -o "$RUNNER_TEMP/install-junie.sh"
          bash "$RUNNER_TEMP/install-junie.sh"
          echo "$HOME/.local/bin" >> "$GITHUB_PATH"

      - name: Run the smoke scenario
        env:
          SCENARIO: ${{ matrix.case.prompt }}
        run: |
          task="Follow .junie/demo.md to build and launch this checked-out revision.
          Execute this scenario through the actual UI: ${SCENARIO}
          Do not modify or fix application code. Do not commit or push.
          Write observed steps, expected and actual results, and any blocked steps
          to demo-output/assessment.md in the project root (mounted at /workspace).
          After finishing, write exactly one word to demo-output/status.txt:
          PASS only if every required check was executed and its expected result observed;
          FAIL if an executed check failed; PARTIAL if any required check could not be completed.
          Never infer a pass from source code or from the absence of an error."
          junie --auth="$JUNIE_API_KEY" --skip-update-check --demo -p . \
            --output-format json --json-output-file demo-output/demo.json --task "$task"

      - name: Collect recordings and reports
        if: ${{ !cancelled() }}
        run: |
          mkdir -p demo-output/runs
          shopt -s nullglob
          for run_dir in "$HOME"/.junie/sessions/*/demo/*; do
            [[ -d "$run_dir" ]] || continue
            session=$(basename "$(dirname "$(dirname "$run_dir")")")
            mkdir -p "demo-output/runs/$session"
            cp -R "$run_dir" "demo-output/runs/$session/"
          done

      - name: Check the scenario result
        if: ${{ !cancelled() }}
        env:
          CASE_ID: ${{ matrix.case.id }}
        run: |
          mkdir -p demo-output
          status=MISSING
          if [[ -f demo-output/status.txt ]]; then
            status=$(<demo-output/status.txt)
            status=${status%$'\r'}
          fi
          case "$status" in PASS|FAIL|PARTIAL|MISSING) ;; *) status=INVALID ;; esac
          printf '### %s: %s\n\n' "$CASE_ID" "$status" >> "$GITHUB_STEP_SUMMARY"
          if [[ -f demo-output/assessment.md ]]; then
            cat demo-output/assessment.md >> "$GITHUB_STEP_SUMMARY"
          fi
          printf '%s\n' "$status" > demo-output/checked-status.txt
          if [[ "$status" != PASS ]]; then
            echo "::error::Smoke scenario did not pass ($status). See its artifacts and run logs."
            exit 1
          fi

      - name: Upload scenario artifacts
        if: ${{ !cancelled() }}
        uses: actions/upload-artifact@v4
        with:
          name: smoke-${{ matrix.case.id }}-${{ github.run_attempt }}
          path: demo-output/
          if-no-files-found: warn
          retention-days: 14
