diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index ac32567..6d394c1 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1340,25 +1340,7 @@ class fudai_analyse: pass # 筛选通过,保存详情数据并标记为已读取 - try: - self._last_fudai_detail_tuple = detail - self._fudai_detail_first_read = True - - # 设置当前福袋的内容标识(从 detail tuple 的第一个元素获取) - if detail and isinstance(detail, tuple) and len(detail) >= 1: - self._current_fudai_content = str( - detail[0] if detail[0] else "" - ) - - # 映射表已在并行OCR中记录,这里不再重复记录 - self._log_kv( - "流程", - "_handle_in_fudai_detail:福袋详情识别成功", - fudai_content=(detail[0] if len(detail) > 0 else "")[:50], - prize_value_yuan=float(detail[4] if len(detail) > 4 else 0.0), - ) - except Exception: - pass + fudai_detail_blocks.save_first_read_detail_success(self, detail) else: # 详情数据无效,不保存,不标记为已读取 try: @@ -1431,16 +1413,7 @@ class fudai_analyse: attend_before_ts = getattr(self, "_last_successful_attend_ts", 0.0) or 0.0 # 记录当前福袋的 future_ts,供 attend_choujiang 使用 - try: - if isinstance(detail, tuple) and len(detail) >= 6: - d_cast: Tuple[Any, ...] = cast(Tuple[Any, ...], detail) - countdown_tuple = d_cast[5] - if isinstance(countdown_tuple, (tuple, list)) and len(countdown_tuple) >= 2: - future_ts = float(countdown_tuple[1] or 0.0) - if future_ts > 0: - self._current_fudai_future_ts = future_ts - except Exception: - pass + fudai_detail_blocks.extract_future_ts_from_detail(self, detail) # 重置详情关闭标志 try: diff --git a/douyin/core/fudai_modules/fudai_detail_blocks.py b/douyin/core/fudai_modules/fudai_detail_blocks.py index 855504b..53e24ee 100644 --- a/douyin/core/fudai_modules/fudai_detail_blocks.py +++ b/douyin/core/fudai_modules/fudai_detail_blocks.py @@ -397,6 +397,45 @@ def handle_second_open_attend(analyser: Any, start_time: float, step1_start: flo return False +def save_first_read_detail_success(analyser: Any, detail: Any) -> None: + """筛选通过后保存详情数据并标记为已读取。""" + try: + analyser._last_fudai_detail_tuple = detail + analyser._fudai_detail_first_read = True + + if detail and isinstance(detail, tuple) and len(detail) >= 1: + analyser._current_fudai_content = str( + detail[0] if detail[0] else "" + ) + + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:福袋详情识别成功", + fudai_content=(detail[0] if len(detail) > 0 else "")[:50], + prize_value_yuan=float(detail[4] if len(detail) > 4 else 0.0), + ) + except Exception: + pass + + +def extract_future_ts_from_detail(analyser: Any, detail: Any) -> None: + """从 detail 的 countdown_tuple 中提取 future_ts 并保存到 analyser。 + + 若提取失败或 future_ts <= 0,不修改 analyser._current_fudai_future_ts。 + """ + try: + if not (isinstance(detail, tuple) and len(detail) >= 6): + return + countdown_tuple = detail[5] + if not (isinstance(countdown_tuple, (tuple, list)) and len(countdown_tuple) >= 2): + return + future_ts = float(countdown_tuple[1] or 0.0) + if future_ts > 0: + analyser._current_fudai_future_ts = future_ts + except Exception: + pass + + def log_entry_flags(analyser: Any) -> None: """记录进入 _handle_in_fudai_detail 时的关键 flags 诊断日志。""" try: diff --git a/tests/test_fudai_detail_blocks.py b/tests/test_fudai_detail_blocks.py index 8217cd0..69bc601 100644 --- a/tests/test_fudai_detail_blocks.py +++ b/tests/test_fudai_detail_blocks.py @@ -723,3 +723,66 @@ def test_log_entry_flags_swallows_exception(): a = MagicMock() a._log_kv.side_effect = RuntimeError("boom") fudai_detail_blocks.log_entry_flags(a) + + +# ---- save_first_read_detail_success ---- # + +def test_save_first_read_sets_flags_and_content(): + a = MagicMock() + detail = ("content_x", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100)) + fudai_detail_blocks.save_first_read_detail_success(a, detail) + assert a._last_fudai_detail_tuple == detail + assert a._fudai_detail_first_read is True + assert a._current_fudai_content == "content_x" + + +def test_save_first_read_logs_success(): + a = MagicMock() + detail = ("c", "x", "x", "x", 10.0, ("s", _time.time() + 100, 100)) + fudai_detail_blocks.save_first_read_detail_success(a, detail) + success_calls = [ + c for c in a._log_kv.call_args_list + if len(c.args) >= 2 and "福袋详情识别成功" in c.args[1] + ] + assert len(success_calls) == 1 + + +def test_save_first_read_swallows_exception(): + a = MagicMock() + a._log_kv.side_effect = RuntimeError("boom") + fudai_detail_blocks.save_first_read_detail_success(a, ("c",)) + + +# ---- extract_future_ts_from_detail ---- # + +def test_extract_future_ts_with_valid_detail(): + a = MagicMock() + a._current_fudai_future_ts = 0.0 + target_ts = _time.time() + 100 + detail = ("c", "x", "x", "x", 5.0, ("s", target_ts, 100)) + fudai_detail_blocks.extract_future_ts_from_detail(a, detail) + assert a._current_fudai_future_ts == target_ts + + +def test_extract_future_ts_short_tuple_no_change(): + a = MagicMock() + a._current_fudai_future_ts = 999.0 + detail = ("only_one_elem",) + fudai_detail_blocks.extract_future_ts_from_detail(a, detail) + assert a._current_fudai_future_ts == 999.0 + + +def test_extract_future_ts_invalid_countdown_no_change(): + a = MagicMock() + a._current_fudai_future_ts = 999.0 + detail = ("c", "x", "x", "x", 5.0, "not_a_tuple") + fudai_detail_blocks.extract_future_ts_from_detail(a, detail) + assert a._current_fudai_future_ts == 999.0 + + +def test_extract_future_ts_zero_value_no_change(): + a = MagicMock() + a._current_fudai_future_ts = 999.0 + detail = ("c", "x", "x", "x", 5.0, ("s", 0, 0)) + fudai_detail_blocks.extract_future_ts_from_detail(a, detail) + assert a._current_fudai_future_ts == 999.0