File size: 6,498 Bytes
fdfe81a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for ``agents.slide_bundle`` — the single-shot pre-computed bundle.

These verify that the bundle reproduces what the legacy 8 tools used to
return for one slide, so the single-shot agent sees the same view of the
deck the agentic agent would have assembled tool-by-tool.
"""
from __future__ import annotations

import json
from pathlib import Path

import pytest

from agents.slide_bundle import (
    SlideReviewBundle,
    build_rubric_full,
    build_slide_bundle,
    bundle_to_prompt_payload,
)
from rubric.loader import load_rubric
from services.parser import parse_deck
from services.renderer import RenderResult


_DOC_REF = Path(__file__).parents[3] / "Doc Reference"
JACOBS_PATH = _DOC_REF / (
    "20260429 Jacobs Assessment of Compensation Peers - DRAFT 20260327 (3).pptx"
)


@pytest.fixture(scope="module")
def rubric():
    return load_rubric()


@pytest.fixture(scope="module")
def jacobs_deck():
    return parse_deck(JACOBS_PATH)


@pytest.fixture(scope="module")
def render_result_skipped() -> RenderResult:
    """Skipped renderer — bundle should still build, just without image."""
    return RenderResult(skipped=True, reason="text-only test")


# ---------------------------------------------------------------------------
# build_rubric_full
# ---------------------------------------------------------------------------


def test_build_rubric_full_lists_every_category(rubric):
    rf = build_rubric_full(rubric)
    expected = {r.category for r in rubric.rules}
    assert set(rf["categories"]) == expected
    # sections keyed by category, every section has at least one rule
    for cat in rf["categories"]:
        assert cat in rf["sections"]
        assert len(rf["sections"][cat]) >= 1
        for entry in rf["sections"][cat]:
            for k in (
                "rule_id", "rule_text", "severity_default", "detection_type",
                "examples_do", "examples_dont", "source_ref", "metadata",
            ):
                assert k in entry


def test_build_rubric_full_must_flag_terms(rubric):
    rf = build_rubric_full(rubric)
    assert "DRAFT" in rf["must_flag_markers"]
    assert rf["must_flag_markers"]["DRAFT"]["rule_id"] == "title_slide.no_draft"


def test_build_rubric_full_color_palette_metadata(rubric):
    rf = build_rubric_full(rubric)
    palette_rules = rf["sections"]["color_palette"]
    has_palette = any(
        "allowed_hex" in r["metadata"] and r["metadata"]["allowed_hex"]
        for r in palette_rules
    )
    assert has_palette


# ---------------------------------------------------------------------------
# build_slide_bundle — Jacobs slide 1 (DRAFT marker)
# ---------------------------------------------------------------------------


def test_bundle_slide_1_has_draft_marker_hit(jacobs_deck, rubric, render_result_skipped):
    bundle = build_slide_bundle(
        1, jacobs_deck, render_result_skipped, rubric, include_image_b64=False,
    )
    assert isinstance(bundle, SlideReviewBundle)
    assert bundle.slide_number == 1
    # Slide 1 is the title slide with DRAFT in the title.
    terms = [h["term"] for h in bundle.text_markers]
    assert "DRAFT" in terms


def test_bundle_image_skipped_when_render_skipped(jacobs_deck, rubric, render_result_skipped):
    bundle = build_slide_bundle(
        1, jacobs_deck, render_result_skipped, rubric, include_image_b64=True,
    )
    assert bundle.image_b64 is None
    assert bundle.image_path is None


def test_bundle_palette_check_runs_per_unique_color(jacobs_deck, rubric, render_result_skipped):
    """Every non-grayscale RGB hex on the slide should be in palette_check.checks."""
    bundle = build_slide_bundle(
        2, jacobs_deck, render_result_skipped, rubric, include_image_b64=False,
    )
    assert "allowed" in bundle.palette_check
    assert isinstance(bundle.palette_check["checks"], list)
    # Each check has both keys.
    for c in bundle.palette_check["checks"]:
        assert {"hex", "in_palette"} <= set(c.keys())


def test_bundle_confidentiality_check_shape(jacobs_deck, rubric, render_result_skipped):
    bundle = build_slide_bundle(
        3, jacobs_deck, render_result_skipped, rubric, include_image_b64=False,
    )
    cc = bundle.confidentiality_check
    assert {"present", "exact_match", "position_hint", "matched_excerpt", "matching_shape_id"} <= set(cc.keys())
    assert cc["position_hint"] in {"header", "footer", "body", "absent"}


def test_bundle_parsed_shapes_match_deck_count(jacobs_deck, rubric, render_result_skipped):
    bundle = build_slide_bundle(
        1, jacobs_deck, render_result_skipped, rubric, include_image_b64=False,
    )
    assert len(bundle.parsed_shapes) == len(jacobs_deck.slides[0].shapes)


def test_bundle_text_passes_through_extracted_text_and_notes(
    jacobs_deck, rubric, render_result_skipped
):
    bundle = build_slide_bundle(
        1, jacobs_deck, render_result_skipped, rubric, include_image_b64=False,
    )
    assert bundle.slide_text == jacobs_deck.slides[0].extracted_text
    assert bundle.slide_notes == jacobs_deck.slides[0].notes_text


def test_bundle_rejects_out_of_range(jacobs_deck, rubric, render_result_skipped):
    with pytest.raises(ValueError):
        build_slide_bundle(
            999, jacobs_deck, render_result_skipped, rubric,
            include_image_b64=False,
        )


# ---------------------------------------------------------------------------
# bundle_to_prompt_payload
# ---------------------------------------------------------------------------


def test_bundle_to_prompt_payload_drops_image_b64(
    jacobs_deck, rubric, render_result_skipped
):
    bundle = build_slide_bundle(
        1, jacobs_deck, render_result_skipped, rubric, include_image_b64=False,
    )
    payload = bundle_to_prompt_payload(bundle)
    # JSON-serialisable round-trip:
    s = json.dumps(payload)
    again = json.loads(s)
    assert again["slide_number"] == 1
    assert again["image_available"] is False
    assert "image_b64" not in again
    assert "rubric" in again and "categories" in again["rubric"]


def test_rubric_full_caching_passes_through(jacobs_deck, rubric, render_result_skipped):
    """Passing a precomputed rubric_full should yield bit-identical bundle.rubric_full."""
    rf = build_rubric_full(rubric)
    b1 = build_slide_bundle(
        1, jacobs_deck, render_result_skipped, rubric,
        rubric_full=rf, include_image_b64=False,
    )
    assert b1.rubric_full is rf  # identity passthrough — no recomputation