|
| 1 | +# End-to-End API Tests |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This directory contains comprehensive end-to-end tests for the conversation tracking functionality. The tests verify that the API correctly: |
| 6 | + |
| 7 | +1. Tracks conversation IDs per user in the local database |
| 8 | +2. Fetches conversation details from OpenAI API on demand |
| 9 | +3. Maintains proper access control for conversations |
| 10 | +4. Handles edge cases like empty lists and response-triggered tracking |
| 11 | + |
| 12 | +## Architecture Being Tested |
| 13 | + |
| 14 | +The conversation management system works as follows: |
| 15 | + |
| 16 | +``` |
| 17 | +┌─────────────┐ ┌─────────────┐ ┌─────────────┐ |
| 18 | +│ Client │────────>│ Chat API │────────>│ Database │ |
| 19 | +│ │ │ │ │ │ |
| 20 | +│ │ │ - Track │ │ - User IDs │ |
| 21 | +│ │ │ Conv IDs │ │ - Conv IDs │ |
| 22 | +└─────────────┘ └─────────────┘ └─────────────┘ |
| 23 | + │ |
| 24 | + │ Fetch Details |
| 25 | + ▼ |
| 26 | + ┌─────────────┐ |
| 27 | + │ OpenAI API │ |
| 28 | + │ │ |
| 29 | + │ - Titles │ |
| 30 | + │ - Messages │ |
| 31 | + │ - Metadata │ |
| 32 | + └─────────────┘ |
| 33 | +``` |
| 34 | + |
| 35 | +## Test Cases |
| 36 | + |
| 37 | +### 1. `test_conversation_workflow` |
| 38 | +Tests the complete conversation lifecycle: |
| 39 | +- Creates a conversation via OpenAI |
| 40 | +- Adds multiple responses to the conversation |
| 41 | +- Lists conversations and verifies details are fetched from OpenAI |
| 42 | +- Confirms that conversation tracking works end-to-end |
| 43 | + |
| 44 | +### 2. `test_conversation_access_control` |
| 45 | +Verifies access control mechanisms: |
| 46 | +- Creates a conversation for a user |
| 47 | +- Attempts to access it as the owner (should succeed) |
| 48 | +- Validates proper authorization checks |
| 49 | + |
| 50 | +### 3. `test_empty_conversation_list` |
| 51 | +Tests edge case handling: |
| 52 | +- Lists conversations when there may be zero or many |
| 53 | +- Ensures the endpoint handles all cases gracefully |
| 54 | + |
| 55 | +### 4. `test_conversation_tracking_on_response_creation` |
| 56 | +Tests automatic conversation tracking: |
| 57 | +- Creates a conversation |
| 58 | +- Adds a response (which triggers automatic tracking) |
| 59 | +- Verifies the conversation appears in the user's list |
| 60 | +- Confirms details are fetched from OpenAI |
| 61 | + |
| 62 | +## Running the Tests |
| 63 | + |
| 64 | +### Prerequisites |
| 65 | + |
| 66 | +1. **Database**: Ensure PostgreSQL is running with the correct schema |
| 67 | +2. **Environment Variables**: Set up your `.env` file with: |
| 68 | + ``` |
| 69 | + OPENAI_API_KEY=your_api_key_here |
| 70 | + DATABASE_HOST=localhost |
| 71 | + DATABASE_PORT=5432 |
| 72 | + DATABASE_NAME=chat_api |
| 73 | + DATABASE_USER=postgres |
| 74 | + DATABASE_PASSWORD=your_password |
| 75 | + ``` |
| 76 | + |
| 77 | +3. **Valid Session Token**: The tests use `SESSION_TOKEN` constant - ensure you have a valid session in the database |
| 78 | + |
| 79 | +### Run All Tests |
| 80 | + |
| 81 | +```bash |
| 82 | +# Run all E2E tests (they make real OpenAI API calls) |
| 83 | +cargo test --test e2e_api_tests -- --ignored --nocapture |
| 84 | + |
| 85 | +# Run a specific test |
| 86 | +cargo test --test e2e_api_tests test_conversation_workflow -- --ignored --nocapture |
| 87 | +``` |
| 88 | + |
| 89 | +### Test Output |
| 90 | + |
| 91 | +The tests provide detailed output showing: |
| 92 | +- Step-by-step progress |
| 93 | +- API request/response status codes |
| 94 | +- Conversation IDs and details |
| 95 | +- Success/failure indicators (✓/✗) |
| 96 | + |
| 97 | +Example output: |
| 98 | +``` |
| 99 | +=== Test: Conversation Workflow === |
| 100 | +1. Creating a conversation via OpenAI... |
| 101 | + Status: 200 |
| 102 | + ✓ Conversation created successfully |
| 103 | + Conversation ID: conv_abc123... |
| 104 | +
|
| 105 | +2. Adding first response to the conversation... |
| 106 | + Status: 200 |
| 107 | + ✓ First response created successfully |
| 108 | + Response ID: resp_xyz789... |
| 109 | +
|
| 110 | +... |
| 111 | +
|
| 112 | +4. Listing conversations (should fetch details from OpenAI)... |
| 113 | + Found 5 total conversations |
| 114 | + ✓ Found our conversation in the list! |
| 115 | + ID: conv_abc123... |
| 116 | + Created: 2025-11-12T10:30:00Z |
| 117 | + Updated: 2025-11-12T10:35:00Z |
| 118 | + ✓ Conversation details fetched from OpenAI |
| 119 | +
|
| 120 | +=== Test Complete === |
| 121 | +✅ Test passed: Created conversation, added responses, and listed conversations with OpenAI details |
| 122 | +``` |
| 123 | + |
| 124 | +## Notes |
| 125 | + |
| 126 | +- Tests are marked with `#[ignore]` because they make real API calls to OpenAI |
| 127 | +- Each test is independent and can be run separately |
| 128 | +- Tests use the actual database and OpenAI API (not mocks) |
| 129 | +- The session token must be valid and exist in your database |
| 130 | + |
| 131 | +## Troubleshooting |
| 132 | + |
| 133 | +**Test fails with "Session not found"** |
| 134 | +- Check that `SESSION_TOKEN` constant matches a valid session in your database |
| 135 | +- Ensure the session hasn't expired |
| 136 | + |
| 137 | +**Test fails with "OpenAI API error"** |
| 138 | +- Verify your `OPENAI_API_KEY` is valid |
| 139 | +- Check your OpenAI account has available credits |
| 140 | + |
| 141 | +**Test fails with "Database error"** |
| 142 | +- Ensure PostgreSQL is running |
| 143 | +- Run migrations: `cargo run --bin migrate` or start the API server once |
| 144 | +- Check database connection settings in `.env` |
| 145 | + |
0 commit comments