๐Ÿ“ฆ EqualifyEverything / equalify-reflow

๐Ÿ“„ test_describe_image.py ยท 791 lines
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791"""Unit tests for the describe_image tool in page correction agents."""

from __future__ import annotations

import base64
from unittest.mock import AsyncMock, MagicMock, patch

import pytest

pytestmark = pytest.mark.unit

from src.services.pipeline_viewer import PipelineViewerService
from src.services.pipeline_viewer_models import (
    DocumentChange,
    FigureData,
    ImageDescriptionResult,
    LayoutType,
    OutlineEntry,
    PageAttributes,
    PageCorrectionResult,
    PipelineViewerResult,
    SectionCorrectionResult,
    StructureResult,
)


# Patch targets โ€” these are imported lazily inside methods
_BEDROCK_PATCH = "pydantic_ai.models.bedrock.BedrockConverseModel"
_AGENT_PATCH = "pydantic_ai.Agent"


@pytest.fixture
def service():
    return PipelineViewerService()


@pytest.fixture
def sample_figure():
    return FigureData(
        ref_id="figure-1.png",
        caption="Figure 1: Sample diagram",
        page_number=1,
        image_base64=base64.b64encode(b"fake-image-data").decode(),
    )


@pytest.fixture
def sample_figure_2():
    return FigureData(
        ref_id="figure-2.png",
        caption="Figure 2: Another diagram",
        page_number=2,
        image_base64=base64.b64encode(b"fake-image-data-2").decode(),
    )


@pytest.fixture
def base_result(sample_figure):
    return PipelineViewerResult(
        filename="test.pdf",
        total_pages=2,
        versions={"v0": "# Intro\n\n![](figures/figure-1.png)\n\nHello world"},
        page_images={"1": "AAAA", "2": "BBBB"},
        page_markdowns={
            "v0": {
                "1": "# Intro\n\n![](figures/figure-1.png)\n\nHello world",
                "2": "# Methods\n\nGoodbye",
            },
        },
        figures=[sample_figure],
        steps=[],
        stats={},
    )


@pytest.fixture
def structure_with_images():
    return StructureResult(
        page_attributes={
            1: PageAttributes(layout=LayoutType.SINGLE_COLUMN, has_images=True),
            2: PageAttributes(layout=LayoutType.SINGLE_COLUMN),
        },
        outline=[
            OutlineEntry(level=2, text="Intro", page=1),
            OutlineEntry(level=2, text="Methods", page=2),
        ],
        footnotes=[],
    )


@pytest.fixture
def structure_no_images():
    return StructureResult(
        page_attributes={
            1: PageAttributes(layout=LayoutType.SINGLE_COLUMN, has_images=False),
            2: PageAttributes(layout=LayoutType.SINGLE_COLUMN),
        },
        outline=[
            OutlineEntry(level=2, text="Intro", page=1),
        ],
        footnotes=[],
    )


def _make_mock_agent():
    """Create a mock Agent that tracks tool_plain registrations."""
    mock_agent = MagicMock()
    mock_agent._registered_tools = []

    def tool_plain(func=None, **kwargs):
        if func is not None:
            mock_agent._registered_tools.append(func.__name__)
            return func

        def decorator(f):
            mock_agent._registered_tools.append(f.__name__)
            return f
        return decorator

    mock_agent.tool_plain = tool_plain

    async def mock_run(user_parts, **kwargs):
        result = MagicMock()
        result.output = None
        usage = MagicMock()
        usage.request_tokens = 100
        usage.response_tokens = 50
        result.usage.return_value = usage
        return result

    mock_agent.run = AsyncMock(side_effect=mock_run)
    return mock_agent


