58f23fe2616a7eb0b171578b8390ec2eecd3ffde
[akkoma] / test / pleroma / translators / deepl_test.exs
1 defmodule Pleroma.Akkoma.Translators.DeepLTest do
2 use Pleroma.DataCase, async: true
3
4 alias Pleroma.Akkoma.Translators.DeepL
5
6 describe "translating with deepl" do
7 setup do
8 clear_config([:deepl, :api_key], "deepl_api_key")
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: "https://api-free.deepl.com/v2/languages?type=target"} = env ->
16 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
17 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
18
19 %Tesla.Env{
20 status: 200,
21 body:
22 Jason.encode!([
23 %{
24 "language" => "BG",
25 "name" => "Bulgarian",
26 "supports_formality" => false
27 },
28 %{
29 "language" => "CS",
30 "name" => "Czech",
31 "supports_formality" => false
32 }
33 ])
34 }
35 end)
36
37 assert {:ok, [%{code: "BG", name: "Bulgarian"}, %{code: "CS", name: "Czech"}]} =
38 DeepL.languages()
39 end
40
41 test "should work with the free tier" do
42 clear_config([:deepl, :tier], :free)
43
44 Tesla.Mock.mock(fn
45 %{method: :post, url: "https://api-free.deepl.com/v2/translate"} = env ->
46 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
47 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
48
49 %Tesla.Env{
50 status: 200,
51 body:
52 Jason.encode!(%{
53 translations: [
54 %{
55 "text" => "I will crush you",
56 "detected_source_language" => "ja"
57 }
58 ]
59 })
60 }
61 end)
62
63 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
64 end
65
66 test "should work with the pro tier" do
67 clear_config([:deepl, :tier], :pro)
68
69 Tesla.Mock.mock(fn
70 %{method: :post, url: "https://api.deepl.com/v2/translate"} = env ->
71 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
72 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
73
74 %Tesla.Env{
75 status: 200,
76 body:
77 Jason.encode!(%{
78 translations: [
79 %{
80 "text" => "I will crush you",
81 "detected_source_language" => "ja"
82 }
83 ]
84 })
85 }
86 end)
87
88 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
89 end
90
91 test "should assign source language if set" do
92 clear_config([:deepl, :tier], :pro)
93
94 Tesla.Mock.mock(fn
95 %{method: :post, url: "https://api.deepl.com/v2/translate"} = env ->
96 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
97 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
98 assert String.contains?(env.body, "source_lang=ja")
99
100 %Tesla.Env{
101 status: 200,
102 body:
103 Jason.encode!(%{
104 translations: [
105 %{
106 "text" => "I will crush you",
107 "detected_source_language" => "ja"
108 }
109 ]
110 })
111 }
112 end)
113
114 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", "ja", "en")
115 end
116
117 test "should gracefully fail if the API errors" do
118 clear_config([:deepl, :tier], :free)
119
120 Tesla.Mock.mock(fn
121 %{method: :post, url: "https://api-free.deepl.com/v2/translate"} ->
122 %Tesla.Env{
123 status: 403,
124 body: ""
125 }
126 end)
127
128 assert {:error, "DeepL request failed (code 403)"} =
129 DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
130 end
131 end
132 end