# XCE project — test targets
# Usage:
#   make test          # unit tests only (no server needed, CGO_ENABLED=0)
#   make test-backend  # backend CGO conversion tests (requires nix dev shell + build dir)
#   make test-integration  # regression suite against live server
#   make test-all      # unit + integration tests

XCE_BIN   ?= /tmp/xce_static
SRV_URL   ?= http://localhost:8080
GOFLAGS   := CGO_ENABLED=0

# build-time-versioning task 4.3: dev-loop -ldflags path. Computes commit id
# (git, -dirty suffixed) and build time (UTC) directly -- independent of
# meson -- mirroring xce-build.sh's (task 2.1) git/dirty-check logic and
# passing the identical -ldflags shape go_build (task 4.1) appends. `:=`
# (simply expanded: the $(shell ...) calls run once, immediately, when this
# line is parsed) rather than `=` (recursively expanded: would re-invoke git
# and date on every reference to these variables) -- this computes each
# value exactly once per `make` invocation, matching xce-build.sh's own
# single-computation-per-invocation model, so build-server and build-cli
# (and multiple references within one recipe) all see the same values.
GIT_COMMIT_HASH := $(shell git rev-parse --short HEAD 2>/dev/null)
GIT_STATUS      := $(shell git status --porcelain 2>/dev/null)
ifeq ($(GIT_COMMIT_HASH),)
# git itself failed (e.g. no .git present) -- Requirement 1.4/4.1's fallback
# discipline: record as unknown rather than fail the build.
GIT_COMMIT := unknown
else ifeq ($(GIT_STATUS),)
GIT_COMMIT := $(GIT_COMMIT_HASH)
else
GIT_COMMIT := $(GIT_COMMIT_HASH)-dirty
endif
BUILD_TIME      := $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
VERSION_LDFLAGS := -ldflags "-X main.GitCommit=$(GIT_COMMIT) -X main.BuildTime=$(BUILD_TIME)"

# bug-1558: test-backend's CGO_CFLAGS/CGO_LDFLAGS, self-derived via
# pkg-config so `make test-backend` works standalone instead of only inside
# meson's custom_target env (apiserver/meson.build is the source of truth
# this mirrors -- librtti/c-capnproto/openssl/tinylog resolve via
# pkg-config there; math/rt are cc.find_library() results with no
# pkg-config package, added directly as -lm/-lrt to match).
# BUILD_DIR defaults to the tree tools/xce-build.sh produces (profile asan);
# override for a non-ASAN build, e.g.
#   make test-backend BUILD_DIR=$(CURDIR)/../build.dbg FRONTEND_CGO_SANITIZE=
BUILD_DIR             ?= $(CURDIR)/../xce.build
FRONTEND_CGO_PKGS     := librtti c-capnproto openssl tinylog
FRONTEND_CGO_SANITIZE ?= -fsanitize=address -g
FRONTEND_CGO_CFLAGS   := $(FRONTEND_CGO_SANITIZE) $(shell pkg-config --cflags $(FRONTEND_CGO_PKGS)) -I$(CURDIR)/../include -I$(BUILD_DIR)/lib
FRONTEND_CGO_LDFLAGS  := $(FRONTEND_CGO_SANITIZE) $(shell pkg-config --libs $(FRONTEND_CGO_PKGS)) -lm -lrt -L$(BUILD_DIR)/lib -lxce

.PHONY: build-cli build-server test test-backend test-integration test-all

build-cli:
	cd xcecli && $(GOFLAGS) go build $(VERSION_LDFLAGS) -o $(XCE_BIN) ./cmd/main.go

build-server:
	cd apiserver && go build $(VERSION_LDFLAGS) -o /tmp/apiserver ./cmd/main.go

## Run all Go unit tests that do not require CGO or a live server.
test:
	@echo "=== Unit tests: apiserver ==="
	cd apiserver && $(GOFLAGS) go test ./store/... ./utils/... ./controllers/... ./backend/... -count=1
	@echo ""
	@echo "=== Unit tests: xcecli ==="
	cd xcecli && $(GOFLAGS) go test ./... -count=1

## Run backend CGO conversion tests. Standalone-runnable: CGO_CFLAGS/
## CGO_LDFLAGS are self-derived above via pkg-config (bug-1558) -- no manual
## env setup needed. Still requires the nix develop shell (for pkg-config's
## .pc files) and a prior `xce-build.sh lib` (or any component that builds
## libxce.a) so BUILD_DIR/lib/libxce.a and BUILD_DIR/lib's generated headers
## exist.
test-backend:
	@echo "=== Backend CGO tests: apiserver/backend ==="
	cd apiserver && CGO_ENABLED=1 \
	  CGO_CFLAGS="$(FRONTEND_CGO_CFLAGS)" \
	  CGO_LDFLAGS="$(FRONTEND_CGO_LDFLAGS)" \
	  go test ./backend/... -count=1

## Run regression suite against a live server (requires XCE_BIN and SRV_URL)
test-integration: build-cli
	@echo "=== Integration regression: $(SRV_URL) ==="
	XCE=$(XCE_BIN) URL=$(SRV_URL) bash tests/regression/run.sh

## Run everything
test-all: test test-integration
