refactor(mobile): damai_app.py 拆分为包(零行为变更) - #44
Merged
Conversation
把单文件 mobile/damai_app.py(2072 行)拆分为 mobile/damai_app/ 包:
- __init__.py 包入口;保留所有原模块级符号 + 自定义 ModuleType
hook 把 patch("mobile.damai_app.{time,datetime,logger}")
镜像到所有子模块,使测试零修改
- __main__.py 支持 python -m damai_app(替代原 python damai_app.py)
- orchestrator.py DamaiBot 主类 + run_ticket_grabbing + run_with_retry
+ 启动/弹窗/会话探测/票价确认等编排
- sale_waiter.py wait_for_sale_start + 自适应轮询
- purchase_flow.py _enter_purchase_flow_from_detail_page + _submit_order_fast
- recovery_strategies.py 状态机 dispatcher + back-press 恢复
- coords_cache.py 热路径坐标缓存命中检查与坐标读取
- state_probe.py 页面状态探测 + SKU 检视 + 标题/场地解析
- delegators.py AttendeeSelector / PriceSelector / EventNavigator /
FastPipeline 的薄封装
零行为变更:
- 仅搬代码,不动逻辑、不改默认值、不重命名(_submit_order_fast 等保持下划线)
- 外部 API 不变:from mobile.damai_app import DamaiBot 仍兼容;
Config / logger / time / datetime 等模块属性仍可被测试 patch
- 测试零修改:1020 通过 + 1 预存 xfail,覆盖率 81.03%(≥80% 门槛)
启动脚本同步:
- mobile/scripts/start_ticket_grabbing.sh:python damai_app.py → python -m damai_app
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mobile/damai_app.py(2072 行)→mobile/damai_app/包,共 9 个文件(含__main__.py)tests/unit/test_mobile_damai_app.py/_u2_adapter.py/test_environment_check.py跑绿from mobile.damai_app import DamaiBot等所有原导入路径不变拆分结构
__init__.py_DamaiPackage镜像 hook__main__.pypython -m damai_app入口(替代原python damai_app.py)orchestrator.pyDamaiBot主类 +run_ticket_grabbing(~390 行) +run_with_retrysale_waiter.pywait_for_sale_start+_is_sale_ready+_purchase_bar_text_readypurchase_flow.py_enter_purchase_flow_from_detail_page+_submit_order_fastrecovery_strategies.py_fast_retry_from_current_state状态机coords_cache.pystate_probe.pydelegators.pyorchestrator.py仍超 500 行限制,主因run_ticket_grabbing单方法 ~390 行。本次重构规约「不动逻辑」,方法体内拆分留到后续 PR。测试兼容关键点:模块属性镜像 hook
测试代码大量使用
patch("mobile.damai_app.time")/patch("mobile.damai_app.datetime")/patch("mobile.damai_app.logger")替换整模块/类对象。拆分后子模块的
import time引用是子模块本地的,原 patch 不会再生效。__init__.py中定义_DamaiPackage(types.ModuleType),重写__setattr__:对
time / datetime / logger / re这几个白名单属性,写入会同步镜像到所有子模块的sys.modules条目。这样测试 patch 包级属性时,子模块代码看到的也是 mock,零测试改动即可跑绿。启动脚本变更
mobile/scripts/start_ticket_grabbing.sh第 298/300 行:python damai_app.pypython -m damai_appmobile/damai_app/__main__.py复刻原if __name__ == \"__main__\":逻辑(构造DamaiBot、run_with_retry(max_retries=3)、finally 关 driver)。Test plan
poetry run pytest:1020 passed, 1 xfail(预存,与 page_probe.pyunknown_threshold=0语义无关)poetry run pytest --cov=mobile --cov-fail-under=80:覆盖率 81.03% ≥ 80%from mobile.damai_app import DamaiBot, logger, SALE_READY_TEXTS, Config均成功python -m damai_app(cwd=mobile/)能找到包并 importmobile/hot_path_benchmark.py --help正常打印(from mobile.damai_app import DamaiBot可达)bash mobile/scripts/benchmark_hot_path.sh --runs 3比对 before/after,确认热路径耗时无回退Why
mobile/damai_app.py单文件 2072 行已远超风格阈值,DamaiBot 类近 100 个方法,维护成本上升、合并冲突频繁。拆分后每个子模块聚焦单一职责,便于后续按模块迭代修复(issue #25/#28/#23/#24 后续若再触及 hot path 都能定位到合适子模块)。不在本 PR 范围
run_ticket_grabbing内部拆分(建议 W4 后做,需要先补 hot-path 真机回归证据)_submit_order_fast→submit_order_fast)关联
reference/03-tech-assessment.md「damai_app.py 拆分建议」🤖 Generated with Claude Code