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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
"""Tests for app/components/ — Streamlit UI components.

Each component is a pure function that takes data and returns a render-spec dict
(not actual Streamlit widgets) so we can test without a running Streamlit app.

Components:
  1. file_uploader   - render_file_uploader_spec
  2. profile_card    - render_profile_card
  3. trial_card      - render_trial_card
  4. gap_card        - render_gap_card
  5. progress_tracker - render_progress_tracker
  6. disclaimer_banner - DISCLAIMER_TEXT, render_disclaimer_spec
"""

import pytest

from trialpath.models import (
    Biomarker,
    CriterionAssessment,
    CriterionDecision,
    Demographics,
    Diagnosis,
    EligibilityLedger,
    GapItem,
    OverallAssessment,
    PatientProfile,
    PerformanceStatus,
    Treatment,
    TrialCandidate,
    UnknownField,
)

# ---------------------------------------------------------------------------
# Fixtures — reusable model instances
# ---------------------------------------------------------------------------


@pytest.fixture
def sample_profile():
    return PatientProfile(
        patient_id="P001",
        demographics=Demographics(age=62, sex="Female"),
        diagnosis=Diagnosis(
            primary_condition="Non-Small Cell Lung Cancer",
            histology="adenocarcinoma",
            stage="IIIB",
        ),
        performance_status=PerformanceStatus(scale="ECOG", value=1),
        biomarkers=[
            Biomarker(name="EGFR", result="Exon 19 deletion"),
            Biomarker(name="ALK", result="Negative"),
            Biomarker(name="PD-L1", result="45%"),
        ],
        treatments=[
            Treatment(drug_name="Carboplatin", line=1),
            Treatment(drug_name="Pemetrexed", line=1),
        ],
        unknowns=[
            UnknownField(field="KRAS", reason="Not in records", importance="high"),
            UnknownField(field="Brain MRI", reason="Not available", importance="medium"),
        ],
    )


@pytest.fixture
def sample_trial():
    return TrialCandidate(
        nct_id="NCT04000001",
        title="KEYNOTE-999: Pembrolizumab + Chemo for NSCLC",
        conditions=["NSCLC"],
        phase="Phase 3",
        status="Recruiting",
        fingerprint_text="Pembrolizumab combination therapy advanced NSCLC",
    )


@pytest.fixture
def sample_ledger():
    return EligibilityLedger(
        patient_id="P001",
        nct_id="NCT04000001",
        overall_assessment=OverallAssessment.UNCERTAIN,
        criteria=[
            CriterionAssessment(
                criterion_id="inc_1",
                type="inclusion",
                text="Confirmed NSCLC diagnosis",
                decision=CriterionDecision.MET,
            ),
            CriterionAssessment(
                criterion_id="inc_2",
                type="inclusion",
                text="ECOG 0-1",
                decision=CriterionDecision.MET,
            ),
            CriterionAssessment(
                criterion_id="inc_3",
                type="inclusion",
                text="PD-L1 >= 50%",
                decision=CriterionDecision.NOT_MET,
            ),
            CriterionAssessment(
                criterion_id="exc_1",
                type="exclusion",
                text="No prior immunotherapy",
                decision=CriterionDecision.UNKNOWN,
            ),
        ],
        gaps=[
            GapItem(
                description="Brain MRI results needed",
                recommended_action="Upload brain MRI report",
                clinical_importance="high",
            ),
        ],
    )


# ===========================================================================
# 1. file_uploader
# ===========================================================================


class TestFileUploader:
    def test_spec_has_accepted_types(self):
        from app.components.file_uploader import render_file_uploader_spec

        spec = render_file_uploader_spec()
        assert "pdf" in spec["accepted_types"]
        assert "png" in spec["accepted_types"]
        assert "jpg" in spec["accepted_types"]
        assert "jpeg" in spec["accepted_types"]

    def test_spec_allows_multiple_files(self):
        from app.components.file_uploader import render_file_uploader_spec

        spec = render_file_uploader_spec()
        assert spec["accept_multiple_files"] is True

    def test_spec_has_label(self):
        from app.components.file_uploader import render_file_uploader_spec

        spec = render_file_uploader_spec()
        assert "label" in spec
        assert len(spec["label"]) > 0

    def test_format_file_info(self):
        from app.components.file_uploader import format_file_info

        result = format_file_info("report.pdf", 245_000)
        assert "report.pdf" in result
        assert "KB" in result

    def test_format_file_info_large_file(self):
        from app.components.file_uploader import format_file_info

        result = format_file_info("scan.pdf", 2_500_000)
        assert "MB" in result


# ===========================================================================
# 2. profile_card
# ===========================================================================


