set positional-arguments

cargo := "rustup run stable cargo"
# What coverage leaves out: the tests themselves, the harness, examples and the standard library.
ignore := "(tests/|tests\\.rs|harness/|examples/|rustlib/)"

# The view, into web/dist/index.html, which hud-server embeds.
web:
    cd web && npm run build

# The `hud` binary, with the view it embeds built first.
build: web
    {{cargo}} build --release -p hud

# The server at http://localhost:8787 (MCP at /mcp), built with the view.
serve: web
    {{cargo}} run -p hud -- serve

# The dev host at http://localhost:5173/dev/: the view in an iframe as an MCP Apps host shows it, its tool calls sent to `just serve`.
dev:
    cd web && npm run dev

# What a change must pass: rustfmt, clippy with warnings denied, the tests under `cov`'s gate, svelte-check, the renderer and the shell drawn on a server and hydrated (`ssr`), the shell's unit tests (`unit`), `lint` and `lex`.
check: web lint lex
    {{cargo}} fmt --check
    {{cargo}} clippy --all-targets -- -D warnings
    just cov
    cd web && npm run check
    cd web && npm run ssr
    cd web && npm run unit

# Every crate's tests under cargo-llvm-cov, failing under 100% of lines or functions, and naming each line no test runs. The stable toolchain's own binaries go first on the PATH: another install's rustc (Homebrew's) writes profiles that rustup's llvm-profdata refuses. Needs `cargo install cargo-llvm-cov` and `rustup component add llvm-tools-preview` once; .knotless/workflows/test.yml runs the same.
cov:
    PATH="$(dirname "$(rustup which --toolchain stable cargo)"):$PATH" {{cargo}} llvm-cov --workspace --locked --ignore-filename-regex '{{ignore}}' --fail-under-lines 100 --fail-under-functions 100 --show-missing-lines

# The workspace's components each use only their own layer and those below (harness/layers.mjs), every text colour reads at 4.5:1 in light and dark (harness/contrast.mjs), and the docs keep docs/_style.md's shape, links and punctuation (harness/docs.mjs).
lint:
    node harness/layers.mjs
    node harness/contrast.mjs
    node harness/docs.mjs

# The workspace drawn from web/dist/workspace.html in front of fixture answers, each given its `_meta.ui` by `hud resolve`, as pictures in target/look: `just look tabs --press n`. Its options are at the top of harness/look.mjs.
look *args: web
    {{cargo}} build -p hud
    node harness/look.mjs "$@"

# Every state of the workspace worth a look (harness/look/states.mjs), afresh in target/look; or only those named: `just looks empty badges`.
looks *names: web
    {{cargo}} build -p hud
    node harness/look/states.mjs {{names}}

# The landing's picture, crates/hud-server/hero/, made again: the `hero` state in every theme, two stills and one animated WebP cycling through them (harness/look/hero.mjs). Needs cwebp, img2webp and sips.
hero: web
    {{cargo}} build -p hud
    node harness/look/hero.mjs

# The gallery's data, target/gallery/gallery.json: every drawing HUD makes as a tile, each answer in harness/gallery/answers.json (recorded, or made up as the signed-in account) given its `_meta.ui` by `hud resolve` and held to the tree lexicon (harness/gallery/data.mjs). Record the answers again with `node harness/gallery/record.mjs [<HUD url>]`.
gallery-data:
    {{cargo}} build -p hud
    node harness/gallery/data.mjs

# The trees the renderer's server test draws (web/renderer/test/ssr.mjs, `cd web && npm run ssr`), web/renderer/test/trees: Grain's screens, the gallery's recorded answers and some made of them, each given its `_meta.ui` by `hud resolve` and held to the tree lexicon (harness/ssr/data.mjs).
ssr-data:
    {{cargo}} build -p hud
    node harness/ssr/data.mjs

# The gallery on the web: every tile of target/gallery/gallery.json on one canvas, in front of a stand-in (harness/gallery.mjs), kept running and opened in the browser; `just gallery --shot` writes the strip's pictures to target/gallery instead.
gallery *args: web gallery-data
    node harness/gallery.mjs "$@"

# The reference PDS and a PLC on free ports, with alice, bob and carol (`<name>.test`, password `password`), as one JSON line in target/test-net.json (harness/pds.mjs). Stop it with ctrl-c. Needs the Node .node-version names, and `cd harness && npm ci` once.
pds:
    mkdir -p target
    tail -f /dev/null | node harness/pds.mjs > target/test-net.json

# The workspace used against a real server (harness/e2e): a world of its own each run, the PDS, `hud serve` over it behind https, and the flows in Chromium, signing in on the PDS's own pages. Arguments go to Playwright: `just e2e -g reload`. HUD's log goes to target/e2e/hud.log. Too slow for `check`.
e2e *args: web
    {{cargo}} build -p hud
    cd harness && npx playwright test -c e2e/playwright.config.mjs "$@"

# The same flows against the spaces alpha (harness/spaces: `cd harness/spaces && npm ci` once), HUD keeping canvases in people's spaces (HUD_SPACES=on).
e2e-spaces *args: web
    {{cargo}} build -p hud
    cd harness && HUD_DEV_ENV=harness/spaces/node_modules/@atproto/dev-env npx playwright test -c e2e/playwright.config.mjs "$@"

