File size: 3,249 Bytes
d77e99f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2223b1
d77e99f
a2223b1
 
 
d77e99f
a2223b1
d77e99f
 
 
 
 
 
 
227ab66
d77e99f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a455a4
d77e99f
 
 
 
 
 
227ab66
 
 
d77e99f
 
 
066c678
 
 
 
227ab66
d77e99f
987c4be
 
d77e99f
227ab66
987c4be
 
 
 
d77e99f
 
227ab66
 
 
 
 
d77e99f
 
987c4be
d77e99f
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
"""Smoke tests for Gradio app."""

from __future__ import annotations

from unittest.mock import MagicMock, patch


def test_app_module_imports() -> None:
    """App module imports without side effects."""
    # This should not launch the app or make network calls
    from stroke_deepisles_demo.ui import app

    assert hasattr(app, "create_app")
    assert hasattr(app, "get_demo")


def test_create_app_returns_blocks() -> None:
    """create_app returns a gr.Blocks instance."""
    import gradio as gr

    from stroke_deepisles_demo.ui.app import create_app

    # No mock needed - create_case_selector is now lazy (empty dropdown)
    # Data loading happens via demo.load() after UI renders
    app = create_app()

    assert isinstance(app, gr.Blocks)


def test_viewer_module_imports() -> None:
    """Viewer module imports without errors."""
    from stroke_deepisles_demo.ui import viewer

    assert hasattr(viewer, "render_3panel_view")
    # create_niivue_html was removed


def test_components_module_imports() -> None:
    """Components module imports without errors."""
    from stroke_deepisles_demo.ui import components

    assert hasattr(components, "create_case_selector")
    assert hasattr(components, "create_results_display")


def test_run_segmentation_logic() -> None:
    """Test run_segmentation logic with mocks."""
    from stroke_deepisles_demo.pipeline import PipelineResult
    from stroke_deepisles_demo.ui.app import run_segmentation

    mock_result = PipelineResult(
        case_id="sub-001",
        input_files={"dwi": MagicMock(), "adc": MagicMock()},
        results_dir=MagicMock(),
        prediction_mask=MagicMock(),
        ground_truth=MagicMock(),
        dice_score=0.85,
        elapsed_seconds=10.5,
    )

    # prediction_mask.exists() needs to be True for mask_url to be generated
    mock_result.prediction_mask.exists.return_value = True  # type: ignore[attr-defined]

    # Mock everything that touches files/network
    with (
        patch("stroke_deepisles_demo.ui.app.run_pipeline_on_case", return_value=mock_result),
        patch(
            "stroke_deepisles_demo.ui.app.nifti_to_gradio_url",
            return_value="/gradio_api/file=/tmp/test.nii.gz",
        ),
        # create_niivue_html mock removed
        patch("stroke_deepisles_demo.ui.app.render_slice_comparison", return_value=MagicMock()),
        patch("stroke_deepisles_demo.ui.app.render_3panel_view", return_value=MagicMock()),
        patch("stroke_deepisles_demo.ui.app.compute_volume_ml", return_value=15.5),
    ):
        niivue_data, _fig, _ortho, metrics, _dl_path, status, _new_results_dir = run_segmentation(
            "sub-001",
            fast_mode=True,
            show_ground_truth=True,
            previous_results_dir=None,  # No previous results in test
        )

        # Assertion updated for Custom Component dictionary output
        assert niivue_data == {
            "background_url": "/gradio_api/file=/tmp/test.nii.gz",
            "overlay_url": "/gradio_api/file=/tmp/test.nii.gz",
        }
        assert metrics["case_id"] == "sub-001"
        assert metrics["dice_score"] == 0.85
        assert "volume_ml" in metrics  # New metric added
        assert "Success" in status