class TestDescribeImageToolRegistration:
    """Test that describe_image is conditionally registered in page agent."""

    @pytest.mark.asyncio
    async def test_tool_registered_when_has_images_and_figures(
        self, service, structure_with_images, sample_figure
    ):
        """describe_image should be registered when has_images=True and figures exist."""
        mock_agent = _make_mock_agent()

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            await service._run_page_agent(
                page_num=1,
                page_markdown="# Intro\n\n![](figures/figure-1.png)\n\nHello",
                page_image_b64="AAAA",
                structure=structure_with_images,
                page_figures=[sample_figure],
            )

        assert "describe_image" in mock_agent._registered_tools

    @pytest.mark.asyncio
    async def test_tool_not_registered_when_no_figures(
        self, service, structure_no_images
    ):
        """describe_image should NOT be registered when no figures."""
        mock_agent = _make_mock_agent()

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            await service._run_page_agent(
                page_num=2,
                page_markdown="# Methods\n\nGoodbye",
                page_image_b64="BBBB",
                structure=structure_no_images,
            )

        assert "describe_image" not in mock_agent._registered_tools

    @pytest.mark.asyncio
    async def test_tool_not_registered_when_has_images_but_no_page_figures(
        self, service, structure_with_images
    ):
        """describe_image should NOT be registered when has_images but page_figures is None."""
        mock_agent = _make_mock_agent()

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            await service._run_page_agent(
                page_num=1,
                page_markdown="# Intro\n\n![](figures/figure-1.png)\n\nHello",
                page_image_b64="AAAA",
                structure=structure_with_images,
                page_figures=None,
            )

        assert "describe_image" not in mock_agent._registered_tools

    @pytest.mark.asyncio
    async def test_describer_token_fields_on_result(
        self, service, structure_with_images, sample_figure
    ):
        """Result should include describer token fields (0 when no actual calls)."""
        mock_agent = _make_mock_agent()

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            result = await service._run_page_agent(
                page_num=1,
                page_markdown="# Intro\n\n![](figures/figure-1.png)\n\nHello",
                page_image_b64="AAAA",
                structure=structure_with_images,
                page_figures=[sample_figure],
            )

        assert result.describer_input_tokens == 0
        assert result.describer_output_tokens == 0


