"""QuestionUnit 领域实体与 GeneratedQuestion 字段扩展的单元测试。

覆盖：
    - 非 AR single 题的默认值向后兼容（新增字段不改变现有构造点行为）。
    - AR pair 孪生对经 QuestionUnit.from_pair 聚合后携带两条题目。
"""

from core.types import GeneratedQuestion, QuestionUnit


def _q(qid, role="single", pair_id=None, flip_axis=None):
    """构造一条 GeneratedQuestion 测试样本。"""
    return GeneratedQuestion(
        question_id=qid,
        video_id="v",
        task_type="Action Recognition",
        question="?",
        options=("A. a", "B. b", "C. c", "D. d"),
        answer="A",
        source_nodes=("n1",),
        difficulty="hard",
        unit_id=pair_id or qid,
        pair_id=pair_id,
        question_role=role,
        flip_axis=flip_axis,
    )


def test_single_defaults_backward_compatible():
    """不传新字段时，single 题默认值应与旧行为一致。"""
    q = GeneratedQuestion(
        question_id="q",
        video_id="v",
        task_type="X",
        question="?",
        options=("A. a", "B. b", "C. c", "D. d"),
        answer="A",
        source_nodes=("n1",),
        difficulty="hard",
    )
    assert q.question_role == "single"
    assert q.pair_id is None and q.flip_axis is None and q.unit_id == "q"


def test_pair_unit_carries_two_questions():
    """孪生对经 from_pair 聚合后 kind=pair、size=2、unit_id=pair_id。"""
    p = _q("q_o", "pair_original", "pid", "before_after")
    m = _q("q_m", "pair_mirror", "pid", "before_after")
    u = QuestionUnit.from_pair(p, m)
    assert u.kind == "pair" and u.size == 2 and u.unit_id == "pid"
    assert {qq.question_id for qq in u.questions} == {"q_o", "q_m"}
