/* * SPDX-FileCopyrightText: 2025 Kozmotronik Tech * * SPDX-License-Identifier: MIT */ #include #include "unity.h" #include "esp_system.h" #include "nvs_flash.h" #include "relay_chn_nvs.h" #include "test_common.h" #define TEST_NVS_TASK_TIME_OUT_MS 300 TEST_CASE("Test direction setting and getting", "[relay_chn][nvs]") { // Test channel 0 TEST_ESP_OK(relay_chn_nvs_set_direction(0, RELAY_CHN_DIRECTION_DEFAULT)); relay_chn_direction_t dir; vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit TEST_ESP_OK(relay_chn_nvs_get_direction(0, &dir, RELAY_CHN_DIRECTION_DEFAULT)); TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, dir); // Test channel 1 TEST_ESP_OK(relay_chn_nvs_set_direction(0, RELAY_CHN_DIRECTION_FLIPPED)); vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit TEST_ESP_OK(relay_chn_nvs_get_direction(0, &dir, RELAY_CHN_DIRECTION_DEFAULT)); TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, dir); } TEST_CASE("Test invalid parameters", "[relay_chn][nvs]") { // Test NULL pointer TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(0, NULL, RELAY_CHN_DIRECTION_DEFAULT)); } TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]") { // Store some test data first relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED; TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction)); #if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT uint16_t run_limit = 123; TEST_ESP_OK(relay_chn_nvs_set_run_limit(0, run_limit)); #endif #if CONFIG_RELAY_CHN_ENABLE_TILTING uint8_t sensitivity = 50; TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, sensitivity)); TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100)); #endif // Test erase all TEST_ESP_OK(relay_chn_nvs_erase_all()); vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit // Verify data was erased by trying to read it back relay_chn_direction_t read_direction; TEST_ESP_OK(relay_chn_nvs_get_direction(0, &read_direction, RELAY_CHN_DIRECTION_DEFAULT)); TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, read_direction); #if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT uint16_t read_run_limit; TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &read_run_limit, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC)); TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC, read_run_limit); #endif #if CONFIG_RELAY_CHN_ENABLE_TILTING const uint8_t default_sensitivity_for_test = 42; uint8_t read_sensitivity; TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity, default_sensitivity_for_test)); TEST_ASSERT_EQUAL(default_sensitivity_for_test, read_sensitivity); uint16_t tilt_count; TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &tilt_count, 0)); TEST_ASSERT_EQUAL(0, tilt_count); #endif } #if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]") { const uint16_t run_limit_sec = 32; TEST_ESP_OK(relay_chn_nvs_set_run_limit(0, run_limit_sec)); vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit uint16_t run_limit_read; TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &run_limit_read, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC)); TEST_ASSERT_EQUAL(run_limit_sec, run_limit_read); } #endif #if CONFIG_RELAY_CHN_ENABLE_TILTING TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]") { const uint8_t test_sensitivity = 75; TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, test_sensitivity)); vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit uint8_t sensitivity; TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(0, &sensitivity, 0)); TEST_ASSERT_EQUAL(test_sensitivity, sensitivity); } TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]") { const uint16_t tilt_count = 100; // Test setting counters TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, tilt_count)); vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit uint16_t tilt_count_read; TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &tilt_count_read, 0)); TEST_ASSERT_EQUAL(tilt_count, tilt_count_read); } TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]") { // Test NULL pointers TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(0, NULL, 0)); TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, NULL, 0)); } #endif // CONFIG_RELAY_CHN_ENABLE_TILTING