Files
iishko-public/tests/test_bot_content_policy.py
reformandClaude Opus 5.5 0ee6fb843b
checks / pytest (push) Has been cancelled
checks / moderation (push) Has been cancelled
checks / node (push) Has been cancelled
IISHKO: Telegram bot, Mini App and gateway
Initial public release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 21:51:30 +03:00

112 lines
5.4 KiB
Python

"""Provider policy failures must remain actionable, terminal and non-replayed."""
import json
import unittest
from test_bot_stability import Session, Response, actual_gateway, frame
class ContentPolicyContracts(unittest.IsolatedAsyncioTestCase):
def response(self, *, status=400, code="data_inspection_failed", stream=False, prefix=""):
body = {"error": {"message": prefix + "private-provider-text", "code": code, "request_id": "Policy-Trace-123"}}
if stream:
return Response([("data: " + json.dumps(body) + "\n\n").encode()])
response = Response(status=status, body=body)
async def read(size):
return json.dumps(body).encode()[:size]
response.content.read = read
return response
async def assert_policy(self, response, streaming):
session = Session([response])
ns, gate = actual_gateway(session)
async def send(text):
pass
with self.assertRaises(RuntimeError) as caught:
if streaming:
await ns["gw_chat_stream"]([], "model-x", send)
else:
await ns["gw_call"]("/chat/completions", {"model": "model-x"})
error = caught.exception
self.assertTrue(getattr(error, "terminal", False))
self.assertTrue(getattr(error, "content_policy", False))
self.assertEqual(getattr(error, "status", None), 400)
self.assertFalse(getattr(error, "retry_safe", True))
self.assertFalse(getattr(error, "stability_dependency_failure", True))
self.assertIn("контент-фильтр", str(error))
self.assertNotIn("private-provider-text", str(error))
self.assertEqual(getattr(error, "request_id", None), "Policy-Trace-123")
self.assertEqual(len(session.calls), 1)
self.assertEqual(session.calls[0][1]["json"]["model"], "model-x")
self.assertEqual(gate.controller.snapshot()["active"], 0)
self.assertEqual(gate.controller.snapshot()["outcomes"]["dependency_failure"], 0)
async def test_nonstream_code_only_policy_is_terminal_without_model_penalty(self):
await self.assert_policy(self.response(), False)
async def test_http_stream_policy_preserves_reason(self):
await self.assert_policy(self.response(), True)
async def test_sse_error_frame_policy_preserves_reason(self):
await self.assert_policy(self.response(stream=True), True)
async def test_long_policy_body_not_truncated_before_code(self):
await self.assert_policy(self.response(prefix="x" * 700), True)
async def test_normalized_policy_code_is_recognized(self):
await self.assert_policy(self.response(code="content_policy_violation"), False)
async def test_wrapped_503_policy_not_retried_as_outage(self):
await self.assert_policy(self.response(status=503), True)
async def test_unsupported_content_filter_parameter_not_a_policy_failure(self):
response = self.response(code="unsupported_parameter", prefix="content_filter unsupported: ")
session = Session([response])
ns, _ = actual_gateway(session)
with self.assertRaises(RuntimeError) as caught:
await ns["gw_call"]("/chat/completions", {"model": "model-x"})
self.assertFalse(getattr(caught.exception, "content_policy", False))
self.assertEqual(len(session.calls), 1)
async def test_partial_policy_failure_never_replays_generated_content(self):
response = self.response(stream=True)
response.content.frames.insert(0, frame("already generated"))
session = Session([response])
ns, gate = actual_gateway(session)
sent = []
async def send(text):
sent.append(text)
result = await ns["gw_chat_stream"]([], "model-x", send)
self.assertIn("already generated", result)
self.assertTrue(getattr(result, "incomplete", False))
self.assertEqual(len(session.calls), 1)
self.assertEqual(gate.controller.snapshot()["outcomes"]["dependency_failure"], 0)
self.assertTrue(sent)
async def test_sse_filtered_finish_is_partial_not_success(self):
session = Session([Response([frame("partial text", finish="content_filter")])])
ns, gate = actual_gateway(session)
async def send(text):
pass
result = await ns["gw_chat_stream"]([], "model-x", send)
self.assertEqual(str(result), "partial text")
self.assertTrue(getattr(result, "incomplete", False))
self.assertEqual(len(session.calls), 1)
self.assertEqual(gate.controller.snapshot()["outcomes"]["success"], 0)
self.assertEqual(gate.controller.snapshot()["outcomes"]["dependency_failure"], 0)
async def test_nonstream_filtered_finish_is_not_an_empty_outage(self):
response = Response(body={"choices": [
{"message": {"content": None}, "finish_reason": "content_filter"}
]})
session = Session([response])
ns, gate = actual_gateway(session)
with self.assertRaises(RuntimeError) as caught:
await ns["gw_chat"]([], "model-x")
self.assertTrue(getattr(caught.exception, "content_policy", False))
self.assertEqual(len(session.calls), 1)
self.assertEqual(gate.controller.snapshot()["outcomes"]["dependency_failure"], 0)
if __name__ == "__main__":
unittest.main()