diff --git a/app/harness/tree_nodes.py b/app/harness/tree_nodes.py index 39f6ccb..5db245d 100644 --- a/app/harness/tree_nodes.py +++ b/app/harness/tree_nodes.py @@ -30,7 +30,7 @@ def load_tree_nodes(store_dir: Path, video_id: str) -> dict[str, Any]: 异常: FileNotFoundError: tree.json 不存在(沿用 factory.py fail-loud 先例)。 - ValueError: 树无有效 roots、节点缺 id、或展平后 nodes 为空。 + ValueError: roots 非 list 或为空、节点缺 id、或节点既无 time_range 又无 timestamp。 关键实现: level 由遍历深度赋值(root=1/child=2/孙=3),不解析 node_id——node_id 累积式 @@ -54,7 +54,11 @@ def load_tree_nodes(store_dir: Path, video_id: str) -> dict[str, Any]: time_range = node.get("time_range") if time_range is None: ts = node.get("timestamp") - time_range = [ts, ts] if ts is not None else [0, 0] + if ts is None: + raise ValueError( + f"节点既无 time_range 又无 timestamp(树损坏): {node_id} in {tree_path}" + ) + time_range = [ts, ts] nodes[node_id] = { "card": node.get("card", {}), "level": level, diff --git a/tests/unit/test_tree_nodes.py b/tests/unit/test_tree_nodes.py index 72cb7e9..89a9091 100644 --- a/tests/unit/test_tree_nodes.py +++ b/tests/unit/test_tree_nodes.py @@ -7,7 +7,7 @@ import pytest from app.harness.tree_nodes import load_tree_data_for_videos, load_tree_nodes -_STORE = Path("store") +_STORE = Path(__file__).resolve().parents[2] / "store" _VID = "0RxMZBLeqRI" # 真实样本,111 节点 @@ -68,6 +68,16 @@ def test_roots_not_list_raises_value_error(tmp_path): load_tree_nodes(tmp_path, "vX") +def test_node_without_time_info_raises_value_error(tmp_path): + vdir = tmp_path / "videos" / "vX" + vdir.mkdir(parents=True) + (vdir / "tree.json").write_text( + json.dumps({"roots": [{"id": "n1", "card": {}}]}), encoding="utf-8" + ) + with pytest.raises(ValueError): + load_tree_nodes(tmp_path, "vX") + + def test_load_for_videos_dedups(): data = load_tree_data_for_videos(_STORE, [_VID, _VID]) assert set(data.keys()) == {_VID}