#!/bin/bash # Semantic tests using ai-test to validate AI tool outputs # Uses ai-test itself to check if outputs meet semantic expectations # Note: Not using set -e to prevent early exit on test failures SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" TOOLS_DIR="$SCRIPT_DIR/../../" TEST_DATA="$SCRIPT_DIR/../data" # Source common constants source "$TOOLS_DIR/ai-common" # Test utilities PASSED=0 FAILED=0 execute_test_command() { local cmd=("$@") "${cmd[@]}" 2>/dev/null } validate_semantic_condition() { local output="$1" local condition="$2" echo "$output" | "$TOOLS_DIR/ai-test" -q "$condition" } report_test_success() { local test_name="$1" echo "✓ $test_name" ((PASSED++)) } report_test_failure() { local test_name="$1" local reason="$2" local output="$3" echo "✗ $test_name - $reason" [ -n "$output" ] && echo "Output was: $output" ((FAILED++)) } test_semantic_property() { local test_name="$1" local condition="$2" shift 2 local cmd=("$@") local output output=$(execute_test_command "${cmd[@]}") || { report_test_failure "$test_name" "command failed to execute" return 1 } # Early return for successful validation if validate_semantic_condition "$output" "$condition"; then report_test_success "$test_name" return 0 fi # Handle validation failure report_test_failure "$test_name" "semantic condition not met: $condition" "$output" } echo "=== Semantic Property Tests ===" echo # Test ai-grep semantic matching echo "--- ai-grep Semantic Tests ---" test_semantic_property \ "ai-grep finds error-related content" \ "contains information about errors or problems" \ "$TOOLS_DIR/ai-grep" "problems" "$TEST_DATA/sample-log.txt" test_semantic_property \ "ai-grep -v excludes error content" \ "does not contain information about errors or problems" \ "$TOOLS_DIR/ai-grep" -v "problems" "$TEST_DATA/sample-log.txt" # Test ai-cut extraction quality echo "--- ai-cut Semantic Tests ---" test_semantic_property \ "ai-cut extracts contact information" \ "contains names and email addresses" \ "$TOOLS_DIR/ai-cut" -f "name,email" "$TEST_DATA/contacts.txt" test_semantic_property \ "ai-cut JSON contains proper fields" \ "contains JSON objects with name and email fields" \ "$TOOLS_DIR/ai-cut" -j -f "name,email" "$TEST_DATA/contacts.txt" # Test ai-class categorization accuracy echo "--- ai-class Semantic Tests ---" test_semantic_property \ "ai-class categorizes feedback correctly" \ "positive feedback is labeled as positive, negative as negative" \ "$TOOLS_DIR/ai-class" "positive,negative,neutral" "$TEST_DATA/feedback.txt" # Test ai-tr transformation preservation echo "--- ai-tr Semantic Tests ---" test_semantic_property \ "ai-tr preserves meaning in transformation" \ "contains the same essential information as the original but in a different format" \ "$TOOLS_DIR/ai-tr" "make more formal" "$TEST_DATA/feedback.txt" # Test ai-test validation logic echo "--- ai-test Semantic Tests ---" # Create a temporary file with valid content echo "john.doe@example.com" > /tmp/valid_emails.txt echo "jane.smith@company.org" >> /tmp/valid_emails.txt if "$TOOLS_DIR/ai-test" -q "contains valid email addresses" /tmp/valid_emails.txt; then echo "✓ ai-test correctly validates valid emails" ((PASSED++)) else echo "✗ ai-test failed to validate valid emails" ((FAILED++)) fi # Test with invalid content echo "not-an-email" > /tmp/invalid_emails.txt echo "also-not-valid" >> /tmp/invalid_emails.txt if ! "$TOOLS_DIR/ai-test" -q "contains valid email addresses" /tmp/invalid_emails.txt; then echo "✓ ai-test correctly rejects invalid emails" ((PASSED++)) else echo "✗ ai-test incorrectly accepted invalid emails" ((FAILED++)) fi # Test ai-test invert flag if "$TOOLS_DIR/ai-test" -v -q "contains valid email addresses" /tmp/invalid_emails.txt; then echo "✓ ai-test -v correctly inverts condition" ((PASSED++)) else echo "✗ ai-test -v failed to invert condition" ((FAILED++)) fi # Cleanup rm -f /tmp/valid_emails.txt /tmp/invalid_emails.txt # Test ai-fix semantic functionality echo "--- ai-fix Semantic Tests ---" test_semantic_property \ "ai-fix review identifies bugs" \ "identifies issues like division by zero, undefined variables, or missing return statements" \ "$TOOLS_DIR/ai-fix" -r "$TEST_DATA/code-review-prompt.txt" "$TEST_DATA/buggy-code.py" test_semantic_property \ "ai-fix follows prompt format instructions" \ "starts with 'ISSUES FOUND:' and contains 'SEVERITY:' and 'RECOMMENDATION:' sections" \ "$TOOLS_DIR/ai-fix" -r "$TEST_DATA/structured-review-prompt.txt" "$TEST_DATA/buggy-code.py" test_semantic_property \ "ai-fix fixes preserve functionality" \ "contains Python code that appears to fix the original bugs while maintaining the same functionality" \ "$TOOLS_DIR/ai-fix" "$TEST_DATA/code-review-prompt.txt" "$TEST_DATA/buggy-code.py" # Test pipeline behavior echo "--- Pipeline Semantic Tests ---" pipeline_output=$(echo "I love this product! It's amazing and works perfectly." | "$TOOLS_DIR/ai-class" "positive,negative,neutral" | "$TOOLS_DIR/ai-grep" "positive") if echo "$pipeline_output" | "$TOOLS_DIR/ai-test" -q "contains positive sentiment"; then echo "✓ Pipeline preserves semantic meaning through multiple tools" ((PASSED++)) else echo "✗ Pipeline lost semantic meaning" echo "Pipeline output was: $pipeline_output" ((FAILED++)) fi echo echo "=== Test Results ===" echo "Passed: $PASSED" echo "Failed: $FAILED" if [ "$FAILED" -gt 0 ]; then exit 1 fi echo "All semantic tests passed!"