11# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22# SPDX-License-Identifier: Apache-2.0
33
4- from collections .abc import Iterable
54import importlib
5+ import json
6+ import os
7+ from typing import List , Tuple
8+ import pytest
69import tempfile
710
8- import pytest
11+ from collections .abc import Iterable
12+ from pathlib import Path
913
1014import garak ._config
1115import garak ._plugins
1216import garak .attempt
1317import garak .evaluators .base
14- import garak .generators .test
18+
19+ from garak .detectors .mitigation import MitigationBypass
20+
1521
1622# probes should be able to return a generator of attempts
1723# -> probes.base.Probe._execute_all (1) should be able to consume a generator of attempts
@@ -26,9 +32,14 @@ def _config_loaded():
2632 importlib .reload (garak ._config )
2733 garak ._config .load_base_config ()
2834 garak ._config .plugins .probes ["test" ]["generations" ] = 1
29- temp_report_file = tempfile .NamedTemporaryFile (mode = "w+" )
30- garak ._config .transient .reportfile = temp_report_file
35+ temp_report_file = tempfile .NamedTemporaryFile (
36+ mode = "w+" , suffix = ".report.jsonl" , delete = False
37+ )
3138 garak ._config .transient .report_filename = temp_report_file .name
39+ garak ._config .transient .reportfile = open (
40+ garak ._config .transient .report_filename , "w" , buffering = 1 , encoding = "utf-8"
41+ )
42+
3243 yield
3344 temp_report_file .close ()
3445
@@ -65,3 +76,34 @@ def test_attempt_outputs_can_consume_generator():
6576 assert len (list (a .outputs )) == len (
6677 outputs_list
6778 ), "attempt.outputs should have the same cardinality every time"
79+
80+
81+ def test_evaluator_detector_naming (mitigation_outputs : Tuple [List [str ], List [str ]]):
82+ COMPLYING_OUTPUTS , REFUSAL_OUTPUTS = mitigation_outputs
83+
84+ d = MitigationBypass ()
85+ attempt = garak .attempt .Attempt (prompt = "testing prompt" , lang = d .lang_spec )
86+ attempt .outputs = COMPLYING_OUTPUTS + REFUSAL_OUTPUTS
87+
88+ detector_probe_name = d .detectorname .replace ("garak.detectors." , "" )
89+
90+ attempt .detector_results [detector_probe_name ] = d .detect (
91+ attempt , case_sensitive = True
92+ )
93+ attempt .probe_classname = detector_probe_name
94+ attempts = [attempt ]
95+
96+ evaluator = garak .evaluators .base .Evaluator ()
97+ evaluator .evaluate (attempts = attempts )
98+
99+ report_filename_path = Path (garak ._config .transient .report_filename )
100+ assert report_filename_path .exists ()
101+
102+ report_json = [
103+ json .loads (line ) for line in report_filename_path .read_text ().split ("\n " )[:- 1 ]
104+ ]
105+ assert len (report_json ) > 0
106+ for report in report_json :
107+ detector = report .get ("detector" , None )
108+ if detector :
109+ assert not detector .startswith ("detector" )
0 commit comments