"""The reference list leads with the answer's own source.

The UI activates the first reference that carries a box, so the id order
handed to the grounding resolver decides the default highlight. A scalar
payload names the cell it was read from; that id must come first whether the
model cited it late or not at all. Payloads without a source keep the
model's citation order.
"""

from quber.playground.answers.shapes import Prose, Scalar, Unanswerable
from quber.playground.app import cited_ids_source_first


def test_source_cell_moves_to_front():
    payload = Scalar(value="2.7", source_id="t15-12-1")
    cited = ["t15-line-12", "t15-12-1", "t15-0-1"]
    assert cited_ids_source_first(payload, cited) == ["t15-12-1", "t15-line-12", "t15-0-1"]


def test_uncited_source_is_prepended():
    payload = Scalar(value="2.7", source_id="t15-12-1")
    assert cited_ids_source_first(payload, ["t15-line-12"]) == ["t15-12-1", "t15-line-12"]


def test_payloads_without_source_keep_citation_order():
    cited = ["a", "b"]
    assert cited_ids_source_first(Scalar(value="2.7"), cited) == ["a", "b"]
    assert cited_ids_source_first(Prose(text="because"), cited) == ["a", "b"]
    assert cited_ids_source_first(Unanswerable(reason="not printed"), cited) == ["a", "b"]
