forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_action_item_date_validation.py
More file actions
826 lines (695 loc) · 32.7 KB
/
Copy pathtest_action_item_date_validation.py
File metadata and controls
826 lines (695 loc) · 32.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
"""Tests for action item due date validation (issue #4841).
Covers:
1. create_action_item_tool rejects due dates more than 1 day in the past
2. update_action_item_tool rejects due dates more than 1 day in the past
3. extract_action_items passes current_time to prompt and clears past due dates
4. Dates within 1-day grace window are accepted
5. Future dates are accepted unchanged
"""
import importlib
import importlib.util
import inspect
import os
import sys
import types
from datetime import datetime, timedelta, timezone
from pathlib import Path
from zoneinfo import ZoneInfo
from unittest.mock import MagicMock, patch
import pytest
from tests.unit.memory_import_isolation import restore_sys_modules, snapshot_sys_modules
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
BACKEND_DIR = Path(__file__).resolve().parent.parent.parent
IST_TZ = timezone(timedelta(hours=5, minutes=30), "IST")
os.environ.setdefault(
"ENCRYPTION_SECRET",
"omi_ZwB2ZNqB2HHpMK6wStk7sTpavJiPTFg7gXUHnc4tFABPU6pZ2c2DKgehtfgi4RZv",
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _stub_module(name):
mod = sys.modules.get(name)
if mod is None:
mod = types.ModuleType(name)
sys.modules[name] = mod
if "." in name:
parent_name, attr_name = name.rsplit(".", 1)
parent = sys.modules.get(parent_name)
if parent is not None:
setattr(parent, attr_name, mod)
return mod
def _stub_package(name):
# Always replace package modules with fresh stubs after the sys.modules
# snapshot. Reusing an already-imported real package and mutating
# ``__path__`` would alter the snapshotted object itself, so restoring the
# sys.modules entry later would still leave the original package corrupted.
mod = types.ModuleType(name)
mod.__path__ = []
sys.modules[name] = mod
if "." in name:
parent_name, attr_name = name.rsplit(".", 1)
parent = sys.modules.get(parent_name)
if parent is not None:
setattr(parent, attr_name, mod)
return mod
def _load_module_from_file(module_name, file_path):
if module_name in sys.modules:
return sys.modules[module_name]
spec = importlib.util.spec_from_file_location(module_name, str(file_path))
mod = importlib.util.module_from_spec(spec)
sys.modules[module_name] = mod
try:
spec.loader.exec_module(mod)
except Exception:
sys.modules.pop(module_name, None)
raise
return mod
# ---------------------------------------------------------------------------
# Stub heavy dependencies
#
# Snapshot touched modules first so the stubs installed below do not leak into
# other test files during bulk ``pytest tests/unit/`` collection (issue #8661).
# They are restored right after the modules under test are imported.
# ---------------------------------------------------------------------------
_SYS_MODULE_NAMES = [
"firebase_admin",
"firebase_admin.firestore",
"firebase_admin.auth",
"firebase_admin.messaging",
"firebase_admin.credentials",
"google.cloud.firestore",
"google.cloud.firestore_v1",
"google.cloud.firestore_v1.base_query",
"google.auth",
"google.auth.transport",
"google.auth.transport.requests",
"google.cloud.storage",
"opuslib",
"sentry_sdk",
"database",
"database._client",
"database.redis_db",
"database.auth",
"database.action_items",
"database.notifications",
"utils",
"utils.notifications",
"utils.byok",
"utils.llm",
"utils.llm.gateway_client",
"utils.llm.gateway_observability",
"utils.llm.clients",
"utils.llm.conversation_folder",
"utils.llm.conversation_prompt_prefix",
"utils.llm.conversation_processing",
"utils.llm.model_config",
"utils.retrieval",
"utils.retrieval.tools",
"utils.retrieval.tools.action_item_tools",
"utils.retrieval.agentic",
"utils.retrieval.chat_scope",
"utils.conversations",
"utils.conversations.render",
"utils.conversations.wake_word",
"langchain_core",
"langchain_core.tools",
"langchain_core.runnables",
"langchain_core.output_parsers",
"langchain_core.prompts",
"langchain_core.messages",
"models",
"models.conversation",
"models.app",
]
_SYS_MODULES_SNAPSHOT = snapshot_sys_modules(_SYS_MODULE_NAMES)
for importable_mod_name in [
"google.auth",
"google.auth.transport",
"google.auth.transport.requests",
]:
try:
importlib.import_module(importable_mod_name)
except Exception:
pass
for mod_name in [
"firebase_admin",
"firebase_admin.firestore",
"firebase_admin.auth",
"firebase_admin.messaging",
"firebase_admin.credentials",
"google.cloud.firestore",
"google.cloud.firestore_v1",
"google.cloud.firestore_v1.base_query",
"google.auth",
"google.auth.transport",
"google.auth.transport.requests",
"google.cloud.storage",
"opuslib",
"sentry_sdk",
"database._client",
"database.redis_db",
"database.auth",
]:
if mod_name not in sys.modules:
_stub_module(mod_name)
sys.modules["database.auth"].get_user_name = MagicMock(return_value="Test User")
# Stub database.action_items
action_items_db = _stub_module("database.action_items")
action_items_db.create_action_item = MagicMock(return_value="test-item-id")
action_items_db.get_action_item = MagicMock(
return_value={
'id': 'test-item-id',
'description': 'Test task',
'completed': False,
'due_at': datetime.now(timezone.utc) + timedelta(hours=24),
}
)
action_items_db.update_action_item = MagicMock(return_value=True)
# Stub database.notifications (action_item_tools imports get_user_time_zone for tz rendering)
notifications_db = _stub_module("database.notifications")
notifications_db.get_user_time_zone = MagicMock(return_value="UTC")
if "database" not in sys.modules:
importlib.import_module("database")
# Stub notifications
notif_mod = _stub_module("utils.notifications")
notif_mod.send_action_item_completed_notification = MagicMock()
notif_mod.send_action_item_created_notification = MagicMock()
notif_mod.send_action_item_data_message = MagicMock()
notif_mod.sync_action_item_reminder = MagicMock()
if "utils.byok" not in sys.modules:
byok_mod = _stub_module("utils.byok")
byok_mod.has_byok_keys = MagicMock(return_value=False)
if "utils.llm.gateway_client" not in sys.modules:
gateway_mod = _stub_module("utils.llm.gateway_client")
else:
gateway_mod = sys.modules["utils.llm.gateway_client"]
gateway_mod.invoke_chat_structured_gateway = MagicMock(return_value=None)
gateway_mod.record_chat_extraction_gateway_result = MagicMock()
gateway_mod.BACKGROUND_CHAT_EXTRACTION_TIMEOUT_SECONDS = 35.0
if "utils.llm.gateway_observability" not in sys.modules:
gateway_observability_mod = _stub_module("utils.llm.gateway_observability")
gateway_observability_mod.record_gateway_shadow_comparison = MagicMock()
# Stub langchain
langchain_core = _stub_package("langchain_core")
langchain_tools = _stub_module("langchain_core.tools")
langchain_runnables = _stub_module("langchain_core.runnables")
class FakeRunnableConfig(dict):
pass
def fake_tool(func=None, **kwargs):
if func is not None:
return func
return lambda f: f
langchain_tools.tool = fake_tool
langchain_runnables.RunnableConfig = FakeRunnableConfig
# Stub langchain output parsers and prompts
langchain_output_parsers = _stub_module("langchain_core.output_parsers")
langchain_output_parsers.PydanticOutputParser = MagicMock()
langchain_prompts = _stub_module("langchain_core.prompts")
langchain_prompts.ChatPromptTemplate = MagicMock()
langchain_messages = _stub_module("langchain_core.messages")
langchain_messages.SystemMessage = MagicMock()
# Stub pydantic (already installed, just need BaseModel/Field accessible)
# pydantic is real, no stub needed
# Stub utils packages
_stub_package("utils")
_stub_package("utils.retrieval")
_stub_package("utils.retrieval.tools")
_stub_package("utils.llm")
_stub_package("utils.conversations")
# Stub utils.conversations.render (action_item_tools imports resolve_display_tz, format_local_time)
_render_stub = _stub_module("utils.conversations.render")
def _real_resolve_display_tz(tz):
if tz:
try:
return ZoneInfo(tz), tz
except Exception:
pass
return timezone.utc, "UTC"
def _real_format_local_time(dt, display_tz, tz_label):
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return f"{dt.astimezone(display_tz).strftime('%Y-%m-%d %H:%M:%S')} {tz_label}"
_render_stub.resolve_display_tz = _real_resolve_display_tz
_render_stub.format_local_time = _real_format_local_time
# Stub utils.retrieval.agentic
import contextvars
agentic_stub = _stub_module("utils.retrieval.agentic")
agentic_stub.agent_config_context = contextvars.ContextVar('agent_config', default=None)
def _apply_chat_scope_dates(scope, start_date, end_date):
return start_date, end_date, None
def _chat_scope_from_config(configurable):
if not isinstance(configurable, dict):
return None
scope = configurable.get("chat_scope")
return scope if isinstance(scope, dict) and scope else None
_chat_scope_stub = _stub_module("utils.retrieval.chat_scope")
_chat_scope_stub.apply_chat_scope_dates = _apply_chat_scope_dates
_chat_scope_stub.chat_scope_from_config = _chat_scope_from_config
# ---------------------------------------------------------------------------
# Load production code
# ---------------------------------------------------------------------------
# Stub utils.llm.clients (conversation_processing imports from .clients)
llm_clients_stub = _stub_module("utils.llm.clients")
llm_clients_stub.llm_mini = MagicMock()
llm_clients_stub.parser = MagicMock()
llm_clients_stub.llm_high = MagicMock()
llm_clients_stub.llm_medium_experiment = MagicMock()
llm_clients_stub.get_llm = MagicMock(return_value=MagicMock())
llm_clients_stub.get_llm_gateway_chat_structured = MagicMock(return_value=MagicMock())
# Stub utils.llm.conversation_folder (conversation_processing imports from it after a recent refactor)
conv_folder_stub = _stub_module("utils.llm.conversation_folder")
conv_folder_stub.FolderAssignment = MagicMock()
conv_folder_stub.assign_conversation_to_folder = MagicMock()
conv_folder_stub.build_folders_context = MagicMock(return_value="")
# Stub utils.llm.gateway_error_contract (conversation_processing imports from it)
gateway_error_contract_stub = _stub_module("utils.llm.gateway_error_contract")
gateway_error_contract_stub.is_byok_rate_limit_gateway_error = MagicMock(return_value=False)
# Load models first
_stub_package("models")
sys.modules["models"].__path__ = [str(BACKEND_DIR / "models")]
_load_module_from_file("models.conversation", BACKEND_DIR / "models" / "conversation.py")
_load_module_from_file("models.app", BACKEND_DIR / "models" / "app.py")
# Load action_item_tools
action_item_tools = _load_module_from_file(
"utils.retrieval.tools.action_item_tools",
BACKEND_DIR / "utils" / "retrieval" / "tools" / "action_item_tools.py",
)
create_action_item_tool = action_item_tools.create_action_item_tool
update_action_item_tool = action_item_tools.update_action_item_tool
# discard_parser only needs pydantic and langchain_core, so load the real module.
_load_module_from_file("utils.llm.discard_parser", BACKEND_DIR / "utils" / "llm" / "discard_parser.py")
# prompt_cache is stdlib-only, so load the real module. utils.llm is stubbed with an
# empty __path__ above, which leaves conversation_processing's absolute import of it
# unresolvable.
_load_module_from_file("utils.llm.prompt_cache", BACKEND_DIR / "utils" / "llm" / "prompt_cache.py")
# model_config pulls in gateway_client; stub the one constant conversation_processing
# imports so the isolated load does not need the real module tree.
model_config_stub = _stub_module("utils.llm.model_config")
model_config_stub.FOREGROUND_REQUEST_TIMEOUT_SECONDS = 60.0
prompt_prefix_stub = _stub_module("utils.llm.conversation_prompt_prefix")
prompt_prefix_stub.ConversationPromptPrefix = MagicMock
prompt_prefix_stub.shared_conversation_cache_supported = MagicMock(return_value=False)
# wake_word is stdlib-only; load the real trust-boundary helper before the
# isolated conversation-processing module imports it.
_load_module_from_file(
"utils.conversations.wake_word",
BACKEND_DIR / "utils" / "conversations" / "wake_word.py",
)
conversation_processing = _load_module_from_file(
"utils.llm.conversation_processing",
BACKEND_DIR / "utils" / "llm" / "conversation_processing.py",
)
# Restore sys.modules now that the modules under test are imported and bound to
# their stubbed dependencies. Tests below patch those module objects directly.
restore_sys_modules(_SYS_MODULES_SNAPSHOT)
del _SYS_MODULES_SNAPSHOT, _SYS_MODULE_NAMES
def _make_config(uid="test-user-123"):
return {"configurable": {"user_id": uid}}
# ===========================================================================
# create_action_item_tool tests
# ===========================================================================
class TestCreateActionItemDateValidation:
def test_rejects_date_months_in_past(self):
"""Due date from September 2025 should be rejected."""
result = create_action_item_tool(
description="Call venue",
due_at="2025-09-15T10:00:00+00:00",
config=_make_config(),
)
assert "Error" in result
assert "in the past" in result
def test_rejects_date_one_year_in_past(self):
"""Due date from a year ago should be rejected."""
one_year_ago = (datetime.now(timezone.utc) - timedelta(days=365)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = create_action_item_tool(
description="Old task",
due_at=one_year_ago,
config=_make_config(),
)
assert "Error" in result
assert "in the past" in result
def test_accepts_future_date(self):
"""Due date in the future should be accepted."""
future_date = (datetime.now(timezone.utc) + timedelta(days=7)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = create_action_item_tool(
description="Future task",
due_at=future_date,
config=_make_config(),
)
assert "Error" not in result
assert "Added" in result or "✅" in result
def test_accepts_date_within_grace_window(self):
"""Due date 12 hours ago should be accepted (within 1-day grace)."""
twelve_hours_ago = (datetime.now(timezone.utc) - timedelta(hours=12)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = create_action_item_tool(
description="Recent task",
due_at=twelve_hours_ago,
config=_make_config(),
)
assert "Error" not in result
assert "Added" in result or "✅" in result
def test_rejects_date_two_days_in_past(self):
"""Due date 2 days ago should be rejected (beyond 1-day grace)."""
two_days_ago = (datetime.now(timezone.utc) - timedelta(days=2)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = create_action_item_tool(
description="Past task",
due_at=two_days_ago,
config=_make_config(),
)
assert "Error" in result
assert "in the past" in result
def test_error_includes_current_time(self):
"""Error message should include current time so LLM can correct."""
result = create_action_item_tool(
description="Past task",
due_at="2025-06-01T10:00:00+00:00",
config=_make_config(),
)
assert "current time is" in result
def test_format_validation_still_works(self):
"""Format validation should still reject invalid formats."""
result = create_action_item_tool(
description="Bad format",
due_at="not-a-date",
config=_make_config(),
)
assert "Error" in result
assert "Invalid due_at format" in result
def test_no_due_date_stays_undated(self):
"""A task the user never dated must be written with no due date.
Inventing ``now + 24h`` put it in neither the overdue nor the due-today
bucket any reader uses, so "remind me to X" followed by "what's on my
list" deterministically answered that there was nothing.
"""
action_items_db.create_action_item.reset_mock()
result = create_action_item_tool(
description="No due date task",
due_at=None,
config=_make_config(),
)
assert "in the past" not in result
action_items_db.create_action_item.assert_called_once()
written = action_items_db.create_action_item.call_args.args[1]
assert written.get("due_at") is None, f"expected no invented due date, got {written.get('due_at')!r}"
def test_boundary_23h_ago_accepted(self):
"""Due date 23h ago should be accepted (within 1-day grace)."""
twenty_three_h = (datetime.now(timezone.utc) - timedelta(hours=23)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = create_action_item_tool(
description="Boundary task",
due_at=twenty_three_h,
config=_make_config(),
)
assert "in the past" not in result
def test_boundary_25h_ago_rejected(self):
"""Due date 25h ago should be rejected (beyond 1-day grace)."""
twenty_five_h = (datetime.now(timezone.utc) - timedelta(hours=25)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = create_action_item_tool(
description="Just beyond boundary",
due_at=twenty_five_h,
config=_make_config(),
)
assert "Error" in result
assert "in the past" in result
def test_accepts_non_utc_timezone_offset(self):
"""Due date with -08:00 offset in the future should be accepted."""
future_pst = (datetime.now(timezone.utc) + timedelta(days=2)).strftime('%Y-%m-%dT%H:%M:%S-08:00')
result = create_action_item_tool(
description="PST task",
due_at=future_pst,
config=_make_config(),
)
assert "Error" not in result
assert "Added" in result or "✅" in result
# ===========================================================================
# update_action_item_tool tests
# ===========================================================================
class TestUpdateActionItemDateValidation:
def test_rejects_past_date_on_update(self):
"""Updating due date to a past date should be rejected."""
result = update_action_item_tool(
action_item_id="test-item-id",
due_at="2025-09-15T10:00:00+00:00",
config=_make_config(),
)
assert "Error" in result
assert "in the past" in result
def test_accepts_future_date_on_update(self):
"""Updating due date to a future date should work."""
future_date = (datetime.now(timezone.utc) + timedelta(days=3)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = update_action_item_tool(
action_item_id="test-item-id",
due_at=future_date,
config=_make_config(),
)
assert "Error" not in result or "in the past" not in result
assert "updated" in result.lower() or "Successfully" in result
def test_accepts_date_within_grace_window_on_update(self):
"""Due date 12 hours ago on update should be accepted."""
twelve_hours_ago = (datetime.now(timezone.utc) - timedelta(hours=12)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = update_action_item_tool(
action_item_id="test-item-id",
due_at=twelve_hours_ago,
config=_make_config(),
)
assert "in the past" not in result
def test_rejects_invalid_format_on_update(self):
"""Invalid date format on update should be rejected."""
result = update_action_item_tool(
action_item_id="test-item-id",
due_at="not-a-date",
config=_make_config(),
)
assert "Error" in result
assert "Invalid due_at format" in result
def test_boundary_25h_ago_rejected_on_update(self):
"""Due date 25h ago on update should be rejected."""
just_beyond = (datetime.now(timezone.utc) - timedelta(hours=25)).strftime('%Y-%m-%dT%H:%M:%S+00:00')
result = update_action_item_tool(
action_item_id="test-item-id",
due_at=just_beyond,
config=_make_config(),
)
assert "Error" in result
assert "in the past" in result
# ===========================================================================
# extract_action_items prompt and post-validation tests
# ===========================================================================
class TestExtractActionItemsPostValidation:
def test_prompt_contains_current_time_and_staleness_rule(self):
"""The extraction prompt source should contain current_time and staleness logic."""
source = inspect.getsource(conversation_processing.extract_action_items)
assert 'current_time' in source, "extract_action_items must pass current_time"
assert '7 days' in source or 'HISTORICAL' in source, "must contain staleness rule"
def test_clears_past_due_dates_from_extraction(self):
"""Due dates more than 1 day in the past should be cleared after extraction."""
from models.structured_extraction import ActionItemsExtraction, ExtractedActionItem
past_due = datetime(2025, 9, 15, 10, 0, tzinfo=timezone.utc)
future_due = datetime.now(timezone.utc) + timedelta(days=3)
mock_response = ActionItemsExtraction(
action_items=[
ExtractedActionItem(description="Past task", due_at=past_due),
ExtractedActionItem(description="Future task", due_at=future_due),
]
)
mock_chain = MagicMock()
mock_chain.invoke.return_value = mock_response
mock_chain.__or__ = MagicMock(return_value=mock_chain)
conv_proc = conversation_processing
mock_llm = MagicMock()
mock_llm.bind.return_value = mock_llm
mock_llm.__or__ = MagicMock(return_value=mock_chain)
with patch.object(conv_proc, 'get_llm', return_value=mock_llm) as mock_get_llm, patch.object(
conv_proc, 'PydanticOutputParser'
) as mock_parser_cls, patch.object(conv_proc, 'ChatPromptTemplate') as mock_prompt_cls:
mock_parser = MagicMock()
mock_parser.get_format_instructions.return_value = "format"
mock_parser_cls.return_value = mock_parser
mock_prompt = MagicMock()
mock_prompt.__or__ = MagicMock(return_value=mock_chain)
mock_prompt_cls.from_messages.return_value = mock_prompt
result = conv_proc.extract_action_items(
transcript="Call venue by Friday, submit report",
started_at=datetime(2025, 9, 10, 10, 0, tzinfo=timezone.utc),
language_code="en",
tz="UTC",
)
assert len(result) == 2
assert result[0].due_at is None, "Past due date should be cleared"
assert result[1].due_at is not None, "Future due date should be preserved"
def test_passes_current_time_to_invoke(self):
"""extract_action_items should pass current_time in the invoke payload."""
from models.structured_extraction import ActionItemsExtraction
mock_response = ActionItemsExtraction(action_items=[])
mock_chain = MagicMock()
mock_chain.invoke.return_value = mock_response
mock_chain.__or__ = MagicMock(return_value=mock_chain)
conv_proc = conversation_processing
mock_llm = MagicMock()
mock_llm.bind.return_value = mock_llm
mock_llm.__or__ = MagicMock(return_value=mock_chain)
with patch.object(conv_proc, 'get_llm', return_value=mock_llm) as mock_get_llm, patch.object(
conv_proc, 'PydanticOutputParser'
) as mock_parser_cls, patch.object(conv_proc, 'ChatPromptTemplate') as mock_prompt_cls:
mock_parser = MagicMock()
mock_parser.get_format_instructions.return_value = "format"
mock_parser_cls.return_value = mock_parser
mock_prompt = MagicMock()
mock_prompt.__or__ = MagicMock(return_value=mock_chain)
mock_prompt_cls.from_messages.return_value = mock_prompt
conv_proc.extract_action_items(
transcript="Test transcript",
started_at=datetime(2025, 6, 1, 10, 0, tzinfo=timezone.utc),
language_code="en",
tz="UTC",
)
invoke_args = mock_chain.invoke.call_args[0][0]
assert 'current_time_local' in invoke_args, "Must pass current_time (local) to prompt"
assert 'started_at_local' in invoke_args, "Must still pass started_at (local)"
def test_preserves_none_due_dates(self):
"""Action items with no due date should remain unchanged."""
from models.structured_extraction import ActionItemsExtraction, ExtractedActionItem
mock_response = ActionItemsExtraction(
action_items=[ExtractedActionItem(description="No due date task", due_at=None)]
)
mock_chain = MagicMock()
mock_chain.invoke.return_value = mock_response
mock_chain.__or__ = MagicMock(return_value=mock_chain)
conv_proc = conversation_processing
mock_llm = MagicMock()
mock_llm.bind.return_value = mock_llm
mock_llm.__or__ = MagicMock(return_value=mock_chain)
with patch.object(conv_proc, 'get_llm', return_value=mock_llm) as mock_get_llm, patch.object(
conv_proc, 'PydanticOutputParser'
) as mock_parser_cls, patch.object(conv_proc, 'ChatPromptTemplate') as mock_prompt_cls:
mock_parser = MagicMock()
mock_parser.get_format_instructions.return_value = "format"
mock_parser_cls.return_value = mock_parser
mock_prompt = MagicMock()
mock_prompt.__or__ = MagicMock(return_value=mock_chain)
mock_prompt_cls.from_messages.return_value = mock_prompt
result = conv_proc.extract_action_items(
transcript="Do something",
started_at=datetime.now(timezone.utc),
language_code="en",
tz="UTC",
)
assert len(result) == 1
assert result[0].due_at is None
def test_preserves_due_date_within_grace_boundary(self):
"""Due date 23h ago should be preserved (within 1-day grace window)."""
from models.structured_extraction import ActionItemsExtraction, ExtractedActionItem
boundary_due = datetime.now(timezone.utc) - timedelta(hours=23)
mock_response = ActionItemsExtraction(
action_items=[ExtractedActionItem(description="Boundary task", due_at=boundary_due)]
)
mock_chain = MagicMock()
mock_chain.invoke.return_value = mock_response
mock_chain.__or__ = MagicMock(return_value=mock_chain)
conv_proc = conversation_processing
mock_llm = MagicMock()
mock_llm.bind.return_value = mock_llm
mock_llm.__or__ = MagicMock(return_value=mock_chain)
with patch.object(conv_proc, 'get_llm', return_value=mock_llm) as mock_get_llm, patch.object(
conv_proc, 'PydanticOutputParser'
) as mock_parser_cls, patch.object(conv_proc, 'ChatPromptTemplate') as mock_prompt_cls:
mock_parser = MagicMock()
mock_parser.get_format_instructions.return_value = "format"
mock_parser_cls.return_value = mock_parser
mock_prompt = MagicMock()
mock_prompt.__or__ = MagicMock(return_value=mock_chain)
mock_prompt_cls.from_messages.return_value = mock_prompt
result = conv_proc.extract_action_items(
transcript="Boundary test",
started_at=datetime.now(timezone.utc),
language_code="en",
tz="UTC",
)
assert len(result) == 1
# At exact boundary, strict < means it should NOT be cleared
assert result[0].due_at is not None
class TestActionItemTimezoneConversion:
"""#7059: the LLM emits naive LOCAL due dates; the server must convert them to UTC in Python."""
def _zone_info(self, key):
if key == "Asia/Kolkata":
return IST_TZ
return ZoneInfo(key)
def _run(self, due_at, tz):
from models.structured_extraction import ActionItemsExtraction, ExtractedActionItem
mock_response = ActionItemsExtraction(action_items=[ExtractedActionItem(description="Task", due_at=due_at)])
mock_chain = MagicMock()
mock_chain.invoke.return_value = mock_response
mock_chain.__or__ = MagicMock(return_value=mock_chain)
conv_proc = conversation_processing
mock_llm = MagicMock()
mock_llm.bind.return_value = mock_llm
mock_llm.__or__ = MagicMock(return_value=mock_chain)
with patch.object(conv_proc, 'get_llm', return_value=mock_llm), patch.object(
conv_proc, 'PydanticOutputParser'
) as mock_parser_cls, patch.object(conv_proc, 'ChatPromptTemplate') as mock_prompt_cls, patch.object(
conv_proc, 'ZoneInfo', side_effect=self._zone_info
):
mock_parser = MagicMock()
mock_parser.get_format_instructions.return_value = "format"
mock_parser_cls.return_value = mock_parser
mock_prompt = MagicMock()
mock_prompt.__or__ = MagicMock(return_value=mock_chain)
mock_prompt_cls.from_messages.return_value = mock_prompt
result = conv_proc.extract_action_items(
transcript="t",
started_at=datetime.now(timezone.utc),
language_code="en",
tz=tz,
)
return result, mock_chain
def test_naive_local_due_converted_to_utc_for_ist(self):
# LLM emits naive local 10:00 IST tomorrow -> server stores 04:30 UTC (the #7059 bug).
naive_local = (datetime.now(timezone.utc).astimezone(IST_TZ) + timedelta(days=1)).replace(
hour=10, minute=0, second=0, microsecond=0, tzinfo=None
)
result, _ = self._run(naive_local, tz="Asia/Kolkata")
assert len(result) == 1 and result[0].due_at is not None
due = result[0].due_at
assert due.utcoffset() == timedelta(0) # stored as UTC
assert (due.hour, due.minute) == (4, 30) # 10:00 IST == 04:30 UTC
def test_utc_tz_no_shift(self):
naive_local = (datetime.now(timezone.utc) + timedelta(days=1)).replace(
hour=15, minute=0, second=0, microsecond=0, tzinfo=None
)
result, _ = self._run(naive_local, tz="UTC")
due = result[0].due_at
assert due.utcoffset() == timedelta(0)
assert (due.hour, due.minute) == (15, 0) # no shift for UTC
def test_invalid_tz_falls_back_to_utc_without_crashing(self):
naive_local = (datetime.now(timezone.utc) + timedelta(days=1)).replace(
hour=9, minute=0, second=0, microsecond=0, tzinfo=None
)
result, _ = self._run(naive_local, tz="Not/AZone")
due = result[0].due_at
assert due is not None and due.utcoffset() == timedelta(0)
assert (due.hour, due.minute) == (9, 0) # treated as UTC on fallback
def test_passes_local_reference_times_to_prompt(self):
naive_local = (datetime.now(timezone.utc) + timedelta(days=1)).replace(microsecond=0, tzinfo=None)
_, mock_chain = self._run(naive_local, tz="Asia/Kolkata")
invoke_args = mock_chain.invoke.call_args[0][0]
assert 'started_at_local' in invoke_args and 'current_time_local' in invoke_args
ct = invoke_args['current_time_local']
assert 'Z' not in ct and '+' not in ct # naive local string, no offset/suffix
def test_aware_datetime_is_normalized_to_utc(self):
# Regression guard for the else-branch: if the LLM ignores the prompt and emits a tz-aware
# value, it must be normalized to UTC (not shifted again, not trusted at a non-UTC offset).
base = datetime.now(timezone.utc) + timedelta(days=1)
# (a) already-UTC aware value stays UTC unchanged
aware_utc = base.replace(hour=4, minute=30, second=0, microsecond=0)
r1, _ = self._run(aware_utc, tz="Asia/Kolkata")
d1 = r1[0].due_at
assert d1 is not None and d1.utcoffset() == timedelta(0)
assert (d1.hour, d1.minute) == (4, 30)
# (b) aware +05:30 value is converted to UTC, not trusted as-is
aware_ist = base.replace(hour=10, minute=0, second=0, microsecond=0, tzinfo=IST_TZ)
r2, _ = self._run(aware_ist, tz="UTC")
d2 = r2[0].due_at
assert d2 is not None and d2.utcoffset() == timedelta(0)
assert (d2.hour, d2.minute) == (4, 30) # 10:00 IST -> 04:30 UTC