Spaces:
Sleeping
Sleeping
File size: 5,393 Bytes
8301014 | 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 | from __future__ import annotations
from highlight_preview import (
CATEGORY_LABELS,
HighlightSpan,
build_static_highlight_preview,
validate_highlight_spans,
)
SYNTHETIC_TEXT = "Cliënt [PERSOON_1] woont aan [ADRES_1]. Zaak [ZAAK_1] blijft context."
def test_build_static_highlight_preview_escapes_text_and_preserves_labels():
text = "SYNTHETIC <script> [PERSOON_1] & context"
start = text.index("[PERSOON_1]")
end = start + len("[PERSOON_1]")
preview = build_static_highlight_preview(
text,
[
HighlightSpan(
span_id="span-1",
row_id="row-1",
start_offset=start,
end_offset=end,
label="[PERSOON_1]",
category="confirmed_sensitive",
entity_type="PERSON",
status="confirmed",
source="synthetic",
replacement_preview="PERSOON_PLACEHOLDER",
reason="Synthetic confirmed value",
)
],
)
assert preview["preview_type"] == "static_highlight_preview"
assert preview["read_only"] is True
assert preview["non_authoritative"] is True
assert preview["mutation_allowed"] is False
assert preview["export_blocking"] is False
assert preview["scrub_key_changes"] is False
assert preview["ui_required"] is False
assert preview["external_assets"] is False
assert preview["safe_to_render"] is True
assert "<script>" in "".join(segment["escaped_text"] for segment in preview["segments"])
highlight = [segment for segment in preview["segments"] if segment["type"] == "highlight"][0]
assert highlight["escaped_text"] == "[PERSOON_1]"
assert highlight["category_label"] == "Bevestigd gevoelig"
assert highlight["aria_label"] == "Bevestigd gevoelig: [PERSOON_1]"
def test_validate_highlight_spans_rejects_invalid_offsets_and_label_mismatch():
result = validate_highlight_spans(
SYNTHETIC_TEXT,
[
{
"span_id": "bad-offset",
"row_id": "row-1",
"start_offset": -1,
"end_offset": 5,
"label": "Cliënt",
"category": "needs_review",
},
{
"span_id": "bad-label",
"row_id": "row-2",
"start_offset": 0,
"end_offset": 6,
"label": "Wrong",
"category": "needs_review",
},
],
)
assert result["valid_spans"] == []
reasons = {reason for span in result["invalid_spans"] for reason in span["reasons"]}
assert "invalid offset range" in reasons
assert "span label does not match displayed text offsets" in reasons
assert result["safe_to_render"] is False
def test_validate_highlight_spans_rejects_duplicate_and_unsupported_category():
start = SYNTHETIC_TEXT.index("[PERSOON_1]")
end = start + len("[PERSOON_1]")
result = validate_highlight_spans(
SYNTHETIC_TEXT,
[
{
"span_id": "same",
"row_id": "row-1",
"start_offset": start,
"end_offset": end,
"label": "[PERSOON_1]",
"category": "confirmed_sensitive",
},
{
"span_id": "same",
"row_id": "row-2",
"start_offset": start,
"end_offset": end,
"label": "[PERSOON_1]",
"category": "not_allowed",
},
],
)
assert len(result["valid_spans"]) == 1
assert len(result["invalid_spans"]) == 1
assert "span_id must be unique" in result["invalid_spans"][0]["reasons"]
assert "unsupported category" in result["invalid_spans"][0]["reasons"]
def test_build_static_highlight_preview_reports_overlapping_spans():
first_start = SYNTHETIC_TEXT.index("[PERSOON_1]")
first_end = first_start + len("[PERSOON_1]")
preview = build_static_highlight_preview(
SYNTHETIC_TEXT,
[
{
"span_id": "a",
"row_id": "row-a",
"start_offset": first_start,
"end_offset": first_end,
"label": "[PERSOON_1]",
"category": "needs_review",
},
{
"span_id": "b",
"row_id": "row-b",
"start_offset": first_start + 1,
"end_offset": first_end,
"label": "PERSOON_1]",
"category": "needs_review",
},
],
)
assert preview["safe_to_render"] is False
assert any("overlapping span" in span["reasons"] for span in preview["invalid_spans"])
def test_preview_contains_all_category_labels_and_no_real_data_examples():
preview = build_static_highlight_preview(SYNTHETIC_TEXT, [])
assert preview["category_labels"] == CATEGORY_LABELS
rendered = repr(preview)
assert "Jan Jansen" not in rendered
assert "Piet de Vries" not in rendered
assert "123456782" not in rendered
def test_non_string_text_is_rejected_safely():
result = validate_highlight_spans(123, [])
assert result["valid_spans"] == []
assert result["safe_to_render"] is False
assert result["errors"] == ["Preview text must be a string."]
|