|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Test suite for cron-calls dynamic time computation |
| 5 | +# Validates the shared compute-time-until-meeting.py helper used in |
| 6 | +# cron-calls-community.sh and cron-calls-office-hours.sh |
| 7 | + |
| 8 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 9 | +HELPER="$SCRIPT_DIR/compute-time-until-meeting.py" |
| 10 | + |
| 11 | +# Color codes for output |
| 12 | +RED='\033[0;31m' |
| 13 | +GREEN='\033[0;32m' |
| 14 | +YELLOW='\033[1;33m' |
| 15 | +NC='\033[0m' |
| 16 | + |
| 17 | +# Test counters |
| 18 | +TESTS_RUN=0 |
| 19 | +TESTS_PASSED=0 |
| 20 | +TESTS_FAILED=0 |
| 21 | + |
| 22 | +print_result() { |
| 23 | + local test_name="$1" |
| 24 | + local result="$2" |
| 25 | + local message="${3:-}" |
| 26 | + |
| 27 | + TESTS_RUN=$((TESTS_RUN + 1)) |
| 28 | + |
| 29 | + if [[ "$result" == "PASS" ]]; then |
| 30 | + TESTS_PASSED=$((TESTS_PASSED + 1)) |
| 31 | + echo -e "${GREEN}✓ PASS${NC}: $test_name" |
| 32 | + else |
| 33 | + TESTS_FAILED=$((TESTS_FAILED + 1)) |
| 34 | + echo -e "${RED}✗ FAIL${NC}: $test_name" |
| 35 | + if [[ -n "$message" ]]; then |
| 36 | + echo -e " ${YELLOW}→${NC} $message" |
| 37 | + fi |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +# Test 1: Standard case — 4 hours before meeting |
| 42 | +test_standard_4_hours() { |
| 43 | + echo "" |
| 44 | + echo "Test 1: Standard case — 4 hours before meeting (10:00 → 14:00)" |
| 45 | + echo "================================================================" |
| 46 | + |
| 47 | + local result |
| 48 | + result=$(python3 "$HELPER" 14 "10:00") |
| 49 | + |
| 50 | + if [[ "$result" == "4 hours" ]]; then |
| 51 | + print_result "4 hours before meeting" "PASS" |
| 52 | + else |
| 53 | + print_result "4 hours before meeting" "FAIL" "Expected '4 hours', got '$result'" |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +# Test 2: Delayed run — 3 hours and 7 minutes remaining |
| 58 | +test_delayed_run() { |
| 59 | + echo "" |
| 60 | + echo "Test 2: Delayed run — 3 hours 7 minutes remaining (10:53 → 14:00)" |
| 61 | + echo "===================================================================" |
| 62 | + |
| 63 | + local result |
| 64 | + result=$(python3 "$HELPER" 14 "10:53") |
| 65 | + |
| 66 | + if [[ "$result" == "3 hours and 7 minutes" ]]; then |
| 67 | + print_result "Delayed run shows 3 hours and 7 minutes" "PASS" |
| 68 | + else |
| 69 | + print_result "Delayed run shows 3 hours and 7 minutes" "FAIL" "Expected '3 hours and 7 minutes', got '$result'" |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +# Test 3: Just minutes remaining |
| 74 | +test_just_minutes() { |
| 75 | + echo "" |
| 76 | + echo "Test 3: Just minutes remaining (13:45 → 14:00)" |
| 77 | + echo "================================================" |
| 78 | + |
| 79 | + local result |
| 80 | + result=$(python3 "$HELPER" 14 "13:45") |
| 81 | + |
| 82 | + if [[ "$result" == "15 minutes" ]]; then |
| 83 | + print_result "15 minutes remaining" "PASS" |
| 84 | + else |
| 85 | + print_result "15 minutes remaining" "FAIL" "Expected '15 minutes', got '$result'" |
| 86 | + fi |
| 87 | +} |
| 88 | + |
| 89 | +# Test 4: Exact whole hours |
| 90 | +test_exact_hours() { |
| 91 | + echo "" |
| 92 | + echo "Test 4: Exact whole hours (12:00 → 14:00)" |
| 93 | + echo "===========================================" |
| 94 | + |
| 95 | + local result |
| 96 | + result=$(python3 "$HELPER" 14 "12:00") |
| 97 | + |
| 98 | + if [[ "$result" == "2 hours" ]]; then |
| 99 | + print_result "Exactly 2 hours remaining" "PASS" |
| 100 | + else |
| 101 | + print_result "Exactly 2 hours remaining" "FAIL" "Expected '2 hours', got '$result'" |
| 102 | + fi |
| 103 | +} |
| 104 | + |
| 105 | +# Test 5: Already past meeting time — clamped to 0 |
| 106 | +test_past_meeting_time() { |
| 107 | + echo "" |
| 108 | + echo "Test 5: Already past meeting time (15:00 → 14:00)" |
| 109 | + echo "===================================================" |
| 110 | + |
| 111 | + local result |
| 112 | + result=$(python3 "$HELPER" 14 "15:00") |
| 113 | + |
| 114 | + if [[ "$result" == "0 minutes" ]]; then |
| 115 | + print_result "Past meeting time shows 0 minutes" "PASS" |
| 116 | + else |
| 117 | + print_result "Past meeting time shows 0 minutes" "FAIL" "Expected '0 minutes', got '$result'" |
| 118 | + fi |
| 119 | +} |
| 120 | + |
| 121 | +# Test 6: Singular hour — 1 hour remaining |
| 122 | +test_singular_hour() { |
| 123 | + echo "" |
| 124 | + echo "Test 6: Singular hour (13:00 → 14:00)" |
| 125 | + echo "=======================================" |
| 126 | + |
| 127 | + local result |
| 128 | + result=$(python3 "$HELPER" 14 "13:00") |
| 129 | + |
| 130 | + if [[ "$result" == "1 hour" ]]; then |
| 131 | + print_result "Singular hour formatting" "PASS" |
| 132 | + else |
| 133 | + print_result "Singular hour formatting" "FAIL" "Expected '1 hour', got '$result'" |
| 134 | + fi |
| 135 | +} |
| 136 | + |
| 137 | +# Test 7: Singular minute — 1 hour and 1 minute |
| 138 | +test_singular_minute() { |
| 139 | + echo "" |
| 140 | + echo "Test 7: Singular minute (12:59 → 14:00)" |
| 141 | + echo "=========================================" |
| 142 | + |
| 143 | + local result |
| 144 | + result=$(python3 "$HELPER" 14 "12:59") |
| 145 | + |
| 146 | + if [[ "$result" == "1 hour and 1 minute" ]]; then |
| 147 | + print_result "Singular hour and minute formatting" "PASS" |
| 148 | + else |
| 149 | + print_result "Singular hour and minute formatting" "FAIL" "Expected '1 hour and 1 minute', got '$result'" |
| 150 | + fi |
| 151 | +} |
| 152 | + |
| 153 | +# Test 8: Different meeting hour |
| 154 | +test_different_meeting_hour() { |
| 155 | + echo "" |
| 156 | + echo "Test 8: Different meeting hour (08:30 → 12:00)" |
| 157 | + echo "================================================" |
| 158 | + |
| 159 | + local result |
| 160 | + result=$(python3 "$HELPER" 12 "08:30") |
| 161 | + |
| 162 | + if [[ "$result" == "3 hours and 30 minutes" ]]; then |
| 163 | + print_result "Different meeting hour works" "PASS" |
| 164 | + else |
| 165 | + print_result "Different meeting hour works" "FAIL" "Expected '3 hours and 30 minutes', got '$result'" |
| 166 | + fi |
| 167 | +} |
| 168 | + |
| 169 | +# Test 9: Rounding with seconds — ceil rounds partial minutes up |
| 170 | +test_rounding_with_seconds() { |
| 171 | + echo "" |
| 172 | + echo "Test 9: Rounding with seconds (10:53:30 → 14:00) rounds up" |
| 173 | + echo "============================================================" |
| 174 | + |
| 175 | + local result |
| 176 | + result=$(python3 "$HELPER" 14 "10:53:30") |
| 177 | + |
| 178 | + if [[ "$result" == "3 hours and 7 minutes" ]]; then |
| 179 | + print_result "Partial minute rounds up correctly" "PASS" |
| 180 | + else |
| 181 | + print_result "Partial minute rounds up correctly" "FAIL" "Expected '3 hours and 7 minutes', got '$result'" |
| 182 | + fi |
| 183 | +} |
| 184 | + |
| 185 | +# Test 10: Seconds just past the minute boundary |
| 186 | +test_seconds_just_past_boundary() { |
| 187 | + echo "" |
| 188 | + echo "Test 10: Seconds just past boundary (13:00:01 → 14:00) rounds up to 60 min" |
| 189 | + echo "============================================================================" |
| 190 | + |
| 191 | + local result |
| 192 | + result=$(python3 "$HELPER" 14 "13:00:01") |
| 193 | + |
| 194 | + if [[ "$result" == "1 hour" ]]; then |
| 195 | + print_result "Just past boundary rounds up to full hour" "PASS" |
| 196 | + else |
| 197 | + print_result "Just past boundary rounds up to full hour" "FAIL" "Expected '1 hour', got '$result'" |
| 198 | + fi |
| 199 | +} |
| 200 | + |
| 201 | +main() { |
| 202 | + echo "==============================================" |
| 203 | + echo " Cron Calls Time Computation - Test Suite" |
| 204 | + echo "==============================================" |
| 205 | + |
| 206 | + if [[ ! -f "$HELPER" ]]; then |
| 207 | + echo -e "${RED}ERROR: Helper script not found at $HELPER${NC}" |
| 208 | + exit 1 |
| 209 | + fi |
| 210 | + |
| 211 | + test_standard_4_hours |
| 212 | + test_delayed_run |
| 213 | + test_just_minutes |
| 214 | + test_exact_hours |
| 215 | + test_past_meeting_time |
| 216 | + test_singular_hour |
| 217 | + test_singular_minute |
| 218 | + test_different_meeting_hour |
| 219 | + test_rounding_with_seconds |
| 220 | + test_seconds_just_past_boundary |
| 221 | + |
| 222 | + echo "" |
| 223 | + echo "==============================================" |
| 224 | + echo " Test Summary" |
| 225 | + echo "==============================================" |
| 226 | + echo "Total tests run: $TESTS_RUN" |
| 227 | + echo -e "${GREEN}Tests passed: $TESTS_PASSED${NC}" |
| 228 | + if [[ $TESTS_FAILED -gt 0 ]]; then |
| 229 | + echo -e "${RED}Tests failed: $TESTS_FAILED${NC}" |
| 230 | + exit 1 |
| 231 | + else |
| 232 | + echo -e "${GREEN}All tests passed!${NC}" |
| 233 | + exit 0 |
| 234 | + fi |
| 235 | +} |
| 236 | + |
| 237 | +main "$@" |
0 commit comments