File size: 30,261 Bytes
67e153c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#!/usr/bin/env python3
"""
Comprehensive tests for LLM Fallback System in VoucherBot

This test suite is designed to challenge the LLM fallback system's ability
to handle complex, ambiguous, and edge case queries that the regex-based
system cannot process effectively.
"""

import unittest
import sys
import os
import json
import time
from unittest.mock import Mock, patch

# Add the parent directory to the path to import modules
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from llm_fallback_router import (
    LLMFallbackRouter, 
    InvalidInputError, 
    LLMProcessingError, 
    InvalidLLMResponseError,
    IntentType,
    RouterResponse
)


class MockLLMClient:
    """Enhanced mock LLM client for comprehensive testing"""
    
    def __init__(self, response_mode="normal", fail_mode=None, delay=0):
        self.response_mode = response_mode
        self.fail_mode = fail_mode
        self.delay = delay
        self.call_count = 0
        self.call_history = []
        
    def generate(self, prompt: str) -> str:
        """Generate mock responses based on test configuration"""
        self.call_count += 1
        self.call_history.append(prompt)
        
        if self.delay > 0:
            time.sleep(self.delay)
            
        if self.fail_mode == "exception":
            raise Exception("Mock LLM client failure")
        elif self.fail_mode == "invalid_json":
            return "This is not valid JSON"
        elif self.fail_mode == "malformed_response":
            return '{"intent": "INVALID_INTENT", "parameters": "not_a_dict"}'
        elif self.fail_mode == "timeout":
            time.sleep(10)  # Simulate timeout
            return self._generate_normal_response(prompt)
        elif self.fail_mode == "partial_response":
            return '{"intent": "SEARCH_LISTINGS"'  # Incomplete JSON
        
        return self._generate_normal_response(prompt)
    
    def _generate_normal_response(self, prompt: str) -> str:
        """Generate realistic mock responses based on prompt content"""
        # Extract message from prompt
        message_start = prompt.find('Message: "') + 10
        message_end = prompt.find('"', message_start)
        message = prompt[message_start:message_end] if message_start > 9 else ""
        
        message_lower = message.lower()
        
        # Sophisticated pattern matching for realistic responses
        if any(phrase in message_lower for phrase in ["find", "search", "looking for", "need apartment"]):
            return json.dumps({
                "intent": "SEARCH_LISTINGS",
                "parameters": {
                    "borough": self._extract_borough(message_lower),
                    "bedrooms": self._extract_bedrooms(message_lower),
                    "max_rent": self._extract_rent(message_lower),
                    "voucher_type": self._extract_voucher(message_lower)
                },
                "reasoning": "User is requesting to search for apartment listings with specific criteria"
            })
        
        elif any(phrase in message_lower for phrase in ["what if", "try", "instead", "change to"]):
            return json.dumps({
                "intent": "REFINE_SEARCH",
                "parameters": {
                    "borough": self._extract_borough(message_lower),
                    "bedrooms": self._extract_bedrooms(message_lower),
                    "max_rent": self._extract_rent(message_lower),
                    "voucher_type": self._extract_voucher(message_lower)
                },
                "reasoning": "User wants to modify their existing search parameters"
            })
        
        elif any(phrase in message_lower for phrase in ["violation", "safe", "building", "inspect"]):
            return json.dumps({
                "intent": "CHECK_VIOLATIONS",
                "parameters": {},
                "reasoning": "User wants to check building safety violations"
            })
        
        elif any(phrase in message_lower for phrase in ["help", "assist", "what can", "how do"]):
            return json.dumps({
                "intent": "HELP_REQUEST",
                "parameters": {},
                "reasoning": "User is requesting help or information"
            })
        
        elif any(phrase in message_lower for phrase in ["what is", "explain", "tell me about"]) and \
             any(voucher in message_lower for voucher in ["section 8", "hasa", "cityfheps", "voucher"]):
            return json.dumps({
                "intent": "ASK_VOUCHER_SUPPORT",
                "parameters": {
                    "voucher_type": self._extract_voucher(message_lower)
                },
                "reasoning": "User is asking for information about voucher programs"
            })
        
        else:
            return json.dumps({
                "intent": "UNKNOWN",
                "parameters": {},
                "reasoning": "Unable to determine clear intent from the message"
            })
    
    def _extract_borough(self, message: str) -> str:
        """Extract borough from message"""
        boroughs = {
            "brooklyn": "Brooklyn", "bk": "Brooklyn",
            "manhattan": "Manhattan", "mnh": "Manhattan",
            "queens": "Queens", "qns": "Queens",
            "bronx": "Bronx", "bx": "Bronx",
            "staten island": "Staten Island", "si": "Staten Island"
        }
        
        for key, value in boroughs.items():
            if key in message:
                return value
        return None
    
    def _extract_bedrooms(self, message: str) -> int:
        """Extract bedroom count from message"""
        import re
        bedroom_match = re.search(r'(\d+)\s*(?:bed|br|bedroom)', message)
        if bedroom_match:
            return int(bedroom_match.group(1))
        elif "studio" in message:
            return 0
        return None
    
    def _extract_rent(self, message: str) -> int:
        """Extract rent amount from message"""
        import re
        rent_match = re.search(r'\$(\d+(?:,\d{3})*)', message)
        if rent_match:
            return int(rent_match.group(1).replace(',', ''))
        return None
    
    def _extract_voucher(self, message: str) -> str:
        """Extract voucher type from message"""
        if "section 8" in message or "section-8" in message:
            return "Section 8"
        elif "cityfheps" in message or "city fheps" in message:
            return "CityFHEPS"
        elif "hasa" in message:
            return "HASA"
        elif "voucher" in message:
            return "Housing Voucher"
        return None


