-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_display.py
More file actions
834 lines (711 loc) · 31.2 KB
/
Copy pathtest_display.py
File metadata and controls
834 lines (711 loc) · 31.2 KB
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations
import logging
import numpy as np
import pandas as pd
import pytest
from anonymizer.engine.constants import (
COL_DETECTED_ENTITIES,
COL_DETECTION_VALID,
COL_ENTITY_COVERAGE,
COL_ENTITY_COVERAGE_N_CANDIDATES,
COL_FINAL_ENTITIES,
COL_JUDGE_EVALUATION,
COL_MISSED_ENTITIES,
COL_REPLACEMENT_MAP,
COL_SENSITIVITY_DISPOSITION,
)
from anonymizer.engine.rewrite.final_judge import PRIVACY_RUBRIC, QUALITY_RUBRIC, STYLE_RUBRIC
from anonymizer.engine.schemas import EntitiesSchema, EntitySchema
from anonymizer.engine.schemas.rewrite import EntityDispositionSchema, SensitivityDispositionSchema
from anonymizer.interface.display import (
_build_replaced_entities,
_extract_judge_scores,
_normalize_replacement_map,
_render_entity_coverage_section,
_render_highlighted_text,
_verdict_badge,
render_record_html,
)
from anonymizer.interface.results import PreviewResult
def _entity(value: str, label: str, start: int, end: int) -> EntitySchema:
return EntitySchema(value=value, label=label, start_position=start, end_position=end)
def _build_detected_entities_payload(payload_kind: str) -> object:
entities_list = [
{"value": "Alice", "label": "first_name", "start_position": 0, "end_position": 5},
{"value": "Acme", "label": "organization", "start_position": 15, "end_position": 19},
]
if payload_kind == "dict_wrapper":
return {"entities": entities_list}
if payload_kind == "numpy_wrapped_dict_wrapper":
return {"entities": np.array(entities_list, dtype=object)}
if payload_kind == "entities_schema":
return EntitiesSchema(entities=entities_list)
raise ValueError(f"Unsupported payload kind: {payload_kind}")
def test_highlighted_text_wraps_entity_in_styled_span() -> None:
entities = [_entity("Alice", "first_name", 0, 5)]
result = _render_highlighted_text("Alice works here", entities)
assert "Alice" in result
assert "first_name" in result
assert "border:" in result
def test_highlighted_text_escapes_html_in_plain_text() -> None:
entities = [_entity("Alice", "first_name", 0, 5)]
result = _render_highlighted_text("Alice <b>works</b> here", entities)
assert "<b>" in result
def test_highlighted_text_preserves_text_between_entities() -> None:
entities = [
_entity("Alice", "first_name", 0, 5),
_entity("Acme", "organization", 15, 19),
]
result = _render_highlighted_text("Alice works at Acme", entities)
assert " works at " in result
def test_highlighted_text_no_entities_returns_escaped_text() -> None:
result = _render_highlighted_text("Hello <world>", [])
assert "<world>" in result
def test_highlighted_text_skips_overlapping_entity() -> None:
entities = [
_entity("John Doe", "full_name", 0, 8),
_entity("Doe", "last_name", 5, 8),
]
result = _render_highlighted_text("John Doe went home", entities)
assert "full_name" in result
assert "last_name" not in result
def test_highlighted_text_consistent_color_per_label() -> None:
entities = [
_entity("Alice", "first_name", 0, 5),
_entity("Bob", "first_name", 10, 13),
]
result = _render_highlighted_text("Alice met Bob here", entities)
border_colors = [
s.split("border:1.5px solid ")[1].split(";")[0] for s in result.split("<span") if "border:1.5px solid" in s
]
assert len(border_colors) == 2
assert border_colors[0] == border_colors[1]
def test_replaced_entities_tracks_shifted_positions() -> None:
original_entities = [
_entity("Alice", "first_name", 0, 5),
_entity("Acme", "organization", 15, 19),
]
replacement_map = [
{"original": "Alice", "label": "first_name", "synthetic": "Maya"},
{"original": "Acme", "label": "organization", "synthetic": "NovaCorp"},
]
result = _build_replaced_entities(
original_entities,
replacement_map,
"Alice works at Acme",
"Maya works at NovaCorp",
)
assert len(result) == 2
assert result[0].value == "Maya"
assert result[0].start_position == 0
assert result[0].end_position == 4
assert result[1].value == "NovaCorp"
assert result[1].start_position == 14
def test_replaced_entities_empty_map_uses_original_values() -> None:
original_entities = [_entity("Alice", "first_name", 0, 5)]
result = _build_replaced_entities(original_entities, [], "Alice works", "Alice works")
assert len(result) == 1
assert result[0].value == "Alice"
def test_normalize_replacement_map_from_dict() -> None:
raw = {"replacements": [{"original": "Alice", "label": "first_name", "synthetic": "Maya"}]}
result = _normalize_replacement_map(raw)
assert len(result) == 1
assert result[0]["synthetic"] == "Maya"
def test_normalize_replacement_map_from_json_string() -> None:
raw = '{"replacements": [{"original": "Alice", "label": "first_name", "synthetic": "Maya"}]}'
result = _normalize_replacement_map(raw)
assert len(result) == 1
def test_normalize_replacement_map_invalid_json_returns_empty() -> None:
assert _normalize_replacement_map("bad json {{{") == []
def test_normalize_replacement_map_non_dict_returns_empty() -> None:
assert _normalize_replacement_map([1, 2, 3]) == []
def test_verdict_badge_satisfied_when_all_correct_and_valid_true() -> None:
badge, rate = _verdict_badge(valid=True, correct=10, total=10)
assert "Satisfied" in badge and "Not" not in badge
assert rate == " (Judge Agreement: 10/10)"
def test_verdict_badge_partial_for_mixed_count() -> None:
badge, _ = _verdict_badge(valid=False, correct=8, total=10)
assert "Partially Satisfied" in badge
def test_verdict_badge_unavailable_when_valid_none() -> None:
badge, rate = _verdict_badge(valid=None, correct=0, total=0)
assert "Unavailable" in badge
assert rate == ""
def test_verdict_badge_not_satisfied_when_valid_false_without_enumerated_failures() -> None:
"""``valid is False`` with ``correct == total`` is an inconsistent LLM response
(the judge said it's invalid but didn't list specifics). The explicit boolean
must override the count so we don't render a misleading green badge."""
badge, rate = _verdict_badge(valid=False, correct=10, total=10)
assert "Not Satisfied" in badge
assert "Satisfied" not in badge.replace("Not Satisfied", "")
assert "10/10" in rate
@pytest.mark.parametrize(
"payload_kind",
["dict_wrapper", "numpy_wrapped_dict_wrapper", "entities_schema"],
)
def test_render_record_html_contains_all_sections(payload_kind: str) -> None:
row = pd.Series(
{
"text": "Alice works at Acme",
"text_replaced": "[REDACTED_FIRST_NAME] works at [REDACTED_ORGANIZATION]",
COL_DETECTED_ENTITIES: _build_detected_entities_payload(payload_kind),
COL_REPLACEMENT_MAP: {
"replacements": [
{"original": "Alice", "label": "first_name", "synthetic": "[REDACTED_FIRST_NAME]"},
{"original": "Acme", "label": "organization", "synthetic": "[REDACTED_ORGANIZATION]"},
]
},
}
)
result = render_record_html(row, record_index=0)
assert "Original" in result
assert "Replaced" in result
assert "Replacement Map" in result
assert "Alice" in result
assert "REDACTED_FIRST_NAME" in result
assert "record 0" in result
def test_render_record_html_original_highlights_from_map_when_entities_empty() -> None:
"""When _detected_entities is empty but replacement_map exists, Original gets highlights."""
row = {
"text": "Alice works at Acme",
"text_replaced": "Maya works at NovaCorp",
COL_DETECTED_ENTITIES: {"entities": []},
COL_REPLACEMENT_MAP: {
"replacements": [
{"original": "Alice", "label": "first_name", "synthetic": "Maya"},
{"original": "Acme", "label": "organization", "synthetic": "NovaCorp"},
]
},
}
html_str = render_record_html(pd.Series(row))
assert "Original" in html_str
assert "Alice" in html_str
assert "Acme" in html_str
assert "first_name" in html_str
def test_render_record_html_replaced_highlights_from_map_when_entities_empty() -> None:
"""When _detected_entities is empty but replacement_map exists, highlights come from map."""
row = {
"text": "Alice works at Acme",
"text_replaced": "Maya works at NovaCorp",
COL_DETECTED_ENTITIES: {"entities": []},
COL_REPLACEMENT_MAP: {
"replacements": [
{"original": "Alice", "label": "first_name", "synthetic": "Maya"},
{"original": "Acme", "label": "organization", "synthetic": "NovaCorp"},
]
},
}
html_str = render_record_html(pd.Series(row))
assert "Maya" in html_str
assert "NovaCorp" in html_str
assert "border:1.5px solid" in html_str
def test_render_record_html_without_replacement_map() -> None:
row = pd.Series(
{
"text": "Alice works here",
"text_replaced": "Alice works here",
COL_DETECTED_ENTITIES: {"entities": []},
COL_REPLACEMENT_MAP: {},
}
)
result = render_record_html(row)
assert "No replacement map available" in result
def test_render_record_html_omits_detection_judge_section_when_judge_did_not_run() -> None:
"""A preview/run without evaluation must not render an empty 'Detection Judge'
heading. The wrapper lives inside ``_render_detection_judge_section`` so the
whole block is omitted when ``COL_DETECTION_VALID`` is absent."""
row = pd.Series(
{
"text": "Alice works here",
"text_replaced": "Bob works here",
COL_DETECTED_ENTITIES: {"entities": []},
COL_REPLACEMENT_MAP: {},
}
)
result = render_record_html(row)
assert "Detection Judge" not in result
assert "Detection Validity" not in result
def _make_preview(rows: int = 2) -> PreviewResult:
df = pd.DataFrame(
{
"text": ["Alice", "Bob"][:rows],
"text_replaced": ["[R]", "[R]"][:rows],
COL_DETECTED_ENTITIES: [{"entities": []} for _ in range(rows)],
COL_REPLACEMENT_MAP: [{} for _ in range(rows)],
}
)
return PreviewResult(
dataframe=df,
trace_dataframe=df,
resolved_text_column="text",
failed_records=[],
preview_num_records=rows,
)
def test_render_record_html_uses_detected_entities_over_map_scan() -> None:
"""When _detected_entities is populated, use them directly instead of map-based scanning."""
row = pd.Series(
{
"text": "She works at the Lantern in Austin",
"text_replaced": "She works at [REDACTED_COMPANY] in [REDACTED_CITY]",
COL_DETECTED_ENTITIES: {
"entities": [
{"value": "The Lantern", "label": "company_name", "start_position": 13, "end_position": 24},
{"value": "Austin", "label": "city", "start_position": 28, "end_position": 34},
]
},
COL_REPLACEMENT_MAP: {
"replacements": [
{"original": "The Lantern", "label": "company_name", "synthetic": "[REDACTED_COMPANY]"},
{"original": "Austin", "label": "city", "synthetic": "[REDACTED_CITY]"},
]
},
}
)
result = render_record_html(row)
assert "company_name" in result
assert "city" in result
assert "[REDACTED_COMPANY]" in result
assert "[REDACTED_CITY]" in result
def test_render_record_html_prefers_final_entities_for_filtered_replace_preview() -> None:
row = pd.Series(
{
"text": "Mara Delgado works in Austin",
"text_replaced": "[REDACTED_FIRST_NAME] Delgado works in Austin",
COL_DETECTED_ENTITIES: {
"entities": [
{"value": "Mara", "label": "first_name", "start_position": 0, "end_position": 4},
{"value": "Delgado", "label": "last_name", "start_position": 5, "end_position": 12},
{"value": "Austin", "label": "city", "start_position": 22, "end_position": 28},
]
},
COL_FINAL_ENTITIES: {
"entities": [
{"value": "Mara", "label": "first_name", "start_position": 0, "end_position": 4},
]
},
COL_REPLACEMENT_MAP: {
"replacements": [
{"original": "Mara", "label": "first_name", "synthetic": "[REDACTED_FIRST_NAME]"},
]
},
}
)
result = render_record_html(row)
assert "first_name" in result
assert "| last_name" not in result
assert "| city" not in result
def test_render_record_html_does_not_fallback_to_detected_when_final_entities_empty() -> None:
row = pd.Series(
{
"text": "BackupAgent config_path=/opt/app/config.yaml",
"text_replaced": "BackupAgent config_path=/opt/app/config.yaml",
COL_DETECTED_ENTITIES: {
"entities": [
{"value": "BackupAgent", "label": "process_name", "start_position": 0, "end_position": 11},
{"value": "/opt/app/config.yaml", "label": "file_path", "start_position": 24, "end_position": 44},
]
},
COL_FINAL_ENTITIES: {"entities": []},
COL_REPLACEMENT_MAP: {"replacements": []},
}
)
result = render_record_html(row)
assert "| process_name" not in result
assert "| file_path" not in result
def test_render_record_html_replaced_tags_positioned_correctly_with_case_mismatch() -> None:
"""Tags in replaced text must not drift when entity value case differs from actual text."""
row = pd.Series(
{
"text": "Hi Mara at the Lantern in Austin TX",
"text_replaced": "Hi [REDACTED_NAME] at [REDACTED_COMPANY] in [REDACTED_CITY] TX",
COL_DETECTED_ENTITIES: {
"entities": [
{"value": "Mara", "label": "first_name", "start_position": 3, "end_position": 7},
{"value": "The Lantern", "label": "company_name", "start_position": 11, "end_position": 22},
{"value": "Austin", "label": "city", "start_position": 26, "end_position": 32},
]
},
COL_REPLACEMENT_MAP: {
"replacements": [
{"original": "Mara", "label": "first_name", "synthetic": "[REDACTED_NAME]"},
{"original": "The Lantern", "label": "company_name", "synthetic": "[REDACTED_COMPANY]"},
{"original": "Austin", "label": "city", "synthetic": "[REDACTED_CITY]"},
]
},
}
)
result = render_record_html(row)
assert "[REDACTED_CITY]" in result
assert "city" in result
assert "[REDACTED| city" not in result
def test_render_record_html_mask_strategy_labels_are_distinct() -> None:
"""When all replacements are identical (e.g. '*****'), each entity keeps its own label."""
row = pd.Series(
{
"text": "Mara in Austin",
"text_replaced": "***** in *****",
COL_DETECTED_ENTITIES: {
"entities": [
{"value": "Mara", "label": "first_name", "start_position": 0, "end_position": 4},
{"value": "Austin", "label": "city", "start_position": 8, "end_position": 14},
]
},
COL_REPLACEMENT_MAP: {
"replacements": [
{"original": "Mara", "label": "first_name", "synthetic": "*****"},
{"original": "Austin", "label": "city", "synthetic": "*****"},
]
},
}
)
result = render_record_html(row)
assert "first_name" in result
assert "city" in result
def test_build_original_entities_from_map_case_insensitive() -> None:
"""Fallback map scanning finds entities regardless of case."""
from anonymizer.interface.display import _build_original_entities_from_map
replacement_map = [{"original": "The Lantern", "label": "company_name"}]
text = "She works at the Lantern daily"
result = _build_original_entities_from_map(replacement_map, text)
assert len(result) == 1
assert result[0].value == "the Lantern"
assert result[0].start_position == 13
def test_display_record_cycles_on_repeated_calls() -> None:
preview = _make_preview(rows=2)
assert preview._display_cycle_index == 0
preview.display_record()
assert preview._display_cycle_index == 1
preview.display_record()
assert preview._display_cycle_index == 0
def test_display_record_explicit_index_does_not_advance_cycle() -> None:
preview = _make_preview(rows=2)
preview.display_record(index=1)
assert preview._display_cycle_index == 0
def test_build_replaced_entities_no_drift_with_case_mismatch() -> None:
"""Regression for #15: case-insensitive expanded entities must resolve correctly.
expand_entity_occurrences stores value=text[start:end] which may differ in case
from the replacement map key (e.g. "the Lantern" vs "The Lantern"). The old
cursor-replay approach missed the map entry and used a wrong-length fallback,
causing cumulative drift on all subsequent entities.
"""
original_text = "Mara works at The Lantern in Austin. the Lantern is her home. Luis visits."
original_entities = [
_entity("Mara", "first_name", 0, 4),
_entity("The Lantern", "company_name", 14, 25),
_entity("Austin", "city", 29, 35),
_entity("the Lantern", "company_name", 37, 48),
_entity("Luis", "first_name", 65, 69),
]
replacement_map = [
{"original": "Mara", "label": "first_name", "synthetic": "Leila"},
{"original": "The Lantern", "label": "company_name", "synthetic": "The Ember"},
{"original": "Austin", "label": "city", "synthetic": "Boulder"},
{"original": "Luis", "label": "first_name", "synthetic": "Diego"},
]
replaced_text = "Leila works at The Ember in Boulder. The Ember is her home. Diego visits."
result = _build_replaced_entities(original_entities, replacement_map, original_text, replaced_text)
assert len(result) == 5
for entity in result:
actual = replaced_text[entity.start_position : entity.end_position]
assert actual == entity.value, (
f"expected {entity.value!r} at [{entity.start_position}:{entity.end_position}], got {actual!r}"
)
def test_build_replaced_entities_no_drift_when_entity_absent_from_map() -> None:
"""When an entity is not in the map, its original text span stays; no drift on later entities."""
original_text = "Contact Sofia and Diego at Acme"
original_entities = [
_entity("Sofia", "first_name", 8, 13),
_entity("Diego", "first_name", 18, 23),
_entity("Acme", "organization", 27, 31),
]
replacement_map = [
{"original": "Diego", "label": "first_name", "synthetic": "Carlos"},
{"original": "Acme", "label": "organization", "synthetic": "NovaCorp"},
]
replaced_text = "Contact Sofia and Carlos at NovaCorp"
result = _build_replaced_entities(original_entities, replacement_map, original_text, replaced_text)
assert len(result) == 3
for entity in result:
actual = replaced_text[entity.start_position : entity.end_position]
assert actual == entity.value
def test_display_record_out_of_bounds_raises() -> None:
preview = _make_preview(rows=1)
with pytest.raises(IndexError, match="out of bounds"):
preview.display_record(index=5)
# ---------------------------------------------------------------------------
# Rewrite-mode display tests
# ---------------------------------------------------------------------------
def test_render_record_html_rewrite_mode_shows_rewrite_layout() -> None:
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {
"entities": [
{"value": "Alice", "label": "first_name", "start_position": 0, "end_position": 5},
{"value": "Acme", "label": "organization", "start_position": 15, "end_position": 19},
]
},
"utility_score": 0.85,
"leakage_mass": 0.3,
"weighted_leakage_rate": 0.23,
"needs_human_review": False,
}
)
result = render_record_html(row, record_index=0)
assert "Rewrite Preview" in result
assert "Original" in result
assert "Rewritten" in result
assert "Scores" in result
assert "0.85" in result
assert "0.30" in result
assert "0.23" in result
assert "Replaced" not in result
assert "Replacement Map" not in result
def test_render_record_html_rewrite_mode_with_judge_scores() -> None:
# Derive keys from the actual rubric configs so test↔runtime drift is impossible.
judge_eval = {
PRIVACY_RUBRIC.name: {"score": "high", "reasoning": "good privacy"},
QUALITY_RUBRIC.name: {"score": "high", "reasoning": "high quality"},
STYLE_RUBRIC.name: {"score": "medium", "reasoning": "mostly natural"},
}
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.9,
"leakage_mass": 0.1,
"needs_human_review": False,
COL_JUDGE_EVALUATION: judge_eval,
}
)
result = render_record_html(row, record_index=0)
assert f"<strong>{PRIVACY_RUBRIC.name}</strong>: high" in result
assert f"<strong>{QUALITY_RUBRIC.name}</strong>: high" in result
assert f"<strong>{STYLE_RUBRIC.name}</strong>: medium" in result
def test_render_record_html_rewrite_mode_nan_judge_column_does_not_warn(
caplog: pytest.LogCaptureFixture,
) -> None:
"""When the judge column exists but a row's value is NaN (typical pandas
missing-value sentinel for object columns), the renderer must not emit
the 'unexpected shape' warning - that's a normal not-yet-judged row."""
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.9,
"leakage_mass": 0.1,
"needs_human_review": False,
COL_JUDGE_EVALUATION: np.nan,
}
)
with caplog.at_level(logging.WARNING, logger="anonymizer.interface.display"):
render_record_html(row, record_index=0)
assert not any("Judge evaluation present but produced no scores" in rec.message for rec in caplog.records)
def test_render_record_html_rewrite_mode_malformed_judge_dict_warns(
caplog: pytest.LogCaptureFixture,
) -> None:
"""A judge column whose value is a dict that yields no extractable scores
is a real upstream-shape regression and SHOULD emit the warning."""
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.9,
"leakage_mass": 0.1,
"needs_human_review": False,
COL_JUDGE_EVALUATION: {"unexpected_key": "no score field here"},
}
)
with caplog.at_level(logging.WARNING, logger="anonymizer.interface.display"):
render_record_html(row, record_index=0)
assert any("Judge evaluation present but produced no scores" in rec.message for rec in caplog.records)
def test_render_record_html_rewrite_mode_with_disposition() -> None:
# Construct the fixture via model_dump() so it matches the exact dict shape
# that LLMStructuredColumnConfig writes to the dataframe at runtime.
disposition = SensitivityDispositionSchema(
sensitivity_disposition=[
EntityDispositionSchema(
id=1,
source="tagged",
category="direct_identifier",
sensitivity="high",
entity_label="first_name",
entity_value="Alice",
needs_protection=True,
protection_reason="Direct identifier that uniquely identifies the subject.",
protection_method_suggestion="replace",
combined_risk_level="high",
),
]
).model_dump()
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.85,
"leakage_mass": 0.3,
"needs_human_review": False,
COL_SENSITIVITY_DISPOSITION: disposition,
}
)
result = render_record_html(row, record_index=0)
assert "Entity Disposition" in result
assert "Alice" in result
assert "first_name" in result
assert "high" in result
assert "replace" in result
def test_render_record_html_replace_mode_unchanged_when_no_rewritten_column() -> None:
"""Existing replace-mode rows still render the Original/Replaced/Replacement Map layout."""
row = pd.Series(
{
"text": "Alice works at Acme",
"text_replaced": "[REDACTED] works at [REDACTED]",
COL_DETECTED_ENTITIES: {"entities": []},
COL_REPLACEMENT_MAP: {"replacements": []},
}
)
result = render_record_html(row)
assert "Replaced" in result
assert "Replacement Map" in result
assert "Rewritten" not in result
assert "Scores" not in result
# ---------------------------------------------------------------------------
# Tests: _extract_judge_scores
# ---------------------------------------------------------------------------
def test_extract_judge_scores_returns_string_scores() -> None:
raw = {
"privacy": {"score": "high", "reasoning": "good"},
"quality": {"score": "medium", "reasoning": "ok"},
"style": {"score": "low", "reasoning": "rough"},
}
result = _extract_judge_scores(raw)
assert result == [("privacy", "high"), ("quality", "medium"), ("style", "low")]
def test_extract_judge_scores_categorical_not_silently_empty() -> None:
"""String scores must not be silently dropped (old int() cast raised ValueError)."""
raw = {"privacy": {"score": "high", "reasoning": "..."}}
result = _extract_judge_scores(raw)
assert len(result) == 1
assert result[0] == ("privacy", "high")
# ---------------------------------------------------------------------------
# Tests: detection_valid and label rendering
# ---------------------------------------------------------------------------
def test_detection_valid_rendered_in_main_scores_section() -> None:
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.9,
"leakage_mass": 0.1,
"needs_human_review": False,
COL_DETECTION_VALID: 0.75,
}
)
result = render_record_html(row, record_index=0)
assert "Detection Validity" in result
assert "0.75" in result
def test_render_record_html_rewrite_mode_detection_valid_none_shows_unavailable() -> None:
"""When evaluate() ran but detection_valid is None, display renders 'Unavailable' not a score."""
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.9,
"leakage_mass": 0.1,
"needs_human_review": False,
COL_DETECTION_VALID: None,
}
)
result = render_record_html(row, record_index=0)
assert "Detection Validity" in result
assert "Unavailable" in result
assert "0." not in result.split("Detection Validity")[1].split("</div>")[0]
def test_render_record_html_rewrite_mode_detection_valid_nan_shows_unavailable() -> None:
"""NaN in COL_DETECTION_VALID (pandas missing-value sentinel) renders 'Unavailable'."""
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.9,
"leakage_mass": 0.1,
"needs_human_review": False,
COL_DETECTION_VALID: np.nan,
}
)
result = render_record_html(row, record_index=0)
assert "Detection Validity" in result
assert "Unavailable" in result
def test_render_record_html_rewrite_mode_no_detection_valid_column_omits_section() -> None:
"""When COL_DETECTION_VALID is absent (evaluate() never called), the row is omitted entirely."""
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.9,
"leakage_mass": 0.1,
"needs_human_review": False,
}
)
result = render_record_html(row, record_index=0)
assert "Detection Validity" not in result
def test_entity_coverage_display_uses_judge_candidate_count_not_anonymizer_detections() -> None:
"""The fraction shown must be n_covered/n_candidates from the judge, not n_detected/total.
4 judge candidates, 3 covered, 1 missed → 3/4 (75%).
If the display used the anonymizer's final_entities count (10) it would show 9/10 instead.
"""
row = pd.Series(
{
COL_ENTITY_COVERAGE: 0.75,
COL_MISSED_ENTITIES: [{"value": "MissedOrg", "label": "org", "reasoning": "org"}],
COL_ENTITY_COVERAGE_N_CANDIDATES: 4,
}
)
html = _render_entity_coverage_section(row)
assert "3/4" in html
assert "75" in html
def test_entity_coverage_display_shows_unavailable_when_score_is_none() -> None:
row = pd.Series(
{
COL_ENTITY_COVERAGE: None,
COL_MISSED_ENTITIES: [],
COL_ENTITY_COVERAGE_N_CANDIDATES: None,
}
)
html = _render_entity_coverage_section(row)
assert "Unavailable" in html
@pytest.mark.parametrize("is_rewrite", [False, True])
def test_entity_coverage_display_explains_zero_candidate_success(is_rewrite: bool) -> None:
row = pd.Series(
{
COL_ENTITY_COVERAGE: 1.0,
COL_MISSED_ENTITIES: [],
COL_ENTITY_COVERAGE_N_CANDIDATES: 0,
}
)
html = _render_entity_coverage_section(row, is_rewrite=is_rewrite)
assert "Satisfied" in html
assert "No candidates found" in html
assert "0/0" not in html
def test_rewrite_needs_human_review_label_is_rewrite_need_review() -> None:
row = pd.Series(
{
"text": "Alice works at Acme",
"text_rewritten": "Beth works at Globex",
COL_DETECTED_ENTITIES: {"entities": []},
"utility_score": 0.9,
"leakage_mass": 0.1,
"needs_human_review": True,
}
)
result = render_record_html(row, record_index=0)
assert "Rewrite Needs Review" in result
assert "<strong>Needs Review:</strong>" not in result