# -*- coding: utf-8 -*- """新视频插入位置纯函数测试: uv run python tests/test_fav_position.py""" import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.stdout.reconfigure(encoding="utf-8") from reaction_board.fav_position import VideoMeta, plan_insertion # noqa: E402 def meta(up_mid, pubdate): return VideoMeta(up_mid=up_mid, pubdate=pubdate) def test_empty_and_single(): assert plan_insertion([], 9, {}, meta(100, 200)).moves == () assert plan_insertion([], 9, {}, meta(100, 200)).target_position == 1 plan = plan_insertion([1], 9, {1: meta(200, 50)}, meta(100, 200)) assert plan.target_position == 1 assert plan.moves == ((1, 9), (9, 1)) def test_same_up_group(): current = [1, 2, 3, 4] metadata = { 1: meta(100, 100), 2: meta(100, 90), 3: meta(200, 80), 4: meta(200, 70), } # 同 UP 组在中间:插到组头 plan = plan_insertion(current, 9, metadata, meta(200, 95)) assert plan.target_position == 3 assert plan.moves == ((2, 9),) # 同 UP 组在首位:两次移动 plan = plan_insertion(current, 9, metadata, meta(100, 200)) assert plan.target_position == 1 assert plan.moves == ((1, 9), (9, 1)) def test_new_group_by_earliest_pubdate(): current = [1, 2, 3, 4] metadata = { 1: meta(100, 100), 2: meta(100, 90), 3: meta(200, 80), 4: meta(200, 70), } # 新 UP 的组键 85,应插到组键 70 的组之前 plan = plan_insertion(current, 9, metadata, meta(300, 85)) assert plan.target_position == 3 assert plan.moves == ((2, 9),) # 比所有组都旧:追加到末尾 plan = plan_insertion(current, 9, metadata, meta(300, 60)) assert plan.target_position == 5 assert plan.moves == ((4, 9),) def test_excludes_new_aid_if_present(): current = [1, 9, 2] metadata = {1: meta(100, 100), 2: meta(100, 90)} plan = plan_insertion(current, 9, metadata, meta(100, 95)) assert plan.target_position == 1 assert plan.moves == ((1, 9), (9, 1)) def main(): test_empty_and_single() test_same_up_group() test_new_group_by_earliest_pubdate() test_excludes_new_aid_if_present() print("OK: 新视频插入位置计算测试全部通过") if __name__ == "__main__": main()