diff --git a/plan.py b/plan.py index 61e920a..81993bd 100644 --- a/plan.py +++ b/plan.py @@ -946,9 +946,10 @@ def from_snapshot(date: str | None = None, top: int = 20, obs_top: int = 10, full_main, full_obs = snap.get("main"), snap.get("observe") if not isinstance(full_main, list) or not isinstance(full_obs, list): raise RuntimeError(f"快照缺全量主榜或观察档: {path}") - ok, why = snapshot_fresh_enough(snap) - if not ok: - raise StaleSnapshot(why) + if not date: # 只有「要今天该用的那份」才谈新鲜度 + ok, why = snapshot_fresh_enough(snap) + if not ok: + raise StaleSnapshot(why) data = {k: v for k, v in snap.items() # 前三个是落盘那次的裁剪结果与参数,与本次请求无关; diff --git a/test_plan_snapshot.py b/test_plan_snapshot.py index 039463b..bce21c1 100644 --- a/test_plan_snapshot.py +++ b/test_plan_snapshot.py @@ -148,6 +148,17 @@ def test_freshness_guard(): t("守卫用的是专门的异常类型,调用方接得住", issubclass(plan.StaleSnapshot, RuntimeError)) + # 新鲜度只管「今天该用哪份」。复盘要看 09-08 那天的计划,拿的就该是那天那份旧快照 + import glob as _g, os as _o + fs = sorted(_g.glob(_o.path.join(config.PLAN_SNAPSHOT_DIR, "plan_*.json"))) + if fs: + ds = _o.path.basename(fs[-1])[len("plan_"):-len(".json")] + try: + plan.from_snapshot(ds, 5, 3, 2) + t("显式指定日期不受今天的新鲜度约束(复盘要拿历史那份)", True) + except plan.StaleSnapshot as e: + t("显式指定日期不受今天的新鲜度约束(复盘要拿历史那份)", False, e) + def test_api_falls_back(): print("[接口回落:读不到快照不能让接口挂掉]")