# browser-native-rts-poc — build/run commands.
#
# Prerequisites (one-time):
#   rustup target add wasm32-unknown-unknown
#   cargo install wasm-bindgen-cli --version 0.2.129 --locked   # must match the pin in crates/client/Cargo.toml
#
# Optional: Binaryen 133's `wasm-opt` on PATH shrinks the client bundle
# (`-Oz`, run by the Rust rts-tools packager before hashing); set
# RTS_WASM_OPT=/path/to/wasm-opt to point at one, or RTS_WASM_OPT=off to skip.
#
# Overridable so alternate toolchain locations work (CI, sandboxes):
CARGO ?= cargo
RTS_CACHE := $(CURDIR)/scripts/rts-cache
# Keep `make -n` read-only: a literal $(MAKE) in this wrapper recipe would
# execute it even under --dry-run. Inner recipes retain ordinary Make recursion.
RTS_RECURSIVE_MAKE = $(MAKE)
.DEFAULT_GOAL := all
UNAME_S := $(shell uname -s)
WASM_BINDGEN ?= wasm-bindgen

WASM_TARGET := wasm32-unknown-unknown
CLIENT_DIST := crates/client/dist
CLIENT_FEATURES ?=
# Override CLIENT_WASM if Cargo config selects another target directory.
CLIENT_WASM ?= $(if $(CARGO_TARGET_DIR),$(CARGO_TARGET_DIR),target)/$(WASM_TARGET)/release/client.wasm

.PHONY: all check test client server dev smoke browser-test package-test browser-helper-test python-test cache-check clean validate golden studio studio-migrate

# The public entry points keep one compiler-cache lease through every recipe,
# artifact copy and directly launched process. Recursive calls validate/reuse it.
# The private targets are implementation details; invoke the public target.
MANAGED_TARGETS := check test client server studio validate golden package-test browser-test
ifneq ($(filter $(MANAGED_TARGETS) clean all dev smoke,$(if $(MAKECMDGOALS),$(MAKECMDGOALS),all)),)
ifneq ($(filter command line,$(origin CARGO_TARGET_DIR) $(origin CARGO_BUILD_TARGET_DIR) $(origin CARGO_BUILD_BUILD_DIR)),)
$(error Managed Make targets own Cargo output directories; use direct Cargo for an unmanaged custom target)
endif
endif
check test _cache-check _cache-test: export CARGO_INCREMENTAL = 0
.PHONY: $(addprefix _cache-,$(MANAGED_TARGETS)) _cache-clean
$(MANAGED_TARGETS):
	"$(RTS_CACHE)" run --repo "$(CURDIR)" --role development -- $(RTS_RECURSIVE_MAKE) --no-print-directory _cache-$@

all: check test client

# Fast compile check: every crate natively, plus the client for wasm in
# every feature set that ships or gates a release (the e2e bridge only
# exists on wasm32 — browser-test builds it, nothing else compiles it).
_cache-check:
	$(CARGO) check --workspace
	$(CARGO) check -p client --target $(WASM_TARGET)
	$(CARGO) check -p client --features e2e --target $(WASM_TARGET)
	$(CARGO) check -p client --features designer
	$(CARGO) check -p client --features designer --target $(WASM_TARGET)

_cache-test:
	$(CARGO) test --workspace
	$(CARGO) test -p client --features designer

# Lint the shipped rules/maps with the designer CLI (PLAN.md D7); a CI gate
# separate from `test` so a strict content failure doesn't read as a code
# test failure.
_cache-validate:
	$(CARGO) run -p rtsdata -- validate --strict
	$(CARGO) run -p rtsdata -- media validate

# Regenerate the content golden-hash pins (crates/data/tests/golden.rs).
# `cargo run -p rtsdata -- golden --engine` regenerates the engine pins
# (crates/sim/src/replay.rs) separately; `-- golden --check [--engine]`
# verifies without writing.
_cache-golden:
	$(CARGO) run -p rtsdata -- golden

# Build the WASM client bundle into $(CLIENT_DIST) (served by the server).
_cache-client:
	$(CARGO) build -p client --target $(WASM_TARGET) --release $(if $(CLIENT_FEATURES),--features $(CLIENT_FEATURES),)
	$(CARGO) run --locked -p rts-tools -- package-client --root "$(CURDIR)" \
		--wasm-bindgen "$(WASM_BINDGEN)" \
		--wasm "$(CLIENT_WASM)" \
		--dist "$(CLIENT_DIST)"
	@ls -la $(CLIENT_DIST)

