File size: 8,960 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""
High-quality tests for api_utils/utils.py - Latest user text extraction (zero mocking).

Focus: Test _get_latest_user_text with pure function testing (no mocks).
Strategy: Comprehensive edge case coverage for message content extraction.
"""

from typing import List, cast

from models import Message, MessageContentItem


def test_get_latest_user_text_empty_messages():
    """
    Test scenario: Empty message list
    Expected: Return empty string
    """
    from api_utils.utils import _get_latest_user_text

    result = _get_latest_user_text([])

    assert result == ""


def test_get_latest_user_text_no_user_messages():
    """
    Test scenario: No user messages in message list
    Expected: Return empty string
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(role="system", content="System prompt"),
        Message(role="assistant", content="AI response"),
    ]

    result = _get_latest_user_text(messages)

    assert result == ""


def test_get_latest_user_text_single_user_message_string():
    """
    Test scenario: Single user message, content as string
    Expected: Return that string
    """
    from api_utils.utils import _get_latest_user_text

    messages = [Message(role="user", content="Hello, world!")]

    result = _get_latest_user_text(messages)

    assert result == "Hello, world!"


def test_get_latest_user_text_multiple_user_messages_returns_latest():
    """
    Test scenario: Multiple user messages
    Expected: Return content of the last user message
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(role="user", content="First message"),
        Message(role="assistant", content="Response"),
        Message(role="user", content="Second message"),
        Message(role="assistant", content="Another response"),
        Message(role="user", content="Latest message"),
    ]

    result = _get_latest_user_text(messages)

    assert result == "Latest message"


def test_get_latest_user_text_mixed_roles_returns_latest_user():
    """
    Test scenario: Mixed role messages (system, user, assistant)
    Expected: Return the last user message
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(role="system", content="System"),
        Message(role="user", content="User 1"),
        Message(role="assistant", content="AI 1"),
        Message(role="system", content="More system"),
        Message(role="user", content="User 2"),
        Message(role="assistant", content="AI 2"),
    ]

    result = _get_latest_user_text(messages)

    assert result == "User 2"


def test_get_latest_user_text_list_content_with_text_items():
    """
    Test scenario: User message content as list (containing text items)
    Expected: Concatenate all text items, joined by newline
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(
            role="user",
            content=cast(
                List[MessageContentItem],
                [
                    {"type": "text", "text": "First part"},
                    {"type": "text", "text": "Second part"},
                    {"type": "text", "text": "Third part"},
                ],
            ),
        )
    ]

    result = _get_latest_user_text(messages)

    assert result == "First part\nSecond part\nThird part"


def test_get_latest_user_text_list_content_with_mixed_types():
    """
    Test scenario: List content contains text and other types (e.g. image_url)
    Expected: Only extract text type content
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(
            role="user",
            content=cast(
                List[MessageContentItem],
                [
                    {"type": "text", "text": "Text before image"},
                    {
                        "type": "image_url",
                        "image_url": {"url": "http://example.com/img.jpg"},
                    },
                    {"type": "text", "text": "Text after image"},
                ],
            ),
        )
    ]

    result = _get_latest_user_text(messages)

    assert result == "Text before image\nText after image"


def test_get_latest_user_text_list_content_empty_text():
    """
    Test scenario: List content has empty text items
    Expected: Skip empty text items
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(
            role="user",
            content=cast(
                List[MessageContentItem],
                [
                    {"type": "text", "text": ""},
                    {"type": "text", "text": "Non-empty"},
                    {"type": "text", "text": ""},
                ],
            ),
        )
    ]

    result = _get_latest_user_text(messages)

    assert result == "Non-empty"


def test_get_latest_user_text_list_content_no_text_items():
    """
    Test scenario: List content has no text type items
    Expected: Return empty string
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(
            role="user",
            content=cast(
                List[MessageContentItem],
                [
                    {
                        "type": "image_url",
                        "image_url": {"url": "http://example.com/img.jpg"},
                    },
                ],
            ),
        )
    ]

    result = _get_latest_user_text(messages)

    assert result == ""


def test_get_latest_user_text_list_content_empty_list():
    """
    Test scenario: Content is an empty list
    Expected: Return empty string
    """
    from api_utils.utils import _get_latest_user_text

    messages = [Message(role="user", content=[])]

    result = _get_latest_user_text(messages)

    assert result == ""


def test_get_latest_user_text_content_is_none():
    """
    Test scenario: Content is None (though unusual)
    Expected: Return empty string
    """
    from api_utils.utils import _get_latest_user_text

    # Directly construct a case where content is None (bypass Pydantic validation)
    class MockMessage:
        def __init__(self):
            self.role = "user"
            self.content = None

    messages = [MockMessage()]

    result = _get_latest_user_text(cast(List[Message], messages))

    # Function will enter else branch and return ""
    assert result == ""


def test_get_latest_user_text_unicode_content():
    """
    Test scenario: Content contains Unicode characters
    Expected: Correctly handle Unicode characters
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(role="user", content="hello world"),
        Message(role="assistant", content="Response"),
        Message(role="user", content="latest message"),
    ]

    result = _get_latest_user_text(messages)

    assert result == "latest message"


def test_get_latest_user_text_multiline_string():
    """
    Test scenario: Content is multiline string
    Expected: Return full multiline string
    """
    from api_utils.utils import _get_latest_user_text

    multiline = """Line 1
Line 2
Line 3"""

    messages = [Message(role="user", content=multiline)]

    result = _get_latest_user_text(messages)

    assert result == multiline


def test_get_latest_user_text_reversed_iteration():
    """
    Test scenario: Verify function iterates messages backwards
    Expected: Should return the last user message even if there are other user messages before it
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(role="user", content="Old message 1"),
        Message(role="user", content="Old message 2"),
        Message(role="assistant", content="Response"),
        Message(role="user", content="Latest message"),
    ]

    result = _get_latest_user_text(messages)

    assert result == "Latest message"


def test_get_latest_user_text_special_characters():
    """
    Test scenario: Content contains special characters
    Expected: Correctly preserve special characters
    """
    from api_utils.utils import _get_latest_user_text

    messages = [
        Message(
            role="user",
            content="Text with \"quotes\" and 'apostrophes' and \\backslashes\\",
        )
    ]

    result = _get_latest_user_text(messages)

    assert result == "Text with \"quotes\" and 'apostrophes' and \\backslashes\\"


def test_get_latest_user_text_very_long_content():
    """
    Test scenario: Very long content (performance test)
    Expected: Able to handle large text
    """
    from api_utils.utils import _get_latest_user_text

    # Create a 10,000 character long text
    long_text = "A" * 10000

    messages = [Message(role="user", content=long_text)]

    result = _get_latest_user_text(messages)

    assert result == long_text
    assert len(result) == 10000