class TestImageDescriberSubagent:
    """Test the _run_image_describer method."""

    @pytest.mark.asyncio
    async def test_successful_description(self, service, sample_figure):
        """Should return formatted alt text instruction."""
        mock_result = ImageDescriptionResult(
            image_category="informative",
            alt_text="Flowchart showing data processing steps",
            is_decorative=False,
            confidence="high",
            reasoning="Clear diagram with labeled boxes",
        )

        mock_agent_result = MagicMock()
        mock_agent_result.output = mock_result
        mock_usage = MagicMock()
        mock_usage.request_tokens = 500
        mock_usage.response_tokens = 100
        mock_agent_result.usage.return_value = mock_usage

        token_updates = []

        def track_tokens(inp, out):
            token_updates.append((inp, out))

        mock_agent = MagicMock()
        mock_agent.run = AsyncMock(return_value=mock_agent_result)

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            result = await service._run_image_describer(
                figure=sample_figure,
                current_markdown="# Intro\n\n![](figures/figure-1.png)\n\nHello",
                page_images={1: "AAAA"},
                describer_tokens={"input": 0, "output": 0},
                update_tokens=track_tokens,
            )

        assert "Flowchart showing data processing steps" in result
        assert "figure-1.png" in result
        assert token_updates == [(500, 100)]

    @pytest.mark.asyncio
    async def test_decorative_image(self, service, sample_figure):
        """Decorative images should return instruction for empty alt."""
        mock_result = ImageDescriptionResult(
            image_category="decorative",
            alt_text="",
            is_decorative=True,
            confidence="high",
            reasoning="Decorative border",
        )

        mock_agent_result = MagicMock()
        mock_agent_result.output = mock_result
        mock_usage = MagicMock()
        mock_usage.request_tokens = 300
        mock_usage.response_tokens = 50
        mock_agent_result.usage.return_value = mock_usage

        mock_agent = MagicMock()
        mock_agent.run = AsyncMock(return_value=mock_agent_result)

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            result = await service._run_image_describer(
                figure=sample_figure,
                current_markdown="![](figures/figure-1.png)",
                page_images={1: "AAAA"},
                describer_tokens={"input": 0, "output": 0},
                update_tokens=lambda i, o: None,
            )

        assert "decorative" in result.lower()
        assert "empty" in result.lower()

    @pytest.mark.asyncio
    async def test_describer_failure_returns_error(self, service, sample_figure):
        """If the describer agent raises, should return error string."""
        mock_agent = MagicMock()
        mock_agent.run = AsyncMock(side_effect=RuntimeError("Model timeout"))

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            result = await service._run_image_describer(
                figure=sample_figure,
                current_markdown="![](figures/figure-1.png)",
                page_images={1: "AAAA"},
                describer_tokens={"input": 0, "output": 0},
                update_tokens=lambda i, o: None,
            )

        assert "ERROR" in result
        assert "figure-1.png" in result

    @pytest.mark.asyncio
    async def test_surrounding_text_extracted(self, service, sample_figure):
        """The describer should receive surrounding markdown context."""
        captured_user_parts = []

        mock_result = ImageDescriptionResult(
            image_category="informative",
            alt_text="Test alt",
            is_decorative=False,
            confidence="high",
        )
        mock_agent_result = MagicMock()
        mock_agent_result.output = mock_result
        mock_usage = MagicMock()
        mock_usage.request_tokens = 100
        mock_usage.response_tokens = 50
        mock_agent_result.usage.return_value = mock_usage

        mock_agent = MagicMock()

        async def capture_run(user_parts, **kwargs):
            captured_user_parts.extend(user_parts)
            return mock_agent_result

        mock_agent.run = AsyncMock(side_effect=capture_run)

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            await service._run_image_describer(
                figure=sample_figure,
                current_markdown="Some text before\n\n![](figures/figure-1.png)\n\nSome text after",
                page_images={1: "AAAA"},
                describer_tokens={"input": 0, "output": 0},
                update_tokens=lambda i, o: None,
            )

        # The last user part should be the text message with surrounding context
        text_parts = [p for p in captured_user_parts if isinstance(p, str)]
        assert any("figure-1.png" in p for p in text_parts)


class TestDescribeImageTokenTracking:
    """Test describer token aggregation in _step_page_content."""

    @pytest.mark.asyncio
    async def test_describer_tokens_aggregated(
        self, service, base_result, structure_with_images
    ):
        """Describer tokens should be included in step totals."""

        async def mock_run_page_agent(page_num, **kwargs):
            describer_in = 500 if page_num == 1 else 0
            describer_out = 100 if page_num == 1 else 0
            return PageCorrectionResult(
                page=page_num,
                corrected_markdown=base_result.page_markdowns["v0"][str(page_num)],
                changes=[],
                issues=[],
                input_tokens=200,
                output_tokens=50,
                describer_input_tokens=describer_in,
                describer_output_tokens=describer_out,
            )

        service._run_page_agent = mock_run_page_agent

        await service._step_page_content(base_result, structure_with_images)

        step = base_result.steps[-1]
        # Page agent tokens: 200*2=400 input, 50*2=100 output
        # Describer tokens: 500 input, 100 output (only page 1)
        # Combined: 900 input, 200 output
        assert step.input_tokens == 900
        assert step.output_tokens == 200
        assert step.metadata["describer_input_tokens"] == 500
        assert step.metadata["describer_output_tokens"] == 100

    @pytest.mark.asyncio
    async def test_describer_tokens_zero_when_no_images(
        self, service, structure_no_images
    ):
        """Describer tokens should be 0 when no images in document."""
        result = PipelineViewerResult(
            filename="test.pdf",
            total_pages=1,
            versions={"v0": "# Intro\n\nHello world"},
            page_images={"1": "AAAA"},
            page_markdowns={"v0": {"1": "# Intro\n\nHello world"}},
            figures=[],
            steps=[],
            stats={},
        )

        async def mock_run_page_agent(page_num, **kwargs):
            return PageCorrectionResult(
                page=page_num,
                corrected_markdown=result.page_markdowns["v0"][str(page_num)],
                changes=[],
                issues=[],
                input_tokens=200,
                output_tokens=50,
            )

        service._run_page_agent = mock_run_page_agent

        await service._step_page_content(result, structure_no_images)

        step = result.steps[-1]
        assert step.metadata["describer_input_tokens"] == 0
        assert step.metadata["describer_output_tokens"] == 0


