refactor(fudai): 拆出 fudai_choujiang 入口的 5 个子块(1879→1704,-175 行)

把 fudai_choujiang 中互相独立的初始化与循环子块抽到 fudai_run_blocks:
- cleanup_old_screenshot_at_startup:启动时清理旧截图
- resolve_device_resolution:分辨率重试(含默认回退)
- write_actions_log_separator:actions.log 分隔符写入
- run_basic_loop_with_timeout:定时循环模式
- run_basic_loop_forever:无限循环模式(含 KeyboardInterrupt 处理)

主入口从 237 行降到 ~60 行。新增 9 个单元测试覆盖各分支。
1172 → 1181 用例全过。

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:27:34 +08:00
1 parent cdcabb665d
commit 9dee41b976
3 files changed
+286 -186

No files matched your search

+109
View File
@@ -737,6 +737,115 @@ def test_log_needswitch_empty_queue_logs_zero_count():
assert call.kwargs["first_reservation"] == {}
# ---- cleanup_old_screenshot_at_startup ---- #
def test_cleanup_old_screenshot_no_file_no_error(tmp_path, monkeypatch):
a = _make_analyser()
fake_path = tmp_path / "no_such.png"
monkeypatch.setattr(
"douyin.core.path_config.get_screenshot_path",
lambda: str(fake_path),
)
fudai_run_blocks.cleanup_old_screenshot_at_startup(a)
def test_cleanup_old_screenshot_removes_existing_file(tmp_path, monkeypatch):
a = _make_analyser()
pic_path = tmp_path / "screenshot.png"
pic_path.write_text("dummy", encoding="utf-8")
monkeypatch.setattr(
"douyin.core.path_config.get_screenshot_path",
lambda: str(pic_path),
)
fudai_run_blocks.cleanup_old_screenshot_at_startup(a)
assert not pic_path.exists()
# ---- resolve_device_resolution ---- #
def test_resolve_resolution_no_device_returns_default():
a = _make_analyser()
a.device_id = ""
rx, ry = fudai_run_blocks.resolve_device_resolution(a)
assert (rx, ry) == (1080, 2400)
def test_resolve_resolution_first_attempt_succeeds():
a = _make_analyser()
a.device_id = "test"
a.operation.get_device_resolution.return_value = (720, 1600)
rx, ry = fudai_run_blocks.resolve_device_resolution(a, max_attempts=5)
assert (rx, ry) == (720, 1600)
assert a.operation.get_device_resolution.call_count == 1
def test_resolve_resolution_all_attempts_fail_falls_back(monkeypatch):
a = _make_analyser()
a.device_id = "test"
a.operation.get_device_resolution.side_effect = RuntimeError("adb")
monkeypatch.setattr("time.sleep", lambda _s: None) # 加速测试
rx, ry = fudai_run_blocks.resolve_device_resolution(a, max_attempts=2)
assert (rx, ry) == (1080, 2400)
assert a.operation.get_device_resolution.call_count == 2
# ---- write_actions_log_separator ---- #
def test_write_actions_log_separator_swallows_exceptions():
"""目录不可写时不应抛错"""
fudai_run_blocks.write_actions_log_separator("test message")
# ---- run_basic_loop_with_timeout ---- #
def test_run_loop_with_timeout_calls_run_once_basic(monkeypatch):
a = _make_analyser()
a.run_once_basic = MagicMock()
monkeypatch.setattr("time.sleep", lambda _s: None)
# 0.001 分钟 ≈ 60ms 后超时退出
fudai_run_blocks.run_basic_loop_with_timeout(a, wait_minutes=0)
# 0 分钟时立即退出,不应跑
def test_run_loop_with_timeout_swallows_exceptions(monkeypatch):
a = _make_analyser()
call_count = [0]
def fake_run():
call_count[0] += 1
if call_count[0] == 1:
raise RuntimeError("boom")
# 第二次让循环条件失败
a.run_once_basic.side_effect = lambda: None
a.run_once_basic.side_effect = fake_run
# 用极短超时让循环只跑 1-2 次后退出
fake_time = [0.0]
monkeypatch.setattr("time.time", lambda: fake_time[0])
def advance(_s):
fake_time[0] += 100 # 大跨步推进时间
monkeypatch.setattr("time.sleep", advance)
a.operation.delay = MagicMock(side_effect=advance)
fudai_run_blocks.run_basic_loop_with_timeout(a, wait_minutes=1)
# 至少触发了一次异常
assert call_count[0] >= 1
# ---- run_basic_loop_forever ---- #
def test_run_loop_forever_handles_keyboard_interrupt(monkeypatch):
a = _make_analyser()
a.run_once_basic = MagicMock(side_effect=KeyboardInterrupt())
monkeypatch.setattr("time.sleep", lambda _s: None)
import pytest
with pytest.raises(KeyboardInterrupt):
fudai_run_blocks.run_basic_loop_forever(a)
a._log_kv.assert_any_call("循环", "收到中断信号,退出循环")
# ---- execute_reservation_attend_or_cleanup ---- #
def _make_reservation_analyser():