|
1 | 1 | from collections import OrderedDict, defaultdict
|
2 | 2 | from datetime import date
|
| 3 | +from typing import Any, Dict, List, Tuple |
3 | 4 |
|
4 | 5 | import pytest
|
5 | 6 | import pytz
|
6 | 7 | from bs4 import BeautifulSoup
|
7 | 8 | from pytest import param as case
|
| 9 | +from pytest_mock import MockerFixture |
8 | 10 | from urwid import Columns, Divider, Padding, Text
|
9 | 11 |
|
10 | 12 | from zulipterminal.config.keys import keys_for_command
|
@@ -1585,14 +1587,120 @@ def test_keypress_EDIT_MESSAGE(
|
1585 | 1587 | # fmt: on
|
1586 | 1588 | ],
|
1587 | 1589 | )
|
1588 |
| - def test_transform_content(self, mocker, raw_html, expected_content): |
| 1590 | + def test_transform_content(self, raw_html: str, expected_content: Any) -> None: |
1589 | 1591 | expected_content = expected_content.replace("{}", QUOTED_TEXT_MARKER)
|
1590 | 1592 |
|
1591 | 1593 | content, *_ = MessageBox.transform_content(raw_html, SERVER_URL)
|
1592 | 1594 |
|
1593 | 1595 | rendered_text = Text(content)
|
1594 | 1596 | assert rendered_text.text == expected_content
|
1595 | 1597 |
|
| 1598 | + @pytest.mark.parametrize( |
| 1599 | + "raw_html, expected_message_links", |
| 1600 | + [ |
| 1601 | + case( |
| 1602 | + """ |
| 1603 | + <p><a href="https://github.com/zulip/zulip-terminal/pull/1">https://github.com/zulip/zulip-terminal/pull/1</a></p> |
| 1604 | + """, |
| 1605 | + { |
| 1606 | + "https://github.com/zulip/zulip-terminal/pull/1": ( |
| 1607 | + "github.com", |
| 1608 | + 1, |
| 1609 | + True, |
| 1610 | + ) |
| 1611 | + }, |
| 1612 | + id="github_pr", |
| 1613 | + ), |
| 1614 | + case( |
| 1615 | + """ |
| 1616 | + <p><a href="https://foo.com">https://foo.com</a></p> |
| 1617 | + """, |
| 1618 | + {"https://foo.com": ("https://foo.com", 1, False)}, |
| 1619 | + id="external_link_no_footlink", |
| 1620 | + ), |
| 1621 | + case( |
| 1622 | + """ |
| 1623 | + <p><a href="#narrow/stream/206-zulip-terminal/topic/announce">https://chat.zulip.zulip/#narrow/stream/206-zulip-terminal/topic/announce</a></p> |
| 1624 | + """, |
| 1625 | + { |
| 1626 | + "https://chat.zulip.zulip#narrow/stream/206-zulip-terminal/topic/announce": ( # noqa: E501 |
| 1627 | + "https://chat.zulip.zulip/#narrow/stream/206-zulip-terminal/topic/announce", |
| 1628 | + 1, |
| 1629 | + True, |
| 1630 | + ) |
| 1631 | + }, |
| 1632 | + id="internal_link", |
| 1633 | + ), |
| 1634 | + case( |
| 1635 | + """ |
| 1636 | + <p><a href="https://github.com/zulip/zulip-terminal">test</a></p> |
| 1637 | + """, |
| 1638 | + {"https://github.com/zulip/zulip-terminal": ("test", 1, True)}, |
| 1639 | + id="github_link_with_text", |
| 1640 | + ), |
| 1641 | + ], |
| 1642 | + ) |
| 1643 | + def test_transform_content_message_links( |
| 1644 | + self, raw_html: str, expected_message_links: Dict[str, Tuple[str, int, bool]] |
| 1645 | + ) -> None: |
| 1646 | + _, message_links, *_ = MessageBox.transform_content(raw_html, SERVER_URL) |
| 1647 | + |
| 1648 | + assert message_links == expected_message_links |
| 1649 | + |
| 1650 | + @pytest.mark.parametrize( |
| 1651 | + "raw_html, expected_time_mentions", |
| 1652 | + [ |
| 1653 | + case( |
| 1654 | + """ |
| 1655 | + <p><time datetime="2024-05-29T11:30:00Z">2024-05-29T17:00:00+05:30</time></p> |
| 1656 | + """, # noqa: E501 |
| 1657 | + [ |
| 1658 | + ( |
| 1659 | + "Wed, May 29 2024, 17:00 (IST)", |
| 1660 | + "Original text was 2024-05-29T17:00:00+05:30", |
| 1661 | + ) |
| 1662 | + ], |
| 1663 | + id="test_date", |
| 1664 | + ), |
| 1665 | + case( |
| 1666 | + """ |
| 1667 | + <p><time datetime="3000-06-01T14:00:00Z">3000-06-01T19:30:00+05:30</time></p> |
| 1668 | + """, # noqa: E501 |
| 1669 | + [ |
| 1670 | + ( |
| 1671 | + "Sun, Jun 1 3000, 19:30 (IST)", |
| 1672 | + "Original text was 3000-06-01T19:30:00+05:30", |
| 1673 | + ) |
| 1674 | + ], |
| 1675 | + id="distant_future_date", |
| 1676 | + ), |
| 1677 | + case( |
| 1678 | + """ |
| 1679 | + <p><time datetime="1947-08-14T19:47:00Z">1947-08-15T01:17:00+05:30</time></p> |
| 1680 | + """, # noqa: E501 |
| 1681 | + [ |
| 1682 | + ( |
| 1683 | + "Fri, Aug 15 1947, 1:17 (IST)", |
| 1684 | + "Original text was 1947-08-15T01:17:00+05:30", |
| 1685 | + ) |
| 1686 | + ], |
| 1687 | + id="past_date", |
| 1688 | + ), |
| 1689 | + ], |
| 1690 | + ) |
| 1691 | + def test_transform_content_time_mentions( |
| 1692 | + self, |
| 1693 | + mocker: MockerFixture, |
| 1694 | + raw_html: str, |
| 1695 | + expected_time_mentions: List[Tuple[str, str]], |
| 1696 | + ) -> None: |
| 1697 | + mocker.patch( |
| 1698 | + MODULE + ".get_localzone", return_value=pytz.timezone("Asia/Kolkata") |
| 1699 | + ) |
| 1700 | + _, _, time_mentions, *_ = MessageBox.transform_content(raw_html, SERVER_URL) |
| 1701 | + |
| 1702 | + assert time_mentions == expected_time_mentions |
| 1703 | + |
1596 | 1704 | # FIXME This is the same parametrize as MsgInfoView:test_height_reactions
|
1597 | 1705 | @pytest.mark.parametrize(
|
1598 | 1706 | "to_vary_in_each_message, expected_text, expected_attributes",
|
|
0 commit comments