class TestProfileCard:
    def test_renders_demographics(self, sample_profile):
        from app.components.profile_card import render_profile_card

        card = render_profile_card(sample_profile)
        assert card["demographics"]["age"] == 62
        assert card["demographics"]["sex"] == "Female"

    def test_renders_diagnosis(self, sample_profile):
        from app.components.profile_card import render_profile_card

        card = render_profile_card(sample_profile)
        assert (
            "NSCLC" in card["diagnosis"]["primary_condition"]
            or "Non-Small Cell" in card["diagnosis"]["primary_condition"]
        )
        assert card["diagnosis"]["stage"] == "IIIB"
        assert card["diagnosis"]["histology"] == "adenocarcinoma"

    def test_renders_biomarkers(self, sample_profile):
        from app.components.profile_card import render_profile_card

        card = render_profile_card(sample_profile)
        assert len(card["biomarkers"]) == 3
        names = [b["name"] for b in card["biomarkers"]]
        assert "EGFR" in names
        assert "PD-L1" in names

    def test_renders_treatments(self, sample_profile):
        from app.components.profile_card import render_profile_card

        card = render_profile_card(sample_profile)
        assert len(card["treatments"]) == 2
        assert card["treatments"][0]["drug_name"] == "Carboplatin"

    def test_renders_unknowns(self, sample_profile):
        from app.components.profile_card import render_profile_card

        card = render_profile_card(sample_profile)
        assert len(card["unknowns"]) == 2
        fields = [u["field"] for u in card["unknowns"]]
        assert "KRAS" in fields
        assert "Brain MRI" in fields

    def test_renders_performance_status(self, sample_profile):
        from app.components.profile_card import render_profile_card

        card = render_profile_card(sample_profile)
        assert card["performance_status"]["scale"] == "ECOG"
        assert card["performance_status"]["value"] == 1

    def test_handles_none_diagnosis(self):
        from app.components.profile_card import render_profile_card

        profile = PatientProfile(patient_id="P002")
        card = render_profile_card(profile)
        assert card["diagnosis"] is None

    def test_handles_empty_biomarkers(self):
        from app.components.profile_card import render_profile_card

        profile = PatientProfile(patient_id="P003")
        card = render_profile_card(profile)
        assert card["biomarkers"] == []

    def test_has_minimum_prescreen_flag(self, sample_profile):
        from app.components.profile_card import render_profile_card

        card = render_profile_card(sample_profile)
        assert card["has_minimum_prescreen_data"] is True

    def test_has_minimum_prescreen_flag_false(self):
        from app.components.profile_card import render_profile_card

        profile = PatientProfile(patient_id="P004")
        card = render_profile_card(profile)
        assert card["has_minimum_prescreen_data"] is False


# ===========================================================================
# 3. trial_card
# ===========================================================================


class TestTrialCard:
    def test_renders_basic_info(self, sample_trial, sample_ledger):
        from app.components.trial_card import render_trial_card

        card = render_trial_card(sample_trial, sample_ledger)
        assert card["nct_id"] == "NCT04000001"
        assert "KEYNOTE" in card["title"]
        assert card["phase"] == "Phase 3"
        assert card["status"] == "Recruiting"

    def test_renders_traffic_light(self, sample_trial, sample_ledger):
        from app.components.trial_card import render_trial_card

        card = render_trial_card(sample_trial, sample_ledger)
        assert card["traffic_light"] == "yellow"

    def test_renders_criteria_counts(self, sample_trial, sample_ledger):
        from app.components.trial_card import render_trial_card

        card = render_trial_card(sample_trial, sample_ledger)
        assert card["met_count"] == 2
        assert card["not_met_count"] == 1
        assert card["unknown_count"] == 1

    def test_renders_criteria_list(self, sample_trial, sample_ledger):
        from app.components.trial_card import render_trial_card

        card = render_trial_card(sample_trial, sample_ledger)
        assert len(card["criteria"]) == 4
        first = card["criteria"][0]
        assert "criterion_id" in first
        assert "text" in first
        assert "decision" in first

    def test_renders_overall_assessment(self, sample_trial, sample_ledger):
        from app.components.trial_card import render_trial_card

        card = render_trial_card(sample_trial, sample_ledger)
        assert card["overall_assessment"] == "uncertain"

    def test_green_traffic_light(self, sample_trial):
        from app.components.trial_card import render_trial_card

        ledger = EligibilityLedger(
            patient_id="P001",
            nct_id="NCT04000001",
            overall_assessment=OverallAssessment.LIKELY_ELIGIBLE,
            criteria=[
                CriterionAssessment(
                    criterion_id="inc_1",
                    type="inclusion",
                    text="Has NSCLC",
                    decision=CriterionDecision.MET,
                ),
            ],
        )
        card = render_trial_card(sample_trial, ledger)
        assert card["traffic_light"] == "green"

    def test_red_traffic_light(self, sample_trial):
        from app.components.trial_card import render_trial_card

        ledger = EligibilityLedger(
            patient_id="P001",
            nct_id="NCT04000001",
            overall_assessment=OverallAssessment.LIKELY_INELIGIBLE,
            criteria=[
                CriterionAssessment(
                    criterion_id="inc_1",
                    type="inclusion",
                    text="Has NSCLC",
                    decision=CriterionDecision.NOT_MET,
                ),
            ],
        )
        card = render_trial_card(sample_trial, ledger)
        assert card["traffic_light"] == "red"

    def test_gaps_included(self, sample_trial, sample_ledger):
        from app.components.trial_card import render_trial_card

        card = render_trial_card(sample_trial, sample_ledger)
        assert len(card["gaps"]) == 1
        assert card["gaps"][0]["description"] == "Brain MRI results needed"


