|
3 | 3 |
|
4 | 4 | from redisvl.extensions.message_history.schema import ChatMessage |
5 | 5 | from redisvl.redis.utils import array_to_buffer |
6 | | -from redisvl.utils.utils import create_ulid, current_timestamp, deserialize, serialize |
| 6 | +from redisvl.utils.utils import create_ulid, current_timestamp, serialize |
7 | 7 |
|
8 | 8 |
|
9 | 9 | def test_chat_message_creation(): |
@@ -41,7 +41,13 @@ def test_chat_message_default_id_generation(): |
41 | 41 | timestamp=timestamp, |
42 | 42 | ) |
43 | 43 |
|
44 | | - assert chat_message.entry_id == f"{session_tag}:{timestamp}" |
| 44 | + # ID should start with session:timestamp and have a UUID suffix for uniqueness |
| 45 | + assert chat_message.entry_id.startswith(f"{session_tag}:{timestamp}:") |
| 46 | + # Verify the UUID suffix is 8 hex characters |
| 47 | + parts = chat_message.entry_id.split(":") |
| 48 | + assert len(parts) == 3 |
| 49 | + assert len(parts[2]) == 8 |
| 50 | + assert all(c in "0123456789abcdef" for c in parts[2]) |
45 | 51 |
|
46 | 52 |
|
47 | 53 | def test_chat_message_with_tool_call_id(): |
@@ -164,3 +170,29 @@ def test_chat_message_invalid_role(): |
164 | 170 | session_tag=session_tag, |
165 | 171 | timestamp=timestamp, |
166 | 172 | ) |
| 173 | + |
| 174 | + |
| 175 | +def test_chat_message_unique_ids_for_rapid_creation(): |
| 176 | + """Test that rapidly created messages get unique IDs even with same timestamp.""" |
| 177 | + session_tag = create_ulid() |
| 178 | + timestamp = current_timestamp() |
| 179 | + |
| 180 | + # Create multiple messages with the same session and timestamp |
| 181 | + messages = [] |
| 182 | + for i in range(10): |
| 183 | + msg = ChatMessage( |
| 184 | + role="user", |
| 185 | + content=f"Message {i}", |
| 186 | + session_tag=session_tag, |
| 187 | + timestamp=timestamp, |
| 188 | + ) |
| 189 | + messages.append(msg) |
| 190 | + |
| 191 | + # All IDs should be unique |
| 192 | + ids = [msg.entry_id for msg in messages] |
| 193 | + assert len(ids) == len(set(ids)), "All message IDs should be unique" |
| 194 | + |
| 195 | + # All IDs should start with the same session:timestamp prefix |
| 196 | + expected_prefix = f"{session_tag}:{timestamp}:" |
| 197 | + for msg_id in ids: |
| 198 | + assert msg_id.startswith(expected_prefix) |
0 commit comments