3 # Format validation tests for ai-unix tools
4 # Tests that output formats are correct (JSON, TSV, line numbers)
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"
19 validate_json_line() {
21 [ -n "$line" ] && echo "$line" | python3 -m json.tool >/dev/null 2>&1
24 validate_all_json_lines() {
28 while IFS= read -r line; do
29 validate_json_line "$line" || return 1
41 output=$("${cmd[@]}" 2>/dev/null) || {
42 echo "✗ $test_name - command failed to execute"
47 # Early return for valid JSON
48 if validate_all_json_lines "$output"; then
49 echo "✓ $test_name (valid JSON format)"
55 echo "✗ $test_name - output is not valid JSON"
56 echo "Output was: $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)"
72 echo "✗ $test_name - output does not contain tab separators"
73 echo "Output was: $output"
77 echo "✗ $test_name - command failed to execute"
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)"
94 echo "✗ $test_name - output does not contain line numbers"
95 echo "Output was: $output"
99 echo "✗ $test_name - command failed to execute"
104 test_count_format() {
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)"
116 echo "✗ $test_name - output is not a single number"
117 echo "Output was: $output"
121 echo "✗ $test_name - command failed to execute"
126 validate_category_line() {
128 [ -n "$line" ] && echo "$line" | grep -q '^[^:]\+:'
131 validate_all_category_lines() {
135 while IFS= read -r line; do
136 validate_category_line "$line" || return 1
142 test_category_prefix() {
148 output=$("${cmd[@]}" 2>/dev/null) || {
149 echo "✗ $test_name - command failed to execute"
154 # Early return for valid category format
155 if validate_all_category_lines "$output"; then
156 echo "✓ $test_name (lines have category prefix)"
161 # Handle invalid format
162 echo "✗ $test_name - lines do not have category: prefix format"
163 echo "Output was: $output"
173 output=$("${cmd[@]}" 2>/dev/null)
175 # Early return for success case
176 if [ -n "$output" ]; then
177 echo "✓ $test_name (has output)"
182 # Handle failure case
183 echo "✗ $test_name - expected output, got none"
187 echo "=== Format Validation Tests ==="
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"
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"
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"
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"
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"
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"
215 echo "=== Test Results ==="
216 echo "Passed: $PASSED"
217 echo "Failed: $FAILED"
219 if [ "$FAILED" -gt 0 ]; then
223 echo "All format tests passed!"