Skip to content

[建议] 为 async engine 和 smart_scheduler 添加单元测试 #39

Description

@gitfox-enter

现状

test_crawler.py 有 294 个测试,覆盖了工具函数、parser、storage 等模块。但以下核心组件完全没有测试:

组件 文件 重要性
异步爬取引擎 crawler/engine.py 核心,负责所有网络请求
智能调度器 crawler/smart_scheduler.py 决定哪些站点需要爬取
GitHub Actions 告警 alerter.py 故障通知
Gist 存储 gist_store.py 数据持久化
Playwright 渲染 crawler/network.py (Playwright path) JS 渲染站点

改进建议

优先级 1:smart_scheduler

def test_should_crawl_high_tier_within_interval():
    """High-tier sites should always be crawled when interval elapsed."""

def test_should_skip_recently_crawched():
    """Sites crawled 5 min ago with 30-min interval should be skipped."""

def test_night_mode_triples_interval():
    """Between 23:00-07:00, interval should be multiplied by 3."""

优先级 2:alerter

def test_alert_triggered_after_consecutive_failures():
    """Alert should fire after N consecutive failures for a site."""

def test_no_duplicate_alerts():
    """Same failure should not trigger multiple alerts."""

优先级 3:engine(使用 mock)

@patch('aiohttp.ClientSession')
async def test_ssrf_blocks_private_ip():
    """Redirect to 192.168.x.x should be blocked."""

预期效果

  • 核心逻辑变更时有回归保护
  • 新贡献者更容易理解各组件的预期行为
  • CI 测试覆盖率从 ~60% 提升到 ~85%

复杂度

高。需要 mock 网络请求和 asyncio 事件循环。

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions