mix format
[akkoma] / test / pleroma / translators / libre_translate_test.exs
1 defmodule Pleroma.Akkoma.Translators.LibreTranslateTest do
2 use Pleroma.DataCase, async: true
3
4 alias Pleroma.Akkoma.Translators.LibreTranslate
5
6 describe "translating with libre translate" do
7 setup do
8 clear_config([:libre_translate, :url], "http://libre.translate/translate")
9 end
10
11 test "should list supported languages" do
12 clear_config([:deepl, :tier], :free)
13
14 Tesla.Mock.mock(fn
15 %{method: :get, url: "http://libre.translate/languages"} = _ ->
16 %Tesla.Env{
17 status: 200,
18 body:
19 Jason.encode!([
20 %{
21 "code" => "en",
22 "name" => "English"
23 },
24 %{
25 "code" => "ar",
26 "name" => "Arabic"
27 }
28 ])
29 }
30 end)
31
32 assert {:ok, [%{code: "en", name: "English"}, %{code: "ar", name: "Arabic"}],
33 [%{code: "en", name: "English"}, %{code: "ar", name: "Arabic"}]} =
34 LibreTranslate.languages()
35 end
36
37 test "should work without an API key" do
38 Tesla.Mock.mock(fn
39 %{method: :post, url: "http://libre.translate/translate"} = env ->
40 assert {:ok, %{"api_key" => nil, "source" => "auto"}} = Jason.decode(env.body)
41
42 %Tesla.Env{
43 status: 200,
44 body:
45 Jason.encode!(%{
46 detectedLanguage: %{
47 confidence: 83,
48 language: "ja"
49 },
50 translatedText: "I will crush you"
51 })
52 }
53 end)
54
55 assert {:ok, "ja", "I will crush you"} =
56 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
57 end
58
59 test "should work with an API key" do
60 clear_config([:libre_translate, :api_key], "libre_translate_api_key")
61
62 Tesla.Mock.mock(fn
63 %{method: :post, url: "http://libre.translate/translate"} = env ->
64 assert {:ok, %{"api_key" => "libre_translate_api_key"}} = Jason.decode(env.body)
65
66 %Tesla.Env{
67 status: 200,
68 body:
69 Jason.encode!(%{
70 detectedLanguage: %{
71 confidence: 83,
72 language: "ja"
73 },
74 translatedText: "I will crush you"
75 })
76 }
77 end)
78
79 assert {:ok, "ja", "I will crush you"} =
80 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
81 end
82
83 test "should gracefully handle API key errors" do
84 clear_config([:libre_translate, :api_key], "")
85
86 Tesla.Mock.mock(fn
87 %{method: :post, url: "http://libre.translate/translate"} ->
88 %Tesla.Env{
89 status: 403,
90 body:
91 Jason.encode!(%{
92 error: "Please contact the server operator to obtain an API key"
93 })
94 }
95 end)
96
97 assert {:error, "libre_translate: request failed (code 403)"} =
98 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
99 end
100
101 test "should set a source language if requested" do
102 Tesla.Mock.mock(fn
103 %{method: :post, url: "http://libre.translate/translate"} = env ->
104 assert {:ok, %{"api_key" => nil, "source" => "ja"}} = Jason.decode(env.body)
105
106 %Tesla.Env{
107 status: 200,
108 body:
109 Jason.encode!(%{
110 translatedText: "I will crush you"
111 })
112 }
113 end)
114
115 assert {:ok, "ja", "I will crush you"} =
116 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", "ja", "en")
117 end
118
119 test "should gracefully handle an unsupported language" do
120 clear_config([:libre_translate, :api_key], "")
121
122 Tesla.Mock.mock(fn
123 %{method: :post, url: "http://libre.translate/translate"} ->
124 %Tesla.Env{
125 status: 400,
126 body:
127 Jason.encode!(%{
128 error: "zoop is not supported"
129 })
130 }
131 end)
132
133 assert {:error, "libre_translate: request failed (code 400)"} =
134 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "zoop")
135 end
136 end
137 end