mail2couch/test/run-tests.sh
Ole-Morten Duesund ea6235b674 feat: implement real IMAP message parsing with native CouchDB attachments
- Replace placeholder message generation with actual IMAP message fetching using go-message library
- Add per-account CouchDB databases for better organization and isolation
- Implement native CouchDB attachment storage with proper revision management
- Add command line argument parsing with --max-messages flag for controlling message processing limits
- Support both sync and archive modes with proper document synchronization
- Add comprehensive test environment with Podman containers (GreenMail IMAP server + CouchDB)
- Implement full MIME multipart parsing for proper body and attachment extraction
- Add TLS and plain IMAP connection support based on port configuration
- Update configuration system to support sync vs archive modes
- Create test scripts and sample data for development and testing

Key technical improvements:
- Real email envelope and header processing with go-imap v2 API
- MIME Content-Type and Content-Disposition parsing for attachment detection
- CouchDB document ID generation using mailbox_uid format for uniqueness
- Duplicate detection and prevention to avoid re-storing existing messages
- Proper error handling and connection management for IMAP operations

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-01 17:04:10 +02:00

128 lines
No EOL
3.3 KiB
Bash
Executable file

#!/bin/bash
# Run integration tests with test containers
set -e
cd "$(dirname "$0")"
echo "🚀 Starting mail2couch integration tests..."
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Cleanup function
cleanup() {
print_status "Cleaning up test containers..."
podman-compose -f podman-compose.yml down -v 2>/dev/null || true
}
# Set up cleanup trap
trap cleanup EXIT
# Start containers
print_status "Starting test containers..."
podman-compose -f podman-compose.yml up -d
# Wait for containers to be ready
print_status "Waiting for containers to be ready..."
sleep 10
# Check if CouchDB is ready
print_status "Checking CouchDB connectivity..."
timeout=30
while ! curl -s http://localhost:5984/_up > /dev/null 2>&1; do
timeout=$((timeout - 1))
if [ $timeout -le 0 ]; then
print_error "CouchDB failed to start within 30 seconds"
exit 1
fi
sleep 1
done
print_status "CouchDB is ready!"
# Check if IMAP server is ready
print_status "Checking IMAP server connectivity..."
timeout=30
while ! nc -z localhost 3143 > /dev/null 2>&1; do
timeout=$((timeout - 1))
if [ $timeout -le 0 ]; then
print_error "IMAP server failed to start within 30 seconds"
exit 1
fi
sleep 1
done
print_status "IMAP server is ready!"
# Populate test messages
print_status "Populating test messages..."
./populate-test-messages.sh
# Build mail2couch
print_status "Building mail2couch..."
cd ../go
go build -o mail2couch .
cd ../test
# Run mail2couch with test configuration
print_status "Running mail2couch with test configuration..."
../go/mail2couch -config config-test.json
# Verify results
print_status "Verifying test results..."
# Check CouchDB databases were created
EXPECTED_DBS=("test_user_1" "test_sync_user" "test_archive_user")
for db in "${EXPECTED_DBS[@]}"; do
if curl -s "http://admin:password@localhost:5984/$db" | grep -q "\"db_name\":\"$db\""; then
print_status "✅ Database '$db' created successfully"
else
print_error "❌ Database '$db' was not created"
exit 1
fi
done
# Check document counts
for db in "${EXPECTED_DBS[@]}"; do
doc_count=$(curl -s "http://admin:password@localhost:5984/$db" | grep -o '"doc_count":[0-9]*' | cut -d':' -f2)
if [ "$doc_count" -gt 0 ]; then
print_status "✅ Database '$db' contains $doc_count documents"
else
print_warning "⚠️ Database '$db' contains no documents"
fi
done
# Test sync mode by running again (should show removed documents if any)
print_status "Running mail2couch again to test sync behavior..."
../go/mail2couch -config config-test.json
print_status "🎉 All tests completed successfully!"
# Show summary
print_status "Test Summary:"
echo " - IMAP Server: localhost:143"
echo " - CouchDB: http://localhost:5984"
echo " - Test accounts: testuser1, syncuser, archiveuser"
echo " - Databases created: ${EXPECTED_DBS[*]}"
echo ""
echo "You can now:"
echo " - Access CouchDB at http://localhost:5984/_utils"
echo " - Connect to IMAP at localhost:143"
echo " - Run manual tests with: ../go/mail2couch -config config-test.json"