# UniSky dev commands. Install just: https://github.com/casey/just
#
# `just --list` shows only the LAST comment line above a recipe, so that line
# is each recipe's one-line summary and any prose goes ABOVE it.

set shell := ["bash", "-uc"]

# THE RECIPES BELOW RUN THIS PROJECT'S OWN TOOLS, AND THEY LIVE IN .venv/bin.
# `unisky` is the console script from `pip install -e .`, and it is what
# frontend/playwright.config.ts starts as its webServer - so on a shell that
# had not activated the venv, `just e2e` (and `just check`, which runs it)
# died before a single test ran:
#
#   [WebServer] /bin/sh: unisky: command not found
#   Error: Process from config.webServer exited early. exit code 127
#
# playwright.config.ts says to run the suite with the project venv on PATH.
# That is a documented precondition nobody can see from `just --list`, and
# `just` is the documented entry point - so the entry point puts it there.
# `python` and `pytest` come along with it: that is the preference `build-fe`
# below already applies to its own interpreter, made once for every recipe.
#
# Harmless when there is no .venv (a PATH entry that is not a directory is
# skipped) and a no-op when the venv is already active, since it is then
# already first.
export PATH := justfile_directory() + "/.venv/bin:" + env_var("PATH")

default: check

# install the python package (editable, dev extras) and frontend deps
setup:
    pip install -e ".[dev]"
    cd frontend && npm install

# python unit tests (realdata tests skip automatically without the volume)
test:
    python -m pytest

# frontend unit tests
test-fe:
    cd frontend && npx vitest run

# THE TWO STEPS ARE ONE COMMAND: `vite build` empties src/unisky/static/, so a
# build without the stamp leaves none, and tests/test_bundle_freshness.py fails
# loudly rather than passing a bundle of unknown age. Commit src/unisky/static/
# with the source change.
# rebuild the viewer bundle into src/unisky/static and stamp it (never a bare `npm run build`)
build-fe:
    cd frontend && npm run build
    # The stamper needs the PROJECT interpreter (it imports pytest), and it
    # runs after `vite build` has already emptied src/unisky/static/ - so a
    # bare `python` that is missing, or is some other environment's, does not
    # merely fail: it leaves no bundle AND no stamp. Prefer the repo's own
    # .venv when there is one; fall back to whatever `python` means, as before.
    PY="{{justfile_directory()}}/.venv/bin/python"; [ -x "$PY" ] || PY=python; \
      "$PY" tests/test_bundle_freshness.py --write

# Offline and instant; the same check the full pytest run makes.
# is the committed viewer bundle the one the committed frontend/ builds?
check-fe-fresh:
    python -m pytest tests/test_bundle_freshness.py -q

# The `unisky` its webServer starts comes from the PATH export at the top of
# this file; without it Playwright exits 127 before the first test.
# Playwright E2E suite over the committed tiny fixture
e2e:
    cd frontend && npm run e2e

# everything CI would run (frontend built first so E2E serves fresh assets)
check: build-fe test test-fe e2e

# build the full RXCJ2211 site (needs the data volume; ~10-40 min, ~2-4 GB)
build-example:
    unisky build examples/rxcj2211.yaml -o build-rxcj2211

# serve the full RXCJ2211 site on http://127.0.0.1:8000
serve-example:
    unisky serve build-rxcj2211 --host 127.0.0.1 --port 8000
