diff --git a/test_apps/main/test_relay_chn.c b/test_apps/main/test_relay_chn.c index d0975fd..92ba06d 100644 --- a/test_apps/main/test_relay_chn.c +++ b/test_apps/main/test_relay_chn.c @@ -647,6 +647,91 @@ TEST_CASE("Tilt functionality is disabled, skipping tilt tests", "[relay_chn][ti #endif // CONFIG_RELAY_CHN_ENABLE_TILTING +// ### Direction Flipping Tests + +TEST_CASE("Single channel direction can be flipped", "[relay_chn][direction]") +{ + TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); + g_is_component_initialized = true; + const uint8_t ch = 0; + + // 1. Initial direction should be default + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(ch)); + + // 2. Flip the direction + relay_chn_flip_direction(ch); + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); // Wait for flip inertia + + // 3. Verify direction is flipped + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction(ch)); + + // 4. Flip back + relay_chn_flip_direction(ch); + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); // Wait for flip inertia + + // 5. Verify direction is back to default + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(ch)); +} + +TEST_CASE("All channels direction can be flipped simultaneously", "[relay_chn][direction]") +{ + TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); + g_is_component_initialized = true; + + // 1. Flip all channels + relay_chn_flip_direction(RELAY_CHN_ID_ALL); + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); + + // 2. Verify all channels are flipped + for (uint8_t i = 0; i < relay_chn_count; i++) { + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction(i)); + } + + // 3. Flip all back + relay_chn_flip_direction(RELAY_CHN_ID_ALL); + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); + + // 4. Verify all channels are back to default + for (uint8_t i = 0; i < relay_chn_count; i++) { + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(i)); + } +} + +TEST_CASE("Flipping a running channel stops it and flips direction", "[relay_chn][direction]") +{ + TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); + g_is_component_initialized = true; + const uint8_t ch = 0; + + // 1. Start channel running and verify state + relay_chn_run_forward(ch); + vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(ch)); + + // 2. Flip the direction while running + relay_chn_flip_direction(ch); + vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms)); // Give time for events to process + + // 3. The channel should stop as part of the flip process + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state(ch)); + + // 4. Wait for the flip inertia to pass, after which it should be FREE and FLIPPED + vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FREE, relay_chn_get_state(ch)); + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, relay_chn_get_direction(ch)); +} + +TEST_CASE("Direction flip handles invalid channel ID gracefully", "[relay_chn][direction]") +{ + TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); + g_is_component_initialized = true; + const uint8_t invalid_ch = relay_chn_count + 5; + + relay_chn_flip_direction(invalid_ch); // Call with an invalid ID + TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(invalid_ch)); +} + + // ### `app_main` Function // --- app_main function ---