78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
"""参考改写授权合同:references 有界、唯一、非空;旧授权载荷向后兼容。"""
|
||
|
||
import pytest
|
||
from pydantic import TypeAdapter, ValidationError
|
||
|
||
from muse.共享.错误 import Muse错误
|
||
from muse.正文写作.接口 import 原文选段, 选段写作授权
|
||
|
||
|
||
def _选段():
|
||
return 原文选段("p1", "0" * 64, 0, 4, "雨声很轻")
|
||
|
||
|
||
def _授权(**changes):
|
||
base = {
|
||
"work_id": "w1",
|
||
"chapter_id": "c1",
|
||
"branch_id": "main",
|
||
"revision": 1,
|
||
"document_hash": "a" * 64,
|
||
"mode": "rewrite",
|
||
"purpose": "改为机甲近身刀法",
|
||
"selections": (_选段(),),
|
||
}
|
||
return 选段写作授权(**{**base, **changes})
|
||
|
||
|
||
@pytest.mark.case_id(
|
||
"NC-w32-a10008",
|
||
environment="离线",
|
||
given="固定选段授权与参考方法版本清单",
|
||
when="构造带 references 的选段写作授权",
|
||
then=["参考清单有界、唯一、非空;默认空清单向后兼容"],
|
||
contract="docs/系统架构/新版设计/模块设计/B06-审校修订.md#参考改写选段改写链路扩展",
|
||
)
|
||
def test_参考清单有界唯一非空且默认兼容__a10008():
|
||
assert _授权().references == ()
|
||
assert _授权(references=("v1", "v2")).references == ("v1", "v2")
|
||
with pytest.raises(Muse错误):
|
||
_授权(references=tuple(f"v{i}" for i in range(9)))
|
||
with pytest.raises(Muse错误):
|
||
_授权(references=("v1", "v1"))
|
||
with pytest.raises(Muse错误):
|
||
_授权(references=("v1", " "))
|
||
|
||
|
||
@pytest.mark.case_id(
|
||
"NC-w32-a10009",
|
||
environment="离线",
|
||
given="不含 references 字段的旧授权载荷",
|
||
when="经公开边界重建授权",
|
||
then=["旧载荷按空参考清单兼容;注入未知字段拒绝"],
|
||
contract="docs/系统架构/新版设计/模块设计/B06-审校修订.md#参考改写选段改写链路扩展",
|
||
)
|
||
def test_旧授权载荷兼容且未知字段拒绝__a10009():
|
||
adapter = TypeAdapter(选段写作授权)
|
||
payload = {
|
||
"work_id": "w1",
|
||
"chapter_id": "c1",
|
||
"branch_id": "main",
|
||
"revision": 1,
|
||
"document_hash": "a" * 64,
|
||
"mode": "rewrite",
|
||
"purpose": "改为机甲近身刀法",
|
||
"selections": [
|
||
{
|
||
"paragraph_id": "p1",
|
||
"paragraph_hash": "0" * 64,
|
||
"start": 0,
|
||
"end": 4,
|
||
"original": "雨声很轻",
|
||
}
|
||
],
|
||
}
|
||
assert adapter.validate_python(payload).references == ()
|
||
with pytest.raises(ValidationError):
|
||
adapter.validate_python({**payload, "references": ["v1"], "unknown_field": True})
|