From fa4a7e220b500aa62e819fe39e774ab1c153d00b Mon Sep 17 00:00:00 2001 From: iomgaa Date: Sun, 16 Aug 2026 06:50:50 -0400 Subject: [PATCH] test: make the 429 red line actually catch its violation Verifier mutation test: flipping _translate_429 to parse the summary left all 824 tests green. The padding was one long string value, so the cut landed inside it - and head-and-tail retention kept the trailing error object, leaving the summary parseable. Many keys put the cut between structural tokens, where the summary stops being valid JSON. Mutation now fails as it should. Also splits OCR 429 out on its own. --- tests/unit/test_monkey_ocr.py | 7 ++++++- tests/unit/test_openai_compat.py | 12 +++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_monkey_ocr.py b/tests/unit/test_monkey_ocr.py index 87f052c..77c4461 100644 --- a/tests/unit/test_monkey_ocr.py +++ b/tests/unit/test_monkey_ocr.py @@ -312,7 +312,12 @@ class TestErrorTranslation: @pytest.mark.parametrize( ("status", "exc_type"), - [(502, TransientError), (401, SourceDeadError), (404, RequestRejectedError)], + [ + (502, TransientError), + (429, TransientError), # 与 5xx 共用分支,但仍单列: 漏分支正是 issue #10 的成因 + (401, SourceDeadError), + (404, RequestRejectedError), + ], ) async def test_body_survives_every_branch(self, status, exc_type): """issue #10: OCR 侧 message 原本只有 HTTP 状态码,拒绝理由同样丢失。""" diff --git a/tests/unit/test_openai_compat.py b/tests/unit/test_openai_compat.py index 62c7389..2e2067c 100644 --- a/tests/unit/test_openai_compat.py +++ b/tests/unit/test_openai_compat.py @@ -16,6 +16,7 @@ from polygateway.errors import ( ) from polygateway.middleware.telemetry import TelemetryEmitter from polygateway.pricing import ModelPrice, PricingTable +from polygateway.transports._http_errors import summarize_body from polygateway.transports.openai_compat import ( OpenAICompatTransport, _iter_sse_deltas, @@ -754,11 +755,20 @@ class TestErrorBodyRetention: 摘要会破坏 JSON 结构,若改用摘要解析,超长 body 的配额耗尽将退化成普通 限速——配额已耗尽的源不再 force_open,一个诊断改进就变成了治理 bug。 + + **填充必须是多个键**,不能是单个超长字符串值: 后者的截断点落在字符串 + *内部*,省略标记成了合法的字符串内容,而头尾保留又让尾部的 error 对象 + 幸存——摘要照样解析得出 `insufficient_quota`,用例即告空转(2026-08-16 + verifier 变异测试发现: 按错误写法实现,全套件 824 项依然全绿)。多键 + 填充让截断点落在结构记号之间,摘要才真正不可解析。 """ body = json.dumps( - {"padding": "P" * 4000, "error": {"type": "insufficient_quota", "message": "spent"}} + {**{f"k{i}": "v" * 10 for i in range(300)}, "error": {"type": "insufficient_quota"}} ) assert len(body) > 2048 + with pytest.raises(json.JSONDecodeError): + # 判别力的前提: 摘要确实不再是合法 JSON,读它必然拿不到 type + json.loads(summarize_body(body)) def handler(request): return httpx.Response(429, content=body.encode())