"""GPT-5.6 이관(2026-07-10) — config_hash 의 모델 민감도 잠금.

Codex 리뷰 NARROW_2 대응: 6개 스텝의 _config_hash payload 가 plain
"gpt-5.5" literal 이어서 물리 모델 교체 시 completed checkpoint 가 stale
감지되지 않던 결함. settings.openai_model 을 payload 에 넣어 다음 물리
모델 교체도 자동 invalidation 되도록 고정한다.
"""
import pytest

from app.core.config import settings


def _hash_of(step_cls, monkeypatch, model):
    step = object.__new__(step_cls)
    monkeypatch.setattr(settings, "openai_model", model)
    return step._config_hash()


def _collect():
    from app.core.steps.background_master_plan_step import (
        BackgroundMasterPlanStep,
    )
    from app.core.steps.background_classify_step import BackgroundClassifyStep
    from app.core.steps.background_chain_planning_step import (
        BackgroundChainPlanningStep,
    )
    from app.core.steps.background_planner_step import BackgroundPlannerStep
    from app.core.steps.outdoor_place_spec_step import OutdoorPlaceSpecStep
    from app.core.steps.outdoor_shot_grounding_step import (
        OutdoorShotGroundingStep,
    )
    return [
        BackgroundMasterPlanStep,
        BackgroundClassifyStep,
        BackgroundChainPlanningStep,
        BackgroundPlannerStep,
        OutdoorPlaceSpecStep,
        OutdoorShotGroundingStep,
    ]


@pytest.mark.parametrize("step_cls", _collect())
def test_config_hash_drifts_on_openai_model_change(step_cls, monkeypatch):
    """openai_model 물리 모델 교체 → _config_hash 변화 (stale 감지)."""
    h_old = _hash_of(step_cls, monkeypatch, "gpt-5.5")
    h_new = _hash_of(step_cls, monkeypatch, "gpt-5.6-sol")
    assert h_old != h_new, step_cls.__name__


@pytest.mark.parametrize("step_cls", _collect())
def test_config_hash_stable_for_same_model(step_cls, monkeypatch):
    """동일 모델 → hash 결정론 (재실행 안정)."""
    h1 = _hash_of(step_cls, monkeypatch, "gpt-5.6-sol")
    h2 = _hash_of(step_cls, monkeypatch, "gpt-5.6-sol")
    assert h1 == h2, step_cls.__name__
