trying a simple shell script and fixing archives

This commit is contained in:
Tim Bendt
2025-07-15 22:13:46 -04:00
parent f7474a3805
commit df4c49c3ef
18 changed files with 1273 additions and 389 deletions

View File

View File

@@ -0,0 +1,6 @@
| 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|

View File

@@ -0,0 +1,3 @@
| ID | FLAGS | SUBJECT | FROM | DATE |
|------|-------|---------------------------------------------------------------------------------------------------|------------------------------|------------------------|
| 42 | | Only Message | single@example.com | 2024-01-15 10:00+00:00|

11
tests/fixtures/message_content_1.txt vendored Normal file
View File

@@ -0,0 +1,11 @@
From: john@example.com
To: user@example.com
Subject: Important Meeting
Date: 2024-01-15
Hi there,
We need to schedule an important meeting for next week. Please let me know your availability.
Best regards,
John

1
tests/himalaya Symbolic link
View File

@@ -0,0 +1 @@
mock_himalaya.sh

152
tests/integration_tests.sh Executable file
View File

@@ -0,0 +1,152 @@
#!/bin/bash
# Integration tests for run_himalaya.sh
# Source test utilities
source "$(dirname "$0")/test_utils.sh"
# Make sure we can find the mock himalaya command
export PATH="$(dirname "$0"):$PATH"
# Common test data in real himalaya format
NORMAL_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|"
EXTENDED_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|"
# Test: Script with no arguments should auto-discover latest message
test_script_auto_discover() {
export MOCK_ENVELOPE_LIST="$NORMAL_ENVELOPE_LIST"
# Simulate user input: quit immediately
local output=$(echo "q" | timeout 10 ./run_himalaya.sh 2>/dev/null)
local exit_code=$?
# Check that it found the latest message (5) and attempted to read it
if echo "$output" | grep -q "Latest message ID: 5" && echo "$output" | grep -q "Reading message 5"; then
return 0
else
echo "Expected auto-discovery of message 5, got: $output"
return 1
fi
}
# Test: Script with valid message ID
test_script_valid_id() {
export MOCK_ENVELOPE_LIST="$NORMAL_ENVELOPE_LIST"
# Simulate user input: quit immediately
local output=$(echo "q" | timeout 10 ./run_himalaya.sh 1 2>/dev/null)
# Check that it read the specified message
if echo "$output" | grep -q "Reading message 1"; then
return 0
else
echo "Expected reading of message 1, got: $output"
return 1
fi
}
# Test: Script with invalid message ID should fallback to latest
test_script_invalid_id_fallback() {
export MOCK_ENVELOPE_LIST="$NORMAL_ENVELOPE_LIST"
export MOCK_INVALID_MESSAGE_ID="99"
# Simulate user input: quit immediately
local output=$(echo "q" | timeout 10 ./run_himalaya.sh 99 2>/dev/null)
# Check that it fell back to latest message
if echo "$output" | grep -q "Opening latest message: 5"; then
return 0
else
echo "Expected fallback to latest message, got: $output"
return 1
fi
}
# Test: Empty inbox handling
test_script_empty_inbox() {
export MOCK_EMPTY_INBOX="true"
# Don't use timeout as it may interfere with exit codes
# Use bash -c to avoid subshell exit code issues
bash -c './run_himalaya.sh' > "$TEST_TEMP_DIR/output.txt" 2>&1
local exit_code=$?
local output=$(cat "$TEST_TEMP_DIR/output.txt")
# Should exit with error message about no messages and exit code 1
if echo "$output" | grep -q "No messages found in inbox" && [ $exit_code -eq 1 ]; then
return 0
else
echo "Expected 'No messages found' error with exit code 1, got: $output (exit code: $exit_code)"
return 1
fi
}
# Test: Navigation to next message
test_navigation_next() {
export MOCK_ENVELOPE_LIST="$EXTENDED_ENVELOPE_LIST"
# Simulate user input: next message, then quit
local output=$(printf "n\nq\n" | timeout 10 ./run_himalaya.sh 1 2>/dev/null)
# Check that it moved to next message (5)
if echo "$output" | grep -q "Opening next message: 5" && echo "$output" | grep -q "Reading message 5"; then
return 0
else
echo "Expected navigation to next message 5, got: $output"
return 1
fi
}
# Test: Navigation to previous message
test_navigation_previous() {
export MOCK_ENVELOPE_LIST="$EXTENDED_ENVELOPE_LIST"
# Simulate user input: previous message, then quit
local output=$(printf "p\nq\n" | timeout 10 ./run_himalaya.sh 7 2>/dev/null)
# Check that it moved to previous message (5)
if echo "$output" | grep -q "Opening previous message: 5" && echo "$output" | grep -q "Reading message 5"; then
return 0
else
echo "Expected navigation to previous message 5, got: $output"
return 1
fi
}
# Test: No next message available
test_navigation_no_next() {
export MOCK_ENVELOPE_LIST="$NORMAL_ENVELOPE_LIST"
# Simulate user input: next message (should fail), then quit
local output=$(printf "n\nq\n" | timeout 10 ./run_himalaya.sh 5 2>/dev/null)
# Check that it shows "No next message available"
if echo "$output" | grep -q "No next message available"; then
return 0
else
echo "Expected 'No next message available', got: $output"
return 1
fi
}
# Run all tests
echo "Running integration tests for run_himalaya.sh..."
echo
run_test "Script auto-discovers latest message when no args" test_script_auto_discover
run_test "Script reads specified valid message ID" test_script_valid_id
run_test "Script falls back to latest when invalid ID provided" test_script_invalid_id_fallback
run_test "Script handles empty inbox gracefully" test_script_empty_inbox
run_test "Navigation to next message works" test_navigation_next
run_test "Navigation to previous message works" test_navigation_previous
run_test "No next message available handled gracefully" test_navigation_no_next
print_test_summary

