|
| 1 | +import logging |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pytest |
1 | 5 | from openai.types.realtime.realtime_audio_formats import AudioPCM, AudioPCMA, AudioPCMU |
2 | 6 |
|
| 7 | +from agents import _debug |
3 | 8 | from agents.realtime.audio_formats import to_realtime_audio_format |
4 | 9 |
|
5 | 10 |
|
@@ -51,3 +56,96 @@ def test_to_realtime_audio_format_from_mapping(): |
51 | 56 | assert alaw.type == "audio/pcma" |
52 | 57 |
|
53 | 58 | assert to_realtime_audio_format({"type": "audio/unknown", "rate": 8000}) is None |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize("tool_data_redacted", [False, True]) |
| 62 | +@pytest.mark.parametrize( |
| 63 | + ("input_audio_format", "expected_message", "expected_type"), |
| 64 | + [ |
| 65 | + ("format-secret", "Unknown input audio format", None), |
| 66 | + ( |
| 67 | + {"type": "audio/pcm", "rate": "rate-secret"}, |
| 68 | + "Unknown PCM rate in input audio format mapping", |
| 69 | + AudioPCM, |
| 70 | + ), |
| 71 | + ( |
| 72 | + {"type": "format-secret", "nested": "mapping-secret"}, |
| 73 | + "Unknown input audio format mapping", |
| 74 | + None, |
| 75 | + ), |
| 76 | + ], |
| 77 | +) |
| 78 | +def test_to_realtime_audio_format_redacts_unknown_values( |
| 79 | + monkeypatch: pytest.MonkeyPatch, |
| 80 | + caplog: pytest.LogCaptureFixture, |
| 81 | + tool_data_redacted: bool, |
| 82 | + input_audio_format: Any, |
| 83 | + expected_message: str, |
| 84 | + expected_type: type[AudioPCM] | None, |
| 85 | +) -> None: |
| 86 | + monkeypatch.setattr(_debug, "DONT_LOG_MODEL_DATA", True) |
| 87 | + monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", tool_data_redacted) |
| 88 | + caplog.set_level(logging.DEBUG, logger="openai.agents") |
| 89 | + |
| 90 | + result = to_realtime_audio_format(input_audio_format) |
| 91 | + |
| 92 | + if expected_type is None: |
| 93 | + assert result is None |
| 94 | + else: |
| 95 | + assert isinstance(result, expected_type) |
| 96 | + record = caplog.records[-1] |
| 97 | + assert record.msg == expected_message |
| 98 | + assert record.args == () |
| 99 | + assert record.exc_info is None |
| 100 | + assert record.exc_text is None |
| 101 | + assert all(value is not input_audio_format for value in record.__dict__.values()) |
| 102 | + assert logging.Formatter().format(record) == expected_message |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize("tool_data_redacted", [False, True]) |
| 106 | +def test_to_realtime_audio_format_preserves_diagnostic_mapping( |
| 107 | + monkeypatch: pytest.MonkeyPatch, |
| 108 | + caplog: pytest.LogCaptureFixture, |
| 109 | + tool_data_redacted: bool, |
| 110 | +) -> None: |
| 111 | + input_audio_format = {"type": "format-secret", "nested": "mapping-secret"} |
| 112 | + monkeypatch.setattr(_debug, "DONT_LOG_MODEL_DATA", False) |
| 113 | + monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", tool_data_redacted) |
| 114 | + caplog.set_level(logging.DEBUG, logger="openai.agents") |
| 115 | + |
| 116 | + assert to_realtime_audio_format(input_audio_format) is None |
| 117 | + |
| 118 | + record = caplog.records[-1] |
| 119 | + assert record.msg == "Unknown input_audio_format mapping: %s" |
| 120 | + assert record.args is input_audio_format |
| 121 | + assert record.exc_info is None |
| 122 | + assert record.exc_text is None |
| 123 | + assert "format-secret" in logging.Formatter().format(record) |
| 124 | + assert "mapping-secret" in logging.Formatter().format(record) |
| 125 | + |
| 126 | + |
| 127 | +def test_to_realtime_audio_format_redaction_does_not_render_hostile_mapping( |
| 128 | + monkeypatch: pytest.MonkeyPatch, |
| 129 | + caplog: pytest.LogCaptureFixture, |
| 130 | +) -> None: |
| 131 | + class HostileMapping(dict[str, object]): |
| 132 | + def __str__(self) -> str: |
| 133 | + raise AssertionError("redacted logging must not call __str__") |
| 134 | + |
| 135 | + def __repr__(self) -> str: |
| 136 | + raise AssertionError("redacted logging must not call __repr__") |
| 137 | + |
| 138 | + input_audio_format = HostileMapping(type="unknown") |
| 139 | + monkeypatch.setattr(_debug, "DONT_LOG_MODEL_DATA", True) |
| 140 | + monkeypatch.setattr(_debug, "DONT_LOG_TOOL_DATA", False) |
| 141 | + caplog.set_level(logging.DEBUG, logger="openai.agents") |
| 142 | + |
| 143 | + assert to_realtime_audio_format(input_audio_format) is None |
| 144 | + |
| 145 | + record = caplog.records[-1] |
| 146 | + assert record.msg == "Unknown input audio format mapping" |
| 147 | + assert record.args == () |
| 148 | + assert record.exc_info is None |
| 149 | + assert record.exc_text is None |
| 150 | + assert all(value is not input_audio_format for value in record.__dict__.values()) |
| 151 | + assert logging.Formatter().format(record) == "Unknown input audio format mapping" |
0 commit comments