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
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
6a28f91
 
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
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
"""Unit tests for Ripeness classification system.

Tests ripeness classification logic, threshold configuration, priority adjustments,
and ripening time estimation.
"""

from datetime import date, datetime, timedelta

import pytest

from src.core.case import Case
from src.core.ripeness import RipenessClassifier, RipenessStatus


@pytest.mark.unit
class TestRipenessClassification:
    """Test basic ripeness classification."""

    def test_ripe_case_classification(self, ripe_case):
        """Test that properly serviced case with hearings is classified as RIPE."""
        status = RipenessClassifier.classify(ripe_case, datetime(2024, 3, 1))

        assert status == RipenessStatus.RIPE
        assert status.is_ripe() is True
        assert status.is_unripe() is False

    def test_unripe_summons_classification(self, unripe_case):
        """Test that case with pending summons is UNRIPE_SUMMONS."""
        status = RipenessClassifier.classify(unripe_case, datetime(2024, 2, 1))

        assert status == RipenessStatus.UNRIPE_SUMMONS
        assert status.is_ripe() is False
        assert status.is_unripe() is True

    def test_unripe_dependent_classification(self):
        """Test UNRIPE_DEPENDENT status (stay/pending cases)."""
        case = Case(
            case_id="STAY-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            hearing_count=2,
        )
        case.purpose_of_hearing = "STAY APPLICATION PENDING"
        case.service_status = "SERVED"

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        assert status == RipenessStatus.UNRIPE_DEPENDENT
        assert status.is_unripe() is True

    def test_unripe_party_classification(self):
        """Test UNRIPE_PARTY status (party non-appearance)."""
        case = Case(
            case_id="PARTY-001",
            case_type="CRP",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            hearing_count=3,
        )
        case.purpose_of_hearing = "APPEARANCE OF PARTIES"
        case.service_status = "SERVED"

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Should be UNRIPE_PARTY or similar
        assert status.is_unripe() is True

    def test_unripe_document_classification(self):
        """Test UNRIPE_DOCUMENT status (documents pending)."""
        case = Case(
            case_id="DOC-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="EVIDENCE",
            hearing_count=5,
        )
        case.purpose_of_hearing = "FOR PRODUCTION OF DOCUMENTS"
        case.service_status = "SERVED"
        case.compliance_status = "PENDING"

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        assert status == RipenessStatus.UNRIPE_DOCUMENT or status.is_unripe()

    def test_unknown_status(self):
        """Test UNKNOWN status for ambiguous cases."""
        case = Case(
            case_id="UNKNOWN-001",
            case_type="MISC.CVL",
            filed_date=date(2024, 1, 1),
            current_stage="OTHER",
            hearing_count=0,
        )
        # No clear indicators
        case.service_status = None
        case.purpose_of_hearing = None

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Should be UNKNOWN or not RIPE
        assert status == RipenessStatus.UNKNOWN or not status.is_ripe()


@pytest.mark.unit
class TestRipenessKeywords:
    """Test keyword-based ripeness detection."""

    def test_summons_keywords(self):
        """Test detection of summons-related keywords."""
        keywords = ["SUMMONS", "NOTICE", "ISSUE", "SERVICE"]

        for keyword in keywords:
            case = Case(
                case_id=f"KEYWORD-{keyword}",
                case_type="RSA",
                filed_date=date(2024, 1, 1),
                current_stage="PRE-ADMISSION",
                hearing_count=1,
            )
            case.purpose_of_hearing = f"FOR {keyword}"

            status = RipenessClassifier.classify(case, datetime(2024, 2, 1))
            assert status.is_unripe(), f"Keyword '{keyword}' should mark case as unripe"

    def test_ripe_keywords(self):
        """Test detection of ripe-indicating keywords."""
        ripe_keywords = ["ARGUMENTS", "HEARING", "FINAL", "JUDGMENT"]

        for keyword in ripe_keywords:
            case = Case(
                case_id=f"RIPE-{keyword}",
                case_type="RSA",
                filed_date=date(2024, 1, 1),
                current_stage="ARGUMENTS",
                hearing_count=5,
            )
            case.service_status = "SERVED"
            case.purpose_of_hearing = keyword

            status = RipenessClassifier.classify(case, datetime(2024, 2, 1))
            # With proper service and hearings, should be RIPE
            assert status.is_ripe() or status == RipenessStatus.RIPE

    def test_conflicting_keywords(self):
        """Test case with both ripe and unripe keywords."""
        case = Case(
            case_id="CONFLICT-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
            hearing_count=3,
        )
        case.purpose_of_hearing = "ARGUMENTS - PENDING SUMMONS"
        case.service_status = "PARTIAL"

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Unripe indicators should dominate
        assert status.is_unripe()


