fix: finalize dynamic choice labels

This commit is contained in:
Jesse_Chen
2026-07-19 04:13:12 +08:00
parent 35212af0fc
commit d9969d0829
8 changed files with 211 additions and 110 deletions
@@ -4,8 +4,6 @@ import json
import re
from pathlib import Path
import pytest
from scripts.dynamic_rectification_opportunities import _dimension_opportunity
TASK2_PACKET_FIXTURE = (
@@ -14,20 +12,7 @@ TASK2_PACKET_FIXTURE = (
)
@pytest.mark.parametrize(
("dimension", "domain_terms"),
[
("education", ("升学", "转学", "学习")),
("relocation", ("搬家", "离乡", "居住")),
("relationship", ("关系",)),
("career", ("工作", "职业", "身份")),
("health_pressure", ("健康", "压力", "生活")),
],
)
def test_every_supported_dimension_emits_localized_validator_safe_copy(
dimension: str,
domain_terms: tuple[str, ...],
) -> None:
def test_every_supported_dimension_emits_distinct_validator_safe_copy() -> None:
windows = [
{
"window_start": "2018-01-01",
@@ -41,26 +26,27 @@ def test_every_supported_dimension_emits_localized_validator_safe_copy(
},
]
opportunity = _dimension_opportunity(dimension, windows, ["04:00", "04:01"])
assert opportunity is not None
context = opportunity["neutral_context"]
prompt = opportunity["fallback_prompt"]
assert any(term in context for term in domain_terms)
assert dimension not in context
assert re.search(r"[\u3400-\u9fff]", context)
assert re.search(r"[A-Za-z]", context) is None
assert context in prompt
assert prompt.endswith("")
assert re.search(r"[A-Za-z]", prompt) is None
assert all(label.endswith("") for label in (
item["fallback_label"] for item in opportunity["partitions"]
))
assert [item["fallback_label"].replace("", "") for item in opportunity["partitions"]] == [
"2018—2020",
"2021—2023",
dimensions = ["education", "relocation", "relationship", "career", "health_pressure"]
opportunities = [
_dimension_opportunity(dimension, windows, ["04:00", "04:01"])
for dimension in dimensions
]
assert all(opportunity is not None for opportunity in opportunities)
contexts = [opportunity["neutral_context"] for opportunity in opportunities if opportunity]
assert len(contexts) == len(set(contexts)) == len(dimensions)
for dimension, opportunity in zip(dimensions, opportunities, strict=True):
assert opportunity is not None
context = opportunity["neutral_context"]
prompt = opportunity["fallback_prompt"]
labels = [item["fallback_label"] for item in opportunity["partitions"]]
assert dimension not in context
assert re.search(r"[\u3400-\u9fff]", context)
assert re.search(r"[A-Za-z]", context + prompt) is None
assert prompt.endswith("") and prompt.count("") == 1
assert len(labels) == len(set(label.replace(" ", "") for label in labels))
assert all(re.search(r"[\u3400-\u9fff]", label) for label in labels)
def test_frontend_adapter_fixture_is_real_task2_opportunity_output() -> None:
packet = json.loads(TASK2_PACKET_FIXTURE.read_text(encoding="utf-8"))
@@ -89,11 +75,10 @@ def test_frontend_adapter_fixture_is_real_task2_opportunity_output() -> None:
]
assert fixture["dimension_code"] == "career"
assert "career" not in fixture["neutral_context"]
assert fixture["neutral_context"] == opportunity["neutral_context"]
assert fixture["fallback_prompt"] == opportunity["fallback_prompt"]
assert [item["fallback_label"] for item in fixture["partitions"]] == [
item["fallback_label"] for item in opportunity["partitions"]
]
assert len(fixture["partitions"]) == len(opportunity["partitions"])
assert re.search(r"[A-Za-z]", fixture["neutral_context"] + fixture["fallback_prompt"]) is None
fixture_labels = [item["fallback_label"] for item in fixture["partitions"]]
assert len(fixture_labels) == len(set(label.replace(" ", "") for label in fixture_labels))
def test_same_year_windows_receive_distinct_visible_labels() -> None:
@@ -116,3 +101,25 @@ def test_same_year_windows_receive_distinct_visible_labels() -> None:
labels = [item["fallback_label"] for item in opportunity["partitions"]]
assert len(labels) == len(set(labels))
assert all("" in label for label in labels)
def test_same_month_windows_receive_distinct_day_precision_labels() -> None:
windows = [
{
"window_start": "2012-01-01",
"window_end": "2012-01-10",
"activations": {"04:00": 1.0, "04:01": 0.0},
},
{
"window_start": "2012-01-11",
"window_end": "2012-01-20",
"activations": {"04:00": 0.0, "04:01": 1.0},
},
]
opportunity = _dimension_opportunity("career", windows, ["04:00", "04:01"])
assert opportunity is not None
labels = [item["fallback_label"] for item in opportunity["partitions"]]
assert len(labels) == len(set(labels))
assert all("" in label for label in labels)