]> begriffs open source - ai-unix/blob - tests/unit/test-basic.sh
ai-fix utility
[ai-unix] / tests / unit / test-basic.sh
1 #!/bin/bash
2
3 # Basic deterministic tests for ai-unix tools
4 # Tests exit codes, option parsing, and basic I/O behavior
5
6 # Note: Not using set -e to prevent early exit on test failures
7
8 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9 TOOLS_DIR="$SCRIPT_DIR/../../"
10 TEST_DATA="$SCRIPT_DIR/../data"
11
12 # Source common constants
13 source "$TOOLS_DIR/ai-common"
14
15 # Test utilities
16 PASSED=0
17 FAILED=0
18
19 test_exit_code() {
20     local test_name="$1"
21     local expected_code="$2"
22     shift 2
23     local cmd=("$@")
24     
25     local actual_code
26     if "${cmd[@]}" >/dev/null 2>&1; then
27         actual_code=$EXIT_SUCCESS
28     else
29         actual_code=$?
30     fi
31     
32     # Early return for success case
33     if [ "$actual_code" -eq "$expected_code" ]; then
34         echo "✓ $test_name (exit code $actual_code)"
35         ((PASSED++))
36         return 0
37     fi
38     
39     # Handle failure case
40     echo "✗ $test_name - expected exit code $expected_code, got $actual_code"
41     ((FAILED++))
42 }
43
44 test_has_output() {
45     local test_name="$1"
46     shift
47     local cmd=("$@")
48     
49     local output
50     output=$("${cmd[@]}" 2>/dev/null)
51     
52     # Early return for success case
53     if [ -n "$output" ]; then
54         echo "✓ $test_name (has output)"
55         ((PASSED++))
56         return 0
57     fi
58     
59     # Handle failure case
60     echo "✗ $test_name - expected output, got none"
61     ((FAILED++))
62 }
63
64 echo "=== Basic Deterministic Tests ==="
65 echo
66
67 # Test help options
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
75
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
84
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"
93
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"
96
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
105
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"
114
115 echo
116 echo "=== Test Results ==="
117 echo "Passed: $PASSED"
118 echo "Failed: $FAILED"
119
120 if [ "$FAILED" -gt 0 ]; then
121     exit 1
122 fi
123
124 echo "All basic tests passed!"