|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Test suite for cron-calls dynamic time computation |
| 5 | +# Validates the Python time-until-meeting logic used in |
| 6 | +# cron-calls-community.sh and cron-calls-office-hours.sh |
| 7 | + |
| 8 | +# Color codes for output |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +NC='\033[0m' |
| 13 | + |
| 14 | +# Test counters |
| 15 | +TESTS_RUN=0 |
| 16 | +TESTS_PASSED=0 |
| 17 | +TESTS_FAILED=0 |
| 18 | + |
| 19 | +print_result() { |
| 20 | + local test_name="$1" |
| 21 | + local result="$2" |
| 22 | + local message="${3:-}" |
| 23 | + |
| 24 | + TESTS_RUN=$((TESTS_RUN + 1)) |
| 25 | + |
| 26 | + if [[ "$result" == "PASS" ]]; then |
| 27 | + TESTS_PASSED=$((TESTS_PASSED + 1)) |
| 28 | + echo -e "${GREEN}✓ PASS${NC}: $test_name" |
| 29 | + else |
| 30 | + TESTS_FAILED=$((TESTS_FAILED + 1)) |
| 31 | + echo -e "${RED}✗ FAIL${NC}: $test_name" |
| 32 | + if [[ -n "$message" ]]; then |
| 33 | + echo -e " ${YELLOW}→${NC} $message" |
| 34 | + fi |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +# Computes time-until-meeting using the same logic as the bot scripts, |
| 39 | +# but with a mocked "now" time for deterministic testing. |
| 40 | +# Arguments: |
| 41 | +# $1 - MEETING_HOUR (integer, 0-23) |
| 42 | +# $2 - Mock current time in "HH:MM" format (UTC) |
| 43 | +compute_time_until() { |
| 44 | + local meeting_hour="$1" |
| 45 | + local mock_time="$2" |
| 46 | + |
| 47 | + python3 - <<EOF |
| 48 | +from datetime import datetime, timezone |
| 49 | +
|
| 50 | +mock_hour, mock_minute = map(int, "$mock_time".split(":")) |
| 51 | +now = datetime.now(timezone.utc).replace(hour=mock_hour, minute=mock_minute, second=0, microsecond=0) |
| 52 | +meeting = now.replace(hour=$meeting_hour, minute=0, second=0, microsecond=0) |
| 53 | +diff = meeting - now |
| 54 | +total_minutes = max(int(diff.total_seconds()) // 60, 0) |
| 55 | +hours = total_minutes // 60 |
| 56 | +minutes = total_minutes % 60 |
| 57 | +if hours > 0 and minutes > 0: |
| 58 | + print(f"{hours} hour{'s' if hours != 1 else ''} and {minutes} minute{'s' if minutes != 1 else ''}") |
| 59 | +elif hours > 0: |
| 60 | + print(f"{hours} hour{'s' if hours != 1 else ''}") |
| 61 | +else: |
| 62 | + print(f"{minutes} minute{'s' if minutes != 1 else ''}") |
| 63 | +EOF |
| 64 | +} |
| 65 | + |
| 66 | +# Test 1: Standard case — 4 hours before meeting |
| 67 | +test_standard_4_hours() { |
| 68 | + echo "" |
| 69 | + echo "Test 1: Standard case — 4 hours before meeting (10:00 → 14:00)" |
| 70 | + echo "================================================================" |
| 71 | + |
| 72 | + local result |
| 73 | + result=$(compute_time_until 14 "10:00") |
| 74 | + |
| 75 | + if [[ "$result" == "4 hours" ]]; then |
| 76 | + print_result "4 hours before meeting" "PASS" |
| 77 | + else |
| 78 | + print_result "4 hours before meeting" "FAIL" "Expected '4 hours', got '$result'" |
| 79 | + fi |
| 80 | +} |
| 81 | + |
| 82 | +# Test 2: Delayed run — 3 hours and 7 minutes remaining |
| 83 | +test_delayed_run() { |
| 84 | + echo "" |
| 85 | + echo "Test 2: Delayed run — 3 hours 7 minutes remaining (10:53 → 14:00)" |
| 86 | + echo "===================================================================" |
| 87 | + |
| 88 | + local result |
| 89 | + result=$(compute_time_until 14 "10:53") |
| 90 | + |
| 91 | + if [[ "$result" == "3 hours and 7 minutes" ]]; then |
| 92 | + print_result "Delayed run shows 3 hours and 7 minutes" "PASS" |
| 93 | + else |
| 94 | + print_result "Delayed run shows 3 hours and 7 minutes" "FAIL" "Expected '3 hours and 7 minutes', got '$result'" |
| 95 | + fi |
| 96 | +} |
| 97 | + |
| 98 | +# Test 3: Just minutes remaining |
| 99 | +test_just_minutes() { |
| 100 | + echo "" |
| 101 | + echo "Test 3: Just minutes remaining (13:45 → 14:00)" |
| 102 | + echo "================================================" |
| 103 | + |
| 104 | + local result |
| 105 | + result=$(compute_time_until 14 "13:45") |
| 106 | + |
| 107 | + if [[ "$result" == "15 minutes" ]]; then |
| 108 | + print_result "15 minutes remaining" "PASS" |
| 109 | + else |
| 110 | + print_result "15 minutes remaining" "FAIL" "Expected '15 minutes', got '$result'" |
| 111 | + fi |
| 112 | +} |
| 113 | + |
| 114 | +# Test 4: Exact whole hours |
| 115 | +test_exact_hours() { |
| 116 | + echo "" |
| 117 | + echo "Test 4: Exact whole hours (12:00 → 14:00)" |
| 118 | + echo "===========================================" |
| 119 | + |
| 120 | + local result |
| 121 | + result=$(compute_time_until 14 "12:00") |
| 122 | + |
| 123 | + if [[ "$result" == "2 hours" ]]; then |
| 124 | + print_result "Exactly 2 hours remaining" "PASS" |
| 125 | + else |
| 126 | + print_result "Exactly 2 hours remaining" "FAIL" "Expected '2 hours', got '$result'" |
| 127 | + fi |
| 128 | +} |
| 129 | + |
| 130 | +# Test 5: Already past meeting time — clamped to 0 |
| 131 | +test_past_meeting_time() { |
| 132 | + echo "" |
| 133 | + echo "Test 5: Already past meeting time (15:00 → 14:00)" |
| 134 | + echo "===================================================" |
| 135 | + |
| 136 | + local result |
| 137 | + result=$(compute_time_until 14 "15:00") |
| 138 | + |
| 139 | + if [[ "$result" == "0 minutes" ]]; then |
| 140 | + print_result "Past meeting time shows 0 minutes" "PASS" |
| 141 | + else |
| 142 | + print_result "Past meeting time shows 0 minutes" "FAIL" "Expected '0 minutes', got '$result'" |
| 143 | + fi |
| 144 | +} |
| 145 | + |
| 146 | +# Test 6: Singular hour — 1 hour remaining |
| 147 | +test_singular_hour() { |
| 148 | + echo "" |
| 149 | + echo "Test 6: Singular hour (13:00 → 14:00)" |
| 150 | + echo "=======================================" |
| 151 | + |
| 152 | + local result |
| 153 | + result=$(compute_time_until 14 "13:00") |
| 154 | + |
| 155 | + if [[ "$result" == "1 hour" ]]; then |
| 156 | + print_result "Singular hour formatting" "PASS" |
| 157 | + else |
| 158 | + print_result "Singular hour formatting" "FAIL" "Expected '1 hour', got '$result'" |
| 159 | + fi |
| 160 | +} |
| 161 | + |
| 162 | +# Test 7: Singular minute — 1 hour and 1 minute |
| 163 | +test_singular_minute() { |
| 164 | + echo "" |
| 165 | + echo "Test 7: Singular minute (12:59 → 14:00)" |
| 166 | + echo "=========================================" |
| 167 | + |
| 168 | + local result |
| 169 | + result=$(compute_time_until 14 "12:59") |
| 170 | + |
| 171 | + if [[ "$result" == "1 hour and 1 minute" ]]; then |
| 172 | + print_result "Singular hour and minute formatting" "PASS" |
| 173 | + else |
| 174 | + print_result "Singular hour and minute formatting" "FAIL" "Expected '1 hour and 1 minute', got '$result'" |
| 175 | + fi |
| 176 | +} |
| 177 | + |
| 178 | +# Test 8: Different meeting hour |
| 179 | +test_different_meeting_hour() { |
| 180 | + echo "" |
| 181 | + echo "Test 8: Different meeting hour (08:30 → 12:00)" |
| 182 | + echo "================================================" |
| 183 | + |
| 184 | + local result |
| 185 | + result=$(compute_time_until 12 "08:30") |
| 186 | + |
| 187 | + if [[ "$result" == "3 hours and 30 minutes" ]]; then |
| 188 | + print_result "Different meeting hour works" "PASS" |
| 189 | + else |
| 190 | + print_result "Different meeting hour works" "FAIL" "Expected '3 hours and 30 minutes', got '$result'" |
| 191 | + fi |
| 192 | +} |
| 193 | + |
| 194 | +main() { |
| 195 | + echo "==============================================" |
| 196 | + echo " Cron Calls Time Computation - Test Suite" |
| 197 | + echo "==============================================" |
| 198 | + |
| 199 | + test_standard_4_hours |
| 200 | + test_delayed_run |
| 201 | + test_just_minutes |
| 202 | + test_exact_hours |
| 203 | + test_past_meeting_time |
| 204 | + test_singular_hour |
| 205 | + test_singular_minute |
| 206 | + test_different_meeting_hour |
| 207 | + |
| 208 | + echo "" |
| 209 | + echo "==============================================" |
| 210 | + echo " Test Summary" |
| 211 | + echo "==============================================" |
| 212 | + echo "Total tests run: $TESTS_RUN" |
| 213 | + echo -e "${GREEN}Tests passed: $TESTS_PASSED${NC}" |
| 214 | + if [[ $TESTS_FAILED -gt 0 ]]; then |
| 215 | + echo -e "${RED}Tests failed: $TESTS_FAILED${NC}" |
| 216 | + exit 1 |
| 217 | + else |
| 218 | + echo -e "${GREEN}All tests passed!${NC}" |
| 219 | + exit 0 |
| 220 | + fi |
| 221 | +} |
| 222 | + |
| 223 | +main "$@" |
0 commit comments