#!/bin/bash # Basic deterministic tests for ai-unix tools # Tests exit codes, option parsing, and basic I/O behavior # 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 test_exit_code() { local test_name="$1" local expected_code="$2" shift 2 local cmd=("$@") local actual_code if "${cmd[@]}" >/dev/null 2>&1; then actual_code=$EXIT_SUCCESS else actual_code=$? fi # Early return for success case if [ "$actual_code" -eq "$expected_code" ]; then echo "✓ $test_name (exit code $actual_code)" ((PASSED++)) return 0 fi # Handle failure case echo "✗ $test_name - expected exit code $expected_code, got $actual_code" ((FAILED++)) } test_has_output() { local test_name="$1" shift local cmd=("$@") local output output=$("${cmd[@]}" 2>/dev/null) # Early return for success case if [ -n "$output" ]; then echo "✓ $test_name (has output)" ((PASSED++)) return 0 fi # Handle failure case echo "✗ $test_name - expected output, got none" ((FAILED++)) } echo "=== Basic Deterministic Tests ===" echo # Test help options echo "--- Help Option Tests ---" test_exit_code "ai-grep help" $EXIT_SUCCESS "$TOOLS_DIR/ai-grep" -h test_exit_code "ai-cut help" $EXIT_SUCCESS "$TOOLS_DIR/ai-cut" -h test_exit_code "ai-class help" $EXIT_SUCCESS "$TOOLS_DIR/ai-class" -h test_exit_code "ai-tr help" $EXIT_SUCCESS "$TOOLS_DIR/ai-tr" -h test_exit_code "ai-test help" $EXIT_SUCCESS "$TOOLS_DIR/ai-test" -h test_exit_code "ai-fix help" $EXIT_SUCCESS "$TOOLS_DIR/ai-fix" -h # Test invalid options echo "--- Invalid Option Tests ---" test_exit_code "ai-grep invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-grep" -x test_exit_code "ai-cut invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-cut" -x test_exit_code "ai-class invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-class" -x test_exit_code "ai-tr invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-tr" -x test_exit_code "ai-test invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-test" -x test_exit_code "ai-fix invalid option" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-fix" -x # Test missing required arguments echo "--- Missing Arguments Tests ---" test_exit_code "ai-grep no pattern" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-grep" test_exit_code "ai-cut no fields" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-cut" test_exit_code "ai-class no categories" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-class" test_exit_code "ai-tr no transformation" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-tr" test_exit_code "ai-test no condition" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-test" test_exit_code "ai-fix no prompt file" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-fix" # Test missing -f flag for ai-cut test_exit_code "ai-cut missing -f flag" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-cut" "name,email" # Test non-existent files echo "--- File Not Found Tests ---" test_exit_code "ai-grep file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-grep" "pattern" /nonexistent/file.txt test_exit_code "ai-cut file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-cut" -f "field" /nonexistent/file.txt test_exit_code "ai-class file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-class" "cat1,cat2" /nonexistent/file.txt test_exit_code "ai-tr file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-tr" "transform" /nonexistent/file.txt test_exit_code "ai-test file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-test" "condition" /nonexistent/file.txt test_exit_code "ai-fix prompt file not found" $EXIT_USAGE_ERROR "$TOOLS_DIR/ai-fix" /nonexistent/prompt.txt # Test empty file behavior echo "--- Empty File Tests ---" test_exit_code "ai-grep empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-grep" "pattern" "$TEST_DATA/empty.txt" test_exit_code "ai-cut empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-cut" -f "field" "$TEST_DATA/empty.txt" test_exit_code "ai-class empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-class" "cat1,cat2" "$TEST_DATA/empty.txt" test_exit_code "ai-tr empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-tr" "transform" "$TEST_DATA/empty.txt" test_exit_code "ai-test empty file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-test" "condition" "$TEST_DATA/empty.txt" test_exit_code "ai-fix empty code file" $EXIT_NO_MATCH "$TOOLS_DIR/ai-fix" "$TEST_DATA/simple-prompt.txt" "$TEST_DATA/empty.txt" echo echo "=== Test Results ===" echo "Passed: $PASSED" echo "Failed: $FAILED" if [ "$FAILED" -gt 0 ]; then exit 1 fi echo "All basic tests passed!"