56 lines
2.3 KiB
Python
56 lines
2.3 KiB
Python
import copy
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
|
|
from test_draft_semantic_review import binding, output, request, sha
|
|
from yuqei_sdk.draft_semantic_review import (
|
|
DraftSemanticReviewOutput,
|
|
DraftSemanticReviewRequest,
|
|
assess_draft_semantic_review,
|
|
draft_semantic_review_output_schema,
|
|
)
|
|
|
|
|
|
TYPED_OUTPUT_SCHEMA_SHA256 = "358e4075892f7e04b8a955008297b9c937a7e5a918e4c515dbf35fcc7f6d9cc5"
|
|
|
|
|
|
def legacy_request() -> DraftSemanticReviewRequest:
|
|
typed = request()
|
|
return typed.model_copy(update={"template": typed.template.model_copy(
|
|
update={"input_schema_sha256": None, "input_fields": ["通知期限", "通知方式"]})})
|
|
|
|
|
|
def test_typed_output_schema_retains_pre_nullable_identity():
|
|
schema = draft_semantic_review_output_schema(request())
|
|
assert sha(schema) == TYPED_OUTPUT_SCHEMA_SHA256
|
|
assert schema["$defs"]["DraftSemanticRequestBinding"]["properties"]["input_schema_sha256"] == {
|
|
"title": "Input Schema Sha256", "type": "string"}
|
|
assert draft_semantic_review_output_schema() == schema
|
|
|
|
|
|
def test_schema_less_request_requires_exact_null_binding_and_keeps_fields():
|
|
req = legacy_request()
|
|
payload = output(request()).model_dump(mode="json")
|
|
payload["request_binding"] = binding(req)
|
|
result = DraftSemanticReviewOutput.model_validate(payload)
|
|
assert result.request_binding.input_schema_sha256 is None
|
|
assert req.template.input_fields == ["通知期限", "通知方式"]
|
|
assert assess_draft_semantic_review(req, result).status == "valid"
|
|
node = draft_semantic_review_output_schema(req)["$defs"]["DraftSemanticRequestBinding"]["properties"]["input_schema_sha256"]
|
|
assert node == {"const": None, "title": "Input Schema Sha256", "type": "null"}
|
|
|
|
forged = copy.deepcopy(payload)
|
|
forged["request_binding"]["input_schema_sha256"] = "f" * 64
|
|
assert "request_binding_mismatch" in assess_draft_semantic_review(
|
|
req, DraftSemanticReviewOutput.model_validate(forged)).errors
|
|
stripped = copy.deepcopy(payload)
|
|
stripped["request_binding"].pop("input_schema_sha256")
|
|
try:
|
|
DraftSemanticReviewOutput.model_validate(stripped)
|
|
except ValueError:
|
|
pass
|
|
else:
|
|
raise AssertionError("schema-less binding must still include explicit null")
|