class TestLLMFallbackSystem(unittest.TestCase):
    """Comprehensive test suite for LLM fallback system"""
    
    def setUp(self):
        """Set up test components"""
        self.mock_llm = MockLLMClient()
        self.router = LLMFallbackRouter(self.mock_llm, debug=True)
        
    def test_basic_functionality(self):
        """Test basic LLM fallback functionality"""
        
        test_cases = [
            {
                "message": "I need to find a 2-bedroom apartment in Brooklyn under $2500",
                "expected_intent": "SEARCH_LISTINGS",
                "expected_params": ["borough", "bedrooms", "max_rent"]
            },
            {
                "message": "What if I try Queens instead?",
                "expected_intent": "REFINE_SEARCH",
                "expected_params": ["borough"]
            },
            {
                "message": "Can you help me understand Section 8?",
                "expected_intent": "ASK_VOUCHER_SUPPORT",
                "expected_params": ["voucher_type"]
            },
            {
                "message": "I need help with this system",
                "expected_intent": "HELP_REQUEST",
                "expected_params": []
            }
        ]
        
        print("\n🧠 Testing Basic LLM Fallback Functionality")
        print("=" * 60)
        
        for test_case in test_cases:
            with self.subTest(message=test_case["message"]):
                result = self.router.route(test_case["message"])
                
                print(f"Message: '{test_case['message']}'")
                print(f"  Intent: {result['intent']}")
                print(f"  Parameters: {result['parameters']}")
                print(f"  Reasoning: {result['reasoning']}")
                
                self.assertEqual(result["intent"], test_case["expected_intent"])
                
                # Check that expected parameters are present (if any)
                for param in test_case["expected_params"]:
                    self.assertIn(param, result["parameters"])
                    self.assertIsNotNone(result["parameters"][param])
                
                print("  ✅ Test passed")
                print()

    def test_complex_ambiguous_queries(self):
        """Test LLM's ability to handle complex and ambiguous queries"""
        
        complex_queries = [
            {
                "message": "I'm not sure what I'm looking for but I need somewhere to live",
                "description": "Vague housing request"
            },
            {
                "message": "My current situation is complicated and I need housing assistance",
                "description": "Complex personal situation"
            },
            {
                "message": "Can you help me figure out what kind of apartment I can afford with my voucher?",
                "description": "Multi-part question with implicit search intent"
            },
            {
                "message": "I've been looking everywhere but nothing seems right, maybe you can suggest something different",
                "description": "Frustration with search refinement request"
            },
            {
                "message": "The landlord said something about my voucher not being accepted, what should I do?",
                "description": "Problem-solving request with voucher context"
            },
            {
                "message": "I heard Brooklyn is good but expensive, what about other places that might work?",
                "description": "Comparative analysis request"
            },
            {
                "message": "My caseworker mentioned some options but I'm confused about the differences",
                "description": "Information clarification request"
            },
            {
                "message": "Is it worth looking in Manhattan or should I focus on outer boroughs?",
                "description": "Strategic advice request"
            }
        ]
        
        print("\n🌀 Testing Complex and Ambiguous Queries")
        print("=" * 60)
        
        for query_info in complex_queries:
            with self.subTest(message=query_info["message"]):
                result = self.router.route(query_info["message"])
                
                print(f"Query: '{query_info['message']}'")
                print(f"Description: {query_info['description']}")
                print(f"  Intent: {result['intent']}")
                print(f"  Parameters: {result['parameters']}")
                print(f"  Reasoning: {result['reasoning']}")
                
                # These should not be UNKNOWN if LLM is working properly
                self.assertNotEqual(result["intent"], "UNKNOWN", 
                                  f"LLM failed to classify complex query: {query_info['message']}")
                
                # Reasoning should be provided
                self.assertIsNotNone(result["reasoning"])
                self.assertNotEqual(result["reasoning"].strip(), "")
                
                print("  ✅ Successfully handled complex query")
                print()

    def test_multilingual_support(self):
        """Test LLM's multilingual capabilities"""
        
        multilingual_queries = [
            {
                "message": "Necesito encontrar un apartamento en Brooklyn",
                "language": "es",
                "expected_intent": "SEARCH_LISTINGS",
                "description": "Spanish apartment search"
            },
            {
                "message": "¿Qué es Section 8?",
                "language": "es", 
                "expected_intent": "ASK_VOUCHER_SUPPORT",
                "description": "Spanish voucher information request"
            },
            {
                "message": "我需要在布鲁克林找房子",
                "language": "zh",
                "expected_intent": "SEARCH_LISTINGS",
                "description": "Chinese apartment search"
            },
            {
                "message": "আমার ব্রুকলিনে একটি অ্যাপার্টমেন্ট দরকার",
                "language": "bn",
                "expected_intent": "SEARCH_LISTINGS", 
                "description": "Bengali apartment search"
            },
            {
                "message": "Help me find housing - ayuda por favor",
                "language": "mixed",
                "expected_intent": "SEARCH_LISTINGS",
                "description": "Mixed language request"
            }
        ]
        
        print("\n🌍 Testing Multilingual Support")
        print("=" * 60)
        
        for query_info in multilingual_queries:
            with self.subTest(message=query_info["message"]):
                result = self.router.route(
                    query_info["message"], 
                    language=query_info["language"]
                )
                
                print(f"Query: '{query_info['message']}'")
                print(f"Language: {query_info['language']}")
                print(f"Description: {query_info['description']}")
                print(f"  Intent: {result['intent']}")
                print(f"  Parameters: {result['parameters']}")
                print(f"  Reasoning: {result['reasoning']}")
                
                # Should handle multilingual queries appropriately
                self.assertNotEqual(result["intent"], "UNKNOWN", 
                                  f"LLM failed to handle multilingual query: {query_info['message']}")
                
                print("  ✅ Successfully handled multilingual query")
                print()

    def test_context_awareness(self):
        """Test LLM's ability to use context for better classification"""
        
        context_tests = [
            {
                "message": "try something else",
                "context": '{"borough": "Brooklyn", "bedrooms": 2, "max_rent": 2500}',
                "expected_intent": "REFINE_SEARCH",
                "description": "Vague refinement with search context"
            },
            {
                "message": "what about Manhattan?",
                "context": '{"last_search": "Brooklyn apartments", "results": 5}',
                "expected_intent": "REFINE_SEARCH",
                "description": "Borough change with search history"
            },
            {
                "message": "show me more",
                "context": '{"current_listings": 3, "total_available": 15}',
                "expected_intent": "SEARCH_LISTINGS",
                "description": "Continuation request with listings context"
            },
            {
                "message": "that's too expensive",
                "context": '{"last_shown_rent": 3000, "user_budget": 2500}',
                "expected_intent": "REFINE_SEARCH",
                "description": "Budget feedback with price context"
            }
        ]
        
        print("\n🧠 Testing Context Awareness")
        print("=" * 60)
        
        for test_case in context_tests:
            with self.subTest(message=test_case["message"]):
                result = self.router.route(test_case["message"], test_case["context"])
                
                print(f"Message: '{test_case['message']}'")
                print(f"Context: {test_case['context']}")
                print(f"Description: {test_case['description']}")
                print(f"  Intent: {result['intent']}")
                print(f"  Parameters: {result['parameters']}")
                print(f"  Reasoning: {result['reasoning']}")
                
                # Context should improve classification
                self.assertNotEqual(result["intent"], "UNKNOWN",
                                  f"LLM failed to use context for: {test_case['message']}")
                
                print("  ✅ Successfully used context")
                print()

    def test_error_handling_and_recovery(self):
        """Test LLM fallback system's error handling and recovery"""
        
        error_scenarios = [
            {
                "fail_mode": "invalid_json",
                "description": "Invalid JSON response from LLM"
            },
            {
                "fail_mode": "malformed_response", 
                "description": "Malformed response structure"
            },
            {
                "fail_mode": "partial_response",
                "description": "Incomplete JSON response"
            },
            {
                "fail_mode": "exception",
                "description": "LLM client exception"
            }
        ]
        
        print("\n🚨 Testing Error Handling and Recovery")
        print("=" * 60)
        
        for scenario in error_scenarios:
            with self.subTest(fail_mode=scenario["fail_mode"]):
                # Create router with failing mock LLM
                failing_llm = MockLLMClient(fail_mode=scenario["fail_mode"])
                failing_router = LLMFallbackRouter(failing_llm, debug=True)
                
                print(f"Scenario: {scenario['description']}")
                
                with self.assertRaises((LLMProcessingError, InvalidLLMResponseError)):
                    failing_router.route("Find apartments in Brooklyn")
                
                print("  ✅ Properly raised expected exception")
                print()

    def test_retry_mechanism(self):
        """Test the retry mechanism for failed LLM calls"""
        
        print("\n🔄 Testing Retry Mechanism")
        print("=" * 60)
        
        # Create a mock that fails twice then succeeds
        class RetryTestLLM:
            def __init__(self):
                self.attempt_count = 0
                
            def generate(self, prompt):
                self.attempt_count += 1
                if self.attempt_count <= 2:
                    raise Exception(f"Attempt {self.attempt_count} failed")
                return json.dumps({
                    "intent": "SEARCH_LISTINGS",
                    "parameters": {"borough": "Brooklyn"},
                    "reasoning": f"Succeeded on attempt {self.attempt_count}"
                })
        
        retry_llm = RetryTestLLM()
        retry_router = LLMFallbackRouter(retry_llm, debug=True, max_retries=3)
        
        result = retry_router.route("Find apartments in Brooklyn")
        
        print(f"Total attempts made: {retry_llm.attempt_count}")
        print(f"Final result: {result}")
        
        self.assertEqual(retry_llm.attempt_count, 3)
        self.assertEqual(result["intent"], "SEARCH_LISTINGS")
        
        print("  ✅ Retry mechanism worked correctly")
        print()

    def test_performance_under_load(self):
        """Test LLM fallback performance under various load conditions"""
        
        print("\n⚡ Testing Performance Under Load")
        print("=" * 60)
        
        # Test with different response delays
        delay_tests = [
            {"delay": 0, "description": "Instant response"},
            {"delay": 0.1, "description": "Fast response (100ms)"},
            {"delay": 0.5, "description": "Moderate response (500ms)"},
            {"delay": 1.0, "description": "Slow response (1s)"}
        ]
        
        for delay_test in delay_tests:
            with self.subTest(delay=delay_test["delay"]):
                delayed_llm = MockLLMClient(delay=delay_test["delay"])
                delayed_router = LLMFallbackRouter(delayed_llm, debug=False)
                
                start_time = time.time()
                result = delayed_router.route("Find apartments in Brooklyn")
                end_time = time.time()
                
                actual_time = end_time - start_time
                expected_time = delay_test["delay"]
                
                print(f"Test: {delay_test['description']}")
                print(f"  Expected delay: {expected_time}s")
                print(f"  Actual time: {actual_time:.3f}s")
                print(f"  Result: {result['intent']}")
                
                # Allow for some overhead but should be close to expected
                self.assertGreaterEqual(actual_time, expected_time)
                self.assertLess(actual_time, expected_time + 0.5)  # Max 500ms overhead
                
                print("  ✅ Performance within acceptable range")
                print()

    def test_parameter_normalization(self):
        """Test parameter normalization and validation"""
        
        normalization_tests = [
            {
                "input_params": {"borough": "bk", "bedrooms": "2", "max_rent": "2,500"},
                "expected_borough": "Brooklyn",
                "expected_bedrooms": 2,
                "expected_rent": 2500,
                "description": "Abbreviation and string normalization"
            },
            {
                "input_params": {"borough": "staten island", "voucher_type": "section 8"},
                "expected_borough": "Staten Island", 
                "expected_voucher": "Section 8",
                "description": "Multi-word and voucher normalization"
            },
            {
                "input_params": {"borough": "manhattan", "bedrooms": 0},
                "expected_borough": "Manhattan",
                "expected_bedrooms": 0,
                "description": "Studio apartment (0 bedrooms)"
            }
        ]
        
        print("\n🔧 Testing Parameter Normalization")
        print("=" * 60)
        
        for test_case in normalization_tests:
            with self.subTest(description=test_case["description"]):
                # Create a custom mock response with test parameters
                class CustomMockLLM:
                    def generate(self, prompt):
                        return json.dumps({
                            "intent": "SEARCH_LISTINGS",
                            "parameters": test_case["input_params"],
                            "reasoning": "Test normalization"
                        })
                
                custom_router = LLMFallbackRouter(CustomMockLLM())
                result = custom_router.route("Test message")
                
                print(f"Test: {test_case['description']}")
                print(f"  Input params: {test_case['input_params']}")
                print(f"  Normalized params: {result['parameters']}")
                
                # Check normalization
                if "expected_borough" in test_case:
                    self.assertEqual(result["parameters"]["borough"], test_case["expected_borough"])
                
                if "expected_bedrooms" in test_case:
                    self.assertEqual(result["parameters"]["bedrooms"], test_case["expected_bedrooms"])
                
                if "expected_rent" in test_case:
                    self.assertEqual(result["parameters"]["max_rent"], test_case["expected_rent"])
                
                if "expected_voucher" in test_case:
                    self.assertEqual(result["parameters"]["voucher_type"], test_case["expected_voucher"])
                
                print("  ✅ Parameters normalized correctly")
                print()

    def test_edge_cases_and_boundary_conditions(self):
        """Test edge cases and boundary conditions"""
        
        edge_cases = [
            {
                "message": "",
                "description": "Empty message"
            },
            {
                "message": "   ",
                "description": "Whitespace only"
            },
            {
                "message": "a" * 10000,
                "description": "Very long message"
            },
            {
                "message": "🏠🏡🏘️🏚️🏗️",
                "description": "Emoji only"
            },
            {
                "message": "!@#$%^&*()_+{}[]|\\:;\"'<>?,./",
                "description": "Special characters only"
            },
            {
                "message": "find apartments" + "\n" * 100,
                "description": "Message with many newlines"
            }
        ]
        
        print("\n🔧 Testing Edge Cases and Boundary Conditions")
        print("=" * 60)
        
        for edge_case in edge_cases:
            with self.subTest(message=edge_case["description"]):
                try:
                    if edge_case["message"] == "":
                        # Empty message should raise InvalidInputError
                        with self.assertRaises(InvalidInputError):
                            self.router.route(edge_case["message"])
                        print(f"Test: {edge_case['description']}")
                        print("  ✅ Correctly raised InvalidInputError for empty message")
                    else:
                        result = self.router.route(edge_case["message"])
                        print(f"Test: {edge_case['description']}")
                        print(f"  Intent: {result['intent']}")
                        print(f"  Parameters: {result['parameters']}")
                        print("  ✅ Handled edge case without error")
                    
                except Exception as e:
                    print(f"Test: {edge_case['description']}")
                    print(f"  ❌ Unexpected error: {e}")
                    self.fail(f"Edge case caused unexpected error: {e}")
                
                print()

    def test_regression_scenarios(self):
        """Test known regression scenarios and previously problematic queries"""
        
        regression_tests = [
            {
                "message": "I live in Brooklyn but work in Manhattan",
                "description": "Multiple borough mentions",
                "expected_behavior": "Should not extract both boroughs"
            },
            {
                "message": "My 3 kids need a place to live",
                "description": "Family size vs bedroom count",
                "expected_behavior": "Should not extract '3' as bedrooms"
            },
            {
                "message": "I make $50,000 per year",
                "description": "Annual income vs monthly rent",
                "expected_behavior": "Should not extract as max_rent"
            },
            {
                "message": "Section 8 is a good program",
                "description": "Informational statement vs request",
                "expected_behavior": "Should not be SEARCH_LISTINGS"
            },
            {
                "message": "What if I told you I need help?",
                "description": "Hypothetical vs what-if scenario",
                "expected_behavior": "Should be HELP_REQUEST, not REFINE_SEARCH"
            }
        ]
        
        print("\n🔍 Testing Regression Scenarios")
        print("=" * 60)
        
        for test_case in regression_tests:
            with self.subTest(message=test_case["message"]):
                result = self.router.route(test_case["message"])
                
                print(f"Message: '{test_case['message']}'")
                print(f"Description: {test_case['description']}")
                print(f"Expected behavior: {test_case['expected_behavior']}")
                print(f"  Intent: {result['intent']}")
                print(f"  Parameters: {result['parameters']}")
                print(f"  Reasoning: {result['reasoning']}")
                
                # Verify the specific expected behavior
                if "Multiple borough mentions" in test_case["description"]:
                    # Should not extract both Brooklyn and Manhattan
                    borough = result["parameters"].get("borough")
                    if borough:
                        self.assertIn(borough, ["Brooklyn", "Manhattan"])
                        print(f"  ✅ Correctly extracted single borough: {borough}")
                    else:
                        print("  ✅ Correctly extracted no borough")
                
                elif "Family size vs bedroom count" in test_case["description"]:
                    # Should not extract 3 as bedrooms
                    bedrooms = result["parameters"].get("bedrooms")
                    self.assertNotEqual(bedrooms, 3)
                    print("  ✅ Correctly did not extract family size as bedrooms")
                
                elif "Annual income vs monthly rent" in test_case["description"]:
                    # Should not extract 50000 as max_rent
                    max_rent = result["parameters"].get("max_rent")
                    self.assertNotEqual(max_rent, 50000)
                    print("  ✅ Correctly did not extract annual income as rent")
                
                elif "Informational statement vs request" in test_case["description"]:
                    # Should not be SEARCH_LISTINGS
                    self.assertNotEqual(result["intent"], "SEARCH_LISTINGS")
                    print("  ✅ Correctly did not classify as search request")
                
                elif "Hypothetical vs what-if scenario" in test_case["description"]:
                    # Should be HELP_REQUEST, not REFINE_SEARCH
                    self.assertEqual(result["intent"], "HELP_REQUEST")
                    print("  ✅ Correctly classified as help request")
                
                print()


if __name__ == "__main__":
    # Run the tests with verbose output
    unittest.main(verbosity=2, buffer=True)