class TestDescribeImageModelDefaults:
    """Test model defaults for describer token fields."""

    def test_section_correction_result_defaults(self):
        """SectionCorrectionResult should default describer tokens to 0."""
        result = SectionCorrectionResult(
            section_index=0,
            heading_text="Test",
            corrected_markdown="test",
            pages=[1],
        )
        assert result.describer_input_tokens == 0
        assert result.describer_output_tokens == 0

    def test_page_correction_result_defaults(self):
        """PageCorrectionResult should default describer tokens to 0."""
        result = PageCorrectionResult(
            page=1,
            corrected_markdown="test",
        )
        assert result.describer_input_tokens == 0
        assert result.describer_output_tokens == 0

    def test_image_description_result_defaults(self):
        """ImageDescriptionResult should have sensible defaults."""
        result = ImageDescriptionResult(
            image_category="informative", alt_text="A chart"
        )
        assert result.is_decorative is False
        assert result.confidence == "high"
        assert result.reasoning == ""
        assert result.image_category == "informative"


class TestPageFigureFiltering:
    """Test that figures are correctly filtered per page."""

    @pytest.mark.asyncio
    async def test_figures_filtered_by_page(
        self, service, sample_figure, sample_figure_2
    ):
        """Each page should only get figures from that page."""
        result = PipelineViewerResult(
            filename="test.pdf",
            total_pages=2,
            versions={"v0": "# Intro\n\n![](figures/figure-1.png)\n\n# Methods\n\n![](figures/figure-2.png)"},
            page_images={"1": "AAAA", "2": "BBBB"},
            page_markdowns={"v0": {"1": "page1", "2": "page2"}},
            figures=[sample_figure, sample_figure_2],
            steps=[],
            stats={},
        )

        structure = StructureResult(
            page_attributes={
                1: PageAttributes(layout=LayoutType.SINGLE_COLUMN, has_images=True),
                2: PageAttributes(layout=LayoutType.SINGLE_COLUMN, has_images=True),
            },
            outline=[
                OutlineEntry(level=2, text="Intro", page=1),
                OutlineEntry(level=2, text="Methods", page=2),
            ],
            footnotes=[],
        )

        captured_figures: dict[int, list[str] | None] = {}

        async def mock_run_page_agent(page_num, **kwargs):
            pf = kwargs.get("page_figures")
            if pf:
                captured_figures[page_num] = [f.ref_id for f in pf]
            else:
                captured_figures[page_num] = None
            return PageCorrectionResult(
                page=page_num,
                corrected_markdown=result.page_markdowns["v0"][str(page_num)],
                changes=[],
                issues=[],
            )

        service._run_page_agent = mock_run_page_agent

        await service._step_page_content(result, structure)

        assert captured_figures[1] == ["figure-1.png"]
        assert captured_figures[2] == ["figure-2.png"]

    @pytest.mark.asyncio
    async def test_no_figures_passes_none(self, service):
        """Pages with no figures should get None."""
        result = PipelineViewerResult(
            filename="test.pdf",
            total_pages=1,
            versions={"v0": "# Intro\n\nHello"},
            page_images={"1": "AAAA"},
            page_markdowns={"v0": {"1": "# Intro\n\nHello"}},
            figures=[],
            steps=[],
            stats={},
        )

        structure = StructureResult(
            page_attributes={
                1: PageAttributes(layout=LayoutType.SINGLE_COLUMN),
            },
            outline=[OutlineEntry(level=2, text="Intro", page=1)],
            footnotes=[],
        )

        captured_figures = {}

        async def mock_run_page_agent(page_num, **kwargs):
            captured_figures[page_num] = kwargs.get("page_figures")
            return PageCorrectionResult(
                page=page_num,
                corrected_markdown=result.page_markdowns["v0"][str(page_num)],
                changes=[],
                issues=[],
            )

        service._run_page_agent = mock_run_page_agent

        await service._step_page_content(result, structure)

        assert captured_figures[1] is None


