From 347d29658959c7e4b368a15134c183f4ce7a25bc Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 17 May 2026 01:31:49 +0530 Subject: Testing infrastructure: AGENTS.md rules + unit test framework + geohash tests (11/11 pass) - Add AGENTS.md: full project context + mandatory testing rules for AI sessions - Add tests/unit/ with host-compiled C unit test infrastructure - Clean stubs approach: ESP-IDF type stubs in tests/unit/stubs/, no source modifications - Fix geohash.c bit extraction bug (3-byte span) found by unit tests - test_geohash: 11/11 passing with reference vectors (Munich, NYC, origin, boundaries) --- tests/unit/stubs/dhcpserver/dhcpserver.h | 4 ++++ tests/unit/stubs/esp_crt_bundle.h | 6 +++++ tests/unit/stubs/esp_err.h | 18 ++++++++++++++ tests/unit/stubs/esp_event.h | 8 +++++++ tests/unit/stubs/esp_http_client.h | 12 ++++++++++ tests/unit/stubs/esp_http_server.h | 10 ++++++++ tests/unit/stubs/esp_log.h | 10 ++++++++ tests/unit/stubs/esp_mac.h | 19 +++++++++++++++ tests/unit/stubs/esp_netif.h | 17 ++++++++++++++ tests/unit/stubs/esp_spiffs.h | 15 ++++++++++++ tests/unit/stubs/esp_system.h | 4 ++++ tests/unit/stubs/esp_tls.h | 25 ++++++++++++++++++++ tests/unit/stubs/esp_wifi.h | 40 ++++++++++++++++++++++++++++++++ tests/unit/stubs/freertos/FreeRTOS.h | 11 +++++++++ tests/unit/stubs/freertos/event_groups.h | 13 +++++++++++ tests/unit/stubs/freertos/task.h | 19 +++++++++++++++ tests/unit/stubs/freertos/timers.h | 15 ++++++++++++ tests/unit/stubs/lwip/ip4_addr.h | 19 +++++++++++++++ tests/unit/stubs/lwip/napt.h | 6 +++++ tests/unit/stubs/lwip/netdb.h | 4 ++++ tests/unit/stubs/lwip/netif.h | 4 ++++ tests/unit/stubs/lwip/sockets.h | 4 ++++ tests/unit/stubs/nvs_flash.h | 12 ++++++++++ 23 files changed, 295 insertions(+) create mode 100644 tests/unit/stubs/dhcpserver/dhcpserver.h create mode 100644 tests/unit/stubs/esp_crt_bundle.h create mode 100644 tests/unit/stubs/esp_err.h create mode 100644 tests/unit/stubs/esp_event.h create mode 100644 tests/unit/stubs/esp_http_client.h create mode 100644 tests/unit/stubs/esp_http_server.h create mode 100644 tests/unit/stubs/esp_log.h create mode 100644 tests/unit/stubs/esp_mac.h create mode 100644 tests/unit/stubs/esp_netif.h create mode 100644 tests/unit/stubs/esp_spiffs.h create mode 100644 tests/unit/stubs/esp_system.h create mode 100644 tests/unit/stubs/esp_tls.h create mode 100644 tests/unit/stubs/esp_wifi.h create mode 100644 tests/unit/stubs/freertos/FreeRTOS.h create mode 100644 tests/unit/stubs/freertos/event_groups.h create mode 100644 tests/unit/stubs/freertos/task.h create mode 100644 tests/unit/stubs/freertos/timers.h create mode 100644 tests/unit/stubs/lwip/ip4_addr.h create mode 100644 tests/unit/stubs/lwip/napt.h create mode 100644 tests/unit/stubs/lwip/netdb.h create mode 100644 tests/unit/stubs/lwip/netif.h create mode 100644 tests/unit/stubs/lwip/sockets.h create mode 100644 tests/unit/stubs/nvs_flash.h (limited to 'tests/unit/stubs') diff --git a/tests/unit/stubs/dhcpserver/dhcpserver.h b/tests/unit/stubs/dhcpserver/dhcpserver.h new file mode 100644 index 0000000..659f2c3 --- /dev/null +++ b/tests/unit/stubs/dhcpserver/dhcpserver.h @@ -0,0 +1,4 @@ +#ifndef STUBS_DHCPSERVER_DHCP_H +#define STUBS_DHCPSERVER_DHCP_H + +#endif diff --git a/tests/unit/stubs/esp_crt_bundle.h b/tests/unit/stubs/esp_crt_bundle.h new file mode 100644 index 0000000..dfb9bb1 --- /dev/null +++ b/tests/unit/stubs/esp_crt_bundle.h @@ -0,0 +1,6 @@ +#ifndef STUBS_ESP_CRT_BUNDLE_H +#define STUBS_ESP_CRT_BUNDLE_H + +static inline void *esp_crt_bundle_attach(void *conf) { (void)conf; return NULL; } + +#endif diff --git a/tests/unit/stubs/esp_err.h b/tests/unit/stubs/esp_err.h new file mode 100644 index 0000000..84c3734 --- /dev/null +++ b/tests/unit/stubs/esp_err.h @@ -0,0 +1,18 @@ +#ifndef STUBS_ESP_ERR_H +#define STUBS_ESP_ERR_H + +#include +#include +#include + +typedef int esp_err_t; + +#define ESP_OK 0 +#define ESP_FAIL -1 +#define ESP_ERR_INVALID_ARG 0x102 +#define ESP_ERR_NO_MEM 0x101 +#define ESP_ERR_NOT_FOUND 0x104 + +#define ESP_ERROR_CHECK(x) do { if ((x) != 0) { fprintf(stderr, "ESP_ERROR_CHECK failed: 0x%x\n", (int)(x)); abort(); } } while(0) + +#endif diff --git a/tests/unit/stubs/esp_event.h b/tests/unit/stubs/esp_event.h new file mode 100644 index 0000000..baea064 --- /dev/null +++ b/tests/unit/stubs/esp_event.h @@ -0,0 +1,8 @@ +#ifndef STUBS_ESP_EVENT_H +#define STUBS_ESP_EVENT_H + +#include "esp_err.h" + +static inline esp_err_t esp_event_loop_create_default(void) { return ESP_OK; } + +#endif diff --git a/tests/unit/stubs/esp_http_client.h b/tests/unit/stubs/esp_http_client.h new file mode 100644 index 0000000..4169714 --- /dev/null +++ b/tests/unit/stubs/esp_http_client.h @@ -0,0 +1,12 @@ +#ifndef STUBS_ESP_HTTP_CLIENT_H +#define STUBS_ESP_HTTP_CLIENT_H + +#include "esp_err.h" + +typedef void *esp_http_client_handle_t; + +typedef struct { + int cert_pem; +} esp_http_client_config_t; + +#endif diff --git a/tests/unit/stubs/esp_http_server.h b/tests/unit/stubs/esp_http_server.h new file mode 100644 index 0000000..22a5624 --- /dev/null +++ b/tests/unit/stubs/esp_http_server.h @@ -0,0 +1,10 @@ +#ifndef STUBS_ESP_HTTP_SERVER_H +#define STUBS_ESP_HTTP_SERVER_H + +#include "esp_err.h" +#include + +typedef void *httpd_handle_t; +typedef struct httpd_req httpd_req_t; + +#endif diff --git a/tests/unit/stubs/esp_log.h b/tests/unit/stubs/esp_log.h new file mode 100644 index 0000000..f353fe9 --- /dev/null +++ b/tests/unit/stubs/esp_log.h @@ -0,0 +1,10 @@ +#ifndef STUBS_ESP_LOG_H +#define STUBS_ESP_LOG_H + +#include + +#define ESP_LOGI(tag, fmt, ...) do { printf("I %s: " fmt "\n", tag, ##__VA_ARGS__); } while(0) +#define ESP_LOGW(tag, fmt, ...) do { printf("W %s: " fmt "\n", tag, ##__VA_ARGS__); } while(0) +#define ESP_LOGE(tag, fmt, ...) do { fprintf(stderr, "E %s: " fmt "\n", tag, ##__VA_ARGS__); } while(0) + +#endif diff --git a/tests/unit/stubs/esp_mac.h b/tests/unit/stubs/esp_mac.h new file mode 100644 index 0000000..ddc80d4 --- /dev/null +++ b/tests/unit/stubs/esp_mac.h @@ -0,0 +1,19 @@ +#ifndef STUBS_ESP_MAC_H +#define STUBS_ESP_MAC_H + +#include +#include + +static inline int esp_read_mac(uint8_t *mac, int type) { + (void)type; + memset(mac, 0, 6); + mac[0] = 0x02; + mac[1] = 0x00; + mac[2] = 0x00; + mac[3] = 0x00; + mac[4] = 0xBE; + mac[5] = 0xEF; + return 0; +} + +#endif diff --git a/tests/unit/stubs/esp_netif.h b/tests/unit/stubs/esp_netif.h new file mode 100644 index 0000000..f009537 --- /dev/null +++ b/tests/unit/stubs/esp_netif.h @@ -0,0 +1,17 @@ +#ifndef STUBS_ESP_NETIF_H +#define STUBS_ESP_NETIF_H + +#include + +typedef struct { + uint32_t addr; +} esp_ip4_addr_t; + +#define IPSTR "%d.%d.%d.%d" +#define IP2STR(ip) ((ip)->addr & 0xff), (((ip)->addr >> 8) & 0xff), (((ip)->addr >> 16) & 0xff), (((ip)->addr >> 24) & 0xff) + +static inline void IP4_ADDR(esp_ip4_addr_t *ip, uint8_t a, uint8_t b, uint8_t c, uint8_t d) { + ip->addr = ((uint32_t)a) | ((uint32_t)b << 8) | ((uint32_t)c << 16) | ((uint32_t)d << 24); +} + +#endif diff --git a/tests/unit/stubs/esp_spiffs.h b/tests/unit/stubs/esp_spiffs.h new file mode 100644 index 0000000..ae6a127 --- /dev/null +++ b/tests/unit/stubs/esp_spiffs.h @@ -0,0 +1,15 @@ +#ifndef STUBS_ESP_SPIFFS_H +#define STUBS_ESP_SPIFFS_H + +#include "esp_err.h" + +typedef struct { + const char *base_path; + const char *partition_label; + int max_files; + bool format_if_mount_failed; +} esp_vfs_spiffs_conf_t; + +static inline esp_err_t esp_vfs_spiffs_register(const esp_vfs_spiffs_conf_t *conf) { (void)conf; return ESP_OK; } + +#endif diff --git a/tests/unit/stubs/esp_system.h b/tests/unit/stubs/esp_system.h new file mode 100644 index 0000000..8e63c80 --- /dev/null +++ b/tests/unit/stubs/esp_system.h @@ -0,0 +1,4 @@ +#ifndef STUBS_ESP_SYSTEM_H +#define STUBS_ESP_SYSTEM_H + +#endif diff --git a/tests/unit/stubs/esp_tls.h b/tests/unit/stubs/esp_tls.h new file mode 100644 index 0000000..7ded63a --- /dev/null +++ b/tests/unit/stubs/esp_tls.h @@ -0,0 +1,25 @@ +#ifndef STUBS_ESP_TLS_H +#define STUBS_ESP_TLS_H + +#include "esp_err.h" + +typedef struct esp_tls esp_tls_t; + +typedef struct { + void *crt_bundle_attach; + int use_global_ca_store; +} esp_tls_cfg_t; + +static inline esp_tls_t *esp_tls_init(void) { return (esp_tls_t*)1; } +static inline int esp_tls_conn_new_sync(const char *h, int hl, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls) { + (void)h; (void)hl; (void)port; (void)cfg; (void)tls; return -1; +} +static inline int esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t len) { + (void)tls; (void)data; (void)len; return len; +} +static inline int esp_tls_conn_read(esp_tls_t *tls, void *data, size_t len) { + (void)tls; (void)data; (void)len; return 0; +} +static inline void esp_tls_conn_destroy(esp_tls_t *tls) { (void)tls; } + +#endif diff --git a/tests/unit/stubs/esp_wifi.h b/tests/unit/stubs/esp_wifi.h new file mode 100644 index 0000000..6aa5787 --- /dev/null +++ b/tests/unit/stubs/esp_wifi.h @@ -0,0 +1,40 @@ +#ifndef STUBS_ESP_WIFI_H +#define STUBS_ESP_WIFI_H + +#include +#include +#include "esp_err.h" + +#define WIFI_IF_STA 0 +#define WIFI_IF_AP 1 + +#define WIFI_AUTH_WPA2_PSK 3 +#define WIFI_AUTH_OPEN 0 + +#define WIFI_MODE_APSTA 3 + +typedef struct { + struct { + uint8_t ssid[32]; + uint8_t password[64]; + uint8_t channel; + uint8_t max_connection; + uint8_t ssid_hidden; + int authmode; + } ap; + struct { + uint8_t ssid[32]; + uint8_t password[64]; + int threshold; + struct { + int authmode; + } sta; + } sta; +} wifi_config_t; + +static inline esp_err_t esp_wifi_set_mac(int ifx, const uint8_t *mac) { (void)ifx; (void)mac; return ESP_OK; } +static inline esp_err_t esp_wifi_set_config(int ifx, const wifi_config_t *cfg) { (void)ifx; (void)cfg; return ESP_OK; } +static inline esp_err_t esp_wifi_set_mode(uint8_t mode) { (void)mode; return ESP_OK; } +static inline esp_err_t esp_wifi_start(void) { return ESP_OK; } + +#endif diff --git a/tests/unit/stubs/freertos/FreeRTOS.h b/tests/unit/stubs/freertos/FreeRTOS.h new file mode 100644 index 0000000..0fee758 --- /dev/null +++ b/tests/unit/stubs/freertos/FreeRTOS.h @@ -0,0 +1,11 @@ +#ifndef STUBS_FREERTOS_FREERTOS_H +#define STUBS_FREERTOS_FREERTOS_H + +#include + +static inline uint32_t xTaskGetTickCount(void) { return 0; } +static inline void vTaskDelay(uint32_t ticks) { (void)ticks; } +#define pdMS_TO_TICKS(ms) ((ms) / 10) +#define portMAX_DELAY 0xFFFFFFFF + +#endif diff --git a/tests/unit/stubs/freertos/event_groups.h b/tests/unit/stubs/freertos/event_groups.h new file mode 100644 index 0000000..28f6403 --- /dev/null +++ b/tests/unit/stubs/freertos/event_groups.h @@ -0,0 +1,13 @@ +#ifndef STUBS_FREERTOS_EVENT_GROUPS_H +#define STUBS_FREERTOS_EVENT_GROUPS_H + +#include + +typedef void *EventGroupHandle_t; +#define BIT0 (1 << 0) + +static inline EventGroupHandle_t xEventGroupCreate(void) { return (EventGroupHandle_t)1; } +static inline uint32_t xEventGroupSetBits(EventGroupHandle_t eg, uint32_t bits) { (void)eg; return bits; } +static inline uint32_t xEventGroupClearBits(EventGroupHandle_t eg, uint32_t bits) { (void)eg; return bits; } + +#endif diff --git a/tests/unit/stubs/freertos/task.h b/tests/unit/stubs/freertos/task.h new file mode 100644 index 0000000..3855d41 --- /dev/null +++ b/tests/unit/stubs/freertos/task.h @@ -0,0 +1,19 @@ +#ifndef STUBS_FREERTOS_TASK_H +#define STUBS_FREERTOS_TASK_H + +#include +#include + +typedef void *TaskHandle_t; +typedef void *SemaphoreHandle_t; + +static inline void vTaskDelete(TaskHandle_t t) { (void)t; } +static inline SemaphoreHandle_t xSemaphoreCreateMutex(void) { return (SemaphoreHandle_t)malloc(1); } +static inline void vSemaphoreDelete(SemaphoreHandle_t s) { free(s); } +static inline int xSemaphoreTake(SemaphoreHandle_t s, uint32_t blk) { (void)s; (void)blk; return 1; } +static inline int xSemaphoreGive(SemaphoreHandle_t s) { (void)s; return 1; } +static inline int xTaskCreate(void (*fn)(void*), const char *n, uint32_t st, void *p, uint32_t pri, TaskHandle_t *h) { + (void)fn; (void)n; (void)st; (void)p; (void)pri; (void)h; return 1; +} + +#endif diff --git a/tests/unit/stubs/freertos/timers.h b/tests/unit/stubs/freertos/timers.h new file mode 100644 index 0000000..7575807 --- /dev/null +++ b/tests/unit/stubs/freertos/timers.h @@ -0,0 +1,15 @@ +#ifndef STUBS_FREERTOS_TIMERS_H +#define STUBS_FREERTOS_TIMERS_H + +#include + +typedef void *TimerHandle_t; + +static inline TimerHandle_t xTimerCreate(const char *n, uint32_t pd, int ux, void *id, void *cb) { + (void)n; (void)pd; (void)ux; (void)id; (void)cb; return (TimerHandle_t)1; +} +static inline int xTimerStart(TimerHandle_t t, uint32_t blk) { (void)t; (void)blk; return 1; } +static inline int xTimerStop(TimerHandle_t t, uint32_t blk) { (void)t; (void)blk; return 1; } +static inline void xTimerDelete(TimerHandle_t t, uint32_t blk) { (void)t; (void)blk; } + +#endif diff --git a/tests/unit/stubs/lwip/ip4_addr.h b/tests/unit/stubs/lwip/ip4_addr.h new file mode 100644 index 0000000..174211b --- /dev/null +++ b/tests/unit/stubs/lwip/ip4_addr.h @@ -0,0 +1,19 @@ +#ifndef STUBS_LWIP_IP4_ADDR_H +#define STUBS_LWIP_IP4_ADDR_H + +#include + +typedef struct { + uint32_t addr; +} ip4_addr_t; + +typedef ip4_addr_t esp_ip4_addr_t; + +#define IPSTR "%d.%d.%d.%d" +#define IP2STR(ip) ((ip)->addr & 0xff), (((ip)->addr >> 8) & 0xff), (((ip)->addr >> 16) & 0xff), (((ip)->addr >> 24) & 0xff) + +static inline void IP4_ADDR(esp_ip4_addr_t *ip, uint8_t a, uint8_t b, uint8_t c, uint8_t d) { + ip->addr = ((uint32_t)a) | ((uint32_t)b << 8) | ((uint32_t)c << 16) | ((uint32_t)d << 24); +} + +#endif diff --git a/tests/unit/stubs/lwip/napt.h b/tests/unit/stubs/lwip/napt.h new file mode 100644 index 0000000..c6a5ca1 --- /dev/null +++ b/tests/unit/stubs/lwip/napt.h @@ -0,0 +1,6 @@ +#ifndef STUBS_LWIP_NAPT_H +#define STUBS_LWIP_NAPT_H + +static inline void ip_napt_enable(uint32_t num, int enable) { (void)num; (void)enable; } + +#endif diff --git a/tests/unit/stubs/lwip/netdb.h b/tests/unit/stubs/lwip/netdb.h new file mode 100644 index 0000000..b71bab8 --- /dev/null +++ b/tests/unit/stubs/lwip/netdb.h @@ -0,0 +1,4 @@ +#ifndef STUBS_LWIP_NETDB_H +#define STUBS_LWIP_NETDB_H + +#endif diff --git a/tests/unit/stubs/lwip/netif.h b/tests/unit/stubs/lwip/netif.h new file mode 100644 index 0000000..461a64e --- /dev/null +++ b/tests/unit/stubs/lwip/netif.h @@ -0,0 +1,4 @@ +#ifndef STUBS_LWIP_NETIF_H +#define STUBS_LWIP_NETIF_H + +#endif diff --git a/tests/unit/stubs/lwip/sockets.h b/tests/unit/stubs/lwip/sockets.h new file mode 100644 index 0000000..44f03ac --- /dev/null +++ b/tests/unit/stubs/lwip/sockets.h @@ -0,0 +1,4 @@ +#ifndef STUBS_LWIP_SOCKETS_H +#define STUBS_LWIP_SOCKETS_H + +#endif diff --git a/tests/unit/stubs/nvs_flash.h b/tests/unit/stubs/nvs_flash.h new file mode 100644 index 0000000..4424a9a --- /dev/null +++ b/tests/unit/stubs/nvs_flash.h @@ -0,0 +1,12 @@ +#ifndef STUBS_NVS_FLASH_H +#define STUBS_NVS_FLASH_H + +#include "esp_err.h" + +#define ESP_ERR_NVS_NO_FREE_PAGES 0x1101 +#define ESP_ERR_NVS_NEW_VERSION_FOUND 0x1102 + +static inline esp_err_t nvs_flash_init(void) { return ESP_OK; } +static inline esp_err_t nvs_flash_erase(void) { return ESP_OK; } + +#endif -- cgit v1.2.3