6 Commits

Author SHA1 Message Date
2e81966afb Bump version to 0.5.0 and update repo URLs. 2025-07-23 17:37:27 +03:00
5734f47cd3 Merge pull request 'feat/1030-more-unit-tests' (!29) from feat/1030-more-unit-tests into dev
Reviewed-on: #29
2025-07-22 16:59:47 +03:00
d884f5f45c Add missing test cases for tilt API.
Added missing test cases for the tilt API.

Closes #1056.
2025-07-22 10:02:40 +03:00
c7678d6084 Add restart chip to make qemu exit in tests. 2025-07-22 10:00:33 +03:00
8527ebea83 Fix imbalanced tilt counts.
Fixed the tilt count logic that causes an imbalance in tilting within the same run frame but opposite direction.

Fixes #1057.
2025-07-22 09:58:59 +03:00
f31eae649f Add a shell utility for running tests. 2025-07-21 18:47:41 +03:00
6 changed files with 276 additions and 8 deletions

View File

@@ -38,7 +38,7 @@ dependencies:
# Add as a custom component from git repository # Add as a custom component from git repository
relay_chn: relay_chn:
git: https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn.git git: https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn.git
version: '>=0.4.0' version: '>=0.5.0'
``` ```
## Usage ## Usage

View File

@@ -1,6 +1,6 @@
name: relay_chn name: relay_chn
version: "0.4.0" version: "0.5.0"
description: "Custom component for relay channel control" description: "Custom component for relay channel control"
license: "MIT" license: "MIT"
url: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn_component" url: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn"
repository: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn_component.git" repository: "https://git.kozmotronik.com.tr/KozmotronikTech/relay_chn.git"

156
scripts/run_tests.sh Executable file
View File

