from datetime import date, datetime from zoneinfo import ZoneInfo from snooper_calendar import ( AI_NOTES_CHAT_ID, _extract_topic, fetch_ai_notes_links, fetch_calendar_events, ) from snooper_storage import SnifferStorage def test_extract_topic_from_card_text(): text = ( "[卡片]\nMeeting recording completed" "Topic: 指数-金科定期AI业务讨论会Owner: 万轶伦" ) assert _extract_topic(text) == "指数-金科定期AI业务讨论会" assert _extract_topic("https://efund.feishu.cn/docx/abc") == "" def test_ai_notes_url_regex_stops_before_json_quote(): from snooper_calendar import AI_NOTES_URL_RE json_text = ( '{"text": "https://efund.feishu.cn/docx/abc123?dcuId=7&from=vc#doxcnX", ' '"richText": {}}' ) urls = AI_NOTES_URL_RE.findall(json_text) assert urls == ["https://efund.feishu.cn/docx/abc123?dcuId=7&from=vc#doxcnX"] def test_fetch_ai_notes_links_reads_topic_from_local_content(): tz = ZoneInfo("Asia/Shanghai") storage = SnifferStorage(url="sqlite:///:memory:") ts = int(datetime(2026, 11, 17, 14, 0, tzinfo=tz).timestamp()) storage.save_message({ "msg_id": "minutes-1", "chat_id": AI_NOTES_CHAT_ID, "chat_type": 2, "from_id": "bot", "msg_type": 14, "create_time": ts, "content": ( "[卡片]\nMeeting recording completed" "Topic: 指数-金科定期AI业务讨论会Owner: 万轶伦\n" "https://efund.feishu.cn/minutes/abc123" ), }) links = fetch_ai_notes_links(object(), date(2026, 11, 17), tz, storage=storage) assert len(links) == 1 assert links[0]["url"].startswith("https://efund.feishu.cn/minutes/abc123") assert links[0]["topic"] == "指数-金科定期AI业务讨论会" def test_permission_change_links_are_dropped(): from snooper_calendar import _drop_permission_change links = [ {"url": "https://efund.feishu.cn/minutes/m1?from_source=permission_change"}, {"url": "https://efund.feishu.cn/minutes/m2?from_source=finish_recording"}, ] kept = _drop_permission_change(links) assert [link["url"] for link in kept] == [ "https://efund.feishu.cn/minutes/m2?from_source=finish_recording" ] class FakeCalendarClient: def __init__(self): self.calls = [] self.native_urls = True def api(self, name, data): self.calls.append((name, data)) if name == "calendars.GetUserPrimaryCalendarRequest": return {"calendar": {"id": "calendar-synthetic"}} assert name == "calendarevents.QueryInstanceRequest" return { "calendarEventInstance": [ { "eventRefId": "event-b", "beginTime": 1_789_000_000, "endTime": 1_789_003_600, }, { "eventRefId": "event-a", "beginTime": 1_788_000_000, "endTime": 1_788_003_600, }, ], "events": { "event-a": { "summary": "合成早会", "locations": [{"name": "合成会议室 A"}], "isDeleted": False, "url": "https://example.com/a" if self.native_urls else "", }, "event-b": { "summary": "合成下午会", "locations": [{"name": "合成会议室 B"}], "isDeleted": True, "meetingMinuteUrl": ( "https://example.com/minutes-b" if self.native_urls else "" ), "url": "https://example.com/event-b" if self.native_urls else "", }, }, } def test_manual_meeting_link_fills_missing_url_without_overriding_native(): client = FakeCalendarClient() client.native_urls = False events, _ = fetch_calendar_events( client, date(2026, 9, 9), ZoneInfo("Asia/Shanghai"), meeting_links={ "2026-09-09|合成早会": "https://example.com/manual-a", "2026-09-09|合成下午会": "https://example.com/manual-b", }, ) assert events[0]["url"] == "https://example.com/manual-a" assert events[1]["url"] == "https://example.com/manual-b" def test_fetch_calendar_events_normalizes_and_sorts_daily_instances(): client = FakeCalendarClient() tz = ZoneInfo("Asia/Shanghai") events, _ = fetch_calendar_events(client, date(2026, 9, 9), tz) assert [event["summary"] for event in events] == ["合成早会", "合成下午会"] assert events[0]["location"] == "合成会议室 A" assert events[0]["url"] == "https://example.com/a" assert events[1]["cancelled"] is True assert events[1]["url"] == "https://example.com/minutes-b" assert "attendees" not in events[0] assert "description" not in events[0] name, request = client.calls[1] assert request["calendarIds"] == ["calendar-synthetic"] assert request["needsBriefEvents"] is True assert request["endTime"] - request["startTime"] == 86_400 class FakeMeetingCalendarClient: def __init__(self, begin_ts, end_ts): self.begin_ts = begin_ts self.end_ts = end_ts def api(self, name, data): if name == "calendars.GetUserPrimaryCalendarRequest": return {"calendar": {"id": "calendar-synthetic"}} assert name == "calendarevents.QueryInstanceRequest" return { "calendarEventInstance": [ {"eventRefId": "event-a", "beginTime": self.begin_ts, "endTime": self.end_ts} ], "events": { "event-a": { "summary": "指数-金科定期AI业务讨论会", "locations": [], "isDeleted": False, } }, } def test_untitled_ai_notes_link_falls_back_to_nearest_meeting_name(): tz = ZoneInfo("Asia/Shanghai") begin = int(datetime(2026, 11, 17, 14, 0, tzinfo=tz).timestamp()) end = int(datetime(2026, 11, 17, 15, 0, tzinfo=tz).timestamp()) minutes_ts = int(datetime(2026, 11, 17, 15, 15, tzinfo=tz).timestamp()) docx_ts = int(datetime(2026, 11, 17, 17, 38, tzinfo=tz).timestamp()) storage = SnifferStorage(url="sqlite:///:memory:") storage.save_message({ "msg_id": "minutes-1", "chat_id": AI_NOTES_CHAT_ID, "chat_type": 2, "from_id": "bot", "msg_type": 14, "create_time": minutes_ts, "content": ( "[卡片]\nMeeting recording completed" "Topic: 指数-金科定期AI业务讨论会Owner: 万轶伦\n" "https://efund.feishu.cn/minutes/m1" ), }) storage.save_message({ "msg_id": "docx-1", "chat_id": AI_NOTES_CHAT_ID, "chat_type": 2, "from_id": "bot", "msg_type": 4, "create_time": docx_ts, "content": "https://efund.feishu.cn/docx/d1", }) client = FakeMeetingCalendarClient(begin, end) events, unmatched = fetch_calendar_events( client, date(2026, 11, 17), tz, storage=storage ) assert events[0]["url"].startswith("https://efund.feishu.cn/minutes/m1") assert any(link["url"].startswith("https://efund.feishu.cn/docx/d1") for link in unmatched) docx = next(link for link in unmatched if "docx/d1" in link["url"]) assert docx["topic"] == "指数-金科定期AI业务讨论会"