# Host test harness Makefile — covers the component's pure-logic units (seq_validator, melody_validator)
UNITY_DIR ?= $(HOME)/esp/esp-idf/components/unity/unity/src
CC ?= cc

# Source paths
COMPONENT_DIR := ../..
TEST_DIR      := ..

# Build output
BUILD_DIR := build

# Test executable
TEST_BIN := $(BUILD_DIR)/test_runner

# Component + harness sources
SRCS := \
	$(COMPONENT_DIR)/seq_validator.c \
	$(COMPONENT_DIR)/melody_validator.c \
	$(UNITY_DIR)/unity.c \
	host_runner.c

# Canonical IDF-style test file (single source of truth for assertions)
TEST_SRCS := \
	$(TEST_DIR)/test_validators.c

ALL_SRCS := $(SRCS) $(TEST_SRCS)

# Include directories
INCS := \
	-I$(COMPONENT_DIR)/include \
	-I$(COMPONENT_DIR) \
	-I$(TEST_DIR)/host \
	-I$(UNITY_DIR)

# Compiler flags
CFLAGS := -std=c11 -Wall -Wextra -Werror
CFLAGS += $(INCS)
CFLAGS += -DUNITY_INCLUDE_CONFIG_H

# Inject the TEST_CASE shim before each IDF-style test source
TEST_CFLAGS := $(CFLAGS) -include unity_test_case.h

.PHONY: all test clean

all: $(TEST_BIN)

$(BUILD_DIR):
	mkdir -p $(BUILD_DIR)
	$(if $(wildcard $(UNITY_DIR)/unity.c),,$(error UNITY_DIR not found: $(UNITY_DIR) — set UNITY_DIR=...))

# Compile test sources with the shim injected, then link everything in one shot
$(TEST_BIN): $(ALL_SRCS) | $(BUILD_DIR)
	$(if $(wildcard $(UNITY_DIR)/unity.c),,$(error UNITY_DIR not found: $(UNITY_DIR) — set UNITY_DIR=...))
	$(CC) $(TEST_CFLAGS) -c -o $(BUILD_DIR)/test_validators.o $(TEST_DIR)/test_validators.c
	$(CC) $(CFLAGS) -o $@ $(SRCS) $(BUILD_DIR)/test_validators.o

test: $(TEST_BIN)
	./$(TEST_BIN)

clean:
	rm -rf $(BUILD_DIR)

.PRECIOUS: $(TEST_BIN)