101
tests/mock_himalaya.sh Executable file
View File

@@ -0,0 +1,101 @@
#!/bin/bash
# Mock himalaya command for testing
# Controlled by environment variables for different test scenarios
# Mock less command that doesn't wait for input in tests
if [ -z "$MOCK_LESS_INTERACTIVE" ]; then
# In test mode, just output directly without paging
alias less='cat'
fi
# Default test data - matches real himalaya output format
DEFAULT_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|"
DEFAULT_MESSAGE_CONTENT="From: test@example.com
To: user@example.com
Subject: Test Message
Date: 2024-01-15
This is a test message content for testing purposes."
# Handle different commands
case "$1" in
"envelope")
case "$2" in
"list")
if [ -n "$MOCK_ENVELOPE_LIST" ]; then
echo "$MOCK_ENVELOPE_LIST"
elif [ "$MOCK_EMPTY_INBOX" = "true" ]; then
echo ""
elif [ "$MOCK_HIMALAYA_FAIL" = "true" ]; then
echo "Error: Unable to connect to server" >&2
exit 1
else
echo "$DEFAULT_ENVELOPE_LIST"
fi
;;
esac
;;
"message")
case "$2" in
"read")
message_id="$3"
if [ "$MOCK_HIMALAYA_FAIL" = "true" ]; then
echo "Error: Unable to read message $message_id" >&2
exit 1
elif [ -n "$MOCK_INVALID_MESSAGE_ID" ] && [ "$message_id" = "$MOCK_INVALID_MESSAGE_ID" ]; then
echo "Error: Message $message_id not found" >&2
exit 1
elif [ -n "$MOCK_ENVELOPE_LIST" ]; then
# Check if the message ID exists in our envelope list
# Handle both old tab-separated format and new table format
if echo "$MOCK_ENVELOPE_LIST" | grep -q "^$message_id " || echo "$MOCK_ENVELOPE_LIST" | grep -q "^| $message_id "; then
if [ -n "$MOCK_MESSAGE_CONTENT" ]; then
echo "$MOCK_MESSAGE_CONTENT"
else
echo "$DEFAULT_MESSAGE_CONTENT"
fi
else
echo "Error: Message $message_id not found" >&2
exit 1
fi
else
if [ -n "$MOCK_MESSAGE_CONTENT" ]; then
echo "$MOCK_MESSAGE_CONTENT"
else
echo "$DEFAULT_MESSAGE_CONTENT"
fi
fi
;;
"delete")
message_id="$3"
if [ "$MOCK_HIMALAYA_FAIL" = "true" ]; then
echo "Error: Unable to delete message $message_id" >&2
exit 1
else
echo "Message $message_id deleted successfully"
fi
;;
"move")
folder="$3"
message_id="$4"
if [ "$MOCK_HIMALAYA_FAIL" = "true" ]; then
echo "Error: Unable to move message $message_id to $folder" >&2
exit 1
else
echo "Message $message_id moved to $folder successfully"
fi
;;
esac
;;
*)
echo "Mock himalaya: Unknown command $1" >&2
exit 1
;;
esac

