Don't try to fetch OP if none is there.
[akkoma] / test / formatter_test.exs
1 defmodule Pleroma.FormatterTest do
2 alias Pleroma.Formatter
3 use Pleroma.DataCase
4
5 import Pleroma.Factory
6
7 describe ".add_hashtag_links" do
8 test "turns hashtags into links" do
9 text = "I love #cofe and #2hu"
10 expected_text = "I love <a href='http://localhost:4001/tag/cofe' rel='tag'>#cofe</a> and <a href='http://localhost:4001/tag/2hu' rel='tag'>#2hu</a>"
11
12 tags = Formatter.parse_tags(text)
13 assert expected_text == Formatter.add_hashtag_links({[], text}, tags) |> Formatter.finalize
14 end
15 end
16
17 describe ".add_links" do
18 test "turning urls into links" do
19 text = "Hey, check out https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla."
20 expected = "Hey, check out <a href='https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla'>https://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a>."
21
22 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
23
24 text = "https://mastodon.social/@lambadalambda"
25 expected = "<a href='https://mastodon.social/@lambadalambda'>https://mastodon.social/@lambadalambda</a>"
26
27 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
28
29 text = "@lambadalambda"
30 expected = "@lambadalambda"
31
32 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
33
34 text = "http://www.cs.vu.nl/~ast/intel/"
35 expected = "<a href='http://www.cs.vu.nl/~ast/intel/'>http://www.cs.vu.nl/~ast/intel/</a>"
36
37 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
38
39 text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
40 expected = "<a href='https://forum.zdoom.org/viewtopic.php?f=44&t=57087'>https://forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
41
42 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
43
44 text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
45 expected = "<a href='https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul'>https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
46
47 assert Formatter.add_links({[], text}) |> Formatter.finalize == expected
48 end
49 end
50
51 describe "add_user_links" do
52 test "gives a replacement for user links" do
53 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
54 gsimg = insert(:user, %{nickname: "gsimg"})
55 archaeme = insert(:user, %{nickname: "archaeme"})
56 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
57
58 mentions = Pleroma.Formatter.parse_mentions(text)
59
60 {subs, text} = Formatter.add_user_links({[], text}, mentions)
61
62 assert length(subs) == 3
63 Enum.each(subs, fn({uuid, _}) -> assert String.contains?(text, uuid) end)
64
65 expected_text = "<span><a href='#{gsimg.ap_id}'>@<span>gsimg</span></a></span> According to <span><a href='#{archaeme.ap_id}'>@<span>archaeme</span></a></span>, that is @daggsy. Also hello <span><a href='#{archaeme_remote.ap_id}'>@<span>archaeme</span></a></span>"
66
67 assert expected_text == Formatter.finalize({subs, text})
68 end
69 end
70
71 describe ".parse_tags" do
72 test "parses tags in the text" do
73 text = "Here's a #Test. Maybe these are #working or not. What about #漢字? And #は。"
74 expected = [
75 {"#Test", "test"},
76 {"#working", "working"},
77 {"#漢字", "漢字"},
78 {"#は", "は"}
79 ]
80
81 assert Formatter.parse_tags(text) == expected
82 end
83 end
84
85 test "it can parse mentions and return the relevant users" do
86 text = "@gsimg According to @archaeme, that is @daggsy. Also hello @archaeme@archae.me"
87
88 gsimg = insert(:user, %{nickname: "gsimg"})
89 archaeme = insert(:user, %{nickname: "archaeme"})
90 archaeme_remote = insert(:user, %{nickname: "archaeme@archae.me"})
91
92 expected_result = [
93 {"@gsimg", gsimg},
94 {"@archaeme", archaeme},
95 {"@archaeme@archae.me", archaeme_remote},
96 ]
97
98 assert Formatter.parse_mentions(text) == expected_result
99 end
100
101 test "it adds cool emoji" do
102 text = "I love :moominmamma:"
103
104 expected_result = "I love <img height='32px' width='32px' alt='moominmamma' title='moominmamma' src='/finmoji/128px/moominmamma-128.png' />"
105
106 assert Formatter.emojify(text) == expected_result
107 end
108
109 test "it returns the emoji used in the text" do
110 text = "I love :moominmamma:"
111
112 assert Formatter.get_emoji(text) == [{"moominmamma", "/finmoji/128px/moominmamma-128.png"}]
113 end
114 end