class TestImageIsolation:
    """Test that the describer only receives the cropped figure, not the full page."""

    @pytest.mark.asyncio
    async def test_only_cropped_figure_sent_when_available(
        self, service, sample_figure
    ):
        """When image_base64 is present, only the cropped figure is sent (no page image)."""
        from pydantic_ai.messages import BinaryContent

        captured_user_parts = []

        mock_result = ImageDescriptionResult(
            image_category="informative",
            alt_text="Test",
            is_decorative=False,
            confidence="high",
        )
        mock_agent_result = MagicMock()
        mock_agent_result.output = mock_result
        mock_usage = MagicMock()
        mock_usage.request_tokens = 100
        mock_usage.response_tokens = 50
        mock_agent_result.usage.return_value = mock_usage

        mock_agent = MagicMock()

        async def capture_run(user_parts, **kwargs):
            captured_user_parts.extend(user_parts)
            return mock_agent_result

        mock_agent.run = AsyncMock(side_effect=capture_run)

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            await service._run_image_describer(
                figure=sample_figure,
                current_markdown="![](figures/figure-1.png)",
                page_images={1: "PAGE_IMAGE_B64"},
                describer_tokens={"input": 0, "output": 0},
                update_tokens=lambda i, o: None,
            )

        # Should have exactly 1 BinaryContent (the cropped figure) + 1 text message
        binary_parts = [
            p for p in captured_user_parts if isinstance(p, BinaryContent)
        ]
        assert len(binary_parts) == 1

        # Text should NOT contain fallback mode guidance
        text_parts = [p for p in captured_user_parts if isinstance(p, str)]
        assert not any("full page image" in p for p in text_parts)

    @pytest.mark.asyncio
    async def test_page_image_fallback_when_no_crop(self, service):
        """When image_base64 is empty, falls back to full page image with guidance."""
        from pydantic_ai.messages import BinaryContent

        figure_no_crop = FigureData(
            ref_id="figure-3.png",
            caption="Figure 3: No crop available",
            page_number=1,
            image_base64="",  # Empty โ€” Docling failed to extract
        )

        captured_user_parts = []

        mock_result = ImageDescriptionResult(
            image_category="informative",
            alt_text="Fallback test",
            is_decorative=False,
            confidence="medium",
        )
        mock_agent_result = MagicMock()
        mock_agent_result.output = mock_result
        mock_usage = MagicMock()
        mock_usage.request_tokens = 100
        mock_usage.response_tokens = 50
        mock_agent_result.usage.return_value = mock_usage

        mock_agent = MagicMock()

        async def capture_run(user_parts, **kwargs):
            captured_user_parts.extend(user_parts)
            return mock_agent_result

        mock_agent.run = AsyncMock(side_effect=capture_run)

        page_b64 = base64.b64encode(b"page-image-data").decode()

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            await service._run_image_describer(
                figure=figure_no_crop,
                current_markdown="![](figures/figure-3.png)",
                page_images={1: page_b64},
                describer_tokens={"input": 0, "output": 0},
                update_tokens=lambda i, o: None,
            )

        # Should have 1 BinaryContent (the page image fallback) + 1 text message
        binary_parts = [
            p for p in captured_user_parts if isinstance(p, BinaryContent)
        ]
        assert len(binary_parts) == 1

        # Text SHOULD contain fallback mode guidance
        text_parts = [p for p in captured_user_parts if isinstance(p, str)]
        assert any("full page image" in p for p in text_parts)
        assert any("figure-3.png" in p for p in text_parts)

    @pytest.mark.asyncio
    async def test_no_images_sent_when_neither_available(self, service):
        """When both crop and page image are missing, only text is sent."""
        from pydantic_ai.messages import BinaryContent

        figure_no_crop = FigureData(
            ref_id="figure-4.png",
            caption="Figure 4: Ghost figure",
            page_number=5,
            image_base64="",
        )

        captured_user_parts = []

        mock_result = ImageDescriptionResult(
            image_category="informative",
            alt_text="No image available",
            is_decorative=False,
            confidence="low",
        )
        mock_agent_result = MagicMock()
        mock_agent_result.output = mock_result
        mock_usage = MagicMock()
        mock_usage.request_tokens = 50
        mock_usage.response_tokens = 30
        mock_agent_result.usage.return_value = mock_usage

        mock_agent = MagicMock()

        async def capture_run(user_parts, **kwargs):
            captured_user_parts.extend(user_parts)
            return mock_agent_result

        mock_agent.run = AsyncMock(side_effect=capture_run)

        with patch(_BEDROCK_PATCH), patch(_AGENT_PATCH, return_value=mock_agent):
            await service._run_image_describer(
                figure=figure_no_crop,
                current_markdown="![](figures/figure-4.png)",
                page_images={},  # No page images at all
                describer_tokens={"input": 0, "output": 0},
                update_tokens=lambda i, o: None,
            )

        binary_parts = [
            p for p in captured_user_parts if isinstance(p, BinaryContent)
        ]
        assert len(binary_parts) == 0


