"""Load-bearing seam for the S5.4 value report's overlap flagging (målbilde §7). DETACH POINTS THAT TURN THIS RED (documented, not stubbed — the assertions ARE the seam lock): 1. Drop the ``overlaps=ledger.overlaps()`` surfacing in ``build_value_report`` -> the marker overlap key vanishes from BOTH the text and the JSON output -> the positive arm goes RED. 2. Bypass the ledger's dimension-free dedup (``_dedup_amount`` / ``_sum_key``) -> the same candidate realized under two dimensions is double-counted -> ``portfolio_total_ore`` doubles to ``2 * _MARKER_ORE`` -> the control arm goes RED. The MARKER amount + identity appear NOWHERE else in the repo (marker convention, ``tests/test_run_cli_loadbearing.py:25``; absence grep-verified before choosing), so a rendered match is provably caused by THIS ledger flowing through the report, not by incidental repo text. Reuses the same-candidate-two-dimensions construction from ``tests/test_ledger_loadbearing.py:52-66``, rendered through the report. """ from __future__ import annotations import json from portfolio_optimiser.ledger import LedgerEntry, SavingsLedger from portfolio_optimiser.value_report import ( build_value_report, dump_report_json, format_report_text, ) _MARKER_ORE = 58917 # marker: appears nowhere else in the codebase _MARKER_ID = "c-marker-overlap-s54" # marker: appears nowhere else in the codebase def _overlap_ledger() -> SavingsLedger: """One candidate (``_MARKER_ID``) realized under TWO dimensions (energi + asfalt) in P1 — a distinct FULL storage key each, so both are stored (the overlap is visible), while the dimension-free dedup counts the ``_MARKER_ORE`` amount ONCE.""" led = SavingsLedger() for dimension in ("energi", "asfalt"): led.add_realized( LedgerEntry( project_id="P1", dimension=dimension, candidate_identity=_MARKER_ID, amount_ore=_MARKER_ORE, verdict_id="v-marker", provenance="marker-prov", ) ) return led def test_overlap_marker_is_flagged_in_both_renderings() -> None: """POSITIVE: the marker overlap key ``(P1, _MARKER_ID)`` reaches BOTH the human table AND the JSON ``overlaps`` list. RED if ``build_value_report`` stops surfacing ``ledger.overlaps()``.""" report = build_value_report(_overlap_ledger()) assert ("P1", _MARKER_ID) in report.overlaps assert _MARKER_ID in format_report_text(report) # flagged in the rendered table payload = json.loads(dump_report_json(report)) assert ["P1", _MARKER_ID] in payload["overlaps"] # flagged in the JSON (tuple -> list) def test_overlap_amount_counted_once_not_doubled() -> None: """CONTROL: the cross-dimension overlap is counted ONCE in the portfolio total, never doubled. RED if the ledger's dimension-free dedup is bypassed (the total would become ``2 * _MARKER_ORE`` = 117834). This is the exact double-count the ledger's dedup was built to prevent.""" report = build_value_report(_overlap_ledger()) assert report.portfolio_total_ore == _MARKER_ORE # once, NOT 2 * _MARKER_ORE assert report.per_project["P1"] == _MARKER_ORE