Merge branch 'remake-remodel' into develop
[akkoma] / test / docs / generator_test.exs
1 defmodule Pleroma.Docs.GeneratorTest do
2 use ExUnit.Case, async: true
3 alias Pleroma.Docs.Generator
4
5 @descriptions [
6 %{
7 group: :pleroma,
8 key: Pleroma.Upload,
9 type: :group,
10 description: "",
11 children: [
12 %{
13 key: :uploader,
14 type: :module,
15 description: "",
16 suggestions:
17 Generator.list_modules_in_dir(
18 "lib/pleroma/upload/filter",
19 "Elixir.Pleroma.Upload.Filter."
20 )
21 },
22 %{
23 key: :filters,
24 type: {:list, :module},
25 description: "",
26 suggestions:
27 Generator.list_modules_in_dir(
28 "lib/pleroma/web/activity_pub/mrf",
29 "Elixir.Pleroma.Web.ActivityPub.MRF."
30 )
31 },
32 %{
33 key: Pleroma.Upload,
34 type: :string,
35 description: "",
36 suggestions: [""]
37 },
38 %{
39 key: :some_key,
40 type: :keyword,
41 description: "",
42 suggestions: [],
43 children: [
44 %{
45 key: :another_key,
46 type: :integer,
47 description: "",
48 suggestions: [5]
49 },
50 %{
51 key: :another_key_with_label,
52 label: "Another label",
53 type: :integer,
54 description: "",
55 suggestions: [7]
56 }
57 ]
58 },
59 %{
60 key: :key1,
61 type: :atom,
62 description: "",
63 suggestions: [
64 :atom,
65 Pleroma.Upload,
66 {:tuple, "string", 8080},
67 [:atom, Pleroma.Upload, {:atom, Pleroma.Upload}]
68 ]
69 },
70 %{
71 key: Pleroma.Upload,
72 label: "Special Label",
73 type: :string,
74 description: "",
75 suggestions: [""]
76 },
77 %{
78 group: {:subgroup, Swoosh.Adapters.SMTP},
79 key: :auth,
80 type: :atom,
81 description: "`Swoosh.Adapters.SMTP` adapter specific setting",
82 suggestions: [:always, :never, :if_available]
83 },
84 %{
85 key: "application/xml",
86 type: {:list, :string},
87 suggestions: ["xml"]
88 },
89 %{
90 key: :versions,
91 type: {:list, :atom},
92 description: "List of TLS version to use",
93 suggestions: [:tlsv1, ":tlsv1.1", ":tlsv1.2"]
94 }
95 ]
96 },
97 %{
98 group: :tesla,
99 key: :adapter,
100 type: :group,
101 description: ""
102 },
103 %{
104 group: :cors_plug,
105 type: :group,
106 children: [%{key: :key1, type: :string, suggestions: [""]}]
107 },
108 %{group: "Some string group", key: "Some string key", type: :group}
109 ]
110
111 describe "convert_to_strings/1" do
112 test "group, key, label" do
113 [desc1, desc2 | _] = Generator.convert_to_strings(@descriptions)
114
115 assert desc1[:group] == ":pleroma"
116 assert desc1[:key] == "Pleroma.Upload"
117 assert desc1[:label] == "Pleroma.Upload"
118
119 assert desc2[:group] == ":tesla"
120 assert desc2[:key] == ":adapter"
121 assert desc2[:label] == "Adapter"
122 end
123
124 test "group without key" do
125 descriptions = Generator.convert_to_strings(@descriptions)
126 desc = Enum.at(descriptions, 2)
127
128 assert desc[:group] == ":cors_plug"
129 refute desc[:key]
130 assert desc[:label] == "Cors plug"
131 end
132
133 test "children key, label, type" do
134 [%{children: [child1, child2, child3, child4 | _]} | _] =
135 Generator.convert_to_strings(@descriptions)
136
137 assert child1[:key] == ":uploader"
138 assert child1[:label] == "Uploader"
139 assert child1[:type] == :module
140
141 assert child2[:key] == ":filters"
142 assert child2[:label] == "Filters"
143 assert child2[:type] == {:list, :module}
144
145 assert child3[:key] == "Pleroma.Upload"
146 assert child3[:label] == "Pleroma.Upload"
147 assert child3[:type] == :string
148
149 assert child4[:key] == ":some_key"
150 assert child4[:label] == "Some key"
151 assert child4[:type] == :keyword
152 end
153
154 test "child with predefined label" do
155 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
156 child = Enum.at(children, 5)
157 assert child[:key] == "Pleroma.Upload"
158 assert child[:label] == "Special Label"
159 end
160
161 test "subchild" do
162 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
163 child = Enum.at(children, 3)
164 %{children: [subchild | _]} = child
165
166 assert subchild[:key] == ":another_key"
167 assert subchild[:label] == "Another key"
168 assert subchild[:type] == :integer
169 end
170
171 test "subchild with predefined label" do
172 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
173 child = Enum.at(children, 3)
174 %{children: subchildren} = child
175 subchild = Enum.at(subchildren, 1)
176
177 assert subchild[:key] == ":another_key_with_label"
178 assert subchild[:label] == "Another label"
179 end
180
181 test "module suggestions" do
182 [%{children: [%{suggestions: suggestions} | _]} | _] =
183 Generator.convert_to_strings(@descriptions)
184
185 Enum.each(suggestions, fn suggestion ->
186 assert String.starts_with?(suggestion, "Pleroma.")
187 end)
188 end
189
190 test "atoms in suggestions with leading `:`" do
191 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
192 %{suggestions: suggestions} = Enum.at(children, 4)
193 assert Enum.at(suggestions, 0) == ":atom"
194 assert Enum.at(suggestions, 1) == "Pleroma.Upload"
195 assert Enum.at(suggestions, 2) == {":tuple", "string", 8080}
196 assert Enum.at(suggestions, 3) == [":atom", "Pleroma.Upload", {":atom", "Pleroma.Upload"}]
197
198 %{suggestions: suggestions} = Enum.at(children, 6)
199 assert Enum.at(suggestions, 0) == ":always"
200 assert Enum.at(suggestions, 1) == ":never"
201 assert Enum.at(suggestions, 2) == ":if_available"
202 end
203
204 test "group, key as string in main desc" do
205 descriptions = Generator.convert_to_strings(@descriptions)
206 desc = Enum.at(descriptions, 3)
207 assert desc[:group] == "Some string group"
208 assert desc[:key] == "Some string key"
209 end
210
211 test "key as string subchild" do
212 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
213 child = Enum.at(children, 7)
214 assert child[:key] == "application/xml"
215 end
216
217 test "suggestion for tls versions" do
218 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
219 child = Enum.at(children, 8)
220 assert child[:suggestions] == [":tlsv1", ":tlsv1.1", ":tlsv1.2"]
221 end
222
223 test "subgroup with module name" do
224 [%{children: children} | _] = Generator.convert_to_strings(@descriptions)
225
226 %{group: subgroup} = Enum.at(children, 6)
227 assert subgroup == {":subgroup", "Swoosh.Adapters.SMTP"}
228 end
229 end
230 end