File size: 13,294 Bytes
795b72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Unit tests for agentgraph/testing/config.py

Tests configuration models and preset configurations.
"""
import pytest
from pydantic import ValidationError

from agentgraph.testing.config import (
    ExecutionConfig,
    JailbreakTestConfig,
    DemographicConfig,
    CounterfactualBiasTestConfig,
    PerturbationTestConfig,
    PRESET_CONFIGS,
    EXTENDED_DEMOGRAPHICS,
    get_preset_config,
    create_config_from_dict,
)


class TestExecutionConfig:
    """Tests for ExecutionConfig model."""

    def test_default_values(self):
        """Test default configuration values."""
        config = ExecutionConfig()
        assert config.max_workers == 5
        assert config.max_retries == 3
        assert config.base_delay == 1.0
        assert config.max_delay == 60.0
        assert config.rate_limit_per_minute == 60

    def test_custom_values(self):
        """Test custom configuration values."""
        config = ExecutionConfig(
            max_workers=10,
            max_retries=5,
            base_delay=2.0,
            max_delay=120.0,
            rate_limit_per_minute=100
        )
        assert config.max_workers == 10
        assert config.max_retries == 5
        assert config.base_delay == 2.0
        assert config.max_delay == 120.0
        assert config.rate_limit_per_minute == 100

    def test_max_workers_validation(self):
        """Test max_workers validation bounds."""
        # Valid bounds
        assert ExecutionConfig(max_workers=1).max_workers == 1
        assert ExecutionConfig(max_workers=20).max_workers == 20

        # Invalid bounds
        with pytest.raises(ValidationError):
            ExecutionConfig(max_workers=0)
        with pytest.raises(ValidationError):
            ExecutionConfig(max_workers=21)

    def test_max_retries_validation(self):
        """Test max_retries validation bounds."""
        assert ExecutionConfig(max_retries=1).max_retries == 1
        assert ExecutionConfig(max_retries=10).max_retries == 10

        with pytest.raises(ValidationError):
            ExecutionConfig(max_retries=0)
        with pytest.raises(ValidationError):
            ExecutionConfig(max_retries=11)

    def test_base_delay_validation(self):
        """Test base_delay validation bounds."""
        assert ExecutionConfig(base_delay=0.1).base_delay == 0.1
        assert ExecutionConfig(base_delay=10.0).base_delay == 10.0

        with pytest.raises(ValidationError):
            ExecutionConfig(base_delay=0.0)
        with pytest.raises(ValidationError):
            ExecutionConfig(base_delay=11.0)


class TestJailbreakTestConfig:
    """Tests for JailbreakTestConfig model."""

    def test_default_values(self):
        """Test default configuration values."""
        config = JailbreakTestConfig()
        assert config.enabled is True
        assert config.num_techniques == 10
        assert config.technique_categories is None
        assert config.random_seed is None
        assert config.prompt_source == "standard"
        assert config.custom_prompts is None

    def test_num_techniques_validation(self):
        """Test num_techniques validation bounds."""
        assert JailbreakTestConfig(num_techniques=1).num_techniques == 1
        assert JailbreakTestConfig(num_techniques=50).num_techniques == 50

        with pytest.raises(ValidationError):
            JailbreakTestConfig(num_techniques=0)
        with pytest.raises(ValidationError):
            JailbreakTestConfig(num_techniques=51)

    def test_technique_categories(self):
        """Test technique categories filtering."""
        config = JailbreakTestConfig(
            technique_categories=["DAN", "Omega"]
        )
        assert config.technique_categories == ["DAN", "Omega"]

    def test_custom_prompts(self):
        """Test custom prompts configuration."""
        custom = [
            {"name": "test", "prompt": "Test prompt", "description": "Test"}
        ]
        config = JailbreakTestConfig(custom_prompts=custom)
        assert config.custom_prompts == custom

    def test_disabled_config(self):
        """Test disabled jailbreak testing."""
        config = JailbreakTestConfig(enabled=False)
        assert config.enabled is False


class TestDemographicConfig:
    """Tests for DemographicConfig model."""

    def test_basic_demographic(self):
        """Test basic demographic configuration."""
        demo = DemographicConfig(gender="male", race="White")
        assert demo.gender == "male"
        assert demo.race == "White"

    def test_str_representation(self):
        """Test string representation."""
        demo = DemographicConfig(gender="female", race="Black")
        assert str(demo) == "female Black"

    def test_various_demographics(self):
        """Test various demographic combinations."""
        demos = [
            ("male", "White"),
            ("female", "Black"),
            ("non-binary", "Asian"),
            ("male", "Hispanic"),
        ]
        for gender, race in demos:
            demo = DemographicConfig(gender=gender, race=race)
            assert demo.gender == gender
            assert demo.race == race


class TestCounterfactualBiasTestConfig:
    """Tests for CounterfactualBiasTestConfig model."""

    def test_default_values(self):
        """Test default configuration values."""
        config = CounterfactualBiasTestConfig()
        assert config.enabled is True
        assert len(config.demographics) == 4
        assert config.include_baseline is True
        assert config.comparison_mode == "both"
        assert config.extended_dimensions is None

    def test_comparison_mode_enum(self):
        """Test comparison mode enumeration."""
        for mode in ["all_pairs", "vs_baseline", "both"]:
            config = CounterfactualBiasTestConfig(comparison_mode=mode)
            assert config.comparison_mode == mode

        with pytest.raises(ValidationError):
            CounterfactualBiasTestConfig(comparison_mode="invalid")

    def test_custom_demographics(self):
        """Test custom demographics configuration."""
        demos = [
            DemographicConfig(gender="male", race="Asian"),
            DemographicConfig(gender="female", race="Hispanic"),
        ]
        config = CounterfactualBiasTestConfig(demographics=demos)
        assert len(config.demographics) == 2
        assert config.demographics[0].race == "Asian"

    def test_extended_dimensions(self):
        """Test extended dimensions configuration."""
        config = CounterfactualBiasTestConfig(
            extended_dimensions=["age", "disability"]
        )
        assert config.extended_dimensions == ["age", "disability"]

    def test_disabled_config(self):
        """Test disabled bias testing."""
        config = CounterfactualBiasTestConfig(enabled=False)
        assert config.enabled is False


class TestPerturbationTestConfig:
    """Tests for PerturbationTestConfig model."""

    def test_default_values(self):
        """Test default configuration values."""
        config = PerturbationTestConfig()
        assert config.model == "gpt-4o-mini"
        assert config.judge_model == "gpt-4o-mini"
        assert config.max_relations is None
        assert isinstance(config.execution, ExecutionConfig)
        assert isinstance(config.jailbreak, JailbreakTestConfig)
        assert isinstance(config.counterfactual_bias, CounterfactualBiasTestConfig)

    def test_custom_models(self):
        """Test custom model configuration."""
        config = PerturbationTestConfig(
            model="gpt-4o",
            judge_model="gpt-4"
        )
        assert config.model == "gpt-4o"
        assert config.judge_model == "gpt-4"

    def test_max_relations(self):
        """Test max_relations configuration."""
        config = PerturbationTestConfig(max_relations=5)
        assert config.max_relations == 5

        config_all = PerturbationTestConfig(max_relations=None)
        assert config_all.max_relations is None

    def test_nested_config(self):
        """Test nested configuration objects."""
        config = PerturbationTestConfig(
            execution=ExecutionConfig(max_workers=10),
            jailbreak=JailbreakTestConfig(num_techniques=15),
            counterfactual_bias=CounterfactualBiasTestConfig(comparison_mode="all_pairs")
        )
        assert config.execution.max_workers == 10
        assert config.jailbreak.num_techniques == 15
        assert config.counterfactual_bias.comparison_mode == "all_pairs"

    def test_model_dump(self):
        """Test model serialization."""
        config = PerturbationTestConfig()
        data = config.model_dump()

        assert "model" in data
        assert "judge_model" in data
        assert "execution" in data
        assert "jailbreak" in data
        assert "counterfactual_bias" in data


class TestPresetConfigs:
    """Tests for preset configurations."""

    def test_preset_keys(self):
        """Test preset configuration keys exist."""
        assert "quick" in PRESET_CONFIGS
        assert "standard" in PRESET_CONFIGS
        assert "comprehensive" in PRESET_CONFIGS

    def test_quick_preset(self):
        """Test quick preset configuration."""
        config = PRESET_CONFIGS["quick"]
        assert config.max_relations == 3
        assert config.execution.max_workers == 3
        assert config.jailbreak.num_techniques == 3
        assert len(config.counterfactual_bias.demographics) == 2
        assert config.counterfactual_bias.comparison_mode == "vs_baseline"

    def test_standard_preset(self):
        """Test standard preset configuration."""
        config = PRESET_CONFIGS["standard"]
        assert config.max_relations == 10
        assert config.execution.max_workers == 5
        assert config.jailbreak.num_techniques == 10
        assert config.counterfactual_bias.comparison_mode == "both"

    def test_comprehensive_preset(self):
        """Test comprehensive preset configuration."""
        config = PRESET_CONFIGS["comprehensive"]
        assert config.max_relations is None
        assert config.execution.max_workers == 10
        assert config.execution.max_retries == 5
        assert config.jailbreak.num_techniques == 20
        assert len(config.counterfactual_bias.demographics) == 9
        assert config.counterfactual_bias.extended_dimensions == ["age"]


class TestGetPresetConfig:
    """Tests for get_preset_config function."""

    def test_valid_presets(self):
        """Test getting valid presets."""
        for preset_name in ["quick", "standard", "comprehensive"]:
            config = get_preset_config(preset_name)
            assert isinstance(config, PerturbationTestConfig)

    def test_invalid_preset(self):
        """Test invalid preset raises error."""
        with pytest.raises(ValueError) as exc_info:
            get_preset_config("invalid")
        assert "Unknown preset" in str(exc_info.value)

    def test_preset_is_copy(self):
        """Test that preset returns a copy."""
        config1 = get_preset_config("standard")
        config2 = get_preset_config("standard")

        # Modify one should not affect the other
        config1.max_relations = 999
        assert config2.max_relations == 10


class TestCreateConfigFromDict:
    """Tests for create_config_from_dict function."""

    def test_basic_dict(self):
        """Test creating config from basic dict."""
        data = {
            "model": "gpt-4",
            "max_relations": 5
        }
        config = create_config_from_dict(data)
        assert config.model == "gpt-4"
        assert config.max_relations == 5

    def test_nested_dict(self):
        """Test creating config from nested dict."""
        data = {
            "model": "gpt-4",
            "execution": {"max_workers": 8},
            "jailbreak": {"num_techniques": 15, "enabled": True},
            "counterfactual_bias": {"comparison_mode": "all_pairs"}
        }
        config = create_config_from_dict(data)
        assert config.execution.max_workers == 8
        assert config.jailbreak.num_techniques == 15
        assert config.counterfactual_bias.comparison_mode == "all_pairs"

    def test_empty_dict(self):
        """Test creating config from empty dict uses defaults."""
        config = create_config_from_dict({})
        assert config.model == "gpt-4o-mini"
        assert config.execution.max_workers == 5


class TestExtendedDemographics:
    """Tests for extended demographics constants."""

    def test_extended_demographics_keys(self):
        """Test extended demographics keys exist."""
        assert "age" in EXTENDED_DEMOGRAPHICS
        assert "disability" in EXTENDED_DEMOGRAPHICS
        assert "socioeconomic" in EXTENDED_DEMOGRAPHICS

    def test_age_options(self):
        """Test age dimension options."""
        assert len(EXTENDED_DEMOGRAPHICS["age"]) == 3
        assert "young (20s)" in EXTENDED_DEMOGRAPHICS["age"]
        assert "elderly (70s)" in EXTENDED_DEMOGRAPHICS["age"]

    def test_disability_options(self):
        """Test disability dimension options."""
        assert len(EXTENDED_DEMOGRAPHICS["disability"]) == 3

    def test_socioeconomic_options(self):
        """Test socioeconomic dimension options."""
        assert len(EXTENDED_DEMOGRAPHICS["socioeconomic"]) == 3