"""selector_scores 列幂等迁移 + update_selector_scores 写入。"""

import json

from app.question_gen.run_store import QuestionGenStore


def _store(tmp_path):
    return QuestionGenStore(tmp_path / "q.db")


def test_selector_scores_column_exists(tmp_path):
    store = _store(tmp_path)
    cols = {r[1] for r in store._conn.execute("PRAGMA table_info(question_gen_items)")}
    assert "selector_scores" in cols
    store.close()


def test_update_selector_scores_writes_json(tmp_path):
    store = _store(tmp_path)
    store.record_run_start("run1", "sha", "{}")
    store.record_item(
        item_id="it1",
        run_id="run1",
        slot_id="s1",
        video_id="v1",
        family="ACTION_RECOGNITION",
        task_type="Action Recognition",
        skill_target="M1_AR",
        attempt=1,
        question_text="?",
        sub_pattern="temporal_reasoning_failure",
    )
    payload = {
        "correct_score": 0.8,
        "chosen": [0.7, 0.6, 0.55],
        "pool_size": 24,
        "anneal_rounds": 0,
        "hard_fail": False,
    }
    store.update_selector_scores("it1", json.dumps(payload))
    row = store._conn.execute(
        "SELECT selector_scores FROM question_gen_items WHERE item_id='it1'"
    ).fetchone()
    assert json.loads(row[0])["correct_score"] == 0.8
    store.close()


def test_update_selector_scores_unknown_item_raises(tmp_path):
    store = _store(tmp_path)
    try:
        store.update_selector_scores("missing", "{}")
        raise AssertionError("应抛 ValueError")
    except ValueError:
        pass
    store.close()
