# NOSTR Test Suite Makefile

CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -g -I.. -I../secp256k1/include -I../mbedtls-install/include
LDFLAGS = -L.. -L../secp256k1/.libs -L../mbedtls-install/lib -lnostr_core -l:libsecp256k1.a -l:libmbedtls.a -l:libmbedx509.a -l:libmbedcrypto.a -lm -static

# ARM64 cross-compilation settings
ARM64_CC = aarch64-linux-gnu-gcc
ARM64_CFLAGS = -Wall -Wextra -std=c99 -g -I..
ARM64_LDFLAGS = -L.. -lnostr_core_arm64 -lm -static

# Test executables
CRYPTO_TEST_EXEC = nostr_crypto_test
CORE_TEST_EXEC = nostr_core_test
RELAY_POOL_TEST_EXEC = relay_pool_test
EVENT_GEN_TEST_EXEC = test_event_generation
POW_LOOP_TEST_EXEC = test_pow_loop
NIP04_TEST_EXEC = nip04_test
STATIC_LINKING_TEST_EXEC = static_linking_only_test
ARM64_CRYPTO_TEST_EXEC = nostr_crypto_test_arm64
ARM64_CORE_TEST_EXEC = nostr_core_test_arm64
ARM64_RELAY_POOL_TEST_EXEC = relay_pool_test_arm64
ARM64_NIP04_TEST_EXEC = nip04_test_arm64

# Default target - build all test suites
all: $(CRYPTO_TEST_EXEC) $(CORE_TEST_EXEC) $(RELAY_POOL_TEST_EXEC) $(EVENT_GEN_TEST_EXEC)

# Build crypto test executable (x86_64)
$(CRYPTO_TEST_EXEC): nostr_crypto_test.c
	@echo "Building crypto test suite (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build core test executable (x86_64)
$(CORE_TEST_EXEC): nostr_core_test.c
	@echo "Building core test suite (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build relay pool test executable (x86_64)
$(RELAY_POOL_TEST_EXEC): relay_pool_test.c
	@echo "Building relay pool test suite (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build event generation test executable (x86_64)
$(EVENT_GEN_TEST_EXEC): test_event_generation.c
	@echo "Building event generation test suite (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build PoW loop test executable (x86_64)
$(POW_LOOP_TEST_EXEC): test_pow_loop.c
	@echo "Building PoW loop test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build NIP-04 test executable (x86_64)
$(NIP04_TEST_EXEC): nip04_test.c
	@echo "Building NIP-04 encryption test suite (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build static linking test executable (x86_64)
$(STATIC_LINKING_TEST_EXEC): static_linking_only_test.c
	@echo "Building static linking verification test (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build simple initialization test executable (x86_64)
simple_init_test: simple_init_test.c
	@echo "Building simple initialization test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build minimal NIP-04 test executable (x86_64)
nip04_minimal_test: nip04_minimal_test.c
	@echo "Building minimal NIP-04 test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build encryption-only NIP-04 test executable (x86_64)
nip04_encrypt_only_test: nip04_encrypt_only_test.c
	@echo "Building encryption-only NIP-04 test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build decryption debug NIP-04 test executable (x86_64)
nip04_decrypt_debug_test: nip04_decrypt_debug_test.c
	@echo "Building decryption debug NIP-04 test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build detailed debug NIP-04 test executable (x86_64)
nip04_detailed_debug_test: nip04_detailed_debug_test.c
	@echo "Building detailed debug NIP-04 test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build ping test executable (x86_64)
ping_test: ping_test.c
	@echo "Building ping test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build ChaCha20 test executable (x86_64)
chacha20_test: chacha20_test.c
	@echo "Building ChaCha20 RFC 8439 test suite (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build frame debug test executable (x86_64)
frame_debug_test: frame_debug_test.c
	@echo "Building frame debug test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

# Build sync test executable (x86_64)
sync_test: sync_test.c
	@echo "Building synchronous relay query test program (x86_64)..."
	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)


# Build crypto test ARM64 executable
$(ARM64_CRYPTO_TEST_EXEC): nostr_crypto_test.c
	@echo "Building crypto test suite (ARM64)..."
	$(ARM64_CC) $(ARM64_CFLAGS) $< -o $@ $(ARM64_LDFLAGS)

