1 defmodule Pleroma.Akkoma.Translators.DeepLTest do
2 use Pleroma.DataCase, async: true
4 alias Pleroma.Akkoma.Translators.DeepL
6 describe "translating with deepl" do
8 clear_config([:deepl, :api_key], "deepl_api_key")
11 test "should list supported languages" do
12 clear_config([:deepl, :tier], :free)
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
25 "name" => "Bulgarian",
26 "supports_formality" => false
31 "supports_formality" => false
36 %{method: :get, url: "https://api-free.deepl.com/v2/languages?type=source"} ->
44 "supports_formality" => false
50 assert {:ok, [%{code: "JA", name: "Japanese"}],
51 [%{code: "BG", name: "Bulgarian"}, %{code: "CS", name: "Czech"}]} =
55 test "should work with the free tier" do
56 clear_config([:deepl, :tier], :free)
59 %{method: :post, url: "https://api-free.deepl.com/v2/translate"} = env ->
60 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
61 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
69 "text" => "I will crush you",
70 "detected_source_language" => "ja"
77 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
80 test "should work with the pro tier" do
81 clear_config([:deepl, :tier], :pro)
84 %{method: :post, url: "https://api.deepl.com/v2/translate"} = env ->
85 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
86 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
94 "text" => "I will crush you",
95 "detected_source_language" => "ja"
102 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
105 test "should assign source language if set" do
106 clear_config([:deepl, :tier], :pro)
109 %{method: :post, url: "https://api.deepl.com/v2/translate"} = env ->
110 auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
111 assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
112 assert String.contains?(env.body, "source_lang=ja")
120 "text" => "I will crush you",
121 "detected_source_language" => "ja"
128 assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", "ja", "en")
131 test "should gracefully fail if the API errors" do
132 clear_config([:deepl, :tier], :free)
135 %{method: :post, url: "https://api-free.deepl.com/v2/translate"} ->
142 assert {:error, "DeepL request failed (code 403)"} =
143 DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")