"""离线诊断注入：build_diagnosis_deps 按 video_ids 填充非空 tree_data。

自包含：在 tmp_path 造最小 tree.json，不依赖真实 store/videos/*（CI/checkout 稳健）。
下面 patch 的三项是 build_diagnosis_deps 的**外部依赖边界**（假 LLM 凭证、LLM HTTP
客户端、redis 缓存），非实现细节——避免测试触真 .env / 打开 httpx.AsyncClient /
连 redis，聚焦"给定 store_dir/video_ids → tree_data 被真实树填充"。
"""

import json
from unittest.mock import patch

from app.harness.video_split_cli import build_diagnosis_deps


def test_build_diagnosis_deps_loads_tree_for_videos(tmp_path):
    vdir = tmp_path / "videos" / "vX"
    vdir.mkdir(parents=True)
    (vdir / "tree.json").write_text(
        json.dumps(
            {
                "metadata": {},
                "roots": [
                    {"id": "vX_L1_000", "card": {"scene_summary": "s"}, "time_range": [0, 1]}
                ],
            }
        ),
        encoding="utf-8",
    )

    with (
        patch("app.harness.video_split_cli._DiagLLMSettings") as settings_cls,
        patch("adapters.llm.GovernedLLMClient"),
        patch("app.harness.video_split_cli._build_redis_cache", return_value=None),
    ):
        s = settings_cls.return_value
        s.search_llm_model = "deepseek-v4-pro"
        s.search_llm_base_url = "http://x"
        s.search_llm_api_key = "k"
        s.llm_circuit_breaker_threshold = 32
        s.llm_circuit_breaker_cooldown = 60
        s.llm_timeout = s.llm_ttft_timeout = s.llm_inter_token_timeout = 60
        s.llm_max_retries = 1
        s.llm_retry_base_delay = s.llm_retry_max_delay = 1
        deps = build_diagnosis_deps(
            harness_db=tmp_path / "h.db",
            store_dir=tmp_path,
            video_ids=["vX"],
            concurrency=1,
            expected_model="deepseek-v4-pro",
        )

    assert "vX" in deps.tree_data
    assert deps.tree_data["vX"]["nodes"]
