tests for emoji mix task
[akkoma] / test / tasks / emoji_test.exs
1 defmodule Mix.Tasks.Pleroma.EmojiTest do
2 use ExUnit.Case, async: true
3
4 import ExUnit.CaptureIO
5 import Tesla.Mock
6
7 alias Mix.Tasks.Pleroma.Emoji
8
9 describe "ls-packs" do
10 test "with default manifest as url" do
11 mock(fn
12 %{
13 method: :get,
14 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
15 } ->
16 %Tesla.Env{
17 status: 200,
18 body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
19 }
20 end)
21
22 capture_io(fn -> Emoji.run(["ls-packs"]) end) =~
23 "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
24 end
25
26 test "with passed manifest as file" do
27 capture_io(fn ->
28 Emoji.run(["ls-packs", "-m", "test/fixtures/emoji/packs/manifest.json"])
29 end) =~ "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip"
30 end
31 end
32
33 describe "get-packs" do
34 test "download pack from default manifest" do
35 mock(fn
36 %{
37 method: :get,
38 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
39 } ->
40 %Tesla.Env{
41 status: 200,
42 body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
43 }
44
45 %{
46 method: :get,
47 url: "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
48 } ->
49 %Tesla.Env{
50 status: 200,
51 body: File.read!("test/fixtures/emoji/packs/blank.png.zip")
52 }
53
54 %{
55 method: :get,
56 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/finmoji.json"
57 } ->
58 %Tesla.Env{
59 status: 200,
60 body: File.read!("test/fixtures/emoji/packs/finmoji.json")
61 }
62 end)
63
64 assert capture_io(fn -> Emoji.run(["get-packs", "finmoji"]) end) =~ "Writing pack.json for"
65
66 emoji_path =
67 Path.join(
68 Pleroma.Config.get!([:instance, :static_dir]),
69 "emoji"
70 )
71
72 assert File.exists?(Path.join([emoji_path, "finmoji", "pack.json"]))
73 on_exit(fn -> File.rm_rf!("test/instance_static/emoji/finmoji") end)
74 end
75
76 test "pack not found" do
77 mock(fn
78 %{
79 method: :get,
80 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/index.json"
81 } ->
82 %Tesla.Env{
83 status: 200,
84 body: File.read!("test/fixtures/emoji/packs/default-manifest.json")
85 }
86 end)
87
88 assert capture_io(fn -> Emoji.run(["get-packs", "not_found"]) end) =~
89 "No pack named \"not_found\" found"
90 end
91
92 test "raise on bad sha256" do
93 mock(fn
94 %{
95 method: :get,
96 url: "https://git.pleroma.social/pleroma/emoji-index/raw/master/packs/blobs_gg.zip"
97 } ->
98 %Tesla.Env{
99 status: 200,
100 body: File.read!("test/fixtures/emoji/packs/blank.png.zip")
101 }
102 end)
103
104 assert_raise RuntimeError, ~r/^Bad SHA256 for blobs.gg/, fn ->
105 capture_io(fn ->
106 Emoji.run(["get-packs", "blobs.gg", "-m", "test/fixtures/emoji/packs/manifest.json"])
107 end)
108 end
109 end
110 end
111
112 describe "gen-pack" do
113 setup do
114 url = "https://finland.fi/wp-content/uploads/2017/06/finland-emojis.zip"
115
116 mock(fn %{
117 method: :get,
118 url: ^url
119 } ->
120 %Tesla.Env{status: 200, body: File.read!("test/fixtures/emoji/packs/blank.png.zip")}
121 end)
122
123 {:ok, url: url}
124 end
125
126 test "with default extensions", %{url: url} do
127 name = "pack1"
128 pack_json = "#{name}.json"
129 files_json = "#{name}_file.json"
130 refute File.exists?(pack_json)
131 refute File.exists?(files_json)
132
133 captured =
134 capture_io(fn ->
135 Emoji.run([
136 "gen-pack",
137 url,
138 "--name",
139 name,
140 "--license",
141 "license",
142 "--homepage",
143 "homepage",
144 "--description",
145 "description",
146 "--files",
147 files_json,
148 "--extensions",
149 ".png .gif"
150 ])
151 end)
152
153 assert captured =~ "#{pack_json} has been created with the pack1 pack"
154 assert captured =~ "Using .png .gif extensions"
155
156 assert File.exists?(pack_json)
157 assert File.exists?(files_json)
158
159 on_exit(fn ->
160 File.rm_rf!(pack_json)
161 File.rm_rf!(files_json)
162 end)
163 end
164
165 test "with custom extensions and update existing files", %{url: url} do
166 name = "pack2"
167 pack_json = "#{name}.json"
168 files_json = "#{name}_file.json"
169 refute File.exists?(pack_json)
170 refute File.exists?(files_json)
171
172 captured =
173 capture_io(fn ->
174 Emoji.run([
175 "gen-pack",
176 url,
177 "--name",
178 name,
179 "--license",
180 "license",
181 "--homepage",
182 "homepage",
183 "--description",
184 "description",
185 "--files",
186 files_json,
187 "--extensions",
188 " .png .gif .jpeg "
189 ])
190 end)
191
192 assert captured =~ "#{pack_json} has been created with the pack2 pack"
193 assert captured =~ "Using .png .gif .jpeg extensions"
194
195 assert File.exists?(pack_json)
196 assert File.exists?(files_json)
197
198 captured =
199 capture_io(fn ->
200 Emoji.run([
201 "gen-pack",
202 url,
203 "--name",
204 name,
205 "--license",
206 "license",
207 "--homepage",
208 "homepage",
209 "--description",
210 "description",
211 "--files",
212 files_json,
213 "--extensions",
214 " .png .gif .jpeg "
215 ])
216 end)
217
218 assert captured =~ "#{pack_json} has been updated with the pack2 pack"
219
220 on_exit(fn ->
221 File.rm_rf!(pack_json)
222 File.rm_rf!(files_json)
223 end)
224 end
225 end
226 end