# A recipe's arguments are its script's "$@" as well, each one word as given.
set positional-arguments

cargo := "rustup run stable cargo"

# What every pull and push must pass: the code gates, the tests and the harness runs, a short crashing one among them, side by side.
[parallel]
gates: code test harness

# The nightly gates, on main's tip every night: the long crash runs, then the performance step, alone.
nightly: build
    cd harness && node gates.mjs crash
    cd harness && node gates.mjs perf

# While working: a plain harness run, a crashing one and one through the MCP tools and CLI commands, a minute or so.
quick: build
    cd harness && node gates.mjs quick

# The `pds` binary, as the harness runs it, with the pages it embeds built first.
build: web
    {{cargo}} build --release -p pds

# The authorization pages (web/AGENTS.md), into web/dist, which pds-server embeds.
web:
    cd web && npm run build

# The authorization pages drawn in a browser, in any state, as pictures in target/look to open: `just look consent --scope 'atproto repo:*'`, or an email's HTML part: `just look email resetPassword` (.claude/skills/ui).
look *args: build
    cd harness && node look.mjs "$@"

# Every kind of email's HTML part drawn, as pictures in target/look to open: `just look-emails --phone` for one size alone (.claude/skills/ui).
look-emails *args: build
    cd harness && node look.mjs email all "$@"

# A server to try by hand (harness/dev.mjs): its own PLC directory, data in target/dev, and what the CLI and MCP need printed; `just dev --seed` makes accounts, `--keep` carries on from the last.
dev *args: build
    cd harness && node dev.mjs "$@"

# The documentation site (docs-site/), at http://localhost:4321/, drawn again as its pages change.
docs-site:
    cd docs-site && npm ci --no-audit --no-fund && npm run dev

# The documentation site, built and deployed as the repository's, at https://chadtmiller-com.knotless.page/monolith/, by hand: the `docs` workflow deploys it on each push to main that changes it. Run by its owner, signed in with `knotless login`.
docs-deploy:
    cd docs-site && npm ci --no-audit --no-fund && npm run build
    knotless repo site knotless://chadtmiller.com/monolith docs-site/dist --not-found 404.html

# The source: cargo fmt, clippy with warnings denied, cargo deny, and #![forbid(unsafe_code)] in every crate; the pages' layers, contrast and svelte-check.
code: web
    cd harness && CARGO="{{cargo}}" node gates.mjs code

# The server against the reference, over XRPC and through its MCP tools and CLI commands.
harness: build
    cd harness && node gates.mjs compare commands

# The server's speed against the reference's, alone on the machine.
perf: build
    cd harness && node gates.mjs perf

# The crates' own tests, among them the server's log cut at every byte: their scratch directories in memory where /dev/shm has room, as each start of the server syncs its logs.
test: web
    {{cargo}} test --no-run
    if [ -d /dev/shm ] && [ "$(df -Pk /dev/shm | awk 'NR==2 {print $4}')" -ge 49152 ]; then export TMPDIR=/dev/shm; fi; {{cargo}} test -- --nocapture

# The PDS benchmark (bench/README.md): build every implementation at its pin (bench/impls.json), then three rounds of each, one at a time, every one in Docker; it refuses a busy machine.
bench:
    cd harness && node bench.mjs --setup && node bench.mjs --rounds 3 --json ../bench/results.json

# The store benchmark (bench/store/README.md): the server's logs and tables beside SQLite, RocksDB and fjall under the same commits (writes, a log and a firehose replayed, reads, sizes), and the server's start and memory by the number of accounts in its service log; Markdown on stdout. Arguments are counts of accounts for that last part, as `just store-bench 10000 200000`. RocksDB's first build takes some minutes and needs libclang.
store-bench *args: build
    CARGO="{{cargo}}" bench/store/run.sh {{args}}

# The compatibility check (compat/README.md): build every implementation at its pin (bench/impls.json), then every core area of each beside the reference; the tables on stdout, the raw results in compat/results.json. Arguments pass through, as `just compat --impls zds --areas oauth`.
compat *args:
    cd harness && node bench.mjs --setup && node compat.mjs --steps 200 --json ../compat/results.json {{args}}
