-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_intent_agent.py
More file actions
606 lines (477 loc) · 23.3 KB
/
Copy pathtest_intent_agent.py
File metadata and controls
606 lines (477 loc) · 23.3 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
"""
Unit tests for RoadSense AI Intent & Context Agent (intent_agent.py)
Tests for filtering noise, sarcasm, and speculation from road-related signals.
"""
import json
import pytest
from unittest.mock import patch, MagicMock
from hypothesis import given, strategies as st, settings
# Mock bedrock_client before importing intent_agent
import sys
mock_bedrock = MagicMock()
sys.modules["bedrock_client"] = mock_bedrock
from agents.intent_agent import (
build_prompt,
parse_response,
process_signal,
process_signals,
lambda_handler,
_fallback_intent,
URGENCY_LEVELS,
CONTEXT_TYPES,
)
# ── Fixtures ──────────────────────────────────────────────────────────────────
@pytest.fixture
def valid_road_signal():
"""A road-related signal with classification."""
return {
"signal_id": "test-001",
"source": "social_media",
"source_name": "reddit",
"translated_content": "Huge pothole on MG Road near Indiranagar, my tire got punctured",
"classification": {
"is_road_related": True,
"damage_type": "pothole",
"confidence": 0.85,
},
}
@pytest.fixture
def non_road_signal():
"""A non-road-related signal."""
return {
"signal_id": "test-002",
"source": "social_media",
"source_name": "reddit",
"original_content": "The new restaurant on MG Road is great!",
"classification": {
"is_road_related": False,
"damage_type": "none",
"confidence": 0.95,
},
}
@pytest.fixture
def weather_signal():
"""A weather-based signal."""
return {
"signal_id": "test-003",
"source": "weather",
"source_name": "openweathermap",
"translated_content": "Heavy rainfall warning for Mumbai",
"classification": {
"is_road_related": True,
"damage_type": "flooding",
"confidence": 0.75,
},
}
@pytest.fixture
def news_signal():
"""A news article signal."""
return {
"signal_id": "test-004",
"source": "news",
"source_name": "times_of_india",
"translated_content": "Waterlogging near Andheri flyover after monsoon rains",
"classification": {
"is_road_related": True,
"damage_type": "flooding",
"confidence": 0.90,
},
}
@pytest.fixture
def valid_model_response():
"""A valid JSON response from Nova Micro."""
return json.dumps({
"is_problem_report": True,
"urgency_level": "high",
"context_type": "direct_report",
"confidence_modifier": 0.1,
"reasoning": "First-hand account with specific location detail."
})
@pytest.fixture
def sarcastic_model_response():
"""Nova Micro response identifying sarcasm."""
return json.dumps({
"is_problem_report": False,
"urgency_level": "low",
"context_type": "sarcasm",
"confidence_modifier": -0.3,
"reasoning": "Clearly sarcastic remark about road quality."
})
# ── Test build_prompt ─────────────────────────────────────────────────────────
class TestBuildPrompt:
"""Tests for the build_prompt function."""
def test_prompt_contains_text(self):
"""Prompt should contain the input text."""
text = "Big pothole near the school"
prompt = build_prompt(text, "reddit", "pothole")
assert text in prompt
def test_prompt_contains_source(self):
"""Prompt should contain the source type."""
prompt = build_prompt("Road damage", "twitter", "crack")
assert "twitter" in prompt
def test_prompt_contains_damage_type(self):
"""Prompt should contain the damage type."""
prompt = build_prompt("Flooded road", "news", "flooding")
assert "flooding" in prompt
def test_prompt_structure(self):
"""Prompt should have required JSON schema in instructions."""
prompt = build_prompt("Test", "test", "test")
assert "is_problem_report" in prompt
assert "urgency_level" in prompt
assert "context_type" in prompt
assert "confidence_modifier" in prompt
assert "reasoning" in prompt
def test_prompt_has_urgency_levels(self):
"""Prompt should document all urgency levels."""
prompt = build_prompt("Test", "test", "test")
for level in URGENCY_LEVELS:
assert level in prompt
def test_prompt_has_context_types(self):
"""Prompt should document context types."""
prompt = build_prompt("Test", "test", "test")
for ctx in ["direct_report", "sarcasm", "speculation"]:
assert ctx in prompt
@given(st.text(min_size=1, max_size=500))
@settings(max_examples=20)
def test_prompt_handles_any_text(self, text):
"""Prompt should handle arbitrary text input."""
prompt = build_prompt(text, "source", "damage")
assert isinstance(prompt, str)
assert len(prompt) > 0
# ── Test parse_response ───────────────────────────────────────────────────────
class TestParseResponse:
"""Tests for the parse_response function."""
def test_parse_valid_json(self, valid_model_response):
"""Should parse valid JSON response correctly."""
result = parse_response(valid_model_response)
assert result["is_problem_report"] is True
assert result["urgency_level"] == "high"
assert result["context_type"] == "direct_report"
assert result["confidence_modifier"] == 0.1
assert "First-hand" in result["reasoning"]
def test_parse_json_with_code_block(self):
"""Should handle response wrapped in markdown code blocks."""
response = '```json\n{"is_problem_report": true, "urgency_level": "medium", "context_type": "indirect_mention", "confidence_modifier": -0.1, "reasoning": "test"}\n```'
result = parse_response(response)
assert result["is_problem_report"] is True
assert result["urgency_level"] == "medium"
def test_parse_invalid_json_returns_fallback(self):
"""Should return fallback for invalid JSON."""
result = parse_response("This is not JSON")
assert result == _fallback_intent()
def test_parse_empty_response_returns_fallback(self):
"""Should return fallback for empty response."""
result = parse_response("")
assert result == _fallback_intent()
def test_invalid_urgency_level_defaults_to_low(self):
"""Should default to 'low' for invalid urgency level."""
response = json.dumps({
"is_problem_report": True,
"urgency_level": "invalid_level",
"context_type": "direct_report",
"confidence_modifier": 0.0,
"reasoning": "test"
})
result = parse_response(response)
assert result["urgency_level"] == "low"
def test_invalid_context_type_defaults_to_ambiguous(self):
"""Should default to 'ambiguous' for invalid context type."""
response = json.dumps({
"is_problem_report": True,
"urgency_level": "high",
"context_type": "invalid_type",
"confidence_modifier": 0.0,
"reasoning": "test"
})
result = parse_response(response)
assert result["context_type"] == "ambiguous"
def test_confidence_modifier_clamped_max(self):
"""Should clamp confidence modifier to max 0.2."""
response = json.dumps({
"is_problem_report": True,
"urgency_level": "high",
"context_type": "direct_report",
"confidence_modifier": 0.5, # Above max
"reasoning": "test"
})
result = parse_response(response)
assert result["confidence_modifier"] == 0.2
def test_confidence_modifier_clamped_min(self):
"""Should clamp confidence modifier to min -0.4."""
response = json.dumps({
"is_problem_report": False,
"urgency_level": "low",
"context_type": "sarcasm",
"confidence_modifier": -0.6, # Below min
"reasoning": "test"
})
result = parse_response(response)
assert result["confidence_modifier"] == -0.4
def test_missing_fields_use_defaults(self):
"""Should use defaults for missing optional fields."""
response = json.dumps({})
result = parse_response(response)
assert result["is_problem_report"] is True # Default
assert result["urgency_level"] == "low"
assert result["context_type"] == "ambiguous"
assert result["confidence_modifier"] == 0.0
assert result["reasoning"] == ""
@given(st.floats(min_value=-0.4, max_value=0.2))
@settings(max_examples=20)
def test_valid_confidence_modifiers_preserved(self, modifier):
"""Valid confidence modifiers should be preserved."""
response = json.dumps({
"is_problem_report": True,
"urgency_level": "medium",
"context_type": "direct_report",
"confidence_modifier": modifier,
"reasoning": "test"
})
result = parse_response(response)
assert abs(result["confidence_modifier"] - modifier) < 0.0001
# ── Test _fallback_intent ─────────────────────────────────────────────────────
class TestFallbackIntent:
"""Tests for the _fallback_intent function."""
def test_fallback_structure(self):
"""Fallback should have all required fields."""
fallback = _fallback_intent()
assert "is_problem_report" in fallback
assert "urgency_level" in fallback
assert "context_type" in fallback
assert "confidence_modifier" in fallback
assert "reasoning" in fallback
def test_fallback_is_conservative(self):
"""Fallback should be conservative (is_problem_report=True, low urgency)."""
fallback = _fallback_intent()
assert fallback["is_problem_report"] is True
assert fallback["urgency_level"] == "low"
assert fallback["context_type"] == "ambiguous"
assert fallback["confidence_modifier"] == 0.0
# ── Test process_signal ───────────────────────────────────────────────────────
class TestProcessSignal:
"""Tests for the process_signal function."""
def test_non_road_signal_skipped(self, non_road_signal):
"""Non-road signals should be skipped with appropriate intent."""
result = process_signal(non_road_signal)
assert result["intent"]["is_problem_report"] is False
assert "not road related" in result["intent"]["reasoning"].lower()
def test_weather_signal_pre_classified(self, weather_signal):
"""Weather signals should be pre-classified without Bedrock call."""
result = process_signal(weather_signal)
assert result["intent"]["is_problem_report"] is True
assert result["intent"]["context_type"] == "weather_alert"
assert result["intent"]["urgency_level"] == "medium"
def test_road_signal_calls_bedrock(self, valid_road_signal, valid_model_response):
"""Road signals should call Bedrock for classification."""
mock_bedrock.classify.return_value = valid_model_response
result = process_signal(valid_road_signal)
assert result["intent"]["is_problem_report"] is True
assert result["intent"]["urgency_level"] == "high"
mock_bedrock.classify.assert_called()
def test_confidence_modifier_applied(self, valid_road_signal, valid_model_response):
"""Confidence modifier should be applied to classification confidence."""
mock_bedrock.classify.return_value = valid_model_response
original_confidence = valid_road_signal["classification"]["confidence"]
result = process_signal(valid_road_signal)
# Model response has +0.1 modifier
expected_conf = min(0.99, max(0.01, original_confidence + 0.1))
assert abs(result["classification"]["confidence"] - expected_conf) < 0.01
def test_empty_content_uses_fallback(self, valid_road_signal):
"""Empty content should use fallback intent."""
valid_road_signal["translated_content"] = ""
valid_road_signal["original_content"] = " "
result = process_signal(valid_road_signal)
assert result["intent"]["context_type"] == "ambiguous"
assert result["intent"]["confidence_modifier"] == 0.0
def test_bedrock_error_uses_fallback(self, valid_road_signal):
"""Bedrock errors should use fallback intent."""
mock_bedrock.classify.side_effect = Exception("API Error")
result = process_signal(valid_road_signal)
assert result["intent"] == _fallback_intent()
mock_bedrock.classify.side_effect = None # Reset
def test_sarcasm_detection(self, valid_road_signal, sarcastic_model_response):
"""Sarcastic content should be identified."""
valid_road_signal["translated_content"] = "Oh wow, Bangalore roads are SO good!"
mock_bedrock.classify.return_value = sarcastic_model_response
result = process_signal(valid_road_signal)
assert result["intent"]["is_problem_report"] is False
assert result["intent"]["context_type"] == "sarcasm"
def test_news_source_processing(self, news_signal, valid_model_response):
"""News signals should be processed correctly."""
mock_bedrock.classify.return_value = valid_model_response
result = process_signal(news_signal)
assert "intent" in result
mock_bedrock.classify.assert_called()
def test_confidence_clamped_to_valid_range(self, valid_road_signal):
"""Adjusted confidence should be clamped between 0.01 and 0.99."""
# Response with large positive modifier
response = json.dumps({
"is_problem_report": True,
"urgency_level": "critical",
"context_type": "direct_report",
"confidence_modifier": 0.2,
"reasoning": "test"
})
mock_bedrock.classify.return_value = response
valid_road_signal["classification"]["confidence"] = 0.95
result = process_signal(valid_road_signal)
assert result["classification"]["confidence"] <= 0.99
def test_uses_translated_content_if_available(self, valid_road_signal, valid_model_response):
"""Should prefer translated_content over original_content."""
valid_road_signal["translated_content"] = "Translated pothole report"
valid_road_signal["original_content"] = "Original Hindi text"
mock_bedrock.classify.return_value = valid_model_response
process_signal(valid_road_signal)
call_args = mock_bedrock.classify.call_args[0][0]
assert "Translated pothole report" in call_args
# ── Test process_signals ──────────────────────────────────────────────────────
class TestProcessSignals:
"""Tests for the process_signals batch function."""
def test_processes_multiple_signals(self, valid_road_signal, weather_signal, valid_model_response):
"""Should process multiple signals in batch."""
mock_bedrock.classify.return_value = valid_model_response
signals = [valid_road_signal, weather_signal]
results = process_signals(signals)
assert len(results) == 2
assert all("intent" in s for s in results)
def test_empty_batch_returns_empty(self):
"""Empty batch should return empty list."""
results = process_signals([])
assert results == []
def test_counts_problem_reports_correctly(self, valid_road_signal, non_road_signal, valid_model_response):
"""Should correctly count problem reports vs noise."""
mock_bedrock.classify.return_value = valid_model_response
signals = [valid_road_signal, non_road_signal]
results = process_signals(signals)
problem_count = sum(1 for s in results if s["intent"]["is_problem_report"])
noise_count = sum(1 for s in results if not s["intent"]["is_problem_report"])
assert problem_count == 1
assert noise_count == 1
# ── Test lambda_handler ───────────────────────────────────────────────────────
class TestLambdaHandler:
"""Tests for the AWS Lambda handler."""
def test_handler_with_signals(self, valid_road_signal, valid_model_response):
"""Handler should process signals and return count."""
mock_bedrock.classify.return_value = valid_model_response
event = {"signals": [valid_road_signal]}
response = lambda_handler(event, None)
assert response["statusCode"] == 200
assert response["count"] == 1
assert len(response["signals"]) == 1
def test_handler_empty_signals(self):
"""Handler should handle empty signals list."""
event = {"signals": []}
response = lambda_handler(event, None)
assert response["statusCode"] == 200
assert response["count"] == 0
assert response["signals"] == []
def test_handler_missing_signals_key(self):
"""Handler should handle missing signals key."""
event = {}
response = lambda_handler(event, None)
assert response["statusCode"] == 200
assert response["count"] == 0
def test_handler_returns_problem_report_count(self, valid_road_signal, weather_signal, valid_model_response):
"""Handler should return problem report count."""
mock_bedrock.classify.return_value = valid_model_response
event = {"signals": [valid_road_signal, weather_signal]}
response = lambda_handler(event, None)
assert "problem_report_count" in response
assert response["problem_report_count"] == 2 # Both should be problem reports
# ── Property-based tests ──────────────────────────────────────────────────────
class TestIntentProperties:
"""Property-based tests using Hypothesis."""
@given(st.sampled_from(URGENCY_LEVELS))
def test_urgency_levels_are_valid(self, level):
"""All urgency levels should be in the valid set."""
assert level in URGENCY_LEVELS
@given(st.sampled_from(CONTEXT_TYPES))
def test_context_types_are_valid(self, ctx_type):
"""All context types should be in the valid set."""
assert ctx_type in CONTEXT_TYPES
@given(
is_problem=st.booleans(),
urgency=st.sampled_from(URGENCY_LEVELS),
context=st.sampled_from(CONTEXT_TYPES),
modifier=st.floats(min_value=-0.4, max_value=0.2),
)
@settings(max_examples=30)
def test_valid_response_parsing_preserves_values(self, is_problem, urgency, context, modifier):
"""Valid responses should preserve all field values."""
response = json.dumps({
"is_problem_report": is_problem,
"urgency_level": urgency,
"context_type": context,
"confidence_modifier": modifier,
"reasoning": "test reasoning"
})
result = parse_response(response)
assert result["is_problem_report"] == is_problem
assert result["urgency_level"] == urgency
assert result["context_type"] == context
assert abs(result["confidence_modifier"] - modifier) < 0.0001
@given(st.floats(min_value=0.01, max_value=0.99))
@settings(max_examples=20)
def test_confidence_stays_in_valid_range(self, confidence):
"""Adjusted confidence should always be between 0.01 and 0.99."""
signal = {
"signal_id": "prop-test",
"source": "social_media",
"translated_content": "Test content",
"classification": {
"is_road_related": True,
"damage_type": "pothole",
"confidence": confidence,
},
}
response = json.dumps({
"is_problem_report": True,
"urgency_level": "high",
"context_type": "direct_report",
"confidence_modifier": 0.15,
"reasoning": "test"
})
mock_bedrock.classify.return_value = response
result = process_signal(signal)
assert 0.01 <= result["classification"]["confidence"] <= 0.99
# ── Edge case tests ───────────────────────────────────────────────────────────
class TestEdgeCases:
"""Tests for edge cases and boundary conditions."""
def test_signal_without_classification(self):
"""Signal without classification should be skipped."""
signal = {
"signal_id": "edge-001",
"source": "social_media",
"translated_content": "Pothole on my street",
}
result = process_signal(signal)
assert result["intent"]["is_problem_report"] is False
assert "not road related" in result["intent"]["reasoning"].lower()
def test_signal_with_empty_classification(self):
"""Signal with empty classification should be skipped."""
signal = {
"signal_id": "edge-002",
"source": "social_media",
"translated_content": "Road damage",
"classification": {},
}
result = process_signal(signal)
assert result["intent"]["is_problem_report"] is False
def test_unicode_content_handling(self, valid_road_signal, valid_model_response):
"""Should handle Unicode content (Hindi, emojis)."""
valid_road_signal["translated_content"] = "सड़क पर बड़ा गड्ढा है 🚗💥"
mock_bedrock.classify.return_value = valid_model_response
result = process_signal(valid_road_signal)
assert "intent" in result
def test_very_long_content(self, valid_road_signal, valid_model_response):
"""Should handle very long content."""
valid_road_signal["translated_content"] = "Pothole report. " * 500
mock_bedrock.classify.return_value = valid_model_response
result = process_signal(valid_road_signal)
assert "intent" in result
def test_special_characters_in_content(self, valid_road_signal, valid_model_response):
"""Should handle special characters in content."""
valid_road_signal["translated_content"] = 'Road damage: "quotes" & <brackets> \n\t tabs'
mock_bedrock.classify.return_value = valid_model_response
result = process_signal(valid_road_signal)
assert "intent" in result