fix: 统一源配置契约字段命名 (#148)

This commit is contained in:
QiuSW
2026-08-31 08:53:22 +08:00
parent 67391acb16
commit 2e61167500
13 changed files with 158 additions and 158 deletions
@@ -67,7 +67,7 @@ def polygon_area(points: list[dict[str, float]]) -> float:
def validate_payload(payload: dict[str, Any]) -> None:
if payload.get("schema") != "yovision.source-config/v1":
if payload.get("schema_version") != "yovision.source-config/v1":
raise ValueError("unknown schema major version")
reject_secrets(payload)
@@ -86,21 +86,21 @@ def validate_payload(payload: dict[str, Any]) -> None:
raise ValueError(f"schema validation failed at {location}: {first.message}")
profile = payload["profile"]
binding = payload["ruleSet"]["profileBinding"]
if (binding["profileId"], binding["width"], binding["height"]) != (
binding = payload["rule_set"]["profile_binding"]
if (binding["profile_id"], binding["width"], binding["height"]) != (
profile["id"],
profile["width"],
profile["height"],
):
raise ValueError("profile binding does not match the media profile")
published_at = datetime.fromisoformat(payload["publishedAt"].replace("Z", "+00:00"))
effective_at = datetime.fromisoformat(payload["effectiveAt"].replace("Z", "+00:00"))
published_at = datetime.fromisoformat(payload["published_at"].replace("Z", "+00:00"))
effective_at = datetime.fromisoformat(payload["effective_at"].replace("Z", "+00:00"))
if effective_at < published_at:
raise ValueError("effectiveAt precedes publishedAt")
raise ValueError("effective_at precedes published_at")
rule_set = payload["ruleSet"]
rules = [*rule_set["areas"], *rule_set["directionalLines"]]
rule_set = payload["rule_set"]
rules = [*rule_set["areas"], *rule_set["directional_lines"]]
identifiers = [rule["id"] for rule in rules]
if len(identifiers) != len(set(identifiers)):
raise ValueError("rule ids must be unique across the rule set")
@@ -110,7 +110,7 @@ def validate_payload(payload: dict[str, Any]) -> None:
for area in rule_set["areas"]:
if polygon_area(area["points"]) <= 1e-12:
raise ValueError(f"area {area['id']} is a degenerate polygon")
for line in rule_set["directionalLines"]:
for line in rule_set["directional_lines"]:
if line["start"] == line["end"]:
raise ValueError(f"directional line {line['id']} has identical endpoints")
@@ -121,8 +121,8 @@ def validate_payload(payload: dict[str, Any]) -> None:
def validate_transition(previous: dict[str, Any], current: dict[str, Any]) -> None:
validate_payload(previous)
validate_payload(current)
if previous["configId"] != current["configId"]:
raise ValueError("configId cannot change within one revision stream")
if previous["config_id"] != current["config_id"]:
raise ValueError("config_id cannot change within one revision stream")
if current["revision"] <= previous["revision"]:
raise ValueError("revision must increase strictly")
@@ -134,16 +134,16 @@ def validate_transition(previous: dict[str, Any], current: dict[str, Any]) -> No
)
previous_rule_versions = sorted(
(rule["id"], rule["version"])
for rule in [*previous["ruleSet"]["areas"], *previous["ruleSet"]["directionalLines"]]
for rule in [*previous["rule_set"]["areas"], *previous["rule_set"]["directional_lines"]]
)
current_rule_versions = sorted(
(rule["id"], rule["version"])
for rule in [*current["ruleSet"]["areas"], *current["ruleSet"]["directionalLines"]]
for rule in [*current["rule_set"]["areas"], *current["rule_set"]["directional_lines"]]
)
if (
profile_changed
and previous_rule_versions == current_rule_versions
and current["ruleSet"]["state"] != "recalibration_required"
and current["rule_set"]["state"] != "recalibration_required"
):
raise ValueError("profile changed without rule recalibration state or new rule versions")
@@ -186,13 +186,13 @@ class SourceConfigV1ContractTests(unittest.TestCase):
def test_profile_revision_and_recalibration_semantics_are_safe(self) -> None:
payload = load_json(VALID_ROOT / "active.json")
payload["ruleSet"]["profileBinding"]["width"] = 1280
payload["rule_set"]["profile_binding"]["width"] = 1280
set_integrity(payload)
with self.assertRaisesRegex(ValueError, "profile binding"):
validate_payload(payload)
payload = load_json(VALID_ROOT / "active.json")
payload["ruleSet"]["state"] = "recalibration_required"
payload["rule_set"]["state"] = "recalibration_required"
set_integrity(payload)
with self.assertRaisesRegex(ValueError, "must not remain enabled"):
validate_payload(payload)
@@ -207,8 +207,8 @@ class SourceConfigV1ContractTests(unittest.TestCase):
current["revision"] += 1
current["profile"].update({"id": "main-stream-v2", "width": 1280, "height": 720})
current["ruleSet"]["profileBinding"].update(
{"profileId": "main-stream-v2", "width": 1280, "height": 720}
current["rule_set"]["profile_binding"].update(
{"profile_id": "main-stream-v2", "width": 1280, "height": 720}
)
set_integrity(current)
with self.assertRaisesRegex(ValueError, "without rule recalibration"):
@@ -218,7 +218,7 @@ class SourceConfigV1ContractTests(unittest.TestCase):
def test_rule_geometry_and_global_ids_are_semantically_validated(self) -> None:
payload = load_json(VALID_ROOT / "active.json")
payload["ruleSet"]["areas"][0]["points"] = [
payload["rule_set"]["areas"][0]["points"] = [
{"x": 0, "y": 0},
{"x": 0.5, "y": 0.5},
{"x": 1, "y": 1},
@@ -228,14 +228,14 @@ class SourceConfigV1ContractTests(unittest.TestCase):
validate_payload(payload)
payload = load_json(VALID_ROOT / "active.json")
payload["ruleSet"]["directionalLines"][0]["id"] = payload["ruleSet"]["areas"][0]["id"]
payload["rule_set"]["directional_lines"][0]["id"] = payload["rule_set"]["areas"][0]["id"]
set_integrity(payload)
with self.assertRaisesRegex(ValueError, "ids must be unique"):
validate_payload(payload)
def test_effective_time_cannot_precede_publication(self) -> None:
payload = load_json(VALID_ROOT / "active.json")
payload["effectiveAt"] = "2026-08-30T23:59:59Z"
payload["effective_at"] = "2026-08-30T23:59:59Z"
set_integrity(payload)
with self.assertRaisesRegex(ValueError, "precedes"):
validate_payload(payload)