From ec1b25d489034310d60e09b7e0029d7d7526f467 Mon Sep 17 00:00:00 2001 From: ismail Date: Wed, 27 Aug 2025 17:26:32 +0300 Subject: [PATCH] Add missing test cases Some missing test cases for the public API have been added. Closes #1090 --- test_apps/main/test_relay_chn_core_multi.c | 31 ++++++++++- test_apps/main/test_relay_chn_core_single.c | 58 ++++++++++++++++++++- test_apps/main/test_relay_chn_tilt_single.c | 48 ++++++++++++++--- 3 files changed, 127 insertions(+), 10 deletions(-) diff --git a/test_apps/main/test_relay_chn_core_multi.c b/test_apps/main/test_relay_chn_core_multi.c index 9ccdd28..23a8e2a 100644 --- a/test_apps/main/test_relay_chn_core_multi.c +++ b/test_apps/main/test_relay_chn_core_multi.c @@ -450,4 +450,33 @@ TEST_CASE("Test run limit persistence across stop/start", "[relay_chn][run_limit TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit(i)); } } -#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1 \ No newline at end of file + +TEST_CASE("Run limit functions handle invalid channel ID", "[relay_chn][run_limit]") +{ + const uint8_t invalid_ch = CONFIG_RELAY_CHN_COUNT + 5; + const uint16_t original_limit = relay_chn_get_run_limit(0); + + // get_run_limit with invalid ID should return 0 + TEST_ASSERT_EQUAL(0, relay_chn_get_run_limit(invalid_ch)); + + // set_run_limit with invalid ID should not crash or affect other channels + relay_chn_set_run_limit(invalid_ch, 999); + TEST_ASSERT_EQUAL(original_limit, relay_chn_get_run_limit(0)); +} +#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT + +TEST_CASE("relay_chn_destroy allows clean-up and re-creation", "[relay_chn][core]") +{ + relay_chn_run_forward_all(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state(i)); + } + + relay_chn_destroy(); + TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); + + for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) { + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i)); + } +} diff --git a/test_apps/main/test_relay_chn_core_single.c b/test_apps/main/test_relay_chn_core_single.c index c26fe7c..171f98a 100644 --- a/test_apps/main/test_relay_chn_core_single.c +++ b/test_apps/main/test_relay_chn_core_single.c @@ -255,4 +255,60 @@ TEST_CASE("Test run limit persistence across stop/start", "[relay_chn][run_limit // Run limit should persist TEST_ASSERT_EQUAL(TEST_RUN_LIMIT_SEC, relay_chn_get_run_limit()); } -#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1 \ No newline at end of file +#endif // CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT == 1 + +TEST_CASE("relay_chn_get_state_str returns correct strings", "[relay_chn][core]") +{ + // This test is a bit contrived as it's hard to force every state + // without complex sequences. We will test the most common ones. + TEST_ASSERT_EQUAL_STRING("IDLE", relay_chn_get_state_str()); + + relay_chn_run_forward(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + TEST_ASSERT_EQUAL_STRING("FORWARD", relay_chn_get_state_str()); + + relay_chn_run_reverse(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + TEST_ASSERT_EQUAL_STRING("REVERSE_PENDING", relay_chn_get_state_str()); + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS)); + TEST_ASSERT_EQUAL_STRING("REVERSE", relay_chn_get_state_str()); + + relay_chn_stop(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + TEST_ASSERT_EQUAL_STRING("STOPPED", relay_chn_get_state_str()); +} + +TEST_CASE("Stop command interrupts pending commands", "[relay_chn][core][inertia]") +{ + // 1. Start running forward + relay_chn_run_forward(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state()); + + // 2. Issue a reverse command, which will make the state REVERSE_PENDING + relay_chn_run_reverse(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE_PENDING, relay_chn_get_state()); + + // 3. Before the inertia timer fires, issue a stop command + relay_chn_stop(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_STOPPED, relay_chn_get_state()); + + // 4. Wait for more than the inertia period + vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS)); + // The channel should transition to IDLE, not REVERSE, because stop cancelled the pending command. + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); +} + +TEST_CASE("relay_chn_destroy allows clean-up and re-creation", "[relay_chn][core]") +{ + relay_chn_run_forward(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state()); + + relay_chn_destroy(); + TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count)); + + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); +} diff --git a/test_apps/main/test_relay_chn_tilt_single.c b/test_apps/main/test_relay_chn_tilt_single.c index 4cef671..5f8b36b 100644 --- a/test_apps/main/test_relay_chn_tilt_single.c +++ b/test_apps/main/test_relay_chn_tilt_single.c @@ -30,7 +30,7 @@ void prepare_channel_for_tilt(int initial_cmd) { TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state()); } -// TEST_CASE: Test transition from running forward to tilt forward +// 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]") { @@ -53,7 +53,7 @@ TEST_CASE("Run Forward to Tilt Forward transition with inertia", "[relay_chn][ti TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); } -// TEST_CASE: Test transition from running reverse to tilt reverse +// 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]") { @@ -74,7 +74,7 @@ TEST_CASE("Run Reverse to Tilt Reverse transition with inertia", "[relay_chn][ti TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state()); } -// TEST_CASE: Test transition from FREE state to tilt forward (now with preparation) +// Test transition from FREE state to tilt forward (now with preparation) // Scenario: RELAY_CHN_STATE_IDLE -> (prepare) -> RELAY_CHN_STATE_IDLE -> (relay_chn_tilt_forward) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_FORWARD TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn][tilt][inertia]") { @@ -89,7 +89,7 @@ TEST_CASE("FREE to Tilt Forward transition with inertia (prepared)", "[relay_chn TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); } -// TEST_CASE: Test transition from FREE state to tilt reverse (now with preparation) +// Test transition from FREE state to tilt reverse (now with preparation) // Scenario: RELAY_CHN_STATE_IDLE -> (prepare) -> RELAY_CHN_STATE_IDLE -> (relay_chn_tilt_reverse) -> RELAY_CHN_STATE_STOPPED -> (inertia) -> RELAY_CHN_STATE_TILT_REVERSE TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn][tilt][inertia]") { @@ -103,7 +103,7 @@ TEST_CASE("FREE to Tilt Reverse transition with inertia (prepared)", "[relay_chn TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state()); } -// TEST_CASE: Test transition from tilt forward to run forward (inertia expected for run) +// Test transition from tilt forward to run forward (inertia expected for run) // Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_forward) -> RELAY_CHN_STATE_FORWARD TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][tilt][inertia]") { @@ -121,7 +121,7 @@ TEST_CASE("Tilt Forward to Run Forward transition with inertia", "[relay_chn][ti TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD, relay_chn_get_state()); } -// TEST_CASE: Test transition from tilt reverse to run reverse (no inertia expected for run) +// Test transition from tilt reverse to run reverse (no inertia expected for run) // Scenario: RELAY_CHN_STATE_TILT_REVERSE -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][tilt][inertia]") { @@ -138,7 +138,7 @@ TEST_CASE("Tilt Reverse to Run Reverse transition with inertia", "[relay_chn][ti TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state()); } -// TEST_CASE: Test transition from tilt forward to run reverse (without inertia) +// Test transition from tilt forward to run reverse (without inertia) // Scenario: RELAY_CHN_STATE_TILT_FORWARD -> (relay_chn_run_reverse) -> RELAY_CHN_STATE_REVERSE TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn][tilt][inertia]") { @@ -154,7 +154,7 @@ TEST_CASE("Tilt Forward to Run Reverse transition without inertia", "[relay_chn] TEST_ASSERT_EQUAL(RELAY_CHN_STATE_REVERSE, relay_chn_get_state()); } -// TEST_CASE: Test stopping from a tilt state (no inertia for stop command itself) +// 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 TEST_CASE("Tilt to Stop transition without immediate inertia for stop", "[relay_chn][tilt][inertia]") { @@ -264,4 +264,36 @@ TEST_CASE("run command during TILT state transitions correctly", "[relay_chn][ti // Should transition to REVERSE or REVERSE_PENDING depending on inertia logic relay_chn_state_t state = relay_chn_get_state(); TEST_ASSERT(state == RELAY_CHN_STATE_REVERSE || state == RELAY_CHN_STATE_REVERSE_PENDING); +} + +// Test run command during active tilt cycle (move/pause) +TEST_CASE("run command during active tilt cycle stops tilt", "[relay_chn][tilt][interrupt]") +{ + // 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 + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); + + // Interrupt with run_reverse while in the MOVE part of the cycle + relay_chn_run_reverse(); + vTaskDelay(pdMS_TO_TICKS(TEST_DELAY_MARGIN_MS)); + // Should stop tilting and go to REVERSE immediately (no inertia from TILT_FORWARD) + 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_tilt_forward(); + vTaskDelay(pdMS_TO_TICKS(move_time_ms + TEST_DELAY_MARGIN_MS)); // Wait past MOVE, into PAUSE + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state()); + + // Interrupt with run_forward while in the PAUSE part of the cycle + relay_chn_run_forward(); + // Should stop tilting and go to FORWARD_PENDING (inertia from TILT_FORWARD) + TEST_ASSERT_EQUAL(RELAY_CHN_STATE_FORWARD_PENDING, relay_chn_get_state()); } \ No newline at end of file