From e5a69544e825327caf0dc55ce584cd5ac8ceabb7 Mon Sep 17 00:00:00 2001 From: jipeng6036-del <241105014+jipeng6036-del@users.noreply.github.com> Date: Fri, 9 Oct 2026 18:43:17 -0700 Subject: [PATCH] fix(whatsapp): normalize neonize message timestamps to seconds Signed-off-by: jipeng6036-del <241105014+jipeng6036-del@users.noreply.github.com> --- nanobot/channels/whatsapp/runtime.py | 3 +- .../whatsapp/tests/test_whatsapp_channel.py | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/nanobot/channels/whatsapp/runtime.py b/nanobot/channels/whatsapp/runtime.py index 458e0b160..9ffd6588b 100644 --- a/nanobot/channels/whatsapp/runtime.py +++ b/nanobot/channels/whatsapp/runtime.py @@ -679,7 +679,8 @@ class WhatsAppChannel(BaseChannel): if chat_jid == "status@broadcast": return - timestamp = float(_safe_attr(info, "Timestamp", 0) or 0) + # Neonize encodes MessageInfo.Timestamp as Unix milliseconds. + timestamp = float(_safe_attr(info, "Timestamp", 0) or 0) / 1000 if self._started_at and timestamp and timestamp < self._started_at: return diff --git a/nanobot/channels/whatsapp/tests/test_whatsapp_channel.py b/nanobot/channels/whatsapp/tests/test_whatsapp_channel.py index ff3ee9063..b66530762 100644 --- a/nanobot/channels/whatsapp/tests/test_whatsapp_channel.py +++ b/nanobot/channels/whatsapp/tests/test_whatsapp_channel.py @@ -588,6 +588,40 @@ async def test_group_allow_from_does_not_allow_same_participant_in_other_group() assert bus.inbound_size == 0 +@pytest.mark.asyncio +async def test_message_before_startup_is_dropped_with_millisecond_timestamp() -> None: + bus = MessageBus() + ch = WhatsAppChannel({"enabled": True, "allowFrom": ["*"]}, bus) + ch._started_at = 1_700_000_000.250 + ch._send_read_receipt = AsyncMock() + + await ch._handle_neonize_message( + SimpleNamespace(download_any=AsyncMock()), + _event(message=_Proto(conversation="old message"), timestamp=1_700_000_000_249), + ) + + assert bus.inbound_size == 0 + ch._send_read_receipt.assert_not_awaited() + + +@pytest.mark.parametrize("timestamp", [1_700_000_000_251, 1_700_000_001_987]) +@pytest.mark.asyncio +async def test_message_after_startup_has_timestamp_in_seconds(timestamp: int) -> None: + bus = MessageBus() + ch = WhatsAppChannel({"enabled": True, "allowFrom": ["*"]}, bus) + ch._started_at = 1_700_000_000.250 + + await ch._handle_neonize_message( + SimpleNamespace(download_any=AsyncMock()), + _event(message=_Proto(conversation="new message"), timestamp=timestamp), + ) + + assert bus.inbound_size == 1 + msg = await bus.consume_inbound() + assert msg.content == "new message" + assert msg.metadata["timestamp"] == timestamp // 1000 + + @pytest.mark.asyncio async def test_read_receipt_is_requested_once_after_dedup() -> None: ch = _make_channel()