82 行
3.1 KiB
Python
82 行
3.1 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import unittest
|
||
|
|
from datetime import datetime
|
||
|
|
from types import SimpleNamespace
|
||
|
|
from zoneinfo import ZoneInfo
|
||
|
|
|
||
|
|
from temporal_context import build_temporal_context, classify_delay, format_temporal_prompt
|
||
|
|
|
||
|
|
|
||
|
|
TZ = ZoneInfo("Asia/Shanghai")
|
||
|
|
|
||
|
|
|
||
|
|
def ts(value: str) -> float:
|
||
|
|
return datetime.fromisoformat(value).replace(tzinfo=TZ).timestamp()
|
||
|
|
|
||
|
|
|
||
|
|
def event_at(value: str | None) -> SimpleNamespace:
|
||
|
|
raw = {} if value is None else {"time": ts(value)}
|
||
|
|
return SimpleNamespace(message_obj=SimpleNamespace(raw_message=raw))
|
||
|
|
|
||
|
|
|
||
|
|
class TemporalContextTests(unittest.TestCase):
|
||
|
|
def test_delay_buckets_and_cross_day(self):
|
||
|
|
self.assertEqual(classify_delay(30), "immediate")
|
||
|
|
self.assertEqual(classify_delay(600), "recent")
|
||
|
|
self.assertEqual(classify_delay(7200), "same_day")
|
||
|
|
self.assertEqual(classify_delay(11 * 3600, crossed_date=True), "overnight")
|
||
|
|
self.assertEqual(classify_delay(3 * 86400), "few_days")
|
||
|
|
|
||
|
|
def test_overnight_reply_uses_platform_timestamp_and_marks_stale_topic(self):
|
||
|
|
context = build_temporal_context(
|
||
|
|
now_ts=ts("2026-07-24T08:00:00"),
|
||
|
|
local_tz=TZ,
|
||
|
|
event=event_at("2026-07-24T08:00:00"),
|
||
|
|
user={"last_companion_message_at": ts("2026-07-23T21:00:00")},
|
||
|
|
)
|
||
|
|
prompt = format_temporal_prompt(context)
|
||
|
|
self.assertEqual(context.delay_class, "overnight")
|
||
|
|
self.assertTrue(context.crossed_date)
|
||
|
|
self.assertEqual(context.event_time_source, "raw_message.time")
|
||
|
|
self.assertIn("隔夜", prompt)
|
||
|
|
self.assertIn("昨晚", prompt)
|
||
|
|
self.assertIn("短期状态可能已经变化", prompt)
|
||
|
|
|
||
|
|
def test_long_term_topic_is_not_marked_stale_only_by_prompt_rule(self):
|
||
|
|
context = build_temporal_context(
|
||
|
|
now_ts=ts("2026-07-27T09:00:00"),
|
||
|
|
local_tz=TZ,
|
||
|
|
event=event_at("2026-07-27T09:00:00"),
|
||
|
|
user={"last_companion_message_at": ts("2026-07-23T21:00:00")},
|
||
|
|
)
|
||
|
|
prompt = format_temporal_prompt(context)
|
||
|
|
self.assertEqual(context.delay_class, "few_days")
|
||
|
|
self.assertIn("项目、创作和长期计划可以继续承接", prompt)
|
||
|
|
|
||
|
|
def test_missing_message_time_falls_back_without_inventing_delay(self):
|
||
|
|
context = build_temporal_context(
|
||
|
|
now_ts=ts("2026-07-24T08:00:00"),
|
||
|
|
local_tz=TZ,
|
||
|
|
event=event_at(None),
|
||
|
|
user={},
|
||
|
|
)
|
||
|
|
prompt = format_temporal_prompt(context)
|
||
|
|
self.assertEqual(context.event_time_source, "received_at_estimated")
|
||
|
|
self.assertIn("不要编造具体等待时长", prompt)
|
||
|
|
|
||
|
|
def test_group_prompt_does_not_add_private_waiting_emotion(self):
|
||
|
|
context = build_temporal_context(
|
||
|
|
now_ts=ts("2026-07-24T08:00:00"),
|
||
|
|
local_tz=TZ,
|
||
|
|
event=event_at("2026-07-24T08:00:00"),
|
||
|
|
user={"last_companion_message_at": ts("2026-07-23T21:00:00")},
|
||
|
|
)
|
||
|
|
prompt = format_temporal_prompt(context, is_group=True)
|
||
|
|
self.assertNotIn("昨晚", prompt)
|
||
|
|
self.assertNotIn("不要机械责怪", prompt)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|