# ===========================================================================
# 4. gap_card
# ===========================================================================


class TestGapCard:
    def test_renders_single_gap(self):
        from app.components.gap_card import render_gap_card

        gap = GapItem(
            description="Brain MRI results needed",
            recommended_action="Upload brain MRI report",
            clinical_importance="high",
        )
        card = render_gap_card(gap, affected_trials=["NCT04000001", "NCT04000003"])
        assert card["description"] == "Brain MRI results needed"
        assert card["recommended_action"] == "Upload brain MRI report"
        assert card["clinical_importance"] == "high"
        assert len(card["affected_trials"]) == 2

    def test_renders_gap_without_affected_trials(self):
        from app.components.gap_card import render_gap_card

        gap = GapItem(
            description="KRAS mutation status",
            recommended_action="Request test from oncologist",
            clinical_importance="medium",
        )
        card = render_gap_card(gap)
        assert card["affected_trials"] == []

    def test_importance_badge(self):
        from app.components.gap_card import render_gap_card

        gap = GapItem(
            description="Test gap",
            recommended_action="Do something",
            clinical_importance="high",
        )
        card = render_gap_card(gap)
        assert card["importance_color"] == "red"

    def test_importance_badge_medium(self):
        from app.components.gap_card import render_gap_card

        gap = GapItem(
            description="Test gap",
            recommended_action="Do something",
            clinical_importance="medium",
        )
        card = render_gap_card(gap)
        assert card["importance_color"] == "orange"

    def test_importance_badge_low(self):
        from app.components.gap_card import render_gap_card

        gap = GapItem(
            description="Test gap",
            recommended_action="Do something",
            clinical_importance="low",
        )
        card = render_gap_card(gap)
        assert card["importance_color"] == "grey"


# ===========================================================================
# 5. progress_tracker
# ===========================================================================


class TestProgressTracker:
    def test_renders_all_states(self):
        from app.components.progress_tracker import render_progress_tracker

        spec = render_progress_tracker("INGEST")
        assert len(spec["steps"]) == 5

    def test_ingest_is_current(self):
        from app.components.progress_tracker import render_progress_tracker

        spec = render_progress_tracker("INGEST")
        steps = spec["steps"]
        assert steps[0]["status"] == "current"
        assert steps[1]["status"] == "upcoming"

    def test_middle_state(self):
        from app.components.progress_tracker import render_progress_tracker

        spec = render_progress_tracker("VALIDATE_TRIALS")
        steps = spec["steps"]
        assert steps[0]["status"] == "completed"
        assert steps[1]["status"] == "completed"
        assert steps[2]["status"] == "current"
        assert steps[3]["status"] == "upcoming"
        assert steps[4]["status"] == "upcoming"

    def test_summary_state(self):
        from app.components.progress_tracker import render_progress_tracker

        spec = render_progress_tracker("SUMMARY")
        steps = spec["steps"]
        for i in range(4):
            assert steps[i]["status"] == "completed"
        assert steps[4]["status"] == "current"

    def test_step_labels(self):
        from app.components.progress_tracker import render_progress_tracker

        spec = render_progress_tracker("INGEST")
        labels = [s["label"] for s in spec["steps"]]
        assert "Upload" in labels[0]
        assert "Summary" in labels[4] or "Export" in labels[4]

    def test_current_index(self):
        from app.components.progress_tracker import render_progress_tracker

        spec = render_progress_tracker("GAP_FOLLOWUP")
        assert spec["current_index"] == 3


# ===========================================================================
# 6. disclaimer_banner
# ===========================================================================


class TestDisclaimerBanner:
    def test_disclaimer_text_content(self):
        from app.components.disclaimer_banner import DISCLAIMER_TEXT

        text_lower = DISCLAIMER_TEXT.lower()
        assert "information" in text_lower or "educational" in text_lower
        assert "medical advice" in text_lower

    def test_disclaimer_spec(self):
        from app.components.disclaimer_banner import render_disclaimer_spec

        spec = render_disclaimer_spec()
        assert spec["type"] == "info"
        assert len(spec["text"]) > 0

    def test_disclaimer_not_empty(self):
        from app.components.disclaimer_banner import DISCLAIMER_TEXT

        assert len(DISCLAIMER_TEXT) > 20