# Build core test ARM64 executable
$(ARM64_CORE_TEST_EXEC): nostr_core_test.c
	@echo "Building core test suite (ARM64)..."
	$(ARM64_CC) $(ARM64_CFLAGS) $< -o $@ $(ARM64_LDFLAGS)

# Build relay pool test ARM64 executable
$(ARM64_RELAY_POOL_TEST_EXEC): relay_pool_test.c
	@echo "Building relay pool test suite (ARM64)..."
	$(ARM64_CC) $(ARM64_CFLAGS) $< -o $@ $(ARM64_LDFLAGS)

# Build NIP-04 test ARM64 executable
$(ARM64_NIP04_TEST_EXEC): nip04_test.c
	@echo "Building NIP-04 encryption test suite (ARM64)..."
	$(ARM64_CC) $(ARM64_CFLAGS) $< -o $@ $(ARM64_LDFLAGS)

# Build both architectures
all-arch: $(CRYPTO_TEST_EXEC) $(CORE_TEST_EXEC) $(RELAY_POOL_TEST_EXEC) $(ARM64_CRYPTO_TEST_EXEC) $(ARM64_CORE_TEST_EXEC) $(ARM64_RELAY_POOL_TEST_EXEC)

# Run crypto tests (x86_64)
test-crypto: $(CRYPTO_TEST_EXEC)
	@echo "Running crypto tests (x86_64)..."
	./$(CRYPTO_TEST_EXEC)

# Run core tests (x86_64)
test-core: $(CORE_TEST_EXEC)
	@echo "Running core tests (x86_64)..."
	./$(CORE_TEST_EXEC)

# Run relay pool tests (x86_64)
test-relay-pool: $(RELAY_POOL_TEST_EXEC)
	@echo "Running relay pool tests (x86_64)..."
	./$(RELAY_POOL_TEST_EXEC)

# Run NIP-04 tests (x86_64)
test-nip04: $(NIP04_TEST_EXEC)
	@echo "Running NIP-04 encryption tests (x86_64)..."
	./$(NIP04_TEST_EXEC)

# Run static linking verification test (x86_64)
test-static-linking: $(STATIC_LINKING_TEST_EXEC)
	@echo "Running static linking verification test (x86_64)..."
	./$(STATIC_LINKING_TEST_EXEC)

# Run all test suites (x86_64)
test: test-crypto test-core test-relay-pool test-nip04 test-static-linking

# Run crypto tests ARM64 (requires qemu-user-static or ARM64 system)
test-crypto-arm64: $(ARM64_CRYPTO_TEST_EXEC)
	@echo "Running crypto tests (ARM64)..."
	@if command -v qemu-aarch64-static >/dev/null 2>&1; then \
		echo "Using qemu-aarch64-static to run ARM64 binary..."; \
		qemu-aarch64-static ./$(ARM64_CRYPTO_TEST_EXEC); \
	else \
		echo "qemu-aarch64-static not found. ARM64 binary built but cannot run on x86_64."; \
		echo "To run: copy $(ARM64_CRYPTO_TEST_EXEC) to ARM64 system and execute."; \
		file ./$(ARM64_CRYPTO_TEST_EXEC); \
	fi

# Run core tests ARM64 (requires qemu-user-static or ARM64 system)
test-core-arm64: $(ARM64_CORE_TEST_EXEC)
	@echo "Running core tests (ARM64)..."
	@if command -v qemu-aarch64-static >/dev/null 2>&1; then \
		echo "Using qemu-aarch64-static to run ARM64 binary..."; \
		qemu-aarch64-static ./$(ARM64_CORE_TEST_EXEC); \
	else \
		echo "qemu-aarch64-static not found. ARM64 binary built but cannot run on x86_64."; \
		echo "To run: copy $(ARM64_CORE_TEST_EXEC) to ARM64 system and execute."; \
		file ./$(ARM64_CORE_TEST_EXEC); \
	fi

