Spaces:
Running
Running
Download tests/core/test_tool_schema_patterns.py from dherain24/claude-proxy: direct link, hf CLI and curl.
- Browser
- Download file 9.02 kB
-
https://huggingface.co/spaces/dherain24/claude-proxy/resolve/main/tests/core/test_tool_schema_patterns.py
- Command line
-
hf download hf://spaces/dherain24/claude-proxy/tests/core/test_tool_schema_patterns.py
-
curl -L -o test_tool_schema_patterns.py https://huggingface.co/spaces/dherain24/claude-proxy/resolve/main/tests/core/test_tool_schema_patterns.py
9.02 kB
| """Regressions for issue #1730 at the shared Claude conversion boundary.""" | |
| import re | |
| import unicodedata | |
| from copy import deepcopy | |
| import pytest | |
| from free_claude_code.core.anthropic.conversion import build_base_request_body | |
| from free_claude_code.core.anthropic.models import MessagesRequest | |
| from free_claude_code.core.openai_responses.provider_input import ( | |
| build_responses_provider_request, | |
| ) | |
| from free_claude_code.core.reasoning import ReasoningPolicy | |
| from free_claude_code.core.tool_schema_patterns import translate_tool_schema_patterns | |
| ARTIFACT_PATTERN = r'^(?!__.*__$)[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}"\\./[\]]{1,200}$' | |
| def test_claude_converters_preserve_artifact_rules_in_portable_pattern( | |
| wire_format, pattern | |
| ): | |
| schema = { | |
| "type": "object", | |
| "properties": {"name": {"type": "string", "pattern": pattern}}, | |
| "required": ["name"], | |
| } | |
| request = MessagesRequest.model_validate( | |
| { | |
| "model": "test-model", | |
| "messages": [{"role": "user", "content": "Call Artifact with demo"}], | |
| "tools": [ | |
| {"name": "Artifact", "description": "Save", "input_schema": schema} | |
| ], | |
| } | |
| ) | |
| original = deepcopy(request.model_dump()) | |
| if wire_format == "chat": | |
| body = build_base_request_body(request) | |
| parameters = body["tools"][0]["function"]["parameters"] | |
| else: | |
| body = build_responses_provider_request( | |
| request, reasoning=ReasoningPolicy.provider_default() | |
| ) | |
| parameters = body["tools"][0]["parameters"] | |
| translated = parameters["properties"]["name"]["pattern"] | |
| assert r"\p{" not in translated | |
| regex = re.compile(translated) | |
| assert regex.fullmatch("demo") | |
| assert not regex.fullmatch("bad\u2028name") | |
| assert not regex.fullmatch("bad\U000e0001name") | |
| assert not regex.fullmatch("a" * 201) | |
| assert not regex.fullmatch("") | |
| assert parameters["required"] == ["name"] | |
| assert request.model_dump() == original | |
| def translated_pattern(pattern): | |
| return translate_tool_schema_patterns({"pattern": pattern})["pattern"] | |
| def test_compiler_limits_preserve_original_schema_in_both_converters( | |
| wire_format, pattern | |
| ): | |
| schema = {"type": "string", "pattern": pattern} | |
| request = MessagesRequest.model_validate( | |
| { | |
| "model": "test-model", | |
| "messages": [{"role": "user", "content": "Test schema passthrough"}], | |
| "tools": [{"name": "Artifact", "input_schema": schema}], | |
| } | |
| ) | |
| original = deepcopy(request.model_dump()) | |
| if wire_format == "chat": | |
| parameters = build_base_request_body(request)["tools"][0]["function"][ | |
| "parameters" | |
| ] | |
| else: | |
| body = build_responses_provider_request( | |
| request, reasoning=ReasoningPolicy.provider_default() | |
| ) | |
| parameters = body["tools"][0]["parameters"] | |
| assert parameters == schema | |
| assert request.model_dump() == original | |
| def test_category_expansion_preserves_every_unicode_scalar(category): | |
| regex = re.compile(translated_pattern(r"[^\p{" + category + "}]+")) | |
| for point in range(0x110000): | |
| if 0xD800 <= point <= 0xDFFF: | |
| continue | |
| char = chr(point) | |
| assert bool(regex.fullmatch(char)) == ( | |
| unicodedata.category(char) != category | |
| ), f"U+{point:04X}, category {category}, Unicode {unicodedata.unidata_version}" | |
| def test_artifact_restrictions_survive_translation(name, accepted): | |
| assert bool(re.fullmatch(translated_pattern(ARTIFACT_PATTERN), name)) == accepted | |
| def test_unsupported_or_literal_patterns_remain_unchanged(pattern): | |
| schema = {"type": "string", "pattern": pattern} | |
| assert translate_tool_schema_patterns(schema) is schema | |
| def test_class_escaping_is_preserved(pattern, excluded, accepted): | |
| translated = translated_pattern(pattern) | |
| assert translated != pattern | |
| regex = re.compile(translated) | |
| if pattern == r"[^\p{Zl}][^\p{Zp}]": | |
| assert regex.fullmatch(accepted) | |
| assert not regex.fullmatch(excluded) | |
| else: | |
| assert all(not regex.fullmatch(char) for char in excluded) | |
| assert all(regex.fullmatch(char) for char in accepted) | |
| def test_schema_maps_visit_values_and_preserve_names(container): | |
| child = {"type": "string", "pattern": r"[^\p{Cc}]"} | |
| schema = {container: {"pattern": child, r"\p{Cc}": True, "required": ["name"]}} | |
| original = deepcopy(schema) | |
| converted = translate_tool_schema_patterns(schema) | |
| assert converted[container]["pattern"]["pattern"] != child["pattern"] | |
| assert converted[container][r"\p{Cc}"] is True | |
| assert converted[container]["required"] == ["name"] | |
| assert schema == original | |
| def test_single_schema_keywords_are_translated(keyword): | |
| schema = {keyword: {"pattern": r"[^\p{Cf}]"}} | |
| converted = translate_tool_schema_patterns(schema) | |
| assert converted[keyword]["pattern"] != schema[keyword]["pattern"] | |
| def test_schema_arrays_preserve_boolean_and_unchanged_children(keyword): | |
| child = {"type": "string", "pattern": r"[a-z]"} | |
| schema = {keyword: [child, False, {"pattern": r"[^\p{Cf}]"}]} | |
| converted = translate_tool_schema_patterns(schema) | |
| assert converted[keyword][0] is child | |
| assert converted[keyword][1] is False | |
| assert converted[keyword][2]["pattern"] != r"[^\p{Cf}]" | |
| def test_instance_data_is_untouched_and_translation_is_idempotent(): | |
| literal = {"pattern": r"[^\p{Cc}]"} | |
| schema = { | |
| "type": "object", | |
| "properties": {"name": literal}, | |
| "default": literal, | |
| "const": literal, | |
| "enum": [literal], | |
| "examples": [literal], | |
| "x-custom": literal, | |
| "description": r"\p{Cc}", | |
| } | |
| before = deepcopy(schema) | |
| converted = translate_tool_schema_patterns(schema) | |
| for key in ("default", "const", "enum", "examples", "x-custom", "description"): | |
| assert converted[key] is schema[key] | |
| assert schema == before | |
| assert translate_tool_schema_patterns(converted) is converted | |
| def test_non_patterns_remain_unchanged(schema): | |
| assert translate_tool_schema_patterns(schema) is schema | |