From 6db63787a095f279e1504c8c829eef7018bee1e2 Mon Sep 17 00:00:00 2001 From: rsashank Date: Wed, 29 May 2024 20:17:23 +0530 Subject: [PATCH] messages: Add tests for transform_content. This commit introduces tests for the `transform_content` class method, specifically focusing on the `message_links` and `time_mentions`. These enhancements improve the reliability and testing coverage of the `transform_content` method. --- tests/ui_tools/test_messages.py | 110 +++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/tests/ui_tools/test_messages.py b/tests/ui_tools/test_messages.py index 8deb4ebf3c..1593bf6c6e 100644 --- a/tests/ui_tools/test_messages.py +++ b/tests/ui_tools/test_messages.py @@ -1,10 +1,12 @@ from collections import OrderedDict, defaultdict from datetime import date +from typing import Any, Dict, List, Tuple import pytest import pytz from bs4 import BeautifulSoup from pytest import param as case +from pytest_mock import MockerFixture from urwid import Columns, Divider, Padding, Text from zulipterminal.config.keys import keys_for_command @@ -1585,7 +1587,7 @@ def test_keypress_EDIT_MESSAGE( # fmt: on ], ) - def test_transform_content(self, mocker, raw_html, expected_content): + def test_transform_content(self, raw_html: str, expected_content: Any) -> None: expected_content = expected_content.replace("{}", QUOTED_TEXT_MARKER) content, *_ = MessageBox.transform_content(raw_html, SERVER_URL) @@ -1593,6 +1595,112 @@ def test_transform_content(self, mocker, raw_html, expected_content): rendered_text = Text(content) assert rendered_text.text == expected_content + @pytest.mark.parametrize( + "raw_html, expected_message_links", + [ + ( + """ +

https://github.com/zulip/zulip-terminal/pull/1

+ """, + { + "https://github.com/zulip/zulip-terminal/pull/1": ( + "github.com", + 1, + True, + ) + }, + ), + ( + """ +

https://foo.com

+ """, + {"https://foo.com": ("https://foo.com", 1, False)}, + ), + ( + """ +

https://chat.zulip.zulip/#narrow/stream/206-zulip-terminal/topic/announce

+ """, + { + "https://chat.zulip.zulip#narrow/stream/206-zulip-terminal/topic/announce": ( # noqa: E501 + "https://chat.zulip.zulip/#narrow/stream/206-zulip-terminal/topic/announce", + 1, + True, + ) + }, + ), + ( + """ +

test

+ """, + {"https://github.com/zulip/zulip-terminal": ("test", 1, True)}, + ), + ], + ids=[ + "github_pr", + "external_link_no_footlink", + "internal_link", + "github_link_with_text", + ], + ) + def test_transform_content_message_links( + self, raw_html: str, expected_message_links: Dict[str, Tuple[str, int, bool]] + ) -> None: + _, message_links, *_ = MessageBox.transform_content(raw_html, SERVER_URL) + + assert message_links == expected_message_links + + @pytest.mark.parametrize( + "raw_html, expected_time_mentions", + [ + ( + """ +

+ """, # noqa: E501 + [ + ( + "Wed, May 29 2024, 17:00 (IST)", + "Original text was 2024-05-29T17:00:00+05:30", + ) + ], + ), + ( + """ +

+ """, # noqa: E501 + [ + ( + "Sun, Jun 1 3000, 19:30 (IST)", + "Original text was 3000-06-01T19:30:00+05:30", + ) + ], + ), + ( + """ +

+ """, # noqa: E501 + [ + ( + "Fri, Aug 15 1947, 1:17 (IST)", + "Original text was 1947-08-15T01:17:00+05:30", + ) + ], + ), + ], + ids=["test_date", "distant_future_date", "past_date"], + ) + def test_transform_content_time_mentions( + self, + mocker: MockerFixture, + raw_html: str, + expected_time_mentions: List[Tuple[str, str]], + ) -> None: + mocker.patch( + MODULE + ".get_localzone", return_value=pytz.timezone("Asia/Kolkata") + ) + _, _, time_mentions, *_ = MessageBox.transform_content(raw_html, SERVER_URL) + + assert time_mentions == expected_time_mentions + # FIXME This is the same parametrize as MsgInfoView:test_height_reactions @pytest.mark.parametrize( "to_vary_in_each_message, expected_text, expected_attributes",