|
| 1 | +""" |
| 2 | +Test suite for correlation_id and thread_sync implementation. |
| 3 | +
|
| 4 | +Tests verify: |
| 5 | +1. Frontend generates a correlation_id when sending a message |
| 6 | +2. Backend echoes it back in user_message_confirmed event |
| 7 | +3. Backend sends thread_sync event at stream start |
| 8 | +4. Frontend can retrieve authoritative message history via GET endpoint |
| 9 | +""" |
| 10 | + |
| 11 | +import pytest |
| 12 | +import uuid |
| 13 | +import json |
| 14 | + |
| 15 | +from schema.schema import StreamInput |
| 16 | +from service.streaming.events import StreamEventFactory |
| 17 | + |
| 18 | + |
| 19 | +def generate_correlation_id() -> str: |
| 20 | + """Simulate frontend generating a correlation ID""" |
| 21 | + return str(uuid.uuid4()) |
| 22 | + |
| 23 | + |
| 24 | +def test_stream_input_accepts_correlation_id(): |
| 25 | + """Test that StreamInput accepts and stores correlation_id""" |
| 26 | + correlation_id = generate_correlation_id() |
| 27 | + |
| 28 | + stream_input = StreamInput( |
| 29 | + message="Test message", |
| 30 | + thread_id="test-thread-123", |
| 31 | + correlation_id=correlation_id, |
| 32 | + stream_tokens=True |
| 33 | + ) |
| 34 | + |
| 35 | + assert stream_input.message == "Test message" |
| 36 | + assert stream_input.thread_id == "test-thread-123" |
| 37 | + assert stream_input.correlation_id == correlation_id |
| 38 | + assert stream_input.stream_tokens is True |
| 39 | + |
| 40 | + |
| 41 | +def test_user_message_confirmed_event_includes_correlation_id(): |
| 42 | + """Test that UserMessageConfirmEvent includes correlation_id""" |
| 43 | + correlation_id = generate_correlation_id() |
| 44 | + backend_id = str(uuid.uuid4()) |
| 45 | + |
| 46 | + event = StreamEventFactory.user_message_confirmed( |
| 47 | + message_id=backend_id, |
| 48 | + correlation_id=correlation_id, |
| 49 | + content="Test message content" |
| 50 | + ) |
| 51 | + |
| 52 | + assert event.message_id == backend_id |
| 53 | + assert event.correlation_id == correlation_id |
| 54 | + assert event.content == "Test message content" |
| 55 | + assert event.type == "user_message_confirmed" |
| 56 | + |
| 57 | + |
| 58 | +def test_thread_sync_event_creation(): |
| 59 | + """Test that ThreadSyncEvent is created correctly""" |
| 60 | + thread_id = "test-thread-456" |
| 61 | + |
| 62 | + sync_event = StreamEventFactory.thread_sync(thread_id=thread_id) |
| 63 | + |
| 64 | + assert sync_event.thread_id == thread_id |
| 65 | + assert sync_event.type == "thread_sync" |
| 66 | + |
| 67 | + |
| 68 | +def test_user_message_confirmed_sse_serialization(): |
| 69 | + """Test SSE serialization of user_message_confirmed event""" |
| 70 | + correlation_id = generate_correlation_id() |
| 71 | + backend_id = str(uuid.uuid4()) |
| 72 | + |
| 73 | + event = StreamEventFactory.user_message_confirmed( |
| 74 | + message_id=backend_id, |
| 75 | + correlation_id=correlation_id, |
| 76 | + content="Test content" |
| 77 | + ) |
| 78 | + |
| 79 | + sse_data = event.to_sse_data() |
| 80 | + |
| 81 | + assert sse_data.startswith("data: ") |
| 82 | + assert sse_data.endswith("\n\n") |
| 83 | + |
| 84 | + # Parse the JSON data |
| 85 | + json_str = sse_data.replace("data: ", "").strip() |
| 86 | + parsed = json.loads(json_str) |
| 87 | + |
| 88 | + assert parsed["type"] == "user_message_confirmed" |
| 89 | + assert parsed["message_id"] == backend_id |
| 90 | + assert parsed["correlation_id"] == correlation_id |
| 91 | + assert parsed["content"] == "Test content" |
| 92 | + |
| 93 | + |
| 94 | +def test_thread_sync_sse_serialization(): |
| 95 | + """Test SSE serialization of thread_sync event""" |
| 96 | + thread_id = "test-thread-789" |
| 97 | + |
| 98 | + sync_event = StreamEventFactory.thread_sync(thread_id=thread_id) |
| 99 | + sse_data = sync_event.to_sse_data() |
| 100 | + |
| 101 | + assert sse_data.startswith("data: ") |
| 102 | + assert sse_data.endswith("\n\n") |
| 103 | + |
| 104 | + # Parse the JSON data |
| 105 | + json_str = sse_data.replace("data: ", "").strip() |
| 106 | + parsed = json.loads(json_str) |
| 107 | + |
| 108 | + assert parsed["type"] == "thread_sync" |
| 109 | + assert parsed["thread_id"] == thread_id |
| 110 | + |
| 111 | + |
| 112 | +def test_correlation_id_matching_flow(): |
| 113 | + """ |
| 114 | + Test the complete correlation ID flow for reliable message matching |
| 115 | + """ |
| 116 | + # 1. Frontend generates correlation_id |
| 117 | + frontend_correlation_id = generate_correlation_id() |
| 118 | + |
| 119 | + # 2. Frontend sends message with correlation_id |
| 120 | + stream_input = StreamInput( |
| 121 | + message="Hello, how can I help?", |
| 122 | + correlation_id=frontend_correlation_id, |
| 123 | + stream_tokens=True |
| 124 | + ) |
| 125 | + |
| 126 | + assert stream_input.correlation_id == frontend_correlation_id |
| 127 | + |
| 128 | + # 3. Backend assigns backend message_id |
| 129 | + backend_message_id = str(uuid.uuid4()) |
| 130 | + |
| 131 | + # 4. Backend emits user_message_confirmed with both IDs |
| 132 | + confirm_event = StreamEventFactory.user_message_confirmed( |
| 133 | + message_id=backend_message_id, |
| 134 | + correlation_id=frontend_correlation_id, |
| 135 | + content="Hello, how can I help?" |
| 136 | + ) |
| 137 | + |
| 138 | + # 5. Verify frontend can match by correlation_id |
| 139 | + assert confirm_event.correlation_id == frontend_correlation_id |
| 140 | + assert confirm_event.message_id == backend_message_id |
| 141 | + |
| 142 | + # Frontend would find message with correlation_id and update to backend_id |
| 143 | + # This eliminates the fragile content-based matching |
| 144 | + |
| 145 | + |
| 146 | +def test_thread_sync_with_provided_thread_id(): |
| 147 | + """Test thread sync when frontend provides thread_id""" |
| 148 | + frontend_thread_id = "existing-thread-789" |
| 149 | + |
| 150 | + # Backend uses provided thread_id |
| 151 | + sync_event = StreamEventFactory.thread_sync(thread_id=frontend_thread_id) |
| 152 | + |
| 153 | + assert sync_event.thread_id == frontend_thread_id |
| 154 | + |
| 155 | + |
| 156 | +def test_thread_sync_with_generated_thread_id(): |
| 157 | + """Test thread sync when backend generates new thread_id""" |
| 158 | + # Frontend doesn't provide thread_id (new conversation) |
| 159 | + # Backend generates one |
| 160 | + backend_thread_id = str(uuid.uuid4()) |
| 161 | + |
| 162 | + sync_event = StreamEventFactory.thread_sync(thread_id=backend_thread_id) |
| 163 | + |
| 164 | + assert sync_event.thread_id == backend_thread_id |
| 165 | + # Frontend would store this as the authoritative thread_id |
| 166 | + |
| 167 | + |
| 168 | +def test_correlation_id_optional_for_backward_compatibility(): |
| 169 | + """Test that correlation_id is optional for backward compatibility""" |
| 170 | + # Should work without correlation_id |
| 171 | + stream_input = StreamInput( |
| 172 | + message="Test message", |
| 173 | + stream_tokens=True |
| 174 | + ) |
| 175 | + |
| 176 | + assert stream_input.message == "Test message" |
| 177 | + assert stream_input.correlation_id is None # Optional field |
0 commit comments