3 # Basic deterministic tests for ai-unix tools
4 # Tests exit codes, option parsing, and basic I/O behavior
6 # Note: Not using set -e to prevent early exit on test failures
8 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9 TOOLS_DIR="$SCRIPT_DIR/../../"
10 TEST_DATA="$SCRIPT_DIR/../data"
12 # Source common constants
13 source "$TOOLS_DIR/ai-common"
21 local expected_code="$2"
26 if "${cmd[@]}" >/dev/null 2>&1; then
27 actual_code=$EXIT_SUCCESS
32 # Early return for success case
33 if [ "$actual_code" -eq "$expected_code" ]; then
34 echo "✓ $test_name (exit code $actual_code)"
40 echo "✗ $test_name - expected exit code $expected_code, got $actual_code"
50 output=$("${cmd[@]}" 2>/dev/null)
52 # Early return for success case
53 if [ -n "$output" ]; then
54 echo "✓ $test_name (has output)"
60 echo "✗ $test_name - expected output, got none"
64 echo "=== Basic Deterministic Tests ==="
68 echo "--- Help Option Tests ---"
69 test_exit_code "ai-grep help" $EXIT_SUCCESS "$TOOLS_DIR/ai-grep" -h
70 test_exit_code "ai-cut help" $EXIT_SUCCESS "$TOOLS_DIR/ai-cut" -h
71 test_exit_code "ai-class help" $EXIT_SUCCESS "$TOOLS_DIR/ai-class" -h
72 test_exit_code "ai-tr help" $EXIT_SUCCESS "$TOOLS_DIR/ai-tr" -h
73 test_exit_code "ai-test help" $EXIT_SUCCESS "$TOOLS_DIR/ai-test" -h
74 test_exit_code "ai-fix help" $EXIT_SUCCESS "$TOOLS_DIR/ai-fix" -h
76 # Test invalid options
77 echo "--- Invalid Option Tests ---"
78 test_exit_code "ai-grep invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-grep" -x
79 test_exit_code "ai-cut invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-cut" -x
80 test_exit_code "ai-class invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-class" -x
81 test_exit_code "ai-tr invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-tr" -x
82 test_exit_code "ai-test invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-test" -x
83 test_exit_code "ai-fix invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-fix" -x
85 # Test missing required arguments
86 echo "--- Missing Arguments Tests ---"
87 test_exit_code "ai-grep no pattern" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-grep"
88 test_exit_code "ai-cut no fields" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-cut"
89 test_exit_code "ai-class no categories" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-class"
90 test_exit_code "ai-tr no transformation" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-tr"
91 test_exit_code "ai-test no condition" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-test"
92 test_exit_code "ai-fix no prompt file" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-fix"
94 # Test missing -f flag for ai-cut
95 test_exit_code "ai-cut missing -f flag" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-cut" "name,email"
97 # Test non-existent files
98 echo "--- File Not Found Tests ---"
99 test_exit_code "ai-grep file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-grep" "pattern" /nonexistent/file.txt
100 test_exit_code "ai-cut file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-cut" -f "field" /nonexistent/file.txt
101 test_exit_code "ai-class file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-class" "cat1,cat2" /nonexistent/file.txt
102 test_exit_code "ai-tr file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-tr" "transform" /nonexistent/file.txt
103 test_exit_code "ai-test file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-test" "condition" /nonexistent/file.txt
104 test_exit_code "ai-fix prompt file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-fix" /nonexistent/prompt.txt
106 # Test empty file behavior
107 echo "--- Empty File Tests ---"
108 test_exit_code "ai-grep empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-grep" "pattern" "$TEST_DATA/empty.txt"
109 test_exit_code "ai-cut empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-cut" -f "field" "$TEST_DATA/empty.txt"
110 test_exit_code "ai-class empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-class" "cat1,cat2" "$TEST_DATA/empty.txt"
111 test_exit_code "ai-tr empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-tr" "transform" "$TEST_DATA/empty.txt"
112 test_exit_code "ai-test empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-test" "condition" "$TEST_DATA/empty.txt"
113 test_exit_code "ai-fix empty code file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-fix" "$TEST_DATA/simple-prompt.txt" "$TEST_DATA/empty.txt"
116 echo "=== Test Results ==="
117 echo "Passed: $PASSED"
118 echo "Failed: $FAILED"
120 if [ "$FAILED" -gt 0 ]; then
124 echo "All basic tests passed!"