# HUD against a PDS of its own, to try by hand: the e2e world kept running (alice, bob and carol, password `password`), and a Chromium window at the workspace that reaches it; `--spaces` on the spaces alpha, `--ios [--device <name>]` the iOS app too, on a simulator, built and pointed at it. Stop it with ctrl-c (harness/local.mjs). `just local-setup` once first.
local *args: web
    {{cargo}} build -p hud
    node harness/local.mjs "$@"

# What `just local` needs, once per checkout and Mac: the harness's packages and the spaces alpha, this checkout's own certificate authority (harness/tls.mjs, in .local/tls), and, for the simulator, which resolves names as the Mac does, `hud.harness.internal` at 127.0.0.1 in /etc/hosts (macOS asks for the password, the one time it is missing, in a dialog of its own, so it works from any terminal or agent).
local-setup:
    cd harness && npm ci
    cd harness/spaces && npm ci
    node -e "import('./harness/tls.mjs').then((t) => console.log('authority: ' + t.certificates().ca))"
    grep -qE '^127\.0\.0\.1[[:space:]]+hud\.harness\.internal([[:space:]]|$)' /etc/hosts || osascript -e 'do shell script "echo \"127.0.0.1 hud.harness.internal\" >> /etc/hosts" with prompt "HUD: map hud.harness.internal to 127.0.0.1 for the local stack" with administrator privileges'

# What the workspace and the server hold in memory over a session (harness/e2e/memory.spec.mjs): three canvases of public feeds, galleries, documents and players, the big feeds scrolled, the tabs switched, 200 tool calls; per stage the page's heap, DOM nodes, pictures and image memory, and the server's RSS (release build), also in target/memory/<label>.json, and the server's caches as it stopped. Reads the public network. Not in `check` or `e2e`.
memory label="run": web
    {{cargo}} build --release -p hud
    cd harness && HUD_MEMORY=1 HUD_MEMORY_LABEL={{label}} npx playwright test -c e2e/playwright.config.mjs
    grep 'caches at shutdown' target/e2e/hud.log || true

# How smoothly a long feed scrolls in a workspace column, and what it holds once scrolled through (harness/scroll.mjs): the renderer's Tree over 300 made-up posts, resolved by `hud resolve`, built on its own, in Chromium under a trace; long tasks, scripting, layouts, frame times, blank frames, heap and nodes. Compare other versions of the list's windowing with `--variant`. Not in `check`.
scroll *args:
    {{cargo}} build -p hud
    node harness/scroll.mjs "$@"

# HUD's own lexicons (schemas/) as the Lexicon Style Guide asks, and only grown since `base` (harness/lexicons.mjs); every XRPC answer hud-server's tests receive as its lexicon says (harness/answers.mjs); and the bundled views and Grain's apps as the JSON Schemas of schemas/json/ say (harness/schemas.mjs). Needs goat, and `cd harness && npm ci` once.
lex base="":
    node harness/lexicons.mjs {{base}}
    rm -rf target/answers
    HUD_ANSWERS="$PWD/target/answers" {{cargo}} test -p hud-server --lib xrpc
    node harness/answers.mjs target/answers
    node harness/schemas.mjs

# The JSON Schemas of an app file and a view, schemas/json/app.json and view.json, written again from the catalog and HUD's lexicons (`hud schema`). A test fails while they're not current.
json-schemas:
    {{cargo}} build -p hud
    target/debug/hud schema app > schemas/json/app.json
    target/debug/hud schema view > schemas/json/view.json

# The iOS app's Xcode project, ios/HUD.xcodeproj, from ios/project.yml (XcodeGen). Run it from Xcode against `just serve`.
ios:
    cd ios && xcodegen generate --quiet

# HUDKit's tests (ios/HUDKit/Tests) on an iOS simulator of the newest runtime: `just ios-test "iPhone Air"`.
ios-test device="iPhone 17 Pro": ios
    cd ios && xcodebuild -project HUD.xcodeproj -scheme HUD -destination 'platform=iOS Simulator,name={{device}}' -derivedDataPath ../target/ios -quiet test

# A Release build of the iOS app up to App Store Connect, for TestFlight: archived, signed for the App Store, and uploaded as the Apple ID Xcode is signed in with. Raise CURRENT_PROJECT_VERSION in ios/project.yml for each upload first: App Store Connect takes each build number once.
testflight: ios
    cd ios && xcodebuild archive -project HUD.xcodeproj -scheme HUD -configuration Release -destination 'generic/platform=iOS' -archivePath ../target/ios/HUD.xcarchive -allowProvisioningUpdates -quiet
    cd ios && xcodebuild -exportArchive -archivePath ../target/ios/HUD.xcarchive -exportPath ../target/ios/export -exportOptionsPlist ExportOptions.plist -allowProvisioningUpdates

# The Android app's debug build, with the gallery's sample answers (`just gallery-data` first), installed on the running emulator or the one device attached, and started at an entry: `just android bsky-thread`. Needs the Android SDK (`android/local.properties`' sdk.dir, or ANDROID_HOME) and a JDK 17.
android entry="":
    cd android && ./gradlew :app:installDebug --console=plain -q
    adb shell am start -n place.hud.app.debug/place.hud.app.MainActivity {{ if entry == "" { "" } else { "--es gallery " + entry } }}

# HUDKit for Android's tests (android/hudkit/src/test), on the JVM under Robolectric.
android-test:
    cd android && ./gradlew :hudkit:testDebugUnitTest --console=plain -q

# The bundled lexicons, fetched again from their canonical repositories (scripts/lexicons.mjs).
lexicons:
    node scripts/lexicons.mjs

# The container image, as it is deployed.
image:
    docker build -t hud .
