11"""Unit tests for ScriptSafetyGuard — the core coordination engine."""
22
33import json
4- import logging
54from unittest .mock import patch
65
76import pytest
@@ -257,19 +256,16 @@ def test_multiple_deny_still_deny(self):
257256class TestAuditLog :
258257 """Test that audit logging is emitted correctly."""
259258
260- def test_audit_log_emitted_on_check (self , caplog ):
259+ def test_audit_log_emitted_on_check (self ):
261260 """Verify audit log is produced with expected structure."""
262261 guard = ScriptSafetyGuard ()
263262 input = _make_input (script = "x = 1" )
264263
265- with caplog . at_level ( logging . INFO , logger = "trpc_agent_sdk.tools.safety.audit" ) :
264+ with patch ( "trpc_agent_sdk.tools.safety.guard._audit_logger" ) as mock_audit :
266265 guard .check (input )
267266
268- # Find the audit log record
269- audit_records = [r for r in caplog .records if r .name == "trpc_agent_sdk.tools.safety.audit" ]
270- assert len (audit_records ) == 1
271-
272- entry = json .loads (audit_records [0 ].message )
267+ mock_audit .info .assert_called_once ()
268+ entry = json .loads (mock_audit .info .call_args [0 ][0 ])
273269 assert entry ["event" ] == "safety_check"
274270 assert entry ["decision" ] == "allow"
275271 assert entry ["language" ] == "python"
@@ -279,18 +275,16 @@ def test_audit_log_emitted_on_check(self, caplog):
279275 assert "findings_count" in entry
280276 assert "script_length" in entry
281277
282- def test_audit_log_contains_findings_summary (self , caplog ):
278+ def test_audit_log_contains_findings_summary (self ):
283279 """Audit log findings are present with desensitized evidence."""
284280 guard = ScriptSafetyGuard ()
285281 input = _make_input (script = "import os\n os.system('rm -rf /')" )
286282
287- with caplog . at_level ( logging . INFO , logger = "trpc_agent_sdk.tools.safety.audit" ) :
283+ with patch ( "trpc_agent_sdk.tools.safety.guard._audit_logger" ) as mock_audit :
288284 guard .check (input )
289285
290- audit_records = [r for r in caplog .records if r .name == "trpc_agent_sdk.tools.safety.audit" ]
291- assert len (audit_records ) == 1
292-
293- entry = json .loads (audit_records [0 ].message )
286+ mock_audit .info .assert_called_once ()
287+ entry = json .loads (mock_audit .info .call_args [0 ][0 ])
294288 assert entry ["findings_count" ] > 0
295289 assert len (entry ["findings" ]) > 0
296290 # Each finding has expected fields
0 commit comments