132
tests/test_utils.sh Normal file
View File

@@ -0,0 +1,132 @@
#!/bin/bash
# Test utilities for run_himalaya.sh testing
# Colors for test output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Test counters
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
# Setup function - prepares test environment
setup_test() {
# Clear any existing mock environment variables
unset MOCK_ENVELOPE_LIST
unset MOCK_MESSAGE_CONTENT
unset MOCK_EMPTY_INBOX
unset MOCK_HIMALAYA_FAIL
unset MOCK_INVALID_MESSAGE_ID
# Set up PATH to use mock himalaya
export PATH="$(pwd)/tests:$PATH"
# Create temporary files for testing
export TEST_TEMP_DIR="/tmp/himalaya_test_$$"
mkdir -p "$TEST_TEMP_DIR"
}
# Cleanup function
cleanup_test() {
# Remove temporary files
if [ -n "$TEST_TEMP_DIR" ] && [ -d "$TEST_TEMP_DIR" ]; then
rm -rf "$TEST_TEMP_DIR"
fi
# Reset PATH
export PATH=$(echo "$PATH" | sed "s|$(pwd)/tests:||")
}
# Function to simulate user input
simulate_input() {
local input="$1"
echo "$input"
}
# Function to run a test
run_test() {
local test_name="$1"
local test_function="$2"
echo -e "${YELLOW}Running: $test_name${NC}"
TESTS_RUN=$((TESTS_RUN + 1))
setup_test
if $test_function; then
echo -e "${GREEN}✓ PASS: $test_name${NC}"
TESTS_PASSED=$((TESTS_PASSED + 1))
else
echo -e "${RED}✗ FAIL: $test_name${NC}"
TESTS_FAILED=$((TESTS_FAILED + 1))
fi
cleanup_test
echo
}
# Function to assert equality
assert_equals() {
local expected="$1"
local actual="$2"
local message="$3"
if [ "$expected" = "$actual" ]; then
return 0
else
echo -e "${RED}Assertion failed: $message${NC}"
echo -e "${RED}Expected: '$expected'${NC}"
echo -e "${RED}Actual: '$actual'${NC}"
return 1
fi
}
# Function to assert command success
assert_success() {
local command="$1"
local message="$2"
if eval "$command" >/dev/null 2>&1; then
return 0
else
echo -e "${RED}Command failed: $message${NC}"
echo -e "${RED}Command: $command${NC}"
return 1
fi
}
# Function to assert command failure
assert_failure() {
local command="$1"
local message="$2"
if eval "$command" >/dev/null 2>&1; then
echo -e "${RED}Command unexpectedly succeeded: $message${NC}"
echo -e "${RED}Command: $command${NC}"
return 1
else
return 0
fi
}
# Function to print test summary
print_test_summary() {
echo "===================="
echo "Test Summary"
echo "===================="
echo "Tests run: $TESTS_RUN"
echo -e "Passed: ${GREEN}$TESTS_PASSED${NC}"
echo -e "Failed: ${RED}$TESTS_FAILED${NC}"
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}All tests passed!${NC}"
return 0
else
echo -e "${RED}Some tests failed!${NC}"
return 1
fi
}

165
tests/unit_tests.sh Executable file
View File

@@ -0,0 +1,165 @@
#!/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