diff --git a/test_apps/main/test_common.c b/test_apps/main/test_common.c index d8fdb4d..87d1811 100644 --- a/test_apps/main/test_common.c +++ b/test_apps/main/test_common.c @@ -1,4 +1,6 @@ #include "test_common.h" +#include "relay_chn_ctl.h" // For resetting the channels +#include "relay_chn_tilt.h" // For resetting tilt count const char *TEST_TAG = "RELAY_CHN_TEST"; @@ -32,36 +34,45 @@ const uint8_t gpio_map[] = {4, 5}; const uint8_t gpio_count = sizeof(gpio_map) / sizeof(gpio_map[0]); +static void reset_channel(relay_chn_ctl_t *ctl) +{ + ctl->pending_cmd = RELAY_CHN_CMD_NONE; + ctl->state = RELAY_CHN_STATE_IDLE; + ctl->output->direction = RELAY_CHN_DIRECTION_DEFAULT; + ctl->run_info->last_run_cmd = RELAY_CHN_CMD_NONE; + ctl->run_info->last_run_cmd_time_ms = 0; + esp_timer_stop(ctl->inertia_timer); +#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT + esp_timer_stop(ctl->run_limit_timer); + ctl->run_limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC; +#endif +#if CONFIG_RELAY_CHN_ENABLE_TILTING + relay_chn_tilt_reset_count(ctl->tilt_ctl); +#endif +#if CONFIG_RELAY_CHN_COUNT > 1 + #else +#endif +} + void reset_channels_to_defaults() { #if CONFIG_RELAY_CHN_COUNT > 1 - relay_chn_stop_all(); -#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT - relay_chn_set_run_limit_all_with(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC); -#endif - vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); - for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { - TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i)); + relay_chn_ctl_t *ctls = relay_chn_ctl_get_all(); + TEST_ASSERT_NOT_NULL_MESSAGE(ctls, "reset_channels_to_defaults: relay_chn_ctl_get_all() returned NULL"); - // Reset directions - if (relay_chn_get_direction(i) != RELAY_CHN_DIRECTION_DEFAULT) { - relay_chn_flip_direction(i); - TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(i)); - } + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + relay_chn_ctl_t *ctl = &ctls[i]; + TEST_ASSERT_NOT_NULL_MESSAGE(ctl, "ctl is NULL"); + reset_channel(ctl); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i)); + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(i)); } #else - relay_chn_stop(); -#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT - relay_chn_set_run_limit(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC); -#endif - - // Reset direction - if (relay_chn_get_direction() != RELAY_CHN_DIRECTION_DEFAULT) { - relay_chn_flip_direction(); - TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction()); - } - vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); + relay_chn_ctl_t *ctl = relay_chn_ctl_get(); + TEST_ASSERT_NOT_NULL_MESSAGE(ctl, "reset_channels_to_defaults: relay_chn_ctl_get() returned NULL"); + reset_channel(ctl); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction()); #endif } diff --git a/test_apps/main/test_relay_chn_core_multi.c b/test_apps/main/test_relay_chn_core_multi.c index 7796039..080bd5f 100644 --- a/test_apps/main/test_relay_chn_core_multi.c +++ b/test_apps/main/test_relay_chn_core_multi.c @@ -402,6 +402,7 @@ TEST_CASE("get_state_all retrieves all channel states", "[relay_chn][core][batch expect_states[i] = RELAY_CHN_STATE_REVERSE; } } + TEST_ESP_OK(relay_chn_get_state_all(states)); TEST_ASSERT_EQUAL_UINT_ARRAY(expect_states, states, CONFIG_RELAY_CHN_COUNT); } @@ -485,13 +486,15 @@ TEST_CASE("Test run limit stops channel after timeout", "[relay_chn][run_limit]" TEST_CASE("Test run limit reset on direction change and time out finally", "[relay_chn][run_limit]") { - for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { - // Set a short run limit - relay_chn_set_run_limit(i, TEST_SHORT_RUN_LIMIT_SEC); - - // Start running forward - relay_chn_run_forward(i); - } + relay_chn_set_run_limit_all_with(TEST_SHORT_RUN_LIMIT_SEC); + +#if CONFIG_RELAY_CHN_ENABLE_NVS + // Wait for the NVS module task to process operations + vTaskDelay(300 / portTICK_PERIOD_MS); // Wait 1 second +#endif + + // Start running forward + relay_chn_run_forward_all(); vTaskDelay(1000 / portTICK_PERIOD_MS); // Wait 1 second diff --git a/test_apps/main/test_relay_chn_nvs_multi.c b/test_apps/main/test_relay_chn_nvs_multi.c index 4270d7d..6ac3bb1 100644 --- a/test_apps/main/test_relay_chn_nvs_multi.c +++ b/test_apps/main/test_relay_chn_nvs_multi.c @@ -9,88 +9,95 @@ #include "esp_system.h" #include "nvs_flash.h" #include "relay_chn_nvs.h" +#include "test_common.h" -TEST_CASE("Test relay storage init/deinit", "[relay_chn][nvs]") -{ - TEST_ESP_OK(relay_chn_nvs_init()); - TEST_ESP_OK(relay_chn_nvs_deinit()); -} +#define TEST_NVS_TASK_TIME_OUT_MS 300 TEST_CASE("Test direction setting and getting", "[relay_chn][nvs]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // Test all channels - relay_chn_direction_t dir; - relay_chn_direction_t test_directions[] = { - RELAY_CHN_DIRECTION_DEFAULT, - RELAY_CHN_DIRECTION_FLIPPED - }; + relay_chn_direction_t dir, expect; - for (int channel = 0; channel < 2; channel++) { - TEST_ESP_OK(relay_chn_nvs_set_direction(channel, test_directions[channel])); - TEST_ESP_OK(relay_chn_nvs_get_direction(channel, &dir)); - TEST_ASSERT_EQUAL(test_directions[channel], dir); + for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { + dir = channel % 2 == 0 ? RELAY_CHN_DIRECTION_DEFAULT : RELAY_CHN_DIRECTION_FLIPPED; + TEST_ESP_OK(relay_chn_nvs_set_direction(channel, dir)); } + + // Wait for the batch commit timeout to ensure the value is written + vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); - TEST_ESP_OK(relay_chn_nvs_deinit()); + for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { + expect = channel % 2 == 0 ? RELAY_CHN_DIRECTION_DEFAULT : RELAY_CHN_DIRECTION_FLIPPED; + TEST_ESP_OK(relay_chn_nvs_get_direction(channel, &dir, RELAY_CHN_DIRECTION_DEFAULT)); + TEST_ASSERT_EQUAL(expect, dir); + } } TEST_CASE("Test invalid parameters", "[relay_chn][nvs]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // Test NULL pointer for all channels for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { - TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(channel, NULL)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(channel, NULL, RELAY_CHN_DIRECTION_DEFAULT)); } - - TEST_ESP_OK(relay_chn_nvs_deinit()); } TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // Store some test data first - relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED; - // Set direction for all channels - TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction)); - TEST_ESP_OK(relay_chn_nvs_set_direction(1, direction)); + for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { + // Set direction for all channels + TEST_ESP_OK(relay_chn_nvs_set_direction(channel, RELAY_CHN_DIRECTION_FLIPPED)); + +#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT + TEST_ESP_OK(relay_chn_nvs_set_run_limit(channel, 100 + channel)); +#endif #if CONFIG_RELAY_CHN_ENABLE_TILTING - uint8_t sensitivity = 50; - for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { - TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(channel, sensitivity)); + TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(channel, 50)); TEST_ESP_OK(relay_chn_nvs_set_tilt_count(channel, 100 + channel)); } #endif + // Wait for the set operations and subsequent commits to complete + // Wait 4 times more since 4 x 8 = 32 operations to process + vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS * 8)); + // Test erase all TEST_ESP_OK(relay_chn_nvs_erase_all()); + // Wait for the erase operation and subsequent commit to complete + vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Verify data was erased by trying to read it back - relay_chn_direction_t read_direction; - TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &read_direction)); - -#if CONFIG_RELAY_CHN_ENABLE_TILTING for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { - uint8_t read_sensitivity; - TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(channel, &read_sensitivity)); + 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); + } - uint16_t tilt_count; - TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(channel, &tilt_count)); +#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT + for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { + uint16_t read_run_limit; + TEST_ESP_OK(relay_chn_nvs_get_run_limit(channel, &read_run_limit, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC)); + TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC, read_run_limit); } #endif - TEST_ESP_OK(relay_chn_nvs_deinit()); +#if CONFIG_RELAY_CHN_ENABLE_TILTING + const uint8_t default_sensitivity_for_test = 42; + for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { + uint8_t read_sensitivity; + TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(channel, &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(channel, &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]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // Use different values for each channel to detect overwrites uint16_t test_limits[CONFIG_RELAY_CHN_COUNT]; for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { @@ -102,32 +109,31 @@ TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]") TEST_ESP_OK(relay_chn_nvs_set_run_limit(i, test_limits[i])); } + // Allow the NVS task to process the batch and commit + vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); + // 2. Then, read them all back and verify for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { uint16_t run_limit_read; - TEST_ESP_OK(relay_chn_nvs_get_run_limit(i, &run_limit_read)); + TEST_ESP_OK(relay_chn_nvs_get_run_limit(i, &run_limit_read, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC)); TEST_ASSERT_EQUAL_UINT16(test_limits[i], run_limit_read); } // 3. Verify that changing one channel doesn't affect another uint16_t new_limit_ch0 = 99; TEST_ESP_OK(relay_chn_nvs_set_run_limit(0, new_limit_ch0)); - + vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write uint16_t read_val_ch0, read_val_ch1; - TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &read_val_ch0)); - TEST_ESP_OK(relay_chn_nvs_get_run_limit(1, &read_val_ch1)); + TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &read_val_ch0, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC)); + TEST_ESP_OK(relay_chn_nvs_get_run_limit(1, &read_val_ch1, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC)); TEST_ASSERT_EQUAL_UINT16(new_limit_ch0, read_val_ch0); TEST_ASSERT_EQUAL_UINT16(test_limits[1], read_val_ch1); // Should still be the old value - - TEST_ESP_OK(relay_chn_nvs_deinit()); } #endif #if CONFIG_RELAY_CHN_ENABLE_TILTING TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]") { - TEST_ESP_OK(relay_chn_nvs_init()); - uint8_t test_sensitivities[CONFIG_RELAY_CHN_COUNT]; for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { test_sensitivities[i] = 70 + i; // e.g., 70, 71, 72... @@ -138,20 +144,19 @@ TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]") TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(i, test_sensitivities[i])); } + // Allow the NVS task to process the batch and commit + vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); + // 2. Then, read them all back and verify for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { uint8_t sensitivity_read; - TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(i, &sensitivity_read)); + TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(i, &sensitivity_read, 0)); TEST_ASSERT_EQUAL_UINT8(test_sensitivities[i], sensitivity_read); } - - TEST_ESP_OK(relay_chn_nvs_deinit()); } TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]") { - TEST_ESP_OK(relay_chn_nvs_init()); - uint16_t test_counts[CONFIG_RELAY_CHN_COUNT]; for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { test_counts[i] = 100 + i; // e.g., 100, 101, 102... @@ -162,26 +167,23 @@ TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]") TEST_ESP_OK(relay_chn_nvs_set_tilt_count(i, test_counts[i])); } + // Allow the NVS task to process the batch and commit + vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); + // 2. Then, read them all back and verify for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { uint16_t count_read; - TEST_ESP_OK(relay_chn_nvs_get_tilt_count(i, &count_read)); + TEST_ESP_OK(relay_chn_nvs_get_tilt_count(i, &count_read, 0)); TEST_ASSERT_EQUAL_UINT16(test_counts[i], count_read); } - - TEST_ESP_OK(relay_chn_nvs_deinit()); } TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // Test NULL pointers for all channels for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) { - TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(channel, NULL)); - TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(channel, NULL, 0)); + TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL, 0)); } - - TEST_ESP_OK(relay_chn_nvs_deinit()); } #endif // CONFIG_RELAY_CHN_ENABLE_TILTING \ No newline at end of file diff --git a/test_apps/main/test_relay_chn_nvs_single.c b/test_apps/main/test_relay_chn_nvs_single.c index 06240a2..135d1d2 100644 --- a/test_apps/main/test_relay_chn_nvs_single.c +++ b/test_apps/main/test_relay_chn_nvs_single.c @@ -9,50 +9,43 @@ #include "esp_system.h" #include "nvs_flash.h" #include "relay_chn_nvs.h" +#include "test_common.h" - -TEST_CASE("Test relay storage init/deinit", "[relay_chn][nvs]") -{ - TEST_ESP_OK(relay_chn_nvs_init()); - TEST_ESP_OK(relay_chn_nvs_deinit()); -} +#define TEST_NVS_TASK_TIME_OUT_MS 300 TEST_CASE("Test direction setting and getting", "[relay_chn][nvs]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // Test channel 0 TEST_ESP_OK(relay_chn_nvs_set_direction(0, RELAY_CHN_DIRECTION_DEFAULT)); relay_chn_direction_t dir; - TEST_ESP_OK(relay_chn_nvs_get_direction(0, &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)); - TEST_ESP_OK(relay_chn_nvs_get_direction(0, &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_FLIPPED, dir); - - TEST_ESP_OK(relay_chn_nvs_deinit()); } TEST_CASE("Test invalid parameters", "[relay_chn][nvs]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // Test NULL pointer - TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(0, NULL)); - - TEST_ESP_OK(relay_chn_nvs_deinit()); + 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]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // 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)); @@ -62,76 +55,71 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]") // 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_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &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_TILTING - uint8_t read_sensitivity; - TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity)); - - uint16_t tilt_count; - TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &tilt_count)); +#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 - TEST_ESP_OK(relay_chn_nvs_deinit()); +#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]") { - TEST_ESP_OK(relay_chn_nvs_init()); - 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)); + 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); - - TEST_ESP_OK(relay_chn_nvs_deinit()); } #endif #if CONFIG_RELAY_CHN_ENABLE_TILTING TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]") { - TEST_ESP_OK(relay_chn_nvs_init()); - 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)); + TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(0, &sensitivity, 0)); TEST_ASSERT_EQUAL(test_sensitivity, sensitivity); - - TEST_ESP_OK(relay_chn_nvs_deinit()); } TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]") { - TEST_ESP_OK(relay_chn_nvs_init()); - 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)); + TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &tilt_count_read, 0)); TEST_ASSERT_EQUAL(tilt_count, tilt_count_read); - - TEST_ESP_OK(relay_chn_nvs_deinit()); } TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]") { - TEST_ESP_OK(relay_chn_nvs_init()); - // Test NULL pointers - TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(0, NULL)); - TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, NULL)); - - TEST_ESP_OK(relay_chn_nvs_deinit()); + 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 \ No newline at end of file diff --git a/test_apps/main/test_relay_chn_tilt_multi.c b/test_apps/main/test_relay_chn_tilt_multi.c index 1de119c..87b8d79 100644 --- a/test_apps/main/test_relay_chn_tilt_multi.c +++ b/test_apps/main/test_relay_chn_tilt_multi.c @@ -362,25 +362,26 @@ TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][ti relay_chn_tilt_set_sensitivity_all_with(100); // Set sentivity to max for fastest execution // Tilt forward 3 times - for (int i = 0; i < 3; ++i) { - relay_chn_tilt_forward_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS)); - check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); - relay_chn_tilt_stop_all(); - } + relay_chn_tilt_forward_all(); + vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS * 3 + TEST_DELAY_MARGIN_MS)); + check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); + + // Stop tilt on all channels + relay_chn_tilt_stop_all(); +#if CONFIG_RELAY_CHN_ENABLE_NVS + // Tilt stop should save the latest tilt count to the NVS + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS + 300)); +#else vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); +#endif // Now tilt reverse 3 times (should succeed) - for (int i = 0; i < 3; ++i) { - relay_chn_tilt_reverse_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS)); - check_all_channels_for_state(RELAY_CHN_STATE_TILT_REVERSE); - relay_chn_tilt_stop_all(); - } - - // Extra reverse tilt should fail (counter exhausted) relay_chn_tilt_reverse_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS * 3 + TEST_DELAY_MARGIN_MS)); + check_all_channels_for_state(RELAY_CHN_STATE_TILT_REVERSE); + + // One more reverse tilt should fail (counter exhausted) + vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS * 3)); // Should not enter TILT_REVERSE, should remain IDLE check_all_channels_for_state(RELAY_CHN_STATE_IDLE); } diff --git a/test_apps/main/test_relay_chn_tilt_single.c b/test_apps/main/test_relay_chn_tilt_single.c index 5f8b36b..a2d6deb 100644 --- a/test_apps/main/test_relay_chn_tilt_single.c +++ b/test_apps/main/test_relay_chn_tilt_single.c @@ -155,7 +155,7 @@ TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn] } // Test stopping from a tilt state (no inertia for stop command itself) -// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_stop) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_IDLE +// Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_tilt_stop) -> RELAY_CHN_STATE_IDLE TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_chn][tilt][inertia]") { // Prepare channel by running forward first to set last_run_cmd, then tilt @@ -165,7 +165,7 @@ TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_ TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); // 2. Issue stop command - relay_chn_stop(); + relay_chn_tilt_stop(); // Stop command should apply immediately, setting state to FREE since last state was tilt. vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state());