test-all:
	julia --project -e 'using Pkg; Pkg.test()'

test-unit:
	TEST_MODE=unit julia --project -e 'using Pkg; Pkg.test()'

test-integration:
	TEST_MODE=integration julia --project -e 'using Pkg; Pkg.test()'

test-jet:
	TEST_MODE=jet julia --project -e 'using Pkg; Pkg.test()'

test-aqua:
	TEST_MODE=aqua julia --project -e 'using Pkg; Pkg.test()'

# Test the MagestyCLI package under cli/. It depends on the core by path,
# so develop the core into the cli/ environment before testing.
test-cli:
	julia --project=cli -e 'using Pkg; Pkg.develop(path="."); Pkg.instantiate(); Pkg.test()'

# Build and install the `magesty` launcher into ~/.julia/bin. MagestyCLI
# depends on the core by path, so develop it into the cli/ environment first,
# then run the Comonicon build step. Add ~/.julia/bin to PATH to use the
# command.
install-cli:
	julia --project=cli -e 'using Pkg; Pkg.develop(path="."); Pkg.instantiate()'
	julia --project=cli cli/deps/build.jl

# Sunny.jl round-trip validation for the sce_to_sunny exporter. Sunny is a heavy
# dependency, so it lives in its own environment under test/sunny/ and is NOT part
# of test-all. Develop the core into that environment (by path) before running.
test-sunny:
	julia --project=test/sunny -e 'using Pkg; Pkg.develop(path="."); Pkg.instantiate()'
	julia --project=test/sunny test/sunny/runtests.jl

# Note: SpheriCart agreement tests are part of test-unit (no separate target).

coverage-setup:
	julia --project=coverage -e 'using Pkg; Pkg.develop(path="."); Pkg.instantiate()'

test-coverage:
	julia --project --code-coverage=user -e 'using Pkg; Pkg.test(coverage=true)'
	julia --project=coverage tools/coverage_report.jl

clean-coverage:
	find src -name '*.cov' -delete
	rm -f coverage.lcov

bench-setup:
	julia --project=bench -e 'using Pkg; Pkg.develop(path="."); Pkg.instantiate()'

bench-sphericart:
	julia --project=bench bench/benchmark_sphericart.jl

bench-salcbasis:
	julia --project=bench bench/benchmark_salcbasis_hotspots.jl

# Per-stage timing + profile of Cluster construction. With no arguments it
# benchmarks all three shipped three-body fixtures in increasing cost
# order (light -> body-3 Fe-Fe open -> body-3 all open); pass --input
# (repeatable) to point at other systems (e.g. --input
# test/integration/fege_2x2x2/input.toml).
bench-cluster:
	julia --project=bench bench/benchmark_cluster.jl

bench-spherical-harmonics:
	julia --project=bench bench/benchmark_spherical_harmonics.jl

bench-threads:
	bash bench/run_threads_scaling.sh

bench-fitting:
	julia --project=bench bench/bench_b1_design_matrix.jl

bench-symmetry:
	julia --project=bench bench/benchmark_symmetry.jl

bench-profile-allocs:
	julia --project=bench bench/profile_remaining_allocs.jl

# Run the same checks GitHub Actions runs, locally, before pushing.
# Uses the juliaup `release` channel (matches CI's `1`, currently 1.12.x).
# CI also tests against the explicit `1.12` minimum; locally we skip the
# duplicate run because `release` resolves to the same Julia until 1.13
# stable ships. Install the channel if missing:
#   juliaup add release
ci-local:
	@# Manifest.toml is gitignored; CI starts without it, so reproduce that
	@# locally by removing the cached one before the test run. The manifest
	@# is regenerated by the run and left in place (gitignored).
	rm -f Manifest.toml
	julia +release --project -e 'using Pkg; Pkg.test()'
	$(MAKE) test-aqua
	$(MAKE) test-jet
	$(MAKE) test-cli