class TestImageClassification:
    """Test the image_category field on ImageDescriptionResult."""

    def test_decorative_category(self):
        result = ImageDescriptionResult(
            image_category="decorative",
            alt_text="",
            is_decorative=True,
            confidence="high",
            reasoning="University logo in page header",
        )
        assert result.image_category == "decorative"
        assert result.is_decorative is True
        assert result.alt_text == ""

    def test_informative_category(self):
        result = ImageDescriptionResult(
            image_category="informative",
            alt_text="Eastern box turtle on forest floor",
            is_decorative=False,
            confidence="high",
        )
        assert result.image_category == "informative"
        assert result.is_decorative is False

    def test_complex_informative_category(self):
        result = ImageDescriptionResult(
            image_category="complex_informative",
            alt_text="Bar chart showing enrollment growth from 450 to 680 students",
            is_decorative=False,
            confidence="high",
            reasoning="Data visualization with multiple data points โ€” long description recommended",
        )
        assert result.image_category == "complex_informative"

    def test_invalid_category_rejected(self):
        """Invalid image_category values should be rejected by Pydantic."""
        with pytest.raises(Exception):
            ImageDescriptionResult(
                image_category="unknown",
                alt_text="Test",
            )


class TestDescriberUserMessage:
    """Test the build_describer_user_message helper."""

    def test_standard_mode(self):
        from src.agents.prompts.image_description import (
            build_describer_user_message,
        )

        msg = build_describer_user_message(
            caption="Figure 1: Test",
            surrounding_text="some context",
            ref_id="figure-1.png",
        )
        assert "figure-1.png" in msg
        assert "Figure 1: Test" in msg
        assert "full page image" not in msg
        assert "classify" in msg.lower()

    def test_fallback_mode(self):
        from src.agents.prompts.image_description import (
            build_describer_user_message,
        )

        msg = build_describer_user_message(
            caption="Figure 2: Chart",
            surrounding_text="nearby text",
            ref_id="figure-2.png",
            fallback_mode=True,
        )
        assert "full page image" in msg
        assert "figure-2.png" in msg
        assert "ONLY that figure" in msg

    def test_no_caption_or_context(self):
        from src.agents.prompts.image_description import (
            build_describer_user_message,
        )

        msg = build_describer_user_message(
            caption="",
            surrounding_text="",
            ref_id="figure-5.png",
        )
        assert "figure-5.png" in msg
        assert "Caption" not in msg
        assert "Surrounding text" not in msg