about summary refs log tree commit diff stats
path: root/bash/talk-to-computer/test_framework.sh
blob: c74ad56132c8e5d97a703dd8b5fc3aa7731549ea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/bin/bash

# Comprehensive Test Framework for AI Thinking Mechanisms
# This script provides automated testing capabilities for all system components.

# Source common functionality
source "$(dirname "${BASH_SOURCE[0]}")/common.sh"
source "$(dirname "${BASH_SOURCE[0]}")/config.sh"

# --- Test Configuration ---

# Test directories
TEST_DIR="${LOG_DIR}/tests"
RESULTS_DIR="${TEST_DIR}/results"
COVERAGE_DIR="${TEST_DIR}/coverage"

# Test counters
TESTS_PASSED=0
TESTS_FAILED=0
TESTS_SKIPPED=0

# --- Test Utilities ---

# Initialize test framework
init_test_framework() {
    mkdir -p "$RESULTS_DIR" "$COVERAGE_DIR"
    echo "🧪 AI Thinking Mechanisms Test Framework"
    echo "========================================"
    echo
}

# Test result functions
test_pass() {
    local test_name="$1"
    echo "✅ PASS: $test_name"
    ((TESTS_PASSED++))
}

test_fail() {
    local test_name="$1"
    local reason="$2"
    echo "❌ FAIL: $test_name - $reason"
    ((TESTS_FAILED++))
}

test_skip() {
    local test_name="$1"
    local reason="$2"
    echo "⏭️  SKIP: $test_name - $reason"
    ((TESTS_SKIPPED++))
}

# Assert functions
assert_equals() {
    local expected="$1"
    local actual="$2"
    local test_name="$3"

    if [ "$expected" = "$actual" ]; then
        test_pass "$test_name"
    else
        test_fail "$test_name" "Expected '$expected', got '$actual'"
    fi
}

assert_not_empty() {
    local value="$1"
    local test_name="$2"

    if [ -n "$value" ]; then
        test_pass "$test_name"
    else
        test_fail "$test_name" "Value is empty"
    fi
}

assert_file_exists() {
    local file_path="$1"
    local test_name="$2"

    if [ -f "$file_path" ]; then
        test_pass "$test_name"
    else
        test_fail "$test_name" "File does not exist: $file_path"
    fi
}

# --- Component Tests ---

test_common_functions() {
    echo "Testing Common Functions..."

    # Test script directory detection
    local script_dir
    script_dir=$(get_script_dir)
    assert_not_empty "$script_dir" "get_script_dir"

    # Test model validation (if ollama is available)
    if command_exists ollama; then
        local result
        result=$(validate_model "gemma3n:e2b" "gemma3n:e2b")
        if [ $? -eq 0 ]; then
            test_pass "validate_model_success"
        else
            test_skip "validate_model_success" "Model not available"
        fi
    else
        test_skip "validate_model_success" "Ollama not available"
    fi
}

