1 defmodule Pleroma.Akkoma.Translators.LibreTranslateTest do
2 use Pleroma.DataCase, async: true
4 alias Pleroma.Akkoma.Translators.LibreTranslate
6 describe "translating with libre translate" do
8 clear_config([:libre_translate, :url], "http://libre.translate/translate")
11 test "should list supported languages" do
12 clear_config([:deepl, :tier], :free)
15 %{method: :get, url: "http://libre.translate/languages"} = _ ->
32 assert {:ok, [%{code: "en", name: "English"}, %{code: "ar", name: "Arabic"}],
33 [%{code: "en", name: "English"}, %{code: "ar", name: "Arabic"}]} =
34 LibreTranslate.languages()
37 test "should work without an API key" do
39 %{method: :post, url: "http://libre.translate/translate"} = env ->
40 assert {:ok, %{"api_key" => nil, "source" => "auto"}} = Jason.decode(env.body)
50 translatedText: "I will crush you"
55 assert {:ok, "ja", "I will crush you"} =
56 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
59 test "should work with an API key" do
60 clear_config([:libre_translate, :api_key], "libre_translate_api_key")
63 %{method: :post, url: "http://libre.translate/translate"} = env ->
64 assert {:ok, %{"api_key" => "libre_translate_api_key"}} = Jason.decode(env.body)
74 translatedText: "I will crush you"
79 assert {:ok, "ja", "I will crush you"} =
80 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
83 test "should gracefully handle API key errors" do
84 clear_config([:libre_translate, :api_key], "")
87 %{method: :post, url: "http://libre.translate/translate"} ->
92 error: "Please contact the server operator to obtain an API key"
97 assert {:error, "libre_translate: request failed (code 403)"} =
98 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
101 test "should set a source language if requested" do
103 %{method: :post, url: "http://libre.translate/translate"} = env ->
104 assert {:ok, %{"api_key" => nil, "source" => "ja"}} = Jason.decode(env.body)
110 translatedText: "I will crush you"
115 assert {:ok, "ja", "I will crush you"} =
116 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", "ja", "en")
119 test "should gracefully handle an unsupported language" do
120 clear_config([:libre_translate, :api_key], "")
123 %{method: :post, url: "http://libre.translate/translate"} ->
128 error: "zoop is not supported"
133 assert {:error, "libre_translate: request failed (code 400)"} =
134 LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "zoop")