# Makefile for pg_knoe_auth unit tests
# Run with: make check
# No PG cluster required — tests link only against OpenSSL.

CC      = gcc
CFLAGS  = -std=c11 -Wall -Wextra -g \
           $(shell pkg-config --cflags openssl 2>/dev/null)
LDFLAGS = $(shell pkg-config --libs openssl 2>/dev/null)

TESTS = test_alg_validation \
        test_aud_check \
        test_aud_pipeline \
        test_jwks_cache \
        test_response_bound \
        test_base64url

all: $(TESTS)

test_alg_validation: test_alg_validation.c test_helpers.h
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

test_aud_check: test_aud_check.c test_helpers.h
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

test_aud_pipeline: test_aud_pipeline.c test_helpers.h
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

test_jwks_cache: test_jwks_cache.c test_helpers.h
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

test_response_bound: test_response_bound.c test_helpers.h
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

test_base64url: test_base64url.c test_helpers.h
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)

check: all
	@echo "=== pg_knoe_auth unit tests ==="
	@failed=0; \
	for t in $(TESTS); do \
	    echo "--- $$t ---"; \
	    ./$$t || failed=$$((failed + 1)); \
	done; \
	echo ""; \
	if [ $$failed -eq 0 ]; then \
	    echo "All unit tests passed."; \
	else \
	    echo "$$failed test(s) FAILED."; \
	    exit 1; \
	fi

clean:
	rm -f $(TESTS)

.PHONY: all check clean
