.PHONY: build test test-modcache test-vet test-fmt test-build test-go

# CGO_ENABLED=0 for the same reason sheets-core sets it: nothing here
# should grow a cgo dependency by accident. estilo has no cgo-driven
# store of its own; this is a floor, not a workaround.
build:
	CGO_ENABLED=0 go build ./...

# The gate, split into named targets so CI can report them individually
# (.amadan/ci.d calls these one at a time, each its own step). `make
# test` runs exactly the same five things in the same order, so the
# local gate and CI stay ONE definition rather than two lists that
# drift apart. Debugging CI is running `make test`.
#
# Order is cheapest-and-fastest first: vet and gofmt are near-instant,
# build catches compile errors before time is spent on tests, and
# go test — which includes the prefix-rule tripwire (internal/lint)
# and the theme's gates (token set, contrast, accent goldens) — runs last.
# test-modcache goes first: nothing after it means anything if the
# dependencies on disk are not the ones go.sum names.
test: test-modcache test-vet test-fmt test-build test-go

# Checks the module cache holds exactly what go.sum names, and re-fetches
# any module Go reports as modified: it removes the exact directory or zip
# go mod verify names (so a module path with capitals, which lives at an
# escaped directory name, is removed too) and downloads again. Anything
# verify reports that is not a modified file is left alone, and the final
# verify still fails on it. Written down because of
# 2026-09-29: a CI runner's copy of rastrillo v0.23.0 had lost
# sessions/migrations/, so `go vet` failed on main and every branch alike
# for a reason no commit here could fix. The cache is a cache; a damaged
# entry is removed and downloaded again, never trusted.
test-modcache:
	@out=$$(go mod verify 2>&1) && { echo "$$out"; exit 0; }; \
	echo "$$out"; \
	for p in $$(echo "$$out" | awk -F'[()]' '/(dir|zip) has been modified \(/ { print $$2 }'); do \
		echo "removing damaged $$p"; \
		if [ -d "$$p" ]; then chmod -R u+w "$$p" 2>/dev/null; rm -rf "$$p"; \
		else rm -f "$$p" "$${p%.zip}.ziphash"; fi; \
	done; \
	go mod download && go mod verify

test-vet:
	CGO_ENABLED=0 go vet ./...

# Fails on unformatted files rather than rewriting them. CI must never
# silently "fix" a diff out from under a branch — a red test-fmt tells
# the author to run `gofmt -w` themselves.
test-fmt:
	@if [ -n "$$(gofmt -l .)" ]; then \
		echo "gofmt needs to be run on:"; \
		gofmt -l .; \
		exit 1; \
	fi

# A compile check, not a release: nothing is stamped or shipped, so VCS
# stamping is off. (On 2026-09-29 a runner's checkout could not answer
# `git status`, and stamping turned that into a build failure.)
test-build:
	CGO_ENABLED=0 go build -buildvcs=false ./...

# Includes internal/lint's prefix-rule tripwire (AGENTS.md's
# estilo-*/rst- split) and theme's gates (AGENTS.md, "Theme"; the
# token-set check is the two-pin case). -count=1 so a cached "ok" can never stand in for
# actually having run.
test-go:
	CGO_ENABLED=0 go test -count=1 ./...
