|
22 | 22 | enforce_anthropic_tool_pairing, |
23 | 23 | get_transition_channel, |
24 | 24 | infer_source_vendor_from_body, |
| 25 | + normalize_for_zhipu, |
25 | 26 | prepare_copilot_to_zhipu, |
26 | 27 | prepare_zhipu_to_anthropic, |
27 | 28 | prepare_zhipu_to_copilot, |
@@ -2147,3 +2148,80 @@ def test_rewrites_srvtoolu_and_strips_vendor_delta(self): |
2147 | 2148 | assert prepared["messages"][1]["content"][0]["tool_use_id"] == new_id |
2148 | 2149 | assert any("zhipu_vendor_blocks" in a for a in adaptations) |
2149 | 2150 | assert any("srvtoolu_ids" in a for a in adaptations) |
| 2151 | + |
| 2152 | + |
| 2153 | +# ── normalize_for_zhipu 共享清洗函数 ──────────────────────── |
| 2154 | + |
| 2155 | + |
| 2156 | +class TestNormalizeForZhipu: |
| 2157 | + """normalize_for_zhipu 共享清洗函数测试.""" |
| 2158 | + |
| 2159 | + def test_strips_cache_control_and_params(self): |
| 2160 | + body = { |
| 2161 | + "model": "claude-sonnet-4-20250514", |
| 2162 | + "messages": [], |
| 2163 | + "thinking": {"type": "enabled", "budget_tokens": 5000}, |
| 2164 | + "extended_thinking": {"type": "enabled"}, |
| 2165 | + "reasoning_effort": "high", |
| 2166 | + "system": [ |
| 2167 | + { |
| 2168 | + "type": "text", |
| 2169 | + "text": "sys", |
| 2170 | + "cache_control": {"type": "ephemeral"}, |
| 2171 | + }, |
| 2172 | + ], |
| 2173 | + "tools": [ |
| 2174 | + { |
| 2175 | + "name": "Bash", |
| 2176 | + "input_schema": {"type": "object"}, |
| 2177 | + "cache_control": {"type": "ephemeral"}, |
| 2178 | + }, |
| 2179 | + ], |
| 2180 | + } |
| 2181 | + result, adaptations = normalize_for_zhipu(body) |
| 2182 | + |
| 2183 | + assert "thinking" not in result |
| 2184 | + assert "extended_thinking" not in result |
| 2185 | + assert "reasoning_effort" not in result |
| 2186 | + assert "cache_control" not in result["system"][0] |
| 2187 | + assert "cache_control" not in result["tools"][0] |
| 2188 | + assert any("cache_control" in a for a in adaptations) |
| 2189 | + assert any("thinking" in a for a in adaptations) |
| 2190 | + assert any("reasoning_effort" in a for a in adaptations) |
| 2191 | + |
| 2192 | + def test_operates_in_place(self): |
| 2193 | + body = {"model": "x", "messages": []} |
| 2194 | + result, _ = normalize_for_zhipu(body) |
| 2195 | + assert result is body |
| 2196 | + |
| 2197 | + def test_idempotent(self): |
| 2198 | + body = { |
| 2199 | + "model": "x", |
| 2200 | + "messages": [], |
| 2201 | + "thinking": {"type": "enabled"}, |
| 2202 | + } |
| 2203 | + normalize_for_zhipu(body) |
| 2204 | + _, adaptations = normalize_for_zhipu(body) |
| 2205 | + assert adaptations == [] |
| 2206 | + |
| 2207 | + def test_no_deep_copy(self): |
| 2208 | + messages = [{"role": "user", "content": "hi"}] |
| 2209 | + body = {"model": "x", "messages": messages} |
| 2210 | + result, _ = normalize_for_zhipu(body) |
| 2211 | + assert result["messages"] is messages |
| 2212 | + |
| 2213 | + def test_preserves_supported_params(self): |
| 2214 | + body = { |
| 2215 | + "model": "x", |
| 2216 | + "messages": [{"role": "user", "content": "hello"}], |
| 2217 | + "max_tokens": 1024, |
| 2218 | + "temperature": 0.7, |
| 2219 | + "stream": True, |
| 2220 | + "metadata": {"user_id": "test"}, |
| 2221 | + } |
| 2222 | + result, adaptations = normalize_for_zhipu(body) |
| 2223 | + assert result["max_tokens"] == 1024 |
| 2224 | + assert result["temperature"] == 0.7 |
| 2225 | + assert result["stream"] is True |
| 2226 | + assert result["metadata"] == {"user_id": "test"} |
| 2227 | + assert adaptations == [] |
0 commit comments