From 497f45e336aadf6ff70c022af8efb5a71bfa4af2 Mon Sep 17 00:00:00 2001 From: ismail Date: Sat, 13 Sep 2025 09:52:09 +0300 Subject: [PATCH] Fix and optimize tilt test case issues Tilt tests started to fail after the latest changes. Had to make some fixes and optimizations so that the test code resets the tilt controls correctly and aligns with relay_chn's timing requirements. Fixes #1115 --- test_apps/main/test_relay_chn_tilt_multi.c | 124 ++++++++++++++------ test_apps/main/test_relay_chn_tilt_single.c | 68 +++++------ 2 files changed, 123 insertions(+), 69 deletions(-) diff --git a/test_apps/main/test_relay_chn_tilt_multi.c b/test_apps/main/test_relay_chn_tilt_multi.c index 0934444..4895f48 100644 --- a/test_apps/main/test_relay_chn_tilt_multi.c +++ b/test_apps/main/test_relay_chn_tilt_multi.c @@ -21,14 +21,13 @@ void check_all_channels_for_state(relay_chn_state_t state) { for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + // ESP_LOGI(TEST_TAG, "Checking channel %d for state %d", i, state); TEST_ASSERT_EQUAL(state, relay_chn_get_state(i)); } } // Helper function to prepare channel for tilt tests void prepare_channels_for_tilt_with_mixed_runs() { - // Ensure the channel reset tilt control - relay_chn_tilt_stop_all(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Ensure the channel has had a 'last_run_cmd' @@ -40,16 +39,19 @@ void prepare_channels_for_tilt_with_mixed_runs() { } } - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Allow command to process - relay_chn_stop_all(); // Stop it to set last_run_cmd but return to FREE for next test - vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); - check_all_channels_for_state(RELAY_CHN_STATE_IDLE); + for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + relay_chn_state_t expect_state; + if (i % 2 == 0) { + expect_state = RELAY_CHN_STATE_FORWARD; + } else { // Assuming initial_cmd is RELAY_CHN_CMD_REVERSE + expect_state = RELAY_CHN_STATE_REVERSE; + } + TEST_ASSERT_EQUAL(expect_state, relay_chn_get_state(i)); + } } // Helper function to prepare channel for tilt tests void prepare_all_channels_for_tilt(int initial_cmd) { - // Ensure the channel reset tilt control - relay_chn_tilt_stop_all(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // If the channels are not IDLE yet, wait more @@ -64,6 +66,8 @@ void prepare_all_channels_for_tilt(int initial_cmd) { if (not_idle) { vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS)); } + // Ensure all channels are IDLE + check_all_channels_for_state(RELAY_CHN_STATE_IDLE); // Ensure the channel has had a 'last_run_cmd' if (initial_cmd == RELAY_CHN_CMD_FORWARD) { @@ -71,20 +75,17 @@ void prepare_all_channels_for_tilt(int initial_cmd) { } else { // Assuming initial_cmd is RELAY_CHN_CMD_REVERSE relay_chn_run_reverse_all(); } - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Allow command to process - relay_chn_stop_all(); // Stop all to set last_run_cmd but return to FREE for next test - vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); - check_all_channels_for_state(RELAY_CHN_STATE_IDLE); + relay_chn_state_t expect_state = initial_cmd == RELAY_CHN_CMD_FORWARD + ? RELAY_CHN_STATE_FORWARD : RELAY_CHN_STATE_REVERSE; + check_all_channels_for_state(expect_state); + ESP_LOGI(TEST_TAG, "All channels prepared for tilt test"); } // TEST_CASE: Test transition from running forward to tilt forward // Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][tilt][inertia]") { - // Prepare channel by running forward first to set last_run_cmd - prepare_all_channels_for_tilt(RELAY_CHN_CMD_FORWARD); - // 1. Start in forward direction relay_chn_run_forward_all(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); @@ -99,15 +100,15 @@ TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][ti // Wait for the inertia period (after which the tilt command will be dispatched) vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS)); check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // TEST_CASE: Test transition from running reverse to tilt reverse // Scenario: RELAY_CHN_STATE_REVERSE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][tilt][inertia]") { - // Prepare channel by running reverse first to set last_run_cmd - prepare_all_channels_for_tilt(RELAY_CHN_CMD_REVERSE); - // 1. Start in reverse direction relay_chn_run_reverse_all(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); @@ -120,6 +121,9 @@ TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][ti vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); check_all_channels_for_state(RELAY_CHN_STATE_TILT_REVERSE); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // TEST_CASE: Test transition from FREE state to tilt forward (now with preparation) @@ -128,12 +132,19 @@ TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn { // Prepare channel by running forward first to set last_run_cmd prepare_all_channels_for_tilt(RELAY_CHN_CMD_FORWARD); + relay_chn_stop_all(); // Stop to trigger IDLE + // Wait for the channel to transition to IDLE + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); + check_all_channels_for_state(RELAY_CHN_STATE_IDLE); // Ensure we are back to IDLE // Issue tilt forward command relay_chn_tilt_forward_all(); // From FREE state, tilt command should still incur the inertia due to the internal timer logic vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // TEST_CASE: Test transition from FREE state to tilt reverse (now with preparation) @@ -142,11 +153,18 @@ TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn { // Prepare channel by running reverse first to set last_run_cmd prepare_all_channels_for_tilt(RELAY_CHN_CMD_REVERSE); + relay_chn_stop_all(); // Stop to trigger IDLE + // Wait for the channel to transition to IDLE + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); + check_all_channels_for_state(RELAY_CHN_STATE_IDLE); // Ensure we are back to IDLE // Issue tilt reverse command relay_chn_tilt_reverse_all(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); check_all_channels_for_state(RELAY_CHN_STATE_TILT_REVERSE); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // TEST_CASE: Test transition from tilt forward to run forward (inertia expected for run) @@ -165,6 +183,9 @@ TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][ti check_all_channels_for_state(RELAY_CHN_STATE_FORWARD_PENDING); vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); check_all_channels_for_state(RELAY_CHN_STATE_FORWARD); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // TEST_CASE: Test transition from tilt reverse to run reverse (no inertia expected for run) @@ -182,6 +203,9 @@ TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][ti check_all_channels_for_state(RELAY_CHN_STATE_REVERSE_PENDING); vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); check_all_channels_for_state(RELAY_CHN_STATE_REVERSE); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // TEST_CASE: Test transition from tilt forward to run reverse (without inertia) @@ -198,6 +222,9 @@ TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn] relay_chn_run_reverse_all(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); check_all_channels_for_state(RELAY_CHN_STATE_REVERSE); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // TEST_CASE: Test stopping from a tilt state (no inertia for stop command itself) @@ -228,10 +255,14 @@ TEST_CASE("tilt_forward_all sets all channels to TILT_FORWARD", "[relay_chn][til // 2. Issue tilt forward to all channels relay_chn_tilt_forward_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Tilt from FREE doesn't have stop-inertia + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); // 3. Verify all channels are tilting forward check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } TEST_CASE("tilt_reverse_all sets all channels to TILT_REVERSE", "[relay_chn][tilt][batch]") @@ -241,10 +272,14 @@ TEST_CASE("tilt_reverse_all sets all channels to TILT_REVERSE", "[relay_chn][til // 2. Issue tilt reverse to all channels relay_chn_tilt_reverse_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); // 3. Verify all channels are tilting reverse check_all_channels_for_state(RELAY_CHN_STATE_TILT_REVERSE); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } TEST_CASE("tilt_stop_all stops all tilting channels", "[relay_chn][tilt][batch]") @@ -253,7 +288,10 @@ TEST_CASE("tilt_stop_all stops all tilting channels", "[relay_chn][tilt][batch]" prepare_all_channels_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); + // 3. Verify all channels are tilting forward + check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); // 2. Stop tilting on all channels relay_chn_tilt_stop_all(); @@ -265,15 +303,13 @@ TEST_CASE("tilt_stop_all stops all tilting channels", "[relay_chn][tilt][batch]" TEST_CASE("tilt_auto_all tilts channels based on last run direction", "[relay_chn][tilt][batch]") { - // This test requires at least 2 channels to demonstrate different behaviors - TEST_ASSERT_GREATER_OR_EQUAL_MESSAGE(2, CONFIG_RELAY_CHN_COUNT, "Test requires at least 2 channels"); - // 1. Prepare channel 0 with last run FORWARD and channel 1 with last run REVERSE prepare_channels_for_tilt_with_mixed_runs(); // 2. Issue auto tilt command to all channels relay_chn_tilt_auto_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Tilt from FREE state is dispatched immediately + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); // 3. Verify even channels tilt forward (last run was forward) and odd channels tilt reverse (last run was reverse) for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { @@ -282,6 +318,9 @@ TEST_CASE("tilt_auto_all tilts channels based on last run direction", "[relay_ch TEST_ASSERT_EQUAL(state, relay_chn_get_state(i)); } + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // Test relay_chn_tilt_auto() chooses correct tilt direction @@ -290,19 +329,24 @@ TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][au // Prepare FORWARD prepare_all_channels_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_auto_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); // Verify all tilt forward check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); + // Ensure the channel reset tilt control relay_chn_tilt_stop_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Prepare REVERSE prepare_all_channels_for_tilt(RELAY_CHN_CMD_REVERSE); relay_chn_tilt_auto_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); // Verify all tilt reverse check_all_channels_for_state(RELAY_CHN_STATE_TILT_REVERSE); + + // Ensure the channel reset tilt control + relay_chn_tilt_stop_all(); } // Test sensitivity set/get @@ -375,7 +419,14 @@ TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][ti #define TEST_TILT_EXECUTION_TIME_MS 100 prepare_all_channels_for_tilt(RELAY_CHN_CMD_FORWARD); + relay_chn_tilt_set_sensitivity_all_with(100); // Set sentivity to max for fastest execution + // Ensure sensitivities are set correctly + uint8_t sensitivities[CONFIG_RELAY_CHN_COUNT]; + uint8_t expect[CONFIG_RELAY_CHN_COUNT]; + memset(expect, 100, CONFIG_RELAY_CHN_COUNT); + relay_chn_tilt_get_sensitivity_all(sensitivities); + TEST_ASSERT_EQUAL_UINT8_ARRAY(expect, sensitivities, CONFIG_RELAY_CHN_COUNT); // Tilt forward 3 times relay_chn_tilt_forward_all(); @@ -393,12 +444,12 @@ TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][ti // Now tilt reverse 3 times (should succeed) relay_chn_tilt_reverse_all(); - vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS * 3 + TEST_DELAY_MARGIN_MS)); + vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS)); check_all_channels_for_state(RELAY_CHN_STATE_TILT_REVERSE); - - // One more reverse tilt should fail (counter exhausted) + // Let it execute 2 at least, or more vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS * 3)); - // Should not enter TILT_REVERSE, should remain IDLE + + // More reverse tilt should fail (counter exhausted) check_all_channels_for_state(RELAY_CHN_STATE_IDLE); } @@ -423,12 +474,12 @@ TEST_CASE("run_all command during active tilt cycle stops tilt", "[relay_chn][ti // Set a known sensitivity for predictable timing. // For sensitivity=50, move_time=30ms, pause_time=270ms. relay_chn_tilt_set_sensitivity_all_with(50); - const uint32_t move_time_ms = 30; // --- Test interrupting during MOVE step --- prepare_all_channels_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward_all(); - vTaskDelay(pdMS_TO_TICKS(move_time_ms / 2)); // Wait for half of the move time + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); // Interrupt with run_reverse_all while in the MOVE part of the cycle @@ -438,9 +489,12 @@ TEST_CASE("run_all command during active tilt cycle stops tilt", "[relay_chn][ti check_all_channels_for_state(RELAY_CHN_STATE_REVERSE); // --- Test interrupting during PAUSE step --- + relay_chn_stop_all(); // Stop the reverse runs + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); prepare_all_channels_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward_all(); - vTaskDelay(pdMS_TO_TICKS(move_time_ms + TEST_DELAY_MARGIN_MS)); // Wait past MOVE, into PAUSE + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); // Wait past MOVE, into PAUSE check_all_channels_for_state(RELAY_CHN_STATE_TILT_FORWARD); // Interrupt with run_forward_all while in the PAUSE part of the cycle diff --git a/test_apps/main/test_relay_chn_tilt_single.c b/test_apps/main/test_relay_chn_tilt_single.c index 481793b..a312c12 100644 --- a/test_apps/main/test_relay_chn_tilt_single.c +++ b/test_apps/main/test_relay_chn_tilt_single.c @@ -30,19 +30,15 @@ void prepare_channel_for_tilt(int initial_cmd) { } else { // Assuming initial_cmd is RELAY_CHN_CMD_REVERSE relay_chn_run_reverse(); } - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); // Allow command to process - relay_chn_stop(); // Stop it to set last_run_cmd but return to FREE for next test vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); - TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); + relay_chn_state_t expect_state = initial_cmd == RELAY_CHN_CMD_FORWARD ? RELAY_CHN_STATE_FORWARD : RELAY_CHN_STATE_REVERSE; + TEST_ASSERT_EQUAL(expect_state, relay_chn_get_state()); } // Test transition from running forward to tilt forward // Scenario: RELAY_CHN_STATE_FORWARD -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][tilt][inertia]") { - // Prepare channel by running forward first to set last_run_cmd - prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); - // 1. Start in forward direction relay_chn_run_forward(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); @@ -63,9 +59,6 @@ TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][ti // Scenario: RELAY_CHN_STATE_REVERSE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][tilt][inertia]") { - // Prepare channel by running reverse first to set last_run_cmd - prepare_channel_for_tilt(RELAY_CHN_CMD_REVERSE); - // 1. Start in reverse direction relay_chn_run_reverse(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); @@ -86,6 +79,9 @@ TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn { // Prepare channel by running forward first to set last_run_cmd prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); + relay_chn_stop(); // Stop to trigger IDLE + // Wait for the channel to transition to IDLE + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); // Ensure we are back to FREE // Issue tilt forward command @@ -101,6 +97,9 @@ TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn { // Prepare channel by running reverse first to set last_run_cmd prepare_channel_for_tilt(RELAY_CHN_CMD_REVERSE); + relay_chn_stop(); // Stop to trigger IDLE + // Wait for the channel to transition to IDLE + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); // Ensure we are back to FREE // Issue tilt reverse command @@ -183,7 +182,8 @@ TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][au // Prepare FORWARD prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_auto(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); relay_chn_tilt_stop(); vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); @@ -191,7 +191,8 @@ TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][au // Prepare REVERSE prepare_channel_for_tilt(RELAY_CHN_CMD_REVERSE); relay_chn_tilt_auto(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state()); } @@ -237,31 +238,25 @@ TEST_CASE("relay_chn_tilt_set_sensitivity handles upper boundary", "[relay_chn][ // Test tilt counter logic: forward x3, reverse x3, extra reverse fails TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][tilt][counter]") { +// Tilt execution time at 100% sensitivity in milliseconds (10 + 90) +#define TEST_TILT_EXECUTION_TIME_MS 100 + prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); // Tilt forward 3 times - for (int i = 0; i < 3; ++i) { - relay_chn_tilt_forward(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); - TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); - relay_chn_tilt_stop(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); - } + relay_chn_tilt_forward(); + vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS * 3 + TEST_DELAY_MARGIN_MS)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); + relay_chn_tilt_stop(); // Now tilt reverse 3 times (should succeed) - for (int i = 0; i < 3; ++i) { - relay_chn_tilt_reverse(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); - if (i < 3) { - TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state()); - relay_chn_tilt_stop(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); - } - } - - // Extra reverse tilt should fail (counter exhausted) relay_chn_tilt_reverse(); - vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + // Let it execute one time + vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state()); + // Let it execute 2 at least, or more + vTaskDelay(pdMS_TO_TICKS(TEST_TILT_EXECUTION_TIME_MS * 3)); + // Should not enter TILT_REVERSE, should remain FREE or STOPPED relay_chn_state_t state = relay_chn_get_state(); TEST_ASSERT(state != RELAY_CHN_STATE_TILT_REVERSE); @@ -289,12 +284,12 @@ TEST_CASE("run command during active tilt cycle stops tilt", "[relay_chn][tilt][ // Set a known sensitivity for predictable timing. // For sensitivity=50, move_time=30ms, pause_time=270ms. relay_chn_tilt_set_sensitivity(50); - const uint32_t move_time_ms = 30; // --- Test interrupting during MOVE step --- prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); relay_chn_tilt_forward(); - vTaskDelay(pdMS_TO_TICKS(move_time_ms / 2)); // Wait for half of the move time + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); // Interrupt with run_reverse while in the MOVE part of the cycle @@ -304,9 +299,14 @@ TEST_CASE("run command during active tilt cycle stops tilt", "[relay_chn][tilt][ TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state()); // --- Test interrupting during PAUSE step --- - prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); + relay_chn_stop(); // Stop the reverse run + // Wait the channel to be IDLE + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); + + prepare_channel_for_tilt(RELAY_CHN_CMD_FORWARD); // Prepare channel again relay_chn_tilt_forward(); - vTaskDelay(pdMS_TO_TICKS(move_time_ms + TEST_DELAY_MARGIN_MS)); // Wait past MOVE, into PAUSE + // Should incur inertia timer + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); // Interrupt with run_forward while in the PAUSE part of the cycle