#!/bin/bash
# Test script to verify force reindex works without LanceDB errors

set -e  # Exit on error

echo "🧪 Testing force reindex with LanceDB error handling..."

# Clean up any existing test directory
TEST_DIR="/tmp/mcp-test-force-reindex"
rm -rf "$TEST_DIR"
mkdir -p "$TEST_DIR"

# Create a simple test file
cat > "$TEST_DIR/test.py" << 'EOF'
def hello():
    """Test function"""
    print("Hello")

class TestClass:
    """Test class"""
    def method(self):
        pass
EOF

echo "✓ Created test directory: $TEST_DIR"

# Run first index
echo "📄 Running initial index..."
mcp-vector-search index "$TEST_DIR" --limit 10 2>&1 | head -20

# Run force reindex (this should not crash with LanceDB errors)
echo ""
echo "🔄 Running force reindex (testing error handling)..."
mcp-vector-search index "$TEST_DIR" -f --limit 10 2>&1 | tee /tmp/force-reindex-output.txt | head -30

# Check for LanceDB errors in output
if grep -i "lance error: Not found" /tmp/force-reindex-output.txt; then
    echo ""
    echo "❌ FAIL: LanceDB 'Not found' errors detected"
    exit 1
fi

if grep -i "data/repos/.mcp-vector-search/code_search.lance/data/" /tmp/force-reindex-output.txt; then
    echo ""
    echo "❌ FAIL: LanceDB data file errors detected"
    exit 1
fi

echo ""
echo "✅ SUCCESS: Force reindex completed without LanceDB errors"
echo ""
echo "Cleanup..."
rm -rf "$TEST_DIR"
rm -f /tmp/force-reindex-output.txt

exit 0
