File size: 15,318 Bytes
eadbc29
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a28f91
eadbc29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Unit tests for Case entity and lifecycle management.

Tests case creation, hearing management, scoring, state transitions, and edge cases.
"""

from datetime import date, timedelta

import pytest

from src.core.case import Case, CaseStatus


@pytest.mark.unit
class TestCaseCreation:
    """Test case initialization and basic properties."""

    def test_create_basic_case(self):
        """Test creating a case with minimal required fields."""
        case = Case(
            case_id="TEST-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        assert case.case_id == "TEST-001"
        assert case.case_type == "RSA"
        assert case.filed_date == date(2024, 1, 1)
        assert case.current_stage == "ADMISSION"
        assert case.status == CaseStatus.PENDING
        assert case.hearing_count == 0
        assert case.age_days >= 0

    def test_case_with_all_fields(self):
        """Test creating a case with all fields populated."""
        case = Case(
            case_id="FULL-001",
            case_type="CRP",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
            last_hearing_date=date(2024, 2, 15),
            age_days=100,
            hearing_count=5,
            status=CaseStatus.ACTIVE,
            is_urgent=True,
        )

        assert case.last_hearing_date == date(2024, 2, 15)
        assert case.age_days == 100
        assert case.hearing_count == 5
        assert case.status == CaseStatus.ACTIVE
        assert case.is_urgent is True

    @pytest.mark.edge_case
    def test_case_filed_today(self):
        """Test case filed today (age should be 0)."""
        today = date.today()
        case = Case(
            case_id="NEW-001",
            case_type="CP",
            filed_date=today,
            current_stage="PRE-ADMISSION",
        )

        case.update_age(today)
        assert case.age_days == 0
        assert (case.age_days / 365) == 0

    @pytest.mark.failure
    def test_invalid_case_type(self):
        """Test that invalid case types are handled."""
        # Note: Current implementation may not validate, but test documents expected behavior
        case = Case(
            case_id="INVALID-001",
            case_type="INVALID_TYPE",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )
        # Case is created but type validation could be added in future
        assert case.case_type == "INVALID_TYPE"


@pytest.mark.unit
class TestCaseAgeCalculation:
    """Test age and time-based calculations."""

    def test_age_calculation(self):
        """Test age_days calculation."""
        case = Case(
            case_id="AGE-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        # Update age to Feb 1 (31 days later)
        case.update_age(date(2024, 2, 1))
        assert case.age_days == 31

    def test_age_in_years(self):
        """Test age conversion to years."""
        case = Case(
            case_id="OLD-001",
            case_type="RSA",
            filed_date=date(2022, 1, 1),
            current_stage="EVIDENCE",
        )

        case.update_age(date(2024, 1, 1))
        assert (case.age_days / 365) == 2.0

    def test_days_since_last_hearing(self):
        """Test calculation of gap since last hearing."""
        case = Case(
            case_id="GAP-001",
            case_type="CRP",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        # Record hearing on Jan 15
        case.record_hearing(date(2024, 1, 15), was_heard=True, outcome="HEARD")

        # Update to Feb 1
        case.update_age(date(2024, 2, 1))
        assert case.days_since_last_hearing == 17


@pytest.mark.unit
class TestHearingManagement:
    """Test hearing recording and history."""

    def test_record_single_hearing(self):
        """Test recording a single hearing."""
        case = Case(
            case_id="HEAR-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        case.record_hearing(date(2024, 1, 15), was_heard=True, outcome="ARGUMENTS")

        assert case.hearing_count == 1
        assert case.last_hearing_date == date(2024, 1, 15)


@pytest.mark.unit
class TestStageProgression:
    """Test case stage transitions."""

    def test_progress_to_next_stage(self):
        """Test progressing case to next stage."""
        case = Case(
            case_id="PROG-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        case.progress_to_stage("EVIDENCE", date(2024, 2, 1))

        assert case.current_stage == "EVIDENCE"

    def test_progress_to_terminal_stage(self):
        """Test progressing to terminal stage (ORDERS/JUDGMENT)."""
        case = Case(
            case_id="TERM-001",
            case_type="CP",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
        )

        case.progress_to_stage("ORDERS", date(2024, 3, 1))

        assert case.current_stage == "ORDERS"

    def test_stage_sequence(self):
        """Test typical stage progression sequence."""
        case = Case(
            case_id="SEQ-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="PRE-ADMISSION",
        )

        stages = ["ADMISSION", "EVIDENCE", "ARGUMENTS", "ORDERS"]
        current_date = date(2024, 1, 1)

        for stage in stages:
            current_date += timedelta(days=60)
            case.progress_to_stage(stage, current_date)
            assert case.current_stage == stage


@pytest.mark.unit
class TestCaseScoring:
    """Test case priority and readiness scoring."""

    def test_priority_score_calculation(self):
        """Test overall priority score computation."""
        case = Case(
            case_id="SCORE-001",
            case_type="RSA",
            filed_date=date(2023, 1, 1),
            current_stage="ARGUMENTS",
        )

        case.update_age(date(2024, 1, 1))  # 1 year old
        case.record_hearing(date(2023, 12, 1), was_heard=True, outcome="HEARD")
        case.update_age(date(2024, 1, 1))

        priority = case.get_priority_score()

        assert isinstance(priority, float)
        assert 0.0 <= priority <= 1.0

    def test_readiness_score_components(self):
        """Test readiness score calculation with different components."""
        case = Case(
            case_id="READY-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
        )

        # Add some hearings
        for i in range(10):
            case.record_hearing(
                date(2024, 1, 1) + timedelta(days=30 * i),
                was_heard=True,
                outcome="HEARD",
            )

        readiness = case.compute_readiness_score()

        assert isinstance(readiness, float)
        assert 0.0 <= readiness <= 1.0

    def test_urgency_boost(self):
        """Test that urgent cases get priority boost."""
        normal_case = Case(
            case_id="NORMAL-001",
            case_type="CP",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            is_urgent=False,
        )

        urgent_case = Case(
            case_id="URGENT-001",
            case_type="CP",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            is_urgent=True,
        )

        # Update ages to same date
        test_date = date(2024, 2, 1)
        normal_case.update_age(test_date)
        urgent_case.update_age(test_date)

        # Urgent case should have higher priority
        assert urgent_case.get_priority_score() > normal_case.get_priority_score()

    def test_adjournment_boost(self):
        """Test that recently adjourned cases get priority boost."""
        case = Case(
            case_id="ADJ-BOOST-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
        )

        # Record adjourned hearing
        case.record_hearing(date(2024, 2, 1), was_heard=False, outcome="ADJOURNED")

        # Priority should be higher shortly after adjournment
        case.update_age(date(2024, 2, 5))
        case.get_priority_score()

        # Priority boost should decay over time
        case.update_age(date(2024, 3, 1))
        case.get_priority_score()

        # Note: This test assumes adjournment boost exists and decays
        # Implementation may vary


@pytest.mark.unit
class TestCaseReadiness:
    """Test case readiness for scheduling."""

    def test_ready_for_scheduling(self):
        """Test case that is ready for scheduling."""
        case = Case(
            case_id="READY-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
        )

        # Record hearing 30 days ago
        case.record_hearing(date(2024, 1, 15), was_heard=True, outcome="HEARD")
        case.update_age(date(2024, 2, 15))

        # Should be ready (30 days > 7 day min gap)
        assert case.is_ready_for_scheduling(min_gap_days=7) is True

    def test_not_ready_min_gap(self):
        """Test case that doesn't meet minimum gap requirement."""
        case = Case(
            case_id="NOT-READY-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        # Record hearing 3 days ago
        case.record_hearing(date(2024, 2, 10), was_heard=True, outcome="HEARD")
        case.update_age(date(2024, 2, 13))

        # Should not be ready (3 days < 7 day min gap)
        assert case.is_ready_for_scheduling(min_gap_days=7) is False

    def test_first_hearing_always_ready(self):
        """Test that case with no hearings is ready for first scheduling."""
        case = Case(
            case_id="FIRST-001",
            case_type="CP",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        case.update_age(date(2024, 1, 15))

        # Should be ready for first hearing
        assert case.is_ready_for_scheduling(min_gap_days=7) is True


@pytest.mark.unit
class TestCaseStatus:
    """Test case status transitions."""

    def test_initial_status_pending(self):
        """Test that new cases start as PENDING."""
        case = Case(
            case_id="STATUS-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="PRE-ADMISSION",
        )

        assert case.status == CaseStatus.PENDING

    def test_mark_disposed(self):
        """Test marking case as disposed."""
        case = Case(
            case_id="DISPOSE-001",
            case_type="CP",
            filed_date=date(2024, 1, 1),
            current_stage="ORDERS",
        )

        case.status = CaseStatus.DISPOSED

        assert case.is_disposed() is True

    def test_disposed_case_properties(self):
        """Test that disposed cases have expected properties."""
        from tests.conftest import disposed_case

        case = disposed_case()

        assert case.status == CaseStatus.DISPOSED
        assert case.is_disposed() is True


@pytest.mark.unit
class TestCaseSerialization:
    """Test case conversion and serialization."""

    def test_to_dict(self):
        """Test converting case to dictionary."""
        case = Case(
            case_id="DICT-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            hearing_count=3,
        )

        case_dict = case.to_dict()

        assert isinstance(case_dict, dict)
        assert case_dict["case_id"] == "DICT-001"
        assert case_dict["case_type"] == "RSA"
        assert case_dict["current_stage"] == "ADMISSION"
        assert case_dict["hearing_count"] == 3

    def test_repr(self):
        """Test case string representation."""
        case = Case(
            case_id="REPR-001",
            case_type="CRP",
            filed_date=date(2024, 1, 1),
            current_stage="ARGUMENTS",
        )

        repr_str = repr(case)

        assert "REPR-001" in repr_str
        assert "CRP" in repr_str


@pytest.mark.edge_case
class TestCaseEdgeCases:
    """Test edge cases and boundary conditions."""

    def test_case_with_null_fields(self):
        """Test case with optional fields set to None."""
        case = Case(
            case_id="NULL-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
            last_hearing_date=None,
            is_urgent=None,
        )

        assert case.last_hearing_date is None
        assert case.is_urgent is None or case.is_urgent is False

    def test_case_age_boundary(self):
        """Test case at exact age boundaries (0, 1 year, 2 years)."""
        case = Case(
            case_id="BOUNDARY-001",
            case_type="RSA",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        # Exactly 0 days
        case.update_age(date(2024, 1, 1))
        assert case.age_days == 0

        # Exactly 365 days
        case.update_age(date(2025, 1, 1))
        assert case.age_days == 365
        assert (case.age_days / 365) == 1.0

        # Exactly 730 days
        case.update_age(date(2026, 1, 1))
        assert case.age_days == 730
        assert (case.age_days / 365) == 2.0

    def test_hearing_on_case_filed_date(self):
        """Test recording hearing on same day case was filed."""
        case = Case(
            case_id="SAME-DAY-001",
            case_type="CP",
            filed_date=date(2024, 1, 1),
            current_stage="ADMISSION",
        )

        # Record hearing on filed date
        case.record_hearing(date(2024, 1, 1), was_heard=True, outcome="ADMISSION")

        assert case.hearing_count == 1
        assert case.last_hearing_date == date(2024, 1, 1)


@pytest.mark.failure
class TestCaseFailureScenarios:
    """Test failure scenarios and error handling."""

    def test_future_filed_date(self):
        """Test case filed in the future (should be invalid)."""
        future_date = date.today() + timedelta(days=365)

        case = Case(
            case_id="FUTURE-001",
            case_type="RSA",
            filed_date=future_date,
            current_stage="ADMISSION",
        )

        # Case is created but update_age should handle gracefully
        case.update_age(date.today())
        # age_days might be negative or handled specially

    def test_disposed_case_operations(self):
        """Test that disposed cases handle operations appropriately."""
        case = Case(
            case_id="DISPOSED-OPS-001",
            case_type="CP",
            filed_date=date(2024, 1, 1),
            current_stage="ORDERS",
            status=CaseStatus.DISPOSED,
        )

        # Should still be able to query properties
        assert case.is_disposed() is True

        # Recording hearing on disposed case (implementation dependent)
        # Some implementations might allow, others might not