test_config_loading() {
    echo "Testing Configuration Loading..."

    # Test that config variables are loaded
    if [ -n "$DEFAULT_MODEL" ]; then
        test_pass "config_default_model"
    else
        test_fail "config_default_model" "DEFAULT_MODEL not set"
    fi

    if [ -n "$FALLBACK_MODEL" ]; then
        test_pass "config_fallback_model"
    else
        test_fail "config_fallback_model" "FALLBACK_MODEL not set"
    fi

    # Test model arrays
    if [ ${#CONSENSUS_MODELS[@]} -gt 0 ]; then
        test_pass "config_consensus_models"
    else
        test_fail "config_consensus_models" "CONSENSUS_MODELS array is empty"
    fi
}

test_quality_guard() {
    echo "Testing Quality Guard..."

    source "./quality_guard.sh"

    # Test quality assessment
    local test_response="This is a comprehensive answer that should pass quality checks."
    local quality_score
    quality_score=$(assess_quality "$test_response" "test prompt" "socratic")
    assert_not_empty "$quality_score" "assess_quality"

    # Test degradation detection
    local degradation_score
    degradation_score=$(detect_degradation_patterns "$test_response")
    assert_not_empty "$degradation_score" "detect_degradation_patterns"

    # Test degraded response detection
    local lorem_response="Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt"
    local lorem_degradation
    lorem_degradation=$(detect_degradation_patterns "$lorem_response")

    if (( $(echo "$lorem_degradation > 0" | bc -l 2>/dev/null || echo "0") )); then
        test_pass "lorem_ipsum_detection"
    else
        test_fail "lorem_ipsum_detection" "Failed to detect lorem ipsum pattern"
    fi
}

test_logging_system() {
    echo "Testing Logging System..."

    source "./logging.sh"

    # Test error logging
    log_error "Test error message"
    if [ -f "$ERROR_LOG" ]; then
        test_pass "error_logging"
    else
        test_fail "error_logging" "Error log file not created"
    fi

    # Test validation functions
    local temp_file
    temp_file=$(create_managed_temp_file "test" "tmp")
    echo "test content" > "$temp_file"

    if validate_file_path "$temp_file"; then
        test_pass "validate_file_path"
    else
        test_fail "validate_file_path" "Failed to validate existing file"
    fi

    # Test invalid file
    if ! validate_file_path "/nonexistent/file.txt" 2>/dev/null; then
        test_pass "validate_invalid_file"
    else
        test_fail "validate_invalid_file" "Should have failed for nonexistent file"
    fi
}

test_resource_management() {
    echo "Testing Resource Management..."

    source "./common.sh"

    # Test temporary directory creation
    local temp_dir
    temp_dir=$(create_managed_temp_dir "test")
    if [ -d "$temp_dir" ]; then
        test_pass "create_temp_dir"
    else
        test_fail "create_temp_dir" "Failed to create temp directory"
    fi

    # Test cleanup registration
    register_cleanup_resource "$temp_dir"
    if [ ${#CLEANUP_RESOURCES[@]} -gt 0 ]; then
        test_pass "register_cleanup_resource"
    else
        test_fail "register_cleanup_resource" "Resource not registered for cleanup"
    fi
}

# --- Integration Tests ---

test_mechanism_integration() {
    echo "Testing Mechanism Integration..."

    # Test if mechanisms are executable
    local mechanisms=("socratic" "exploration" "consensus" "critique" "synthesis" "peer-review" "puzzle")

    for mechanism in "${mechanisms[@]}"; do
        if [ -x "./$mechanism" ]; then
            test_pass "mechanism_executable_$mechanism"
        else
            test_fail "mechanism_executable_$mechanism" "Mechanism not executable"
        fi
    done
}

test_classifier_integration() {
    echo "Testing Classifier Integration..."

    if [ -x "./classifier.sh" ]; then
        test_pass "classifier_executable"

        # Test basic classification (if possible without models)
        local test_result
        test_result=$(source "./classifier.sh" && analyze_intent_patterns "What are the different approaches to solving this problem?" 2>/dev/null)
        if [ -n "$test_result" ]; then
            test_pass "classifier_basic_functionality"
        else
            test_skip "classifier_basic_functionality" "Cannot test without models"
        fi
    else
        test_fail "classifier_executable" "Classifier script not executable"
    fi
}

# --- Performance Tests ---

test_performance_metrics() {
    echo "Testing Performance Metrics..."

    source "./logging.sh"

    # Test metrics functions exist
    if command -v log_session_start >/dev/null 2>&1; then
        test_pass "performance_functions_available"
    else
        test_fail "performance_functions_available" "Performance logging functions not available"
    fi

    # Test metrics file creation
    if [ -f "$METRICS_FILE" ] || touch "$METRICS_FILE" 2>/dev/null; then
        test_pass "metrics_file_accessible"
    else
        test_fail "metrics_file_accessible" "Cannot access metrics file"
    fi
}

# --- Main Test Runner ---

run_all_tests() {
    init_test_framework

    echo "Running Test Suite..."
    echo "====================="
    echo

    # Unit Tests
    test_common_functions
    echo

    test_config_loading
    echo

    test_quality_guard
    echo

    test_logging_system
    echo

    test_resource_management
    echo

    # Integration Tests
    test_mechanism_integration
    echo

    test_classifier_integration
    echo

    # Performance Tests
    test_performance_metrics
    echo

    # Test Summary
    echo "Test Summary"
    echo "============"
    echo "✅ Passed: $TESTS_PASSED"
    echo "❌ Failed: $TESTS_FAILED"
    echo "⏭️  Skipped: $TESTS_SKIPPED"
    echo

    local total_tests=$((TESTS_PASSED + TESTS_FAILED))
    if [ $total_tests -gt 0 ]; then
        local pass_rate=$((TESTS_PASSED * 100 / total_tests))
        echo "Pass Rate: $pass_rate%"

        if [ $TESTS_FAILED -eq 0 ]; then
            echo "🎉 All tests completed successfully!"
            return 0
        else
            echo "⚠️  Some tests failed. Please review the results above."
            return 1
        fi
    else
        echo "No tests were run."
        return 1
    fi
}

# --- CLI Interface ---

show_help() {
    echo "AI Thinking Mechanisms Test Framework"
    echo "Usage: $0 [OPTIONS]"
    echo
    echo "Options:"
    echo "  -a, --all         Run all tests (default)"
    echo "  -u, --unit        Run only unit tests"
    echo "  -i, --integration Run only integration tests"
    echo "  -p, --performance Run only performance tests"
    echo "  -v, --verbose     Enable verbose output"
    echo "  -h, --help        Show this help message"
    echo
    echo "Examples:"
    echo "  $0 -a              # Run all tests"
    echo "  $0 -u -v          # Run unit tests with verbose output"
    echo "  $0 -p             # Run only performance tests"
}

# Parse command line arguments
VERBOSE=false
TEST_TYPE="all"

while [[ $# -gt 0 ]]; do
    case $1 in
        -a|--all)
            TEST_TYPE="all"
            shift
            ;;
        -u|--unit)
            TEST_TYPE="unit"
            shift
            ;;
        -i|--integration)
            TEST_TYPE="integration"
            shift
            ;;
        -p|--performance)
            TEST_TYPE="performance"
            shift
            ;;
        -v|--verbose)
            VERBOSE=true
            shift
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
done

# Set verbose output
if [ "$VERBOSE" = true ]; then
    set -x
fi

# Run tests based on type
case $TEST_TYPE in
    "all")
        run_all_tests
        ;;
    "unit")
        init_test_framework
        test_common_functions
        echo
        test_config_loading
        echo
        test_quality_guard
        echo
        test_logging_system
        echo
        test_resource_management
        ;;
    "integration")
        init_test_framework
        test_mechanism_integration
        echo
        test_classifier_integration
        ;;
    "performance")
        init_test_framework
        test_performance_metrics
        ;;
    *)
        echo "Invalid test type: $TEST_TYPE"
        show_help
        exit 1
        ;;
esac