Ties and all-unsure lines keep the side holding the reported time (toward it once it has left the range) and the flow continues; the page shows only 上一步 / 下一步. Choices now carry the per-card picks, the tally and how the side was decided; old line_at + side choices still read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
114 lines
6.1 KiB
Python
114 lines
6.1 KiB
Python
"""BUG-2355 (负责人 2026-11-25): self-choice ties and all-unsure lines are decided by the system.
|
||
|
||
Fictional reference input only (the same as tests/test_chart_choice.py), no user record.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import sys
|
||
from datetime import datetime
|
||
from pathlib import Path
|
||
|
||
import pytest
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
sys.path.insert(0, str(ROOT))
|
||
sys.path.insert(0, str(ROOT / "scripts"))
|
||
|
||
from scripts.chart_choice.boundaries import ChartChoiceInputError, Line # noqa: E402
|
||
from scripts.chart_choice.service import _card_choice, build_packet, decide_packet # noqa: E402
|
||
|
||
BODY = {
|
||
"lat": 36.42, "lon": 114.21, "tz": 8, "self_choice": True,
|
||
"start_at": "1997-08-08T04:30", "end_at": "1997-08-08T05:30", "reported_at": "1997-08-08T05:00",
|
||
}
|
||
|
||
|
||
def cards(line: dict, *picks: str) -> dict:
|
||
return {"line_at": line["at"], "cards": dict(zip(line["charts"], picks, strict=True))}
|
||
|
||
|
||
def test_tie_keeps_the_side_holding_the_reported_time():
|
||
first = build_packet({**BODY, "choices": []})["next"]
|
||
assert first["charts"] == ["D10", "D2", "D6", "D8"]
|
||
tie = build_packet({**BODY, "choices": [cards(first, "before", "before", "after", "after")]})
|
||
legacy_after = build_packet({**BODY, "choices": [{"line_at": first["at"], "side": "after"}]})
|
||
# 05:00 sits after the 04:59:43 line, so the tie goes 线后 and the flow goes on like any pick.
|
||
assert tie["decision"]["segment"] == legacy_after["decision"]["segment"]
|
||
assert tie["next"]["at"] == legacy_after["next"]["at"]
|
||
assert tie["decision"]["choices"] == [{
|
||
"line_at": first["at"], "side": "after",
|
||
"cards": {"D10": "before", "D2": "before", "D6": "after", "D8": "after"},
|
||
"tally": "tie", "decided_by": "reported_side",
|
||
}]
|
||
# 「两边都不好说」 does not count: 1 : 1 plus two unsure is still a tie.
|
||
mixed = build_packet({**BODY, "choices": [cards(first, "before", "unsure", "after", "unsure")]})
|
||
assert mixed["decision"]["choices"][0]["side"] == "after"
|
||
assert mixed["decision"]["choices"][0]["tally"] == "tie"
|
||
|
||
|
||
def test_majority_still_decides():
|
||
first = build_packet({**BODY, "choices": []})["next"]
|
||
lean = build_packet({**BODY, "choices": [cards(first, "before", "before", "after", "unsure")]})
|
||
assert lean["decision"]["choices"][0] == {
|
||
"line_at": first["at"], "side": "before",
|
||
"cards": {"D10": "before", "D2": "before", "D6": "after", "D8": "unsure"},
|
||
"tally": "majority", "decided_by": "picks",
|
||
}
|
||
assert lean["next"]["at"].endswith("04:51:30")
|
||
|
||
|
||
def test_all_unsure_line_continues_on_the_reported_side():
|
||
first = build_packet({**BODY, "choices": []})["next"]
|
||
unsure = build_packet({**BODY, "choices": [cards(first, "unsure", "unsure", "unsure", "unsure")]})
|
||
# Was: the whole flow ended here. Now it keeps 05:00's side and asks the next line.
|
||
assert "next" in unsure
|
||
assert unsure["decision"]["choices"][0]["side"] == "after"
|
||
assert unsure["decision"]["choices"][0]["tally"] == "all_unsure"
|
||
assert unsure["decision"]["choices"][0]["decided_by"] == "reported_side"
|
||
assert unsure["decision"]["segment"]["start_at"] == first["at"]
|
||
|
||
|
||
def test_reported_time_outside_the_range_goes_toward_it():
|
||
first = build_packet({**BODY, "choices": []})["next"]
|
||
went_before = build_packet({**BODY, "choices": [cards(first, "before", "before", "before", "after")]})
|
||
second = went_before["next"]
|
||
assert second["at"].endswith("04:51:30") and second["charts"] == ["D9"]
|
||
# 05:00 is no longer in 04:30–04:59:42; the undecided line goes toward it, i.e. 线后.
|
||
choices = [*went_before["decision"]["choices"], cards(second, "unsure")]
|
||
decided = decide_packet({**BODY, "choices": choices})["decision"]
|
||
assert decided["choices"][1]["side"] == "after"
|
||
assert decided["choices"][1]["decided_by"] == "toward_reported"
|
||
assert decided["segment"]["start_at"] == second["at"]
|
||
assert decided["saved_at"] == "1997-08-08T04:58"
|
||
# A synthetic tie on a two-chart line, the reported time outside on either side.
|
||
line = Line(datetime(1997, 8, 8, 4, 40), 0, {"D1": (1, 2), "D9": (3, 4)})
|
||
entry = {"line_at": "1997-08-08T04:40:00", "cards": {"D1": "before", "D9": "after"}}
|
||
early = (datetime(1997, 8, 8, 4, 30), datetime(1997, 8, 8, 4, 50))
|
||
assert _card_choice(entry, line, early, datetime(1997, 8, 8, 5, 0))["side"] == "after"
|
||
assert _card_choice(entry, line, early, datetime(1997, 8, 8, 4, 20))["side"] == "before"
|
||
assert _card_choice(entry, line, early, datetime(1997, 8, 8, 4, 35))["decided_by"] == "reported_side"
|
||
|
||
|
||
def test_card_picks_are_checked():
|
||
first = build_packet({**BODY, "choices": []})["next"]
|
||
with pytest.raises(ChartChoiceInputError, match="every chart"):
|
||
build_packet({**BODY, "choices": [{"line_at": first["at"], "cards": {"D10": "before"}}]})
|
||
with pytest.raises(ChartChoiceInputError, match="every chart"):
|
||
build_packet({**BODY, "choices": [cards(first, "before", "before", "after", "maybe")]})
|
||
with pytest.raises(ChartChoiceInputError, match="does not follow"):
|
||
build_packet({**BODY, "choices": [{**cards(first, "before", "before", "after", "after"), "side": "before"}]})
|
||
with pytest.raises(ChartChoiceInputError, match="line_at and side or cards"):
|
||
build_packet({**BODY, "choices": [{"line_at": first["at"], "side": "after", "votes": 3}]})
|
||
# The engine recomputes the tally; stale or forged labels sent back are ignored, not trusted.
|
||
forged = {**cards(first, "before", "before", "after", "after"), "tally": "majority", "decided_by": "picks"}
|
||
assert build_packet({**BODY, "choices": [forged]})["decision"]["choices"][0]["tally"] == "tie"
|
||
|
||
|
||
def test_old_shape_choices_still_read():
|
||
"""Requests and records from before BUG-2355 carry only line_at and side."""
|
||
first = build_packet({**BODY, "choices": []})["next"]
|
||
old = decide_packet({**BODY, "choices": [{"line_at": first["at"], "side": "after"}]})["decision"]
|
||
assert old["choices"] == [{"line_at": first["at"], "side": "after"}]
|
||
ended = build_packet({**BODY, "choices": [{"line_at": first["at"], "side": "unsure"}]})
|
||
assert "next" not in ended and ended["decision"]["choices"] == [{"line_at": first["at"], "side": "unsure"}]
|