]> begriffs open source - ai-unix/blob - tests/unit/test-formats.sh
Simpler prompt for fixer
[ai-unix] / tests / unit / test-formats.sh
1 #!/bin/bash
2
3 # Format validation tests for ai-unix tools
4 # Tests that output formats are correct (JSON, TSV, line numbers)
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 validate_json_line() {
20     local line="$1"
21     [ -n "$line" ] && echo "$line" | python3 -m json.tool >/dev/null 2>&1
22 }
23
24 validate_all_json_lines() {
25     local output="$1"
26     local line
27     
28     while IFS= read -r line; do
29         validate_json_line "$line" || return 1
30     done <<< "$output"
31     
32     return 0
33 }
34
35 test_json_format() {
36     local test_name="$1"
37     shift
38     local cmd=("$@")
39     
40     local output
41     output=$("${cmd[@]}" 2>/dev/null) || {
42         echo "✗ $test_name - command failed to execute"
43         ((FAILED++))
44         return 1
45     }
46     
47     # Early return for valid JSON
48     if validate_all_json_lines "$output"; then
49         echo "✓ $test_name (valid JSON format)"
50         ((PASSED++))
51         return 0
52     fi
53     
54     # Handle invalid JSON
55     echo "✗ $test_name - output is not valid JSON"
56     echo "Output was: $output"
57     ((FAILED++))
58 }
59
60 test_tsv_format() {
61     local test_name="$1"
62     shift
63     local cmd=("$@")
64     
65     local output
66     if output=$("${cmd[@]}" 2>/dev/null); then
67         # Check if output contains tab characters
68         if echo "$output" | grep -q $'\t'; then
69             echo "✓ $test_name (contains tabs for TSV)"
70             ((PASSED++))
71         else
72             echo "✗ $test_name - output does not contain tab separators"
73             echo "Output was: $output"
74             ((FAILED++))
75         fi
76     else
77         echo "✗ $test_name - command failed to execute"
78         ((FAILED++))
79     fi
80 }
81
82 test_line_numbers() {
83     local test_name="$1"
84     shift
85     local cmd=("$@")
86     
87     local output
88     if output=$("${cmd[@]}" 2>/dev/null); then
89         # Check if output contains line numbers (format: number:)
90         if echo "$output" | grep -q '^[0-9]\+:'; then
91             echo "✓ $test_name (contains line numbers)"
92             ((PASSED++))
93         else
94             echo "✗ $test_name - output does not contain line numbers"
95             echo "Output was: $output"
96             ((FAILED++))
97         fi
98     else
99         echo "✗ $test_name - command failed to execute"
100         ((FAILED++))
101     fi
102 }
103
104 test_count_format() {
105     local test_name="$1"
106     shift
107     local cmd=("$@")
108     
109     local output
110     if output=$("${cmd[@]}" 2>/dev/null); then
111         # Check if output is a single number
112         if echo "$output" | grep -qE '^[0-9]+$'; then
113             echo "✓ $test_name (single number format)"
114             ((PASSED++))
115         else
116             echo "✗ $test_name - output is not a single number"
117             echo "Output was: $output"
118             ((FAILED++))
119         fi
120     else
121         echo "✗ $test_name - command failed to execute"
122         ((FAILED++))
123     fi
124 }
125
126 validate_category_line() {
127     local line="$1"
128     [ -n "$line" ] && echo "$line" | grep -q '^[^:]\+:'
129 }
130
131 validate_all_category_lines() {
132     local output="$1"
133     local line
134     
135     while IFS= read -r line; do
136         validate_category_line "$line" || return 1
137     done <<< "$output"
138     
139     return 0
140 }
141
142 test_category_prefix() {
143     local test_name="$1"
144     shift
145     local cmd=("$@")
146     
147     local output
148     output=$("${cmd[@]}" 2>/dev/null) || {
149         echo "✗ $test_name - command failed to execute"
150         ((FAILED++))
151         return 1
152     }
153     
154     # Early return for valid category format
155     if validate_all_category_lines "$output"; then
156         echo "✓ $test_name (lines have category prefix)"
157         ((PASSED++))
158         return 0
159     fi
160     
161     # Handle invalid format
162     echo "✗ $test_name - lines do not have category: prefix format"
163     echo "Output was: $output"
164     ((FAILED++))
165 }
166
167 test_has_output() {
168     local test_name="$1"
169     shift
170     local cmd=("$@")
171     
172     local output
173     output=$("${cmd[@]}" 2>/dev/null)
174     
175     # Early return for success case
176     if [ -n "$output" ]; then
177         echo "✓ $test_name (has output)"
178         ((PASSED++))
179         return 0
180     fi
181     
182     # Handle failure case
183     echo "✗ $test_name - expected output, got none"
184     ((FAILED++))
185 }
186
187 echo "=== Format Validation Tests ==="
188 echo
189
190 # Test ai-cut JSON format
191 echo "--- ai-cut JSON Format Tests ---"
192 test_json_format "ai-cut JSON output" "$TOOLS_DIR/ai-cut" -j -f "name,email" "$TEST_DATA/contacts.txt"
193
194 # Test ai-cut TSV format (default)
195 echo "--- ai-cut TSV Format Tests ---"
196 test_tsv_format "ai-cut TSV output" "$TOOLS_DIR/ai-cut" -f "name,email" "$TEST_DATA/contacts.txt"
197
198 # Test ai-grep line numbers
199 echo "--- ai-grep Line Number Tests ---"
200 test_line_numbers "ai-grep line numbers" "$TOOLS_DIR/ai-grep" -n "error" "$TEST_DATA/sample-log.txt"
201
202 # Test ai-grep count format
203 echo "--- ai-grep Count Format Tests ---"
204 test_count_format "ai-grep count output" "$TOOLS_DIR/ai-grep" -c "error" "$TEST_DATA/sample-log.txt"
205
206 # Test ai-class category prefix format
207 echo "--- ai-class Category Prefix Tests ---"
208 test_category_prefix "ai-class category prefix" "$TOOLS_DIR/ai-class" "positive,negative,neutral" "$TEST_DATA/feedback.txt"
209
210 # Test ai-fix review-only format (should produce text output)
211 echo "--- ai-fix Review Format Tests ---"
212 test_has_output "ai-fix review output" "$TOOLS_DIR/ai-fix" -r "$TEST_DATA/simple-prompt.txt" "$TEST_DATA/contacts.txt"
213
214 echo
215 echo "=== Test Results ==="
216 echo "Passed: $PASSED"
217 echo "Failed: $FAILED"
218
219 if [ "$FAILED" -gt 0 ]; then
220     exit 1
221 fi
222
223 echo "All format tests passed!"