@@ -0,0 +1,156 @@
#!/usr/bin/env bash
set -e
# ==== 1. Check ESP-IDF environment ====
if [[ -z "$IDF_PATH" ]]; then
echo "❌ ESP-IDF environment not found. Please source the export.sh file first:"
echo "'. $HOME/esp/esp-idf/export.sh' or wherever the ESP-IDF is installed"
exit 1
fi
# ==== 2. Valid Modes and Defaults ====
valid_test_tags=("core" "tilt" "listener" "all" "relay_chn")
arg_tag="all" # Default to 'all' if no tag specified
arg_clean=false
arg_log=false
arg_dry_run=false
arg_sdkconfig_file=""
flag_file=false
print_help() {
echo "Usage: $0 -t <tags> [OPTIONS]"
echo ""
echo "This script builds and runs tests for the relay_chn component using QEMU."
echo ""
echo "Arguments:"
echo " -t, --tag [relay_chn|core|tilt|listener|all] Specify which test tag to run."
echo ""
echo " If no tag is specified, it defaults to 'all'."
echo ""
echo "Options:"
echo " -f, --file <path> Specify a custom sdkconfig file to use for the build."
echo " Defaults to 'sdkconfig.defaults' if not provided."
echo " -c, --clean Perform a 'fullclean' before building the tests."
echo " -l, --log Log the test output to a timestamped file."
echo " -n, --dry-run Build the project without running qemu."
echo " -h, --help Show this help message and exit."
}
help() {
print_help
exit 0
}
usage() {
print_help
exit 1
}
# ==== 3. Argument Parsing ====
while [[ $# -gt 0 ]]; do
case $1 in
--tag|-t)
arg_tag="$2"
shift 2
;;
--file|-f)
arg_sdkconfig_file="$2"
flag_file=true
shift 2
;;
--clean|-c)
arg_clean=true
shift
;;
--log|-l)
arg_log=true
shift
;;
--dry-run|-n)
arg_dry_run=true
shift
;;
--help|-h)
help
;;
*)
usage
;;
esac
done
# ==== 4. Validity Check ====
if [[ ! " ${valid_test_tags[*]} " =~ " $arg_tag " ]]; then
echo "❌ Invalid mode: '$arg_tag'"
usage
fi
# ==== 5. Resolve Paths and Switch to Working Directory ====
script_dir=$(dirname "$(readlink -f "$0")")
project_root=$(dirname "$script_dir")
echo "🔍 Searching for 'test_apps' directory in '$project_root'..."
test_apps_dir=$(find "$project_root" -type d -name "test_apps" | head -n 1)
if [[ -z "$test_apps_dir" || ! -d "$test_apps_dir" ]]; then
echo "❌ 'test_apps' directory not found within the project root: '$project_root'"
echo " Please ensure the script is in a 'scripts' directory and 'test_apps' is a sibling."
exit 1
fi
echo "✅ Found 'test_apps' at: $test_apps_dir"
if $flag_file; then
if [[ -z "$arg_sdkconfig_file" || ! -f "$arg_sdkconfig_file" ]]; then
echo "❌ Invalid or missing file: '$arg_sdkconfig_file'"
usage
fi
# Resolve to an absolute path to work correctly after changing directory
arg_sdkconfig_file=$(readlink -f "$arg_sdkconfig_file")
else
echo "⚠️ No SDK configuration file provided. Using default sdkconfig."
arg_sdkconfig_file="$test_apps_dir/sdkconfig.defaults"
fi
echo "🧪 Test mode: $arg_tag"
echo "🧹 Clean: $arg_clean | 📄 Log: $arg_log"
echo "📂 Changing to working directory: $test_apps_dir"
cd "$test_apps_dir" || exit 1
# ==== 6. Clean if requested ====
if $arg_clean; then
echo "🧹 Doing Fullclean..."
idf.py fullclean
rm sdkconfig
fi
# ==== 7. Building and Running Tests ====
# In some locales, we can get errors like: "Error: unknown opcode or format name 'wsr.IBREAKA1'"
# The 'LC_ALL=C' env variable is set to ensure consistent locale settings.
LC_ALL=C \
SDKCONFIG_DEFAULTS="$arg_sdkconfig_file" \
RELAY_CHN_UNITY_TEST_GROUP_TAG="$arg_tag" \
idf.py reconfigure build
echo "🚀 Running test with QEMU..."
if $arg_log; then
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
LOGFILE="test_log_${arg_tag}_$TIMESTAMP.txt"
if $arg_dry_run; then
echo "🔍 Dry run mode: Logging to $LOGFILE but not executing." | tee "$LOGFILE"
echo "Command: idf.py qemu" | tee "$LOGFILE"
else
echo "📜 Logging test output to: $LOGFILE"
idf.py qemu --qemu-extra-args "-no-reboot" | tee "$LOGFILE"
fi
else
if $arg_dry_run; then
echo "🔍 Dry run mode: Not executing idf.py qemu."
echo "Command: idf.py qemu"
else
echo "🚀 Running idf.py qemu..."
idf.py qemu --qemu-extra-args "-no-reboot"
fi
fi

View File

@@ -1268,8 +1268,11 @@ static uint32_t relay_chn_tilt_count_update(relay_chn_t *relay_chn)
return ++relay_chn->tilt_control.tilt_counter.tilt_forward_count; return ++relay_chn->tilt_control.tilt_counter.tilt_forward_count;
} }
else if (relay_chn->tilt_control.cmd == RELAY_CHN_TILT_CMD_REVERSE) { else if (relay_chn->tilt_control.cmd == RELAY_CHN_TILT_CMD_REVERSE) {
if (relay_chn->tilt_control.tilt_counter.tilt_forward_count > 0) if (relay_chn->tilt_control.tilt_counter.tilt_forward_count > 0) {
return --relay_chn->tilt_control.tilt_counter.tilt_forward_count; --relay_chn->tilt_control.tilt_counter.tilt_forward_count;
// Still should do one more move, return non-zero value
return 1;
}
else else
return 0; return 0;
} }
@@ -1283,8 +1286,11 @@ static uint32_t relay_chn_tilt_count_update(relay_chn_t *relay_chn)
return ++relay_chn->tilt_control.tilt_counter.tilt_reverse_count; return ++relay_chn->tilt_control.tilt_counter.tilt_reverse_count;
} }
else if (relay_chn->tilt_control.cmd == RELAY_CHN_TILT_CMD_FORWARD) { else if (relay_chn->tilt_control.cmd == RELAY_CHN_TILT_CMD_FORWARD) {
if (relay_chn->tilt_control.tilt_counter.tilt_reverse_count > 0) if (relay_chn->tilt_control.tilt_counter.tilt_reverse_count > 0) {
return --relay_chn->tilt_control.tilt_counter.tilt_reverse_count; --relay_chn->tilt_control.tilt_counter.tilt_reverse_count;
// Still should do one more move, return non-zero value
return 1;
}
else else
return 0; return 0;
} }