@pytest.mark.unit
class TestRipenessThresholds:
    """Test ripeness classification thresholds."""

    def test_min_service_hearings_threshold(self):
        """Test MIN_SERVICE_HEARINGS threshold (default 3)."""
        # Get current thresholds
        original_thresholds = RipenessClassifier.get_current_thresholds()
        min_hearings = original_thresholds.get("MIN_SERVICE_HEARINGS", 3)

        # Case with exactly min_hearings - 1 (should be unripe or unknown)
        case_below = Case(
            case_id="BELOW-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            hearing_count=min_hearings - 1,
        )
        case_below.service_status = "SERVED"

        # Case with exactly min_hearings (should have better chance of being ripe)
        case_at = Case(
            case_id="AT-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
            hearing_count=min_hearings,
        )
        case_at.service_status = "SERVED"
        case_at.purpose_of_hearing = "ARGUMENTS"

        status_below = RipenessClassifier.classify(case_below, datetime(2024, 2, 1))
        status_at = RipenessClassifier.classify(case_at, datetime(2024, 2, 1))

        # Case at threshold with ripe indicators should be more likely RIPE
        assert not status_below.is_ripe() or status_at.is_ripe()

    def test_threshold_configuration(self):
        """Test getting and setting thresholds."""
        original_thresholds = RipenessClassifier.get_current_thresholds()

        # Set new threshold
        new_thresholds = {"MIN_SERVICE_HEARINGS": 5}
        RipenessClassifier.set_thresholds(new_thresholds)

        # Verify update
        updated_thresholds = RipenessClassifier.get_current_thresholds()
        assert updated_thresholds["MIN_SERVICE_HEARINGS"] == 5

        # Restore original
        RipenessClassifier.set_thresholds(original_thresholds)
        restored = RipenessClassifier.get_current_thresholds()
        assert restored == original_thresholds

    def test_multiple_threshold_updates(self):
        """Test updating multiple thresholds at once."""
        original_thresholds = RipenessClassifier.get_current_thresholds()

        new_thresholds = {"MIN_SERVICE_HEARINGS": 4, "MIN_STAGE_DAYS": 10}
        RipenessClassifier.set_thresholds(new_thresholds)

        updated = RipenessClassifier.get_current_thresholds()
        assert updated["MIN_SERVICE_HEARINGS"] == 4
        assert updated["MIN_STAGE_DAYS"] == 10

        # Restore
        RipenessClassifier.set_thresholds(original_thresholds)


@pytest.mark.unit
class TestRipenessPriority:
    """Test ripeness priority adjustments."""

    def test_ripe_priority_multiplier(self):
        """Test that RIPE cases get priority boost (1.5x)."""
        case = Case(
            case_id="RIPE-PRI-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
            hearing_count=5,
        )
        case.service_status = "SERVED"
        case.purpose_of_hearing = "ARGUMENTS"

        priority = RipenessClassifier.get_ripeness_priority(case, datetime(2024, 2, 1))

        # RIPE cases should get 1.5 multiplier
        assert priority >= 1.0  # At least 1.0, ideally 1.5

    def test_unripe_priority_multiplier(self):
        """Test that UNRIPE cases get priority penalty (0.7x)."""
        case = Case(
            case_id="UNRIPE-PRI-001",
            case_type="CRP",
            filed_date=date(2024, 1, 1),
            current_stage="PRE-ADMISSION",
            hearing_count=1,
        )
        case.service_status = "PENDING"
        case.purpose_of_hearing = "FOR SUMMONS"

        priority = RipenessClassifier.get_ripeness_priority(case, datetime(2024, 2, 1))

        # UNRIPE cases should get 0.7 multiplier (less than 1.0)
        assert priority < 1.0


