#!/bin/bash # Unit tests for run_himalaya.sh functions # Source test utilities source "$(dirname "$0")/test_utils.sh" # Make sure we can find the mock himalaya command export PATH="$(dirname "$0"):$PATH" # Source the functions from run_himalaya.sh for testing # We need to extract just the functions for testing create_function_file() { # Create a temporary file with just the functions cat > "$TEST_TEMP_DIR/functions.sh" << 'EOFUNC' #!/bin/bash # Function to get available message IDs from himalaya get_available_message_ids() { himalaya envelope list | awk 'NR > 2 && /^\| [0-9]/ {gsub(/[| ]/, "", $2); if($2 ~ /^[0-9]+$/) print $2}' | sort -n } # Function to get the latest (most recent) message ID get_latest_message_id() { get_available_message_ids | tail -1 } # Function to find the next valid message ID find_next_message_id() { local current_id="$1" get_available_message_ids | awk -v current="$current_id" '$1 > current {print $1; exit}' } # Function to find the previous valid message ID find_previous_message_id() { local current_id="$1" get_available_message_ids | awk -v current="$current_id" '$1 < current {prev=$1} END {if(prev) print prev}' } EOFUNC source "$TEST_TEMP_DIR/functions.sh" } # Test: get_available_message_ids with normal data test_get_available_message_ids_normal() { export MOCK_ENVELOPE_LIST="| ID | FLAGS | SUBJECT | FROM | DATE | |------|-------|---------------------------------------------------------------------------------------------------|------------------------------|------------------------| | 1 | | Important Meeting | john@example.com | 2024-01-15 10:00+00:00| | 5 | * | Project Update | sarah@company.com | 2024-01-14 15:30+00:00| | 7 | | Weekly Standup | team@startup.com | 2024-01-13 09:00+00:00| | 10 | | Contract Review | client@business.com | 2024-01-12 14:00+00:00|" create_function_file local result=$(get_available_message_ids | tr '\n' ' ') local expected="1 5 7 10 " assert_equals "$expected" "$result" "Should return sorted message IDs" } # Test: get_available_message_ids with empty inbox test_get_available_message_ids_empty() { export MOCK_EMPTY_INBOX="true" create_function_file local result=$(get_available_message_ids) assert_equals "" "$result" "Should return empty for empty inbox" } # Test: get_latest_message_id with normal data test_get_latest_message_id_normal() { export MOCK_ENVELOPE_LIST="| ID | FLAGS | SUBJECT | FROM | DATE | |------|-------|---------------------------------------------------------------------------------------------------|------------------------------|------------------------| | 1 | | Important Meeting | john@example.com | 2024-01-15 10:00+00:00| | 5 | * | Project Update | sarah@company.com | 2024-01-14 15:30+00:00| | 7 | | Weekly Standup | team@startup.com | 2024-01-13 09:00+00:00| | 10 | | Contract Review | client@business.com | 2024-01-12 14:00+00:00|" create_function_file local result=$(get_latest_message_id) assert_equals "10" "$result" "Should return the highest message ID" } # Test: get_latest_message_id with single message test_get_latest_message_id_single() { export MOCK_ENVELOPE_LIST="| ID | FLAGS | SUBJECT | FROM | DATE | |------|-------|---------------------------------------------------------------------------------------------------|------------------------------|------------------------| | 42 | | Only Message | single@example.com | 2024-01-15 10:00+00:00|" create_function_file local result=$(get_latest_message_id) assert_equals "42" "$result" "Should return the single message ID" } # Test: find_next_message_id with valid next test_find_next_message_id_valid() { export MOCK_ENVELOPE_LIST="| ID | FLAGS | SUBJECT | FROM | DATE | |------|-------|---------------------------------------------------------------------------------------------------|------------------------------|------------------------| | 1 | | Important Meeting | john@example.com | 2024-01-15 10:00+00:00| | 5 | * | Project Update | sarah@company.com | 2024-01-14 15:30+00:00| | 7 | | Weekly Standup | team@startup.com | 2024-01-13 09:00+00:00| | 10 | | Contract Review | client@business.com | 2024-01-12 14:00+00:00|" create_function_file local result=$(find_next_message_id "5") assert_equals "7" "$result" "Should return next available message ID" } # Test: find_next_message_id with no next available test_find_next_message_id_none() { export MOCK_ENVELOPE_LIST="| ID | FLAGS | SUBJECT | FROM | DATE | |------|-------|---------------------------------------------------------------------------------------------------|------------------------------|------------------------| | 1 | | Important Meeting | john@example.com | 2024-01-15 10:00+00:00| | 5 | * | Project Update | sarah@company.com | 2024-01-14 15:30+00:00|" create_function_file local result=$(find_next_message_id "5") assert_equals "" "$result" "Should return empty when no next message" } # Test: find_previous_message_id with valid previous test_find_previous_message_id_valid() { export MOCK_ENVELOPE_LIST="| ID | FLAGS | SUBJECT | FROM | DATE | |------|-------|---------------------------------------------------------------------------------------------------|------------------------------|------------------------| | 1 | | Important Meeting | john@example.com | 2024-01-15 10:00+00:00| | 5 | * | Project Update | sarah@company.com | 2024-01-14 15:30+00:00| | 7 | | Weekly Standup | team@startup.com | 2024-01-13 09:00+00:00| | 10 | | Contract Review | client@business.com | 2024-01-12 14:00+00:00|" create_function_file local result=$(find_previous_message_id "7") assert_equals "5" "$result" "Should return previous available message ID" } # Test: find_previous_message_id with no previous available test_find_previous_message_id_none() { export MOCK_ENVELOPE_LIST="| ID | FLAGS | SUBJECT | FROM | DATE | |------|-------|---------------------------------------------------------------------------------------------------|------------------------------|------------------------| | 5 | * | Project Update | sarah@company.com | 2024-01-14 15:30+00:00| | 7 | | Weekly Standup | team@startup.com | 2024-01-13 09:00+00:00|" create_function_file local result=$(find_previous_message_id "5") assert_equals "" "$result" "Should return empty when no previous message" } # Run all tests echo "Running unit tests for run_himalaya.sh functions..." echo run_test "get_available_message_ids with normal data" test_get_available_message_ids_normal run_test "get_available_message_ids with empty inbox" test_get_available_message_ids_empty run_test "get_latest_message_id with normal data" test_get_latest_message_id_normal run_test "get_latest_message_id with single message" test_get_latest_message_id_single run_test "find_next_message_id with valid next" test_find_next_message_id_valid run_test "find_next_message_id with no next available" test_find_next_message_id_none run_test "find_previous_message_id with valid previous" test_find_previous_message_id_valid run_test "find_previous_message_id with no previous available" test_find_previous_message_id_none print_test_summary