# Run relay pool tests ARM64 (requires qemu-user-static or ARM64 system)
test-relay-pool-arm64: $(ARM64_RELAY_POOL_TEST_EXEC)
	@echo "Running relay pool tests (ARM64)..."
	@if command -v qemu-aarch64-static >/dev/null 2>&1; then \
		echo "Using qemu-aarch64-static to run ARM64 binary..."; \
		qemu-aarch64-static ./$(ARM64_RELAY_POOL_TEST_EXEC); \
	else \
		echo "qemu-aarch64-static not found. ARM64 binary built but cannot run on x86_64."; \
		echo "To run: copy $(ARM64_RELAY_POOL_TEST_EXEC) to ARM64 system and execute."; \
		file ./$(ARM64_RELAY_POOL_TEST_EXEC); \
	fi

# Run all test suites on ARM64
test-arm64: test-crypto-arm64 test-core-arm64 test-relay-pool-arm64

# Run tests on both architectures
test-all: test test-arm64

# Clean
clean:
	@echo "Cleaning test artifacts..."
	rm -f $(CRYPTO_TEST_EXEC) $(CORE_TEST_EXEC) $(RELAY_POOL_TEST_EXEC) $(EVENT_GEN_TEST_EXEC) $(POW_LOOP_TEST_EXEC) $(NIP04_TEST_EXEC) $(STATIC_LINKING_TEST_EXEC) $(ARM64_CRYPTO_TEST_EXEC) $(ARM64_CORE_TEST_EXEC) $(ARM64_RELAY_POOL_TEST_EXEC) $(ARM64_NIP04_TEST_EXEC)

# Help
help:
	@echo "NOSTR Test Suite"
	@echo "================"
	@echo ""
	@echo "Available targets:"
	@echo "  all                  - Build all test executables (x86_64)"
	@echo "  all-arch             - Build test executables for both x86_64 and ARM64"
	@echo "  test-crypto          - Build and run crypto tests (x86_64)"
	@echo "  test-core            - Build and run core tests (x86_64)"
	@echo "  test-relay-pool      - Build and run relay pool tests (x86_64)"
	@echo "  test-nip04           - Build and run NIP-04 encryption tests (x86_64)"
	@echo "  test-static-linking  - Build and run static linking verification test (x86_64)"
	@echo "  test                 - Build and run all test suites (x86_64)"
	@echo "  test-arm64           - Build and run all test suites (ARM64)"
	@echo "  test-all             - Run tests on both architectures"
	@echo "  clean                - Remove test artifacts"
	@echo "  help                 - Show this help"
	@echo ""
	@echo "Test Executables:"
	@echo "  $(CRYPTO_TEST_EXEC)           - x86_64 crypto test binary"
	@echo "  $(CORE_TEST_EXEC)             - x86_64 core test binary"
	@echo "  $(RELAY_POOL_TEST_EXEC)       - x86_64 relay pool test binary"
	@echo "  $(NIP04_TEST_EXEC)            - x86_64 NIP-04 encryption test binary"
	@echo "  $(ARM64_CRYPTO_TEST_EXEC)     - ARM64 crypto test binary"
	@echo "  $(ARM64_CORE_TEST_EXEC)       - ARM64 core test binary"
	@echo "  $(ARM64_RELAY_POOL_TEST_EXEC) - ARM64 relay pool test binary"
	@echo "  $(ARM64_NIP04_TEST_EXEC)      - ARM64 NIP-04 encryption test binary"
	@echo ""
	@echo "Test Coverage:"
	@echo "  Crypto Tests         - Low-level cryptographic primitives (SHA-256, HMAC, secp256k1, BIP39, BIP32)"
	@echo "  Core Tests           - High-level NOSTR functionality with nak compatibility validation"
	@echo "  Relay Pool Tests     - Relay pool event processing with real NOSTR relays"
	@echo "  NIP-04 Tests         - NOSTR NIP-04 encryption/decryption with reference test vectors"
	@echo "  Static Linking Tests - Verify library has no external crypto dependencies (self-contained)"

.PHONY: all all-arch test-crypto test-core test-relay-pool test-nip04 test-static-linking test test-crypto-arm64 test-core-arm64 test-relay-pool-arm64 test-arm64 test-all clean help
