test(ai): add comprehensive functionality tests

Add test coverage to ensure AI features are working
correctly and remain functional.

- Add RSpec tests for endpoint construction
- Add RSpec tests for environment context gathering
- Add RSpec tests for dual prompt modes
- Add RSpec tests for temperature configuration
- Add integration test script for manual validation
- Add simple functionality test for CI/verification

Tests verify base URL handling, context collection,
prompt modes, and all new AI strategy features.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Frad LEE 2026-02-05 11:30:38 +08:00
commit 81672cc7fe
3 changed files with 568 additions and 0 deletions

View file

@ -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 <<EOFCURL
{"choices":[{"message":{"content":"correct endpoint"}}]}
200
EOFCURL
else
cat <<EOFCURL
{"error":"wrong endpoint"}
400
EOFCURL
fi
}')
}
end
it 'appends /chat/completions to base URL' do
session.send_string('test')
wait_for { session.content }.to eq('correct endpoint')
end
context 'with custom base URL' do
let(:options) { ["ZSH_AUTOSUGGEST_AI_API_KEY=test-key", "ZSH_AUTOSUGGEST_AI_ENDPOINT=http://custom.api/v1", "ZSH_AUTOSUGGEST_STRATEGY=(ai)"] }
it 'constructs endpoint correctly' do
session.send_string('test')
wait_for { session.content }.to eq('correct endpoint')
end
end
end
context 'environmental context gathering' do
let(:options) { ["ZSH_AUTOSUGGEST_AI_API_KEY=test-key", "ZSH_AUTOSUGGEST_STRATEGY=(ai)"] }
let(:before_sourcing) do
-> {
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 <<EOFCURL
{"choices":[{"message":{"content":"has directory context"}}]}
200
EOFCURL
else
cat <<EOFCURL
{"choices":[{"message":{"content":"no context"}}]}
200
EOFCURL
fi
}')
session.run_command('ls() { echo "file1.txt\nfile2.txt"; }')
}
end
it 'includes directory contents in context' do
session.send_string('test')
wait_for { session.content }.to eq('has directory context')
end
end
context 'dual prompt modes' do
let(:options) { ["ZSH_AUTOSUGGEST_AI_API_KEY=test-key", "ZSH_AUTOSUGGEST_ALLOW_EMPTY_BUFFER=1", "ZSH_AUTOSUGGEST_STRATEGY=(ai)"] }
context 'empty buffer mode' do
let(:before_sourcing) do
-> {
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 <<EOFCURL
{"choices":[{"message":{"content":"prediction mode active"}}]}
200
EOFCURL
elif [[ "$data" == *"auto-completion engine"* ]]; then
cat <<EOFCURL
{"choices":[{"message":{"content":"completion mode active"}}]}
200
EOFCURL
fi
}')
}
end
it 'uses prediction prompt for empty buffer' do
session.send_keys('C-c')
wait_for { session.content(esc_seqs: true) }.to match(/prediction mode/)
end
it 'uses completion prompt for partial input' do
session.send_string('git')
wait_for { session.content }.to match(/completion mode/)
end
end
end
context 'temperature configuration' do
let(:options) { ["ZSH_AUTOSUGGEST_AI_API_KEY=test-key", "ZSH_AUTOSUGGEST_ALLOW_EMPTY_BUFFER=1", "ZSH_AUTOSUGGEST_STRATEGY=(ai)"] }
let(:before_sourcing) do
-> {
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 <<EOFCURL
{"choices":[{"message":{"content":"temp 0.5"}}]}
200
EOFCURL
elif [[ "$data" == *"\"temperature\": 0.3"* ]]; then
cat <<EOFCURL
{"choices":[{"message":{"content":"temp 0.3"}}]}
200
EOFCURL
fi
}')
}
end
it 'uses temperature 0.5 for empty buffer' do
session.send_keys('C-c')
wait_for { session.content(esc_seqs: true) }.to match(/temp 0.5/)
end
it 'uses temperature 0.3 for partial input' do
session.send_string('test')
wait_for { session.content }.to eq('temp 0.3')
end
end

258
test_ai_integration.zsh Executable file
View file

@ -0,0 +1,258 @@
#!/usr/bin/env zsh
#
# AI Strategy Integration Test
# Tests that AI functionality is working correctly without requiring real API calls
#
set -e
# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Test counter
TESTS_RUN=0
TESTS_PASSED=0
TESTS_FAILED=0
# Helper functions
print_test() {
echo "${YELLOW}[TEST]${NC} $1"
}
print_pass() {
echo "${GREEN}[PASS]${NC} $1"
((TESTS_PASSED++))
}
print_fail() {
echo "${RED}[FAIL]${NC} $1"
((TESTS_FAILED++))
}
# Source the plugin
source ./zsh-autosuggestions.zsh
# Mock curl for testing
curl() {
local url=""
local data=""
local expect_url="https://api.openai.com/v1/chat/completions"
# Parse arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-d)
data="$2"
shift 2
;;
http*)
url="$1"
shift
;;
*)
shift
;;
esac
done
# Verify endpoint construction
if [[ "$url" != "$expect_url" ]]; then
echo '{"error":"wrong endpoint"}'
echo '400'
return 1
fi
# Return mock response based on data
echo '{"choices":[{"message":{"content":"git status"}}]}'
echo '200'
}
# Mock jq
jq() {
if [[ "$1" == "-r" ]]; then
# Simple extraction of content field
grep -o '"content":"[^"]*"' | cut -d'"' -f4
fi
}
# Mock ls for environment context
ls() {
if [[ "$1" == "-1" ]]; then
echo "file1.txt"
echo "file2.txt"
echo "README.md"
fi
}
# Mock git for environment context
git() {
if [[ "$1" == "branch" ]]; then
echo "master"
elif [[ "$1" == "status" ]]; then
echo "M file1.txt"
echo "?? file2.txt"
fi
}
echo "=========================================="
echo "AI Strategy Integration Tests"
echo "=========================================="
echo ""
# Test 1: Endpoint construction
print_test "Endpoint construction with base URL"
((TESTS_RUN++))
export ZSH_AUTOSUGGEST_AI_API_KEY="test-key"
export ZSH_AUTOSUGGEST_AI_ENDPOINT="https://api.openai.com/v1"
result=$(_zsh_autosuggest_strategy_ai "test" 2>&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

146
test_ai_simple.sh Executable file
View file

@ -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 ""