refactor(fudai): 拆出第一次打开读取/验证/筛选 + 第二次重读详情(1212→1121,-91 行)

- reread_detail_for_second_open_fallthrough:第二次打开兜底重读
- perform_first_open_read_validate_filter:第一次打开整套(读取 → 验证失败兜底 →
  筛选 → 高价值决策 → 通过则保存)

新增 8 个单元测试覆盖各分支。1231 → 1239 用例全过。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-22 03:58:19 +08:00
1 parent df8424b706
commit f075f94b31
3 files changed
+275 -99

No files matched your search

+8 -99
View File
@@ -936,103 +936,16 @@ class fudai_analyse:
if is_second_open:
# 第二次打开但 handle_second_open_attend 返回 False(无详情数据需要重读,
# 或参与函数抛错),落入第一次打开路径继续处理
detail = getattr(self, "_last_fudai_detail_tuple", None)
if detail is None or not isinstance(detail, tuple) or len(detail) < 6:
detail = douyin_fudai_detail.read_fudai_detail_with_retries(
self,
fudai_x=0,
read_once_callback=douyin_fudai_detail.read_fudai_detail_once,
close_detail_callback=douyin_fudai_detail.close_fudai_detail,
ensure_open_callback=douyin_fudai_detail.ensure_fudai_detail_open,
)
if detail and isinstance(detail, tuple) and len(detail) >= 6:
self._last_fudai_detail_tuple = detail
detail_t: Tuple[Any, ...] = tuple(detail)
self._current_fudai_content = (
detail_t[0] if len(detail_t) > 0 else ""
)
detail = fudai_detail_blocks.reread_detail_for_second_open_fallthrough(self)
skip = False
skip_reason = ""
else:
# 步骤2:读取福袋详情
step2_start = time.time()
# 第一次打开:完整读取详情(包括内容、倒计时、价值)
detail = douyin_fudai_detail.read_fudai_detail_with_retries(
self,
fudai_x=0,
read_once_callback=douyin_fudai_detail.read_fudai_detail_once,
close_detail_callback=douyin_fudai_detail.close_fudai_detail,
ensure_open_callback=douyin_fudai_detail.ensure_fudai_detail_open,
detail, should_return, skip, skip_reason = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
self, start_time
)
)
# 在第一次打开时立即进行筛选检查,如果不符合要求则直接跳过
# 注意:这里只检查筛选条件,不执行参与操作
if detail and isinstance(detail, tuple) and len(detail) >= 6:
# 先进行数据验证
is_valid = True
try:
is_valid, reason = douyin_fudai_detail.validate_fudai_detail(
detail, self
)
if not is_valid:
try:
self._log_kv(
"流程",
"_handle_in_fudai_detail:第一次打开时数据验证失败,跳过",
validation_reason=reason,
)
except Exception:
pass
fudai_detail_blocks.handle_validation_failure_cleanup(
self, start_time, step2_start
)
return
except Exception:
pass
# 步骤3:筛选检查
step3_start = time.time()
# 执行筛选检查(不执行参与操作,只检查是否符合筛选条件)
try:
skip, skip_reason = douyin_fudai_core.should_skip_fudai_by_config(
self, detail
)
if skip:
# 检查是否是高价值福袋(如果是则记录预约或立即参与)
should_return, skip = (
fudai_detail_blocks.handle_high_value_skip_decide(
self, detail, skip_reason, start_time
)
)
if should_return:
return
# 如果不是高价值福袋,或者不是倒计时原因,筛选不通过,直接跳过
if skip:
fudai_detail_blocks.handle_filter_skip_close_and_switch(
self, start_time, skip_reason
)
return
except Exception as e:
# 筛选检查异常,记录日志但继续流程
try:
self._log_kv(
"流程",
"_handle_in_fudai_detail:筛选检查异常",
error=str(e),
)
except Exception:
pass
# 筛选通过,保存详情数据并标记为已读取
fudai_detail_blocks.save_first_read_detail_success(self, detail)
else:
# 详情数据无效,不保存,不标记为已读取
try:
self._log_kv(
"流程",
"_handle_in_fudai_detail:第一次打开时详情数据无效,跳过",
)
except Exception:
pass
if should_return:
return
# 检查是否是优惠券弹窗、观看任务或识别错误:如果检测到,应该直接跳过,不继续处理
@@ -1104,10 +1017,6 @@ class fudai_analyse:
except Exception:
pass
# 步骤3结束计时
step3_elapsed = (time.time() - step3_start) * 1000
self._log_kv("详情处理", "步骤3[筛选检查]-完成", 总耗时毫秒=step3_elapsed)
# 步骤4:参与决策
step4_start = time.time()
# 执行参与决策
@@ -436,6 +436,118 @@ def extract_future_ts_from_detail(analyser: Any, detail: Any) -> None:
pass
def reread_detail_for_second_open_fallthrough(analyser: Any) -> Any:
"""第二次打开但 handle_second_open_attend 返回 False 时:尝试重读详情。
返回新的 detail(可能为 None)。同时把读到的 detail 保存到
analyser._last_fudai_detail_tuple 和 _current_fudai_content。
"""
from douyin.fudai import douyin_fudai_detail
detail = getattr(analyser, "_last_fudai_detail_tuple", None)
if detail is not None and isinstance(detail, tuple) and len(detail) >= 6:
return detail
detail = douyin_fudai_detail.read_fudai_detail_with_retries(
analyser,
fudai_x=0,
read_once_callback=douyin_fudai_detail.read_fudai_detail_once,
close_detail_callback=douyin_fudai_detail.close_fudai_detail,
ensure_open_callback=douyin_fudai_detail.ensure_fudai_detail_open,
)
if detail and isinstance(detail, tuple) and len(detail) >= 6:
analyser._last_fudai_detail_tuple = detail
detail_t = tuple(detail)
analyser._current_fudai_content = (
detail_t[0] if len(detail_t) > 0 else ""
)
return detail
def perform_first_open_read_validate_filter(
analyser: Any, start_time: float
) -> tuple[Any, bool, bool, str]:
"""第一次打开:读取详情 + 数据验证 + 筛选检查。
返回 (detail, should_return, skip, skip_reason):
- detail:详情 tuple(可能为 None 或长度不够)
- should_return:True 表示主流程已处理完毕、应当 return
- skip / skip_reason:筛选检查结果,供后续步骤使用
"""
from douyin.fudai import douyin_fudai_core, douyin_fudai_detail
step2_start = time.time()
detail = douyin_fudai_detail.read_fudai_detail_with_retries(
analyser,
fudai_x=0,
read_once_callback=douyin_fudai_detail.read_fudai_detail_once,
close_detail_callback=douyin_fudai_detail.close_fudai_detail,
ensure_open_callback=douyin_fudai_detail.ensure_fudai_detail_open,
)
skip: bool = False
skip_reason: str = ""
if not (detail and isinstance(detail, tuple) and len(detail) >= 6):
# 详情数据无效,不保存,不标记为已读取,主流程应 return
try:
analyser._log_kv(
"流程",
"_handle_in_fudai_detail:第一次打开时详情数据无效,跳过",
)
except Exception:
pass
return detail, True, skip, skip_reason
# 数据验证
try:
is_valid, reason = douyin_fudai_detail.validate_fudai_detail(detail, analyser)
if not is_valid:
try:
analyser._log_kv(
"流程",
"_handle_in_fudai_detail:第一次打开时数据验证失败,跳过",
validation_reason=reason,
)
except Exception:
pass
handle_validation_failure_cleanup(analyser, start_time, step2_start)
return detail, True, skip, skip_reason
except Exception:
pass
# 筛选检查
try:
skip, skip_reason = douyin_fudai_core.should_skip_fudai_by_config(
analyser, detail
)
if skip:
should_return, skip = handle_high_value_skip_decide(
analyser, detail, skip_reason, start_time
)
if should_return:
return detail, True, skip, skip_reason
if skip:
handle_filter_skip_close_and_switch(
analyser, start_time, skip_reason
)
return detail, True, skip, skip_reason
except Exception as e:
try:
analyser._log_kv(
"流程",
"_handle_in_fudai_detail:筛选检查异常",
error=str(e),
)
except Exception:
pass
# 筛选通过,保存详情数据并标记为已读取
save_first_read_detail_success(analyser, detail)
return detail, False, skip, skip_reason
def log_entry_flags(analyser: Any) -> None:
"""记录进入 _handle_in_fudai_detail 时的关键 flags 诊断日志。"""
try:
+155
View File
@@ -786,3 +786,158 @@ def test_extract_future_ts_zero_value_no_change():
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
# ---- reread_detail_for_second_open_fallthrough ---- #
def test_reread_detail_returns_existing_if_valid():
"""已有有效 detail 时直接返回,不重读"""
a = MagicMock()
valid = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
a._last_fudai_detail_tuple = valid
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries"
) as fake:
result = fudai_detail_blocks.reread_detail_for_second_open_fallthrough(a)
assert result == valid
fake.assert_not_called()
def test_reread_detail_rereads_when_invalid():
a = MagicMock()
a._last_fudai_detail_tuple = None
new_detail = ("new_content", "x", "x", "x", 10.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=new_detail,
):
result = fudai_detail_blocks.reread_detail_for_second_open_fallthrough(a)
assert result == new_detail
assert a._last_fudai_detail_tuple == new_detail
assert a._current_fudai_content == "new_content"
def test_reread_detail_returns_none_when_reread_fails():
a = MagicMock()
a._last_fudai_detail_tuple = None
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=None,
):
result = fudai_detail_blocks.reread_detail_for_second_open_fallthrough(a)
assert result is None
# ---- perform_first_open_read_validate_filter ---- #
def test_first_open_invalid_detail_returns_should_return_true():
a = MagicMock()
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=None,
):
detail, should_return, skip, skip_reason = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
a, _time.time()
)
)
assert should_return is True
assert detail is None
def test_first_open_validation_failed_triggers_cleanup():
a = MagicMock()
bad_detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=bad_detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(False, "test_reason"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
):
detail, should_return, _, _ = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
a, _time.time()
)
)
assert should_return is True
def test_first_open_filter_passes_returns_should_return_false():
"""筛选通过 → should_return=False,主流程继续"""
a = MagicMock()
good_detail = ("content", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=good_detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(False, ""),
):
detail, should_return, skip, skip_reason = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
a, _time.time()
)
)
assert should_return is False
assert skip is False
# detail 已被保存
assert a._last_fudai_detail_tuple == good_detail
def test_first_open_filter_skip_high_value_records_returns_true():
"""筛选不通过 + 高价值决策返回 should_return=True"""
a = MagicMock()
a.high_value_fudai_enabled = True
a.high_value_fudai_threshold_yuan = 500.0
a.high_value_fudai_join_ahead_seconds = 100
now = _time.time()
detail = ("c", "x", "x", "x", 1000.0, (now - 50, now + 500, 550)) # 远超阈值
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(True, "countdown_gt_120"),
), patch(
"douyin.fudai.douyin_fudai_core.basic_join_decision",
return_value=False,
):
_, should_return, _, _ = (
fudai_detail_blocks.perform_first_open_read_validate_filter(a, now)
)
assert should_return is True
def test_first_open_filter_skip_normal_triggers_close_switch():
"""筛选不通过 + 非高价值 → handle_filter_skip_close_and_switch + should_return=True"""
a = MagicMock()
a.high_value_fudai_enabled = False
a.needswitch = True
detail = ("c", "x", "x", "x", 5.0, ("s", _time.time() + 100, 100))
with patch(
"douyin.fudai.douyin_fudai_detail.read_fudai_detail_with_retries",
return_value=detail,
), patch(
"douyin.fudai.douyin_fudai_detail.validate_fudai_detail",
return_value=(True, ""),
), patch(
"douyin.fudai.douyin_fudai_core.should_skip_fudai_by_config",
return_value=(True, "value_too_low"),
), patch(
"douyin.fudai.douyin_fudai_detail.close_fudai_detail"
):
_, should_return, _, _ = (
fudai_detail_blocks.perform_first_open_read_validate_filter(
a, _time.time()
)
)
assert should_return is True