Skip to content

Latest commit

 

History

History
308 lines (229 loc) · 6.29 KB

File metadata and controls

308 lines (229 loc) · 6.29 KB

How to Run the Repflow Lambda Project

Option 1: Run Tests (Recommended) ✅

This is what you should do for the case interview - all external services are mocked.

Step 1: Install Dependencies

# Navigate to the lambda directory
cd c:/Users/1/Downloads/case/lambda

# Install Python dependencies
pip install -r requirements.txt

# Install test dependencies
pip install pytest pytest-asyncio pytest-cov

Step 2: Run All Tests

# Run all 80 tests
pytest tests/ -v

# Expected output:
# ========================== 80 passed in 3.45s ===========================

Step 3: Run Tests with Coverage

# See code coverage
pytest tests/ --cov=lambda_function --cov=api_client --cov-report=term-missing

# Generate HTML report
pytest tests/ --cov=. --cov-report=html
# Then open: htmlcov/index.html

Step 4: Run Specific Test Files

# Just pure function tests (fastest)
pytest tests/test_email_utils.py -v
pytest tests/test_budget_utils.py -v

# FSM orchestration tests
pytest tests/test_deal_conversation_fsm.py -v

# Full handler integration tests
pytest tests/test_lambda_handler.py -v

Step 5: Run a Single Test

# Run one specific test
pytest tests/test_email_utils.py::TestExtractEmailAddress::test_extract_with_name_and_brackets -v

# Run all tests in a class
pytest tests/test_budget_utils.py::TestExtractBudgetValue -v

Option 2: Test the Lambda Locally (Advanced) 🔧

If you want to invoke the actual Lambda handler locally (not recommended for case interview):

Requirements

  • AWS credentials configured
  • SES set up to receive emails
  • S3 bucket created
  • Backend API running
  • OpenAI API key

Step 1: Set Environment Variables

# On Windows (PowerShell)
$env:API_BASE_URL = "http://localhost:8000"
$env:API_AUTH_TOKEN = "your-token"
$env:S3_BUCKET = "your-bucket"
$env:SES_REGION = "us-east-1"
$env:OPENAI_API_KEY = "sk-..."

# On Linux/Mac
export API_BASE_URL="http://localhost:8000"
export API_AUTH_TOKEN="your-token"
export S3_BUCKET="your-bucket"
export SES_REGION="us-east-1"
export OPENAI_API_KEY="sk-..."

Step 2: Create Test Event

Create test_event.json:

{
  "Records": [{
    "ses": {
      "mail": {
        "messageId": "test-msg-123",
        "timestamp": "2025-10-10T10:00:00Z",
        "commonHeaders": {
          "from": ["Brand Rep <[email protected]>"],
          "to": ["[email protected]"],
          "subject": "Sponsorship opportunity"
        }
      },
      "receipt": {}
    }
  }]
}

Step 3: Invoke Locally

# Create test_invoke.py
import json
from lambda_function import lambda_handler

with open('test_event.json') as f:
    event = json.load(f)

result = lambda_handler(event, {})
print(json.dumps(result, indent=2))
python test_invoke.py

⚠️ Warning: This will make real API calls to OpenAI and your backend!


Option 3: Interactive Testing (Python REPL)

Test individual functions interactively:

python
# Import functions
from lambda_function import (
    extract_email_address,
    extract_budget_value,
    determine_deal_type_from_budget,
    normalize_subject_for_thread,
    generate_thread_id,
)

# Test email extraction
extract_email_address("John Doe <[email protected]>")
# Output: '[email protected]'

# Test budget parsing
extract_budget_value("$1,000 - $5,000")
# Output: 1000.0

# Test deal type detection
determine_deal_type_from_budget("10% affiliate commission")
# Output: 'AFFILIATE'

# Test subject normalization
normalize_subject_for_thread("Re: Sponsorship Opportunity")
# Output: 'Sponsorship Opportunity'

# Test thread ID generation
generate_thread_id("[email protected]", "[email protected]", "Partnership")
# Output: 'a1b2c3d4e5f6g7h8-partnershi'

Quick Test Commands Reference

# Most common commands:

# Run all tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=. --cov-report=term-missing

# Run specific file
pytest tests/test_email_utils.py -v

# Run specific test
pytest tests/test_email_utils.py::test_extract_with_name_and_brackets -v

# Run and show print statements
pytest tests/ -v -s

# Run and stop on first failure
pytest tests/ -x

# Run only async tests
pytest tests/ -m asyncio -v

# Run with verbose output
pytest tests/ -vv

Troubleshooting

Import Errors

# Add current directory to Python path
export PYTHONPATH="${PYTHONPATH}:$(pwd)"  # Linux/Mac
$env:PYTHONPATH = "$env:PYTHONPATH;$PWD"  # Windows PowerShell

Async Test Warnings

# Upgrade pytest-asyncio
pip install --upgrade pytest-asyncio

Missing Dependencies

# Install everything
pip install -r requirements.txt pytest pytest-asyncio pytest-cov

Python Version Issues

# Check Python version (need 3.10+)
python --version

# Use specific Python version
python3.12 -m pytest tests/ -v

What Gets Tested?

The test suite covers:

Pure Functions (61 tests)

  • Email parsing utilities
  • Budget extraction
  • Deal type determination
  • Thread ID generation
  • Conversation formatting

FSM Logic (6 tests)

  • State transitions
  • Agent orchestration
  • Deal qualification
  • Recommendation handling

Handler Integration (13 tests)

  • Early exit scenarios
  • Full email processing flow
  • SES reply logic
  • Error handling

All external services are mocked - no real AWS/API calls!


Recommended Workflow

  1. First time:

    pip install -r requirements.txt pytest pytest-asyncio
    pytest tests/ -v
  2. During development:

    # Run tests in watch mode (requires pytest-watch)
    pip install pytest-watch
    ptw tests/ -- -v
  3. Before committing:

    pytest tests/ --cov=. --cov-report=term-missing
    # Ensure coverage > 70%

Expected Test Output

tests/test_budget_utils.py::TestExtractBudgetValue::test_simple_dollar_amount PASSED
tests/test_budget_utils.py::TestExtractBudgetValue::test_amount_with_commas PASSED
tests/test_budget_utils.py::TestExtractBudgetValue::test_range_returns_lower_bound PASSED
...
tests/test_lambda_handler.py::TestLambdaHandler::test_new_sponsorship_thread_full_flow PASSED
tests/test_lambda_handler.py::TestLambdaHandler::test_ses_send_success_stores_outbound_message PASSED

========================== 80 passed in 3.45s ===========================

Success! ✅