View File

@@ -44,4 +44,6 @@ void app_main(void)
UNITY_END(); UNITY_END();
ESP_LOGI(TEST_TAG, "All tests complete."); ESP_LOGI(TEST_TAG, "All tests complete.");
esp_restart(); // Restart to invoke qemu exit
} }

View File

@@ -282,4 +282,108 @@ TEST_CASE("tilt_auto with ID_ALL tilts channels based on last run direction", "[
// 3. Verify channel 0 tilts forward (last run was forward) and channel 1 tilts reverse (last run was reverse) // 3. Verify channel 0 tilts forward (last run was forward) and channel 1 tilts reverse (last run was reverse)
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(0)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(0));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(1)); TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(1));
}
// Test relay_chn_tilt_auto() chooses correct tilt direction
TEST_CASE("relay_chn_tilt_auto chooses correct direction", "[relay_chn][tilt][auto]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
// Prepare FORWARD
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_auto(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// Prepare REVERSE
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_REVERSE);
relay_chn_tilt_auto(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
}
// Test sensitivity set/get
TEST_CASE("relay_chn_tilt_sensitivity_set and get", "[relay_chn][tilt][sensitivity]") {
uint8_t ch = 0;
uint8_t val = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
relay_chn_tilt_sensitivity_set(ch, 0);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(0, val);
relay_chn_tilt_sensitivity_set(ch, 50);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(50, val);
relay_chn_tilt_sensitivity_set(ch, 100);
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(ch, &val, 1));
TEST_ASSERT_EQUAL_UINT8(100, val);
// Set all channels
relay_chn_tilt_sensitivity_set(RELAY_CHN_ID_ALL, 42);
uint8_t vals[CONFIG_RELAY_CHN_COUNT] = {0};
TEST_ESP_OK(relay_chn_tilt_sensitivity_get(RELAY_CHN_ID_ALL, vals, relay_chn_count));
for (int i = 0; i < relay_chn_count; ++i) {
TEST_ASSERT_EQUAL_UINT8(42, vals[i]);
}
}
// Test tilt counter logic: forward x3, reverse x3, extra reverse fails
TEST_CASE("tilt counter logic: forward and reverse consumption", "[relay_chn][tilt][counter]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
// Tilt forward 3 times
for (int i = 0; i < 3; ++i) {
relay_chn_tilt_forward(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
}
// Now tilt reverse 3 times (should succeed)
for (int i = 0; i < 3; ++i) {
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
if (i < 3) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_REVERSE, relay_chn_get_state(ch));
relay_chn_tilt_stop(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
}
}
// Extra reverse tilt should fail (counter exhausted)
relay_chn_tilt_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// Should not enter TILT_REVERSE, should remain FREE or STOPPED
relay_chn_state_t state = relay_chn_get_state(ch);
TEST_ASSERT(state != RELAY_CHN_STATE_TILT_REVERSE);
}
// Test run command during TILT state
TEST_CASE("run command during TILT state transitions correctly", "[relay_chn][tilt][run-during-tilt]") {
uint8_t ch = 0;
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
g_is_component_initialized = true;
prepare_channel_for_tilt(ch, RELAY_CHN_CMD_FORWARD);
relay_chn_tilt_forward(ch);
vTaskDelay(pdMS_TO_TICKS(opposite_inertia_ms + test_delay_margin_ms));
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_TILT_FORWARD, relay_chn_get_state(ch));
// Issue run reverse while in TILT_FORWARD
relay_chn_run_reverse(ch);
vTaskDelay(pdMS_TO_TICKS(test_delay_margin_ms));
// Should transition to REVERSE or REVERSE_PENDING depending on inertia logic
relay_chn_state_t state = relay_chn_get_state(ch);
TEST_ASSERT(state == RELAY_CHN_STATE_REVERSE || state == RELAY_CHN_STATE_REVERSE_PENDING);
} }