# ==============================================================================
# Fortress Chat - system-level integration test suite (apps/tests)
# ==============================================================================
# A black-box driver (e2e) that forks/execs the REAL fortress-chatd daemon and
# fortress-chat-cli harness and drives them over pipes. It links no fortress
# libraries -- it is plain C (fork/exec/poll). Auto-discovered by the root
# `make apps` / `make apps-test` targets (sorts after 'server' alphabetically).
#
# Build:  make            (or `make apps` / `make -C apps/tests` from root)
# Test:   make test
# ==============================================================================

CC := gcc

ROOT_DIR  := ../..
BUILD_DIR := build
BIN_DIR   := $(BUILD_DIR)/bin

CFLAGS := -std=c11 -D_GNU_SOURCE
CFLAGS += -Wall -Wextra -Werror -O2
CFLAGS += -fstack-protector-strong -D_FORTIFY_SOURCE=2
CFLAGS += $(EXTRA_CFLAGS)

# The driver depends on the built daemon + cli; those are produced by their own
# subdir Makefiles (guaranteed when invoked via the root `make apps`).
DAEMON := $(ROOT_DIR)/apps/server/build/bin/fortress-chatd
CLI    := $(ROOT_DIR)/apps/client/build/bin/fortress-chat-cli

E2E_BIN := $(BIN_DIR)/e2e

.PHONY: all test clean dirs

all: dirs $(E2E_BIN)

dirs:
	@mkdir -p $(BIN_DIR)

$(E2E_BIN): e2e.c | dirs
	@echo "[CC] $@"
	@$(CC) $(CFLAGS) -o $@ e2e.c

test: all
	@echo "Running fortress-chat system integration suite..."
	@FORTRESS_CHATD=$(abspath $(DAEMON)); export FORTRESS_CHATD; \
	 FORTRESS_CLI=$(abspath $(CLI)); export FORTRESS_CLI; \
	 FORTRESS_ROOT=$(abspath $(ROOT_DIR)); export FORTRESS_ROOT; \
	 $(E2E_BIN)
	@echo "All system integration checks passed!"

clean:
	@rm -rf $(BUILD_DIR)