@pytest.mark.unit
class TestRipenessSchedulability:
    """Test is_schedulable logic."""

    def test_ripe_case_schedulable(self, ripe_case):
        """Test that RIPE case is schedulable."""
        schedulable = RipenessClassifier.is_schedulable(ripe_case, datetime(2024, 3, 1))

        assert schedulable is True

    def test_unripe_case_not_schedulable(self, unripe_case):
        """Test that UNRIPE case is not schedulable."""
        schedulable = RipenessClassifier.is_schedulable(
            unripe_case, datetime(2024, 2, 1)
        )

        assert schedulable is False

    def test_disposed_case_not_schedulable(self, disposed_case):
        """Test that disposed case is not schedulable."""
        schedulable = RipenessClassifier.is_schedulable(
            disposed_case, datetime(2024, 6, 1)
        )

        assert schedulable is False

    def test_recent_hearing_not_schedulable(self):
        """Test that case with recent hearing is not schedulable."""
        case = Case(
            case_id="RECENT-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
            hearing_count=5,
        )
        case.service_status = "SERVED"

        # Hearing yesterday
        case.record_hearing(date(2024, 2, 14), was_heard=True, outcome="HEARD")

        # Should not be schedulable (too soon)
        schedulable = RipenessClassifier.is_schedulable(case, datetime(2024, 2, 15))

        assert schedulable is False


@pytest.mark.unit
class TestRipenessExplanations:
    """Test ripeness reason explanations."""

    def test_ripe_reason(self):
        """Test explanation for RIPE status."""
        reason = RipenessClassifier.get_ripeness_reason(RipenessStatus.RIPE)

        assert isinstance(reason, str)
        assert len(reason) > 0
        assert "ready" in reason.lower() or "ripe" in reason.lower()

    def test_unripe_summons_reason(self):
        """Test explanation for UNRIPE_SUMMONS."""
        reason = RipenessClassifier.get_ripeness_reason(RipenessStatus.UNRIPE_SUMMONS)

        assert isinstance(reason, str)
        assert "summons" in reason.lower() or "service" in reason.lower()

    def test_unripe_dependent_reason(self):
        """Test explanation for UNRIPE_DEPENDENT."""
        reason = RipenessClassifier.get_ripeness_reason(RipenessStatus.UNRIPE_DEPENDENT)

        assert isinstance(reason, str)
        assert (
            "dependent" in reason.lower()
            or "stay" in reason.lower()
            or "pending" in reason.lower()
        )

    def test_unknown_reason(self):
        """Test explanation for UNKNOWN status."""
        reason = RipenessClassifier.get_ripeness_reason(RipenessStatus.UNKNOWN)

        assert isinstance(reason, str)
        assert "unknown" in reason.lower() or "unclear" in reason.lower()


@pytest.mark.unit
class TestRipeningTimeEstimation:
    """Test ripening time estimation."""

    def test_already_ripe_no_estimation(self, ripe_case):
        """Test that RIPE cases return None for ripening time."""
        estimate = RipenessClassifier.estimate_ripening_time(
            ripe_case, datetime(2024, 3, 1)
        )

        assert estimate is None

    def test_summons_ripening_time(self):
        """Test estimated time for summons cases (~30 days)."""
        case = Case(
            case_id="EST-SUMMONS-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="PRE-ADMISSION",
            hearing_count=1,
        )
        case.purpose_of_hearing = "FOR SUMMONS"

        estimate = RipenessClassifier.estimate_ripening_time(case, datetime(2024, 2, 1))

        if estimate is not None:
            assert isinstance(estimate, timedelta)
            # Summons typically ~30 days
            assert 20 <= estimate.days <= 45

    def test_dependent_ripening_time(self):
        """Test estimated time for dependent cases (~60 days)."""
        case = Case(
            case_id="EST-DEP-001",
            case_type="CRP",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            hearing_count=2,
        )
        case.purpose_of_hearing = "STAY APPLICATION"
        case.service_status = "SERVED"

        estimate = RipenessClassifier.estimate_ripening_time(case, datetime(2024, 2, 1))

        if estimate is not None:
            assert isinstance(estimate, timedelta)
            # Dependent cases typically longer
            assert estimate.days >= 30


