Allow listing languages, setting source language (#192)
[akkoma] / test / pleroma / translators / deepl_test.exs
index 286d21d3ef2d72787d06c3782cfef266ca02b6c2..58f23fe2616a7eb0b171578b8390ec2eecd3ffde 100644 (file)
@@ -8,6 +8,36 @@ defmodule Pleroma.Akkoma.Translators.DeepLTest do
       clear_config([:deepl, :api_key], "deepl_api_key")
     end
 
+    test "should list supported languages" do
+      clear_config([:deepl, :tier], :free)
+
+      Tesla.Mock.mock(fn
+        %{method: :get, url: "https://api-free.deepl.com/v2/languages?type=target"} = env ->
+          auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
+          assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
+
+          %Tesla.Env{
+            status: 200,
+            body:
+              Jason.encode!([
+                %{
+                  "language" => "BG",
+                  "name" => "Bulgarian",
+                  "supports_formality" => false
+                },
+                %{
+                  "language" => "CS",
+                  "name" => "Czech",
+                  "supports_formality" => false
+                }
+              ])
+          }
+      end)
+
+      assert {:ok, [%{code: "BG", name: "Bulgarian"}, %{code: "CS", name: "Czech"}]} =
+               DeepL.languages()
+    end
+
     test "should work with the free tier" do
       clear_config([:deepl, :tier], :free)
 
@@ -30,7 +60,7 @@ defmodule Pleroma.Akkoma.Translators.DeepLTest do
           }
       end)
 
-      assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", "en")
+      assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
     end
 
     test "should work with the pro tier" do
@@ -55,7 +85,33 @@ defmodule Pleroma.Akkoma.Translators.DeepLTest do
           }
       end)
 
-      assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", "en")
+      assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
+    end
+
+    test "should assign source language if set" do
+      clear_config([:deepl, :tier], :pro)
+
+      Tesla.Mock.mock(fn
+        %{method: :post, url: "https://api.deepl.com/v2/translate"} = env ->
+          auth_header = Enum.find(env.headers, fn {k, _v} -> k == "authorization" end)
+          assert {"authorization", "DeepL-Auth-Key deepl_api_key"} = auth_header
+          assert String.contains?(env.body, "source_lang=ja")
+
+          %Tesla.Env{
+            status: 200,
+            body:
+              Jason.encode!(%{
+                translations: [
+                  %{
+                    "text" => "I will crush you",
+                    "detected_source_language" => "ja"
+                  }
+                ]
+              })
+          }
+      end)
+
+      assert {:ok, "ja", "I will crush you"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", "ja", "en")
     end
 
     test "should gracefully fail if the API errors" do
@@ -69,7 +125,8 @@ defmodule Pleroma.Akkoma.Translators.DeepLTest do
           }
       end)
 
-      assert {:error, "DeepL request failed (code 403)"} = DeepL.translate("ギュギュ握りつぶしちゃうぞ", "en")
+      assert {:error, "DeepL request failed (code 403)"} =
+               DeepL.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
     end
   end
 end