]> begriffs open source - ai-unix/blob - tests/run-tests.sh
Simpler prompt for fixer
[ai-unix] / tests / run-tests.sh
1 #!/bin/bash
2
3 # Main test runner for ai-unix tools
4 # Runs all test suites and provides comprehensive test results
5
6 set -e
7
8 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9 TOOLS_DIR="$SCRIPT_DIR/../"
10
11 # Source common constants
12 source "$TOOLS_DIR/ai-common"
13
14 # Colors for output
15 RED='\033[0;31m'
16 GREEN='\033[0;32m'
17 YELLOW='\033[1;33m'
18 NC='\033[0m' # No Color
19
20 # Test results tracking
21 TOTAL_FAILED=0
22 SUITE_RESULTS=()
23
24 run_test_suite() {
25     local suite_name="$1"
26     local test_script="$2"
27     
28     echo -e "${YELLOW}Running $suite_name...${NC}"
29     echo "========================================="
30     
31     if "$test_script"; then
32         echo -e "${GREEN}$suite_name PASSED${NC}"
33         SUITE_RESULTS+=("✓ $suite_name")
34         echo
35     else
36         echo -e "${RED}$suite_name FAILED${NC}"
37         SUITE_RESULTS+=("✗ $suite_name")
38         ((TOTAL_FAILED++))
39         echo
40         return 1
41     fi
42 }
43
44 show_usage() {
45     echo "Usage: $0 [OPTIONS]"
46     echo "Run test suites for ai-unix tools"
47     echo ""
48     echo "Options:"
49     echo "  --basic       Run only basic deterministic tests"
50     echo "  --format      Run only format validation tests"
51     echo "  --semantic    Run only semantic tests (requires Claude access)"
52     echo "  --no-semantic Skip semantic tests (run basic + format only)"
53     echo "  --help        Show this help message"
54     echo ""
55     echo "By default, runs all test suites."
56 }
57
58 # Check if tools exist
59 check_prerequisites() {
60     local missing_tools=()
61     
62     for tool in ai-grep ai-cut ai-class ai-tr ai-test; do
63         if [ ! -x "$TOOLS_DIR/$tool" ]; then
64             missing_tools+=("$tool")
65         fi
66     done
67     
68     if [ ${#missing_tools[@]} -gt 0 ]; then
69         echo -e "${RED}Missing tools: ${missing_tools[*]}${NC}"
70         echo "Please ensure all ai-unix tools are built and executable."
71         exit $EXIT_USAGE_ERROR
72     fi
73     
74     # Check if python3 is available for JSON validation
75     if ! command -v python3 >/dev/null 2>&1; then
76         echo -e "${YELLOW}Warning: python3 not found. JSON format tests may fail.${NC}"
77     fi
78     
79     # Check if claude is available for semantic tests
80     if ! command -v claude >/dev/null 2>&1; then
81         echo -e "${YELLOW}Warning: claude command not found. Semantic tests will be skipped.${NC}"
82         SKIP_SEMANTIC=true
83     fi
84 }
85
86 # Option configuration lookup
87 declare -A option_configs=(
88     ["--basic"]="true false false"
89     ["--format"]="false true false"
90     ["--semantic"]="false false true"
91 )
92
93 set_test_config() {
94     local config="$1"
95     read -r RUN_BASIC RUN_FORMAT RUN_SEMANTIC <<< "$config"
96 }
97
98 handle_option() {
99     local option="$1"
100     
101     case "$option" in
102         --no-semantic)
103             RUN_SEMANTIC=false
104             return 0
105             ;;
106         --help)
107             show_usage
108             exit $EXIT_SUCCESS
109             ;;
110         --basic|--format|--semantic)
111             set_test_config "${option_configs[$option]}"
112             return 0
113             ;;
114         *)
115             echo "Unknown option: $option"
116             show_usage
117             exit $EXIT_USAGE_ERROR
118             ;;
119     esac
120 }
121
122 # Parse command line options
123 RUN_BASIC=true
124 RUN_FORMAT=true  
125 RUN_SEMANTIC=true
126 SKIP_SEMANTIC=false
127
128 while [[ $# -gt 0 ]]; do
129     handle_option "$1"
130     shift
131 done
132
133 echo "AI-Unix Tools Test Suite"
134 echo "========================"
135 echo
136
137 # Check prerequisites
138 check_prerequisites
139
140 # Run test suites based on options
141 if [ "$RUN_BASIC" = true ]; then
142     run_test_suite "Basic Tests" "$SCRIPT_DIR/unit/test-basic.sh" || true
143 fi
144
145 if [ "$RUN_FORMAT" = true ]; then
146     run_test_suite "Format Tests" "$SCRIPT_DIR/unit/test-formats.sh" || true
147 fi
148
149 if [ "$RUN_SEMANTIC" = true ] && [ "$SKIP_SEMANTIC" = false ]; then
150     run_test_suite "Semantic Tests" "$SCRIPT_DIR/integration/test-semantic.sh" || true
151 elif [ "$RUN_SEMANTIC" = true ] && [ "$SKIP_SEMANTIC" = true ]; then
152     echo -e "${YELLOW}Skipping semantic tests (claude command not available)${NC}"
153     SUITE_RESULTS+=("⚠ Semantic Tests (skipped)")
154 fi
155
156 # Final results
157 echo "========================================="
158 echo -e "${YELLOW}TEST SUITE SUMMARY${NC}"
159 echo "========================================="
160
161 for result in "${SUITE_RESULTS[@]}"; do
162     if [[ "$result" == ✓* ]]; then
163         echo -e "${GREEN}$result${NC}"
164     elif [[ "$result" == ✗* ]]; then
165         echo -e "${RED}$result${NC}"
166     else
167         echo -e "${YELLOW}$result${NC}"
168     fi
169 done
170
171 echo
172 if [ "$TOTAL_FAILED" -eq 0 ]; then
173     echo -e "${GREEN}All test suites completed successfully!${NC}"
174     exit $EXIT_SUCCESS
175 else
176     echo -e "${RED}$TOTAL_FAILED test suite(s) failed.${NC}"
177     exit 1
178 fi