@pytest.mark.edge_case
class TestRipenessEdgeCases:
    """Test ripeness edge cases."""

    def test_case_with_no_hearings(self):
        """Test classification of case with zero hearings."""
        case = Case(
            case_id="ZERO-HEAR-001",
            case_type="CP",
            filed_date=date(2024, 1, 1),
            current_stage="PRE-ADMISSION",
            hearing_count=0,
        )

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Should be UNKNOWN or UNRIPE (not enough evidence)
        assert not status.is_ripe()

    def test_case_with_null_service_status(self):
        """Test case with missing service status."""
        case = Case(
            case_id="NULL-SERVICE-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            hearing_count=3,
        )
        case.service_status = None

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Should handle gracefully (UNKNOWN or conservative classification)
        assert status in list(RipenessStatus)

    def test_case_in_unknown_stage(self):
        """Test case in unrecognized stage."""
        case = Case(
            case_id="UNKNOWN-STAGE-001",
            case_type="MISC.CVL",
            filed_date=date(2024, 1, 1),
            current_stage="UNKNOWN_STAGE",
            hearing_count=5,
        )
        case.service_status = "SERVED"

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Should handle gracefully
        assert status in list(RipenessStatus)

    def test_very_old_case(self):
        """Test classification of very old case (5+ years)."""
        case = Case(
            case_id="OLD-001",
            case_type="RSA",
            filed_date=date(2019, 1, 1),
            current_stage="EVIDENCE",
            hearing_count=50,
        )
        case.service_status = "SERVED"
        case.purpose_of_hearing = "EVIDENCE"

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Age shouldn't prevent proper classification
        assert status in list(RipenessStatus)

    def test_case_with_100_hearings(self):
        """Test case with very high hearing count."""
        from tests.conftest import create_case_with_hearings

        case = create_case_with_hearings(n_hearings=100, days_between=10)
        case.service_status = "SERVED"
        case.current_stage = "ARGUMENTS"

        status = RipenessClassifier.classify(case, datetime(2024, 6, 1))

        # High hearing count + proper service = RIPE
        assert status.is_ripe()


@pytest.mark.failure
class TestRipenessFailureScenarios:
    """Test ripeness failure scenarios."""

    def test_null_case(self):
        """Test handling of None case."""
        with pytest.raises(AttributeError):
            RipenessClassifier.classify(None, datetime(2024, 2, 1))

    def test_invalid_ripeness_status(self):
        """Test that only valid RipenessStatus values are used."""
        case = Case(
            case_id="VALID-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            hearing_count=3,
        )

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Should be a valid RipenessStatus enum value
        assert status in list(RipenessStatus)
        assert hasattr(status, "is_ripe")
        assert hasattr(status, "is_unripe")

    def test_threshold_invalid_type(self):
        """Test setting thresholds with invalid types."""
        original_thresholds = RipenessClassifier.get_current_thresholds()

        # Try setting invalid threshold
        try:
            RipenessClassifier.set_thresholds({"MIN_SERVICE_HEARINGS": "invalid"})
            # If it doesn't raise, just restore and continue
        except (TypeError, ValueError):
            # Expected behavior
            pass
        finally:
            # Always restore
            RipenessClassifier.set_thresholds(original_thresholds)

    def test_missing_required_case_fields(self):
        """Test classification with minimal case data."""
        case = Case(
            case_id="MINIMAL-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )
        # Don't set any optional fields

        status = RipenessClassifier.classify(case, datetime(2024, 2, 1))

        # Should handle gracefully and return some status
        assert status in list(RipenessStatus)