From 54f892e3487f499047ae45e82f3667cf5206aac9 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Mon, 27 Jul 2026 05:43:19 +0800 Subject: [PATCH] test: assert the provider-not-ready round actually writes nothing The readiness fix had unit coverage for _chat_provider_ready and the fallback upgrade helpers, but nothing asserting the thing that actually went wrong in production: that a not-ready round leaves the day untouched. "It returned None" and "it wrote a fallback and pinned the whole day" are both plausible readings of a green pure-function test. Three cases through _ensure_daily_plan itself: not ready writes no plan and counts one wait; the round after the configured limit is the first one allowed to degrade, and it must actually produce a plan; a ready provider generates immediately and clears the wait counter, so a later genuine outage gets its full budget. Companion suite 246 passed, joint E2E 146/146. Co-Authored-By: Claude Opus 5 --- tests/test_p0_runtime_fixes.py | 59 ++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/test_p0_runtime_fixes.py b/tests/test_p0_runtime_fixes.py index b03f977..dd07397 100644 --- a/tests/test_p0_runtime_fixes.py +++ b/tests/test_p0_runtime_fixes.py @@ -13,6 +13,7 @@ """ from __future__ import annotations +import asyncio import importlib import json import logging @@ -138,5 +139,63 @@ class FallbackUpgradeTests(unittest.TestCase): self.assertEqual(host.saves, 1, "重试计数必须落盘,否则重启后额度重置") + +class ProviderNotReadySkipsTheRoundTests(unittest.IsolatedAsyncioTestCase): + """Provider 未就绪时必须跳过本轮,而不是写一份兜底把当天钉死。 + + 这是 2518f7b 的核心分支,此前只有纯函数单测,没有一条断言证明 + 「这一轮真的什么都没写」——而生产事故恰恰是「写了,而且写死了一整天」。 + """ + + def build(self, ready: bool): + host = Host() + host.enable_daily_plan = True + host.data["users"] = {"u1": {"umo": "qq:FriendMessage:u1"}} + host._provider_ready = ready + host._data_lock = asyncio.Lock() + host.generated = 0 + + async def ensure_state(force=False): + return None + + async def generate(): + host.generated += 1 + return {"date": TODAY, "source": "llm", "version": 1, + "items": [{"time": "09:00", "end": "10:00", "activity": "真实计划"}]} + + host._ensure_daily_state = ensure_state + host._generate_daily_plan = generate + host._is_daily_plan_due = lambda: True + host._is_plan_date_active = lambda date: False + host._sanitize_daily_plan_inplace = lambda plan: False + host._refresh_daily_state_location_from_plan = lambda **kw: None + host._memory_companion_record_daily_plan = None + return host + + async def test_not_ready_writes_nothing_and_counts_a_wait(self): + host = self.build(ready=False) + self.assertIsNone(await host._ensure_daily_plan()) + self.assertEqual(host.generated, 0, "Provider 没就绪却仍然调用了模型") + self.assertFalse(host.data.get("daily_plan"), "写了兜底计划,当天就被钉死了") + self.assertEqual(host.data.get("daily_plan_provider_wait_count"), 1) + + async def test_it_gives_up_after_the_configured_number_of_rounds(self): + host = self.build(ready=False) + for _ in range(daily_state.DAILY_PLAN_PROVIDER_MAX_WAITS): + self.assertIsNone(await host._ensure_daily_plan()) + # 超过上限这一轮才允许退化,且退化必须真的产出计划 + plan = await host._ensure_daily_plan() + self.assertIsNotNone(plan) + self.assertEqual(host.generated, 1) + + async def test_ready_generates_immediately_and_clears_the_counter(self): + host = self.build(ready=True) + host.data["daily_plan_provider_wait_count"] = 3 + plan = await host._ensure_daily_plan() + self.assertEqual(plan["source"], "llm") + self.assertEqual(host.generated, 1) + self.assertNotIn("daily_plan_provider_wait_count", host.data, + "就绪之后等待计数没清,下次真失败时额度会被提前吃掉") + if __name__ == "__main__": unittest.main()