diff --git a/spec/strategies/ai_spec.rb b/spec/strategies/ai_spec.rb index 23cedfb..cc1ff24 100644 --- a/spec/strategies/ai_spec.rb +++ b/spec/strategies/ai_spec.rb @@ -241,3 +241,167 @@ EOFCURL end end end + + context 'endpoint construction' do + let(:options) { ["ZSH_AUTOSUGGEST_AI_API_KEY=test-key", "ZSH_AUTOSUGGEST_STRATEGY=(ai)"] } + + let(:before_sourcing) do + -> { + session.run_command('curl() { + local url="" + for arg in "$@"; do + if [[ "$arg" == http* ]]; then + url="$arg" + break + fi + done + if [[ "$url" == */chat/completions ]]; then + cat < { + session.run_command('curl() { + local data="" + for arg in "$@"; do + if [[ "$arg" == -d ]]; then + shift + data="$1" + break + fi + shift + done + if [[ "$data" == *"Directory contents"* ]]; then + cat < { + session.run_command('curl() { + local data="" + for arg in "$@"; do + if [[ "$arg" == -d ]]; then + shift + data="$1" + break + fi + shift + done + if [[ "$data" == *"prediction engine"* ]]; then + cat < { + session.run_command('curl() { + local data="" + for arg in "$@"; do + if [[ "$arg" == -d ]]; then + shift + data="$1" + break + fi + shift + done + if [[ "$data" == *"\"temperature\": 0.5"* ]]; then + cat <&1) +if [[ $? -eq 0 ]]; then + print_pass "Base URL correctly constructs full endpoint" +else + print_fail "Endpoint construction failed" +fi + +# Test 2: Custom base URL +print_test "Custom base URL endpoint construction" +((TESTS_RUN++)) +export ZSH_AUTOSUGGEST_AI_ENDPOINT="http://localhost:11434/v1" +# Override curl mock for this test +curl() { + local url="" + while [[ $# -gt 0 ]]; do + case "$1" in + http*) + url="$1" + shift + ;; + *) + shift + ;; + esac + done + + if [[ "$url" == "http://localhost:11434/v1/chat/completions" ]]; then + echo '{"choices":[{"message":{"content":"ollama response"}}]}' + echo '200' + else + echo '{"error":"wrong endpoint"}' + echo '400' + fi +} +result=$(_zsh_autosuggest_strategy_ai "test" 2>&1) +if [[ $? -eq 0 ]]; then + print_pass "Custom base URL works correctly" +else + print_fail "Custom base URL failed" +fi + +# Reset curl mock +curl() { + local url="" + local data="" + while [[ $# -gt 0 ]]; do + case "$1" in + -d) + data="$2" + shift 2 + ;; + http*) + url="$1" + shift + ;; + *) + shift + ;; + esac + done + echo '{"choices":[{"message":{"content":"git status"}}]}' + echo '200' +} + +# Test 3: Environment context gathering +print_test "Environment context gathering" +((TESTS_RUN++)) +typeset -g reply +_zsh_autosuggest_strategy_ai_gather_env_context +if [[ -n "${reply[dir_contents]}" ]]; then + print_pass "Directory contents captured: ${reply[dir_contents]}" +else + print_fail "Directory contents not captured" +fi + +# Test 4: PWD-aware history gathering +print_test "PWD-aware history gathering" +((TESTS_RUN++)) +# Add some history +fc -p # Push history +print -s "git status" +print -s "ls -la" +print -s "cd /tmp" +_zsh_autosuggest_strategy_ai_gather_context +if [[ ${#reply[@]} -gt 0 ]]; then + print_pass "History context gathered: ${#reply[@]} entries" +else + print_fail "History gathering failed" +fi + +# Test 5: Response normalization with prompt artifacts +print_test "Prompt artifact stripping" +((TESTS_RUN++)) +result=$(_zsh_autosuggest_strategy_ai_normalize "$ git status" "git") +if [[ "$result" == "git status" ]]; then + print_pass "$ prompt artifact stripped correctly" +else + print_fail "Prompt artifact stripping failed: got '$result'" +fi + +# Test 6: Response normalization with > artifact +print_test "> prompt artifact stripping" +((TESTS_RUN++)) +result=$(_zsh_autosuggest_strategy_ai_normalize "> ls -la" "ls") +if [[ "$result" == "ls -la" ]]; then + print_pass "> prompt artifact stripped correctly" +else + print_fail "> artifact stripping failed: got '$result'" +fi + +# Test 7: Minimum input length (now default 0) +print_test "Minimum input length allows empty buffer" +((TESTS_RUN++)) +export ZSH_AUTOSUGGEST_AI_MIN_INPUT=0 +result=$(_zsh_autosuggest_strategy_ai "" 2>&1) +# Should not fail due to length check +if [[ $? -eq 0 ]] || [[ "$result" != *"too short"* ]]; then + print_pass "Empty buffer allowed with MIN_INPUT=0" +else + print_fail "Empty buffer rejected incorrectly" +fi + +# Test 8: JSON escaping +print_test "JSON escaping for special characters" +((TESTS_RUN++)) +result=$(_zsh_autosuggest_strategy_ai_json_escape 'test "quote" and \backslash') +if [[ "$result" == *'\"'* ]] && [[ "$result" == *'\\'* ]]; then + print_pass "Special characters escaped correctly" +else + print_fail "JSON escaping failed: got '$result'" +fi + +# Summary +echo "" +echo "==========================================" +echo "Test Summary" +echo "==========================================" +echo "Tests run: $TESTS_RUN" +echo "Tests passed: ${GREEN}$TESTS_PASSED${NC}" +echo "Tests failed: ${RED}$TESTS_FAILED${NC}" +echo "" + +if [[ $TESTS_FAILED -eq 0 ]]; then + echo "${GREEN}✓ All tests passed!${NC}" + exit 0 +else + echo "${RED}✗ Some tests failed${NC}" + exit 1 +fi diff --git a/test_ai_simple.sh b/test_ai_simple.sh new file mode 100755 index 0000000..28b6fb2 --- /dev/null +++ b/test_ai_simple.sh @@ -0,0 +1,146 @@ +#!/bin/bash +# +# Simple AI Strategy Test +# Validates core AI functionality +# + +echo "=== AI Strategy Core Functionality Tests ===" +echo "" + +# Test 1: Check source files exist +echo "[TEST 1] Source files exist" +if [[ -f "src/strategies/ai.zsh" ]] && [[ -f "src/config.zsh" ]]; then + echo "✓ PASS: Source files found" +else + echo "✗ FAIL: Source files missing" + exit 1 +fi + +# Test 2: Check built file includes AI strategy +echo "" +echo "[TEST 2] Built file includes AI strategy" +if grep -q "_zsh_autosuggest_strategy_ai" zsh-autosuggestions.zsh; then + echo "✓ PASS: AI strategy function present in built file" +else + echo "✗ FAIL: AI strategy missing from built file" + exit 1 +fi + +# Test 3: Verify base URL configuration +echo "" +echo "[TEST 3] Base URL configuration" +if grep -q "ZSH_AUTOSUGGEST_AI_ENDPOINT='https://api.openai.com/v1'" zsh-autosuggestions.zsh; then + echo "✓ PASS: Base URL configured correctly" +else + echo "✗ FAIL: Base URL configuration incorrect" + exit 1 +fi + +# Test 4: Verify endpoint construction code +echo "" +echo "[TEST 4] Endpoint construction logic" +if grep -q 'local base_url=.*ZSH_AUTOSUGGEST_AI_ENDPOINT' zsh-autosuggestions.zsh && \ + grep -q 'local endpoint=.*chat/completions' zsh-autosuggestions.zsh; then + echo "✓ PASS: Endpoint construction code present" +else + echo "✗ FAIL: Endpoint construction logic missing" + exit 1 +fi + +# Test 5: Environment context function exists +echo "" +echo "[TEST 5] Environment context gathering function" +if grep -q "_zsh_autosuggest_strategy_ai_gather_env_context" zsh-autosuggestions.zsh; then + echo "✓ PASS: Environment context function present" +else + echo "✗ FAIL: Environment context function missing" + exit 1 +fi + +# Test 6: Prompt artifact stripping +echo "" +echo "[TEST 6] Prompt artifact stripping code" +if grep -q 'response=.*##\\$ ' zsh-autosuggestions.zsh && \ + grep -q 'response=.*##> ' zsh-autosuggestions.zsh; then + echo "✓ PASS: Prompt artifact stripping present" +else + echo "✗ FAIL: Prompt artifact stripping missing" + exit 1 +fi + +# Test 7: Empty buffer support +echo "" +echo "[TEST 7] Empty buffer configuration" +if grep -q "ALLOW_EMPTY_BUFFER" zsh-autosuggestions.zsh; then + echo "✓ PASS: Empty buffer support present" +else + echo "✗ FAIL: Empty buffer support missing" + exit 1 +fi + +# Test 8: Dual prompt system +echo "" +echo "[TEST 8] Dual prompt system (predict vs complete)" +if grep -q "prediction engine" zsh-autosuggestions.zsh && \ + grep -q "auto-completion engine" zsh-autosuggestions.zsh; then + echo "✓ PASS: Dual prompt system present" +else + echo "✗ FAIL: Dual prompt system missing" + exit 1 +fi + +# Test 9: Temperature configuration +echo "" +echo "[TEST 9] Temperature configuration" +if grep -q '"temperature": %s' zsh-autosuggestions.zsh; then + echo "✓ PASS: Dynamic temperature configuration present" +else + echo "✗ FAIL: Temperature configuration missing" + exit 1 +fi + +# Test 10: MIN_INPUT default is 0 +echo "" +echo "[TEST 10] MIN_INPUT default value" +if grep -q 'ZSH_AUTOSUGGEST_AI_MIN_INPUT=0' zsh-autosuggestions.zsh; then + echo "✓ PASS: MIN_INPUT default is 0" +else + echo "✗ FAIL: MIN_INPUT default incorrect" + exit 1 +fi + +# Test 11: Documentation updated +echo "" +echo "[TEST 11] Documentation updates" +if grep -q "ALLOW_EMPTY_BUFFER" README.md && \ + grep -q "Empty Buffer Suggestions" README.md; then + echo "✓ PASS: Documentation includes new features" +else + echo "✗ FAIL: Documentation missing updates" + exit 1 +fi + +# Test 12: RSpec tests added +echo "" +echo "[TEST 12] RSpec test coverage" +if grep -q "empty buffer" spec/strategies/ai_spec.rb && \ + grep -q "prompt artifact" spec/strategies/ai_spec.rb; then + echo "✓ PASS: New test cases added" +else + echo "✗ FAIL: Test coverage incomplete" + exit 1 +fi + +echo "" +echo "==========================================" +echo "✓ All 12 core functionality tests passed!" +echo "==========================================" +echo "" +echo "AI Strategy is ready for use with:" +echo " - Base URL configuration" +echo " - Empty buffer suggestions" +echo " - Environment context gathering" +echo " - PWD-aware history" +echo " - Dual prompt modes" +echo " - Prompt artifact stripping" +echo ""