"""★HITL 0 (2026-09-03): A/B 의 arm B 는 사람 리뷰를 지우는 것이 아니라 **참조 줄을 뺀다**.
production 이 리뷰를 안 읽으므로 리뷰 삭제는 아무것도 안 바꾼다 — 그러면 두 arm 이 같아진다."""
from __future__ import annotations

import ast
import inspect
import textwrap

import pytest

from tools.grounding_audit import canary_ab_arms as ab


def _cp():
    return {"status": "completed", "data": {"rows": [
        {"research_subject_id": "LP01#detail", "outcome": "selected"},
        {"research_subject_id": "LP03#detail", "outcome": "selected"},
        {"research_subject_id": "P01", "outcome": "selected"}]}}


def test_ablating_removes_only_those_rows_and_records_it():
    got = ab.ablate_rows(_cp(), ["LP01#detail", "LP03#detail"])
    assert [r["research_subject_id"] for r in got["data"]["rows"]] == ["P01"]
    assert got["data"]["ab_ablated_subjects"] == ["LP01#detail", "LP03#detail"]
    assert len(_cp()["data"]["rows"]) == 3, "★원본을 건드렸다"


def test_an_unknown_subject_stops():
    with pytest.raises(RuntimeError):
        ab.ablate_rows(_cp(), ["없는 대상"])


def test_reproject_reads_the_raw_checkpoint_not_the_reviews():
    src = textwrap.dedent(inspect.getsource(ab.reproject_sidecar))
    called = {getattr(n.func, "attr", "") or getattr(n.func, "id", "")
              for n in ast.walk(ast.parse(src)) if isinstance(n, ast.Call)}
    assert "central_cp_with_reviews" not in called, "★production 과 다른 입력(리뷰 얹은 CP)으로 arm 을 만든다"
    assert "ablate_rows" in called


def test_the_clone_command_no_longer_deletes_reviews():
    src = textwrap.dedent(inspect.getsource(ab.main))
    called = {getattr(n.func, "id", "") for n in ast.walk(ast.parse(src)) if isinstance(n, ast.Call)}
    assert "ablate_reviews" not in called