# Run the native in-client designer (feature-gated; never shipped in the
# wasm bundle — `make client` does not build with this feature).
_cache-studio:
ifeq ($(UNAME_S),Darwin)
	python3 scripts/studio-macos.py --cargo $(CARGO)
else
	$(MAKE) studio-migrate
	$(CARGO) build --locked -p studio-publishing --bin studio-publish-client
	$(CARGO) run -p client --features designer -- designer=1
endif

# Run once before cleaning an older checkout; preserves and verifies frozen bytes.
studio-migrate:
	python3 -B scripts/studio-storage.py

# Run both filename conventions explicitly: unittest discovery skips hyphenated modules.
python-test:
	@set -eu; for test in scripts/test-studio-*.py scripts/test_*.py tests/publishing-legacy/test-studio-publish-*.py; do \
		PYTHONDONTWRITEBYTECODE=1 python3 -B "$$test" -v; \
	done

_cache-package-test:
	$(CARGO) test --locked -p rts-tools

# Qualification evidence only; this gate never enables automatic pruning.
cache-check: package-test python-test
	node --test tests/browser/helpers/cache-boundary.unit.mjs
	@set -eu; cache_test_dir=$$(mktemp -d "$${TMPDIR:-/tmp}/rts-cache-bootstrap-test.XXXXXXXX"); \
		trap 'rm -f -- "$$cache_test_dir/bootstrap-test"; rmdir -- "$$cache_test_dir"' EXIT; \
		rustc --edition=2024 --test scripts/cache-bootstrap.rs -o "$$cache_test_dir/bootstrap-test"; \
		"$$cache_test_dir/bootstrap-test"

# Node is limited to the Playwright testing/profiling boundary.
browser-helper-test:
	node --test scripts/profile-client.test.mjs tests/browser/helpers/game-button.unit.mjs tests/browser/helpers/cache-boundary.unit.mjs tests/browser/helpers/world-point.unit.mjs

# Browser suite installs are intentionally explicit; `npm ci` is a one-time
# prerequisite in CI/development so this target never mutates dependencies.
# On a Linux box without a GPU, headless Firefox cannot create a WebGL2
# context (llvmpipe only reaches it headful, through GLX), so
# playwright.config.mjs runs the firefox project headful on Linux and the
# whole Playwright run goes under Xvfb there. macOS: plain `npx playwright`.
PLAYWRIGHT := $(if $(shell command -v xvfb-run 2>/dev/null),xvfb-run -a --server-args="-screen 0 1280x1024x24" ,)npx playwright
_cache-browser-test:
	$(MAKE) client CLIENT_FEATURES=
	@if rg -a -q '__rtsE2e|Back to designer' $(CLIENT_DIST); then echo 'normal release contains a test/designer feature leak' >&2; exit 1; fi
	# Compile outside Playwright's readiness deadline, while the normal bundle remains installed.
	"$(RTS_CACHE)" run --repo "$(CURDIR)" --role browser-server -- $(CARGO) build -p server
	$(MAKE) client CLIENT_FEATURES=e2e
	@status=0; $(PLAYWRIGHT) test || status=$$?; \
		$(MAKE) client CLIENT_FEATURES= || exit $$?; \
		exit $$status

# Run the server (serves $(CLIENT_DIST) + /ws). RTS_PORT overrides port 8080.
_cache-server:
	$(CARGO) run -p server

# Build the client, then run the server — open http://127.0.0.1:8080/
dev: client server

# The end-to-end scaffolding smoke test: unit tests + the WS ping/echo
# integration test + a from-scratch client bundle.
smoke: test client
	@echo "smoke: tests green and client bundle built; run 'make server' and open http://127.0.0.1:8080/"

# Clean only this managed compiler cache and the generated browser bundle.
# Legacy target trees and the installed native app are never selected here.
clean:
	"$(RTS_CACHE)" clean --repo "$(CURDIR)" --role development
	rm -rf "$(CLIENT_DIST)"

# Private package-scoped recipe retained for compatibility/preservation checks.
_cache-clean:
	$(CARGO) clean --workspace
	$(CARGO) clean --workspace --release
	$(CARGO) clean --workspace --target $(WASM_TARGET)
	$(CARGO) clean --workspace --release --target $(WASM_TARGET)
	rm -rf "$(CLIENT_DIST)"
