# -*- coding: utf-8 -*- """日程版本水位回归。 真机上暴露的问题:daily_plan 的幂等键恒为 daily_plan:,而计划本身从不带 version。 兜底计划先落库(v1),同一天模型恢复后生成真实计划仍发 v1 —— 记忆侧判 version_conflict 丢弃,陪伴侧 outbox 因同键同版本 accepted=False 连错误状态都不落,整条归档静默消失。 库里于是永远停在那份 8 段兜底上。 """ from __future__ import annotations import importlib import logging import pathlib import sys import types import unittest if "astrbot.api" not in sys.modules: _astrbot = types.ModuleType("astrbot") _api = types.ModuleType("astrbot.api") _api.logger = logging.getLogger("test.astrbot") _astrbot.api = _api sys.modules.setdefault("astrbot", _astrbot) sys.modules.setdefault("astrbot.api", _api) _package_name = "_planver_companion" _package = types.ModuleType(_package_name) _package.__path__ = [str(pathlib.Path(__file__).resolve().parents[1])] sys.modules.setdefault(_package_name, _package) planning = importlib.import_module(f"{_package_name}.planning") _next_daily_plan_version = planning._next_daily_plan_version class _Host: def __init__(self, data=None): self.data = data if data is not None else {} class DailyPlanVersionTests(unittest.TestCase): def test_first_plan_of_the_day_starts_at_one(self): self.assertEqual(_next_daily_plan_version(_Host(), "2026-07-27"), 1) def test_regenerating_the_same_day_bumps_the_version(self): host = _Host({"daily_plan": {"date": "2026-07-27", "version": 1}}) self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 2) def test_a_different_day_restarts_at_one(self): host = _Host({"daily_plan": {"date": "2026-07-26", "version": 7}}) self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 1) def test_history_keeps_the_watermark_when_daily_plan_is_cleared(self): """运维清空 daily_plan 后版本不能回退到 1,否则又撞上库里已有的同版本记录。""" host = _Host({"daily_plan_history": [ {"date": "2026-07-26", "version": 2}, {"date": "2026-07-27", "version": 3}, ]}) self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 4) def test_takes_the_higher_of_plan_and_history(self): host = _Host({ "daily_plan": {"date": "2026-07-27", "version": 5}, "daily_plan_history": [{"date": "2026-07-27", "version": 2}], }) self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 6) def test_legacy_entries_without_version_count_as_one(self): """升级前写下的计划没有 version 字段,必须视为 v1 而不是 v0。""" host = _Host({"daily_plan": {"date": "2026-07-27"}}) self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 2) def test_missing_or_broken_data_is_tolerated(self): self.assertEqual(_next_daily_plan_version(_Host(None), "2026-07-27"), 1) host = _Host({"daily_plan": "不是字典", "daily_plan_history": "也不是列表"}) self.assertEqual(_next_daily_plan_version(host, "2026-07-27"), 1) if __name__ == "__main__": unittest.main()