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