2022-08-29 19:42:22 +00:00
|
|
|
defmodule Pleroma.Akkoma.Translators.LibreTranslateTest do
|
|
|
|
use Pleroma.DataCase, async: true
|
|
|
|
|
|
|
|
alias Pleroma.Akkoma.Translators.LibreTranslate
|
|
|
|
|
|
|
|
describe "translating with libre translate" do
|
|
|
|
setup do
|
|
|
|
clear_config([:libre_translate, :url], "http://libre.translate/translate")
|
|
|
|
end
|
|
|
|
|
2022-08-30 14:58:54 +00:00
|
|
|
test "should list supported languages" do
|
|
|
|
clear_config([:deepl, :tier], :free)
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{method: :get, url: "http://libre.translate/languages"} = _ ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body:
|
|
|
|
Jason.encode!([
|
|
|
|
%{
|
|
|
|
"code" => "en",
|
|
|
|
"name" => "English"
|
|
|
|
},
|
|
|
|
%{
|
|
|
|
"code" => "ar",
|
|
|
|
"name" => "Arabic"
|
|
|
|
}
|
|
|
|
])
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2022-08-30 16:59:33 +00:00
|
|
|
assert {:ok, [%{code: "en", name: "English"}, %{code: "ar", name: "Arabic"}],
|
|
|
|
[%{code: "en", name: "English"}, %{code: "ar", name: "Arabic"}]} =
|
2022-08-30 14:58:54 +00:00
|
|
|
LibreTranslate.languages()
|
|
|
|
end
|
|
|
|
|
2022-08-29 19:42:22 +00:00
|
|
|
test "should work without an API key" do
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{method: :post, url: "http://libre.translate/translate"} = env ->
|
2022-08-30 14:58:54 +00:00
|
|
|
assert {:ok, %{"api_key" => nil, "source" => "auto"}} = Jason.decode(env.body)
|
2022-08-29 19:42:22 +00:00
|
|
|
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body:
|
|
|
|
Jason.encode!(%{
|
|
|
|
detectedLanguage: %{
|
|
|
|
confidence: 83,
|
|
|
|
language: "ja"
|
|
|
|
},
|
|
|
|
translatedText: "I will crush you"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2022-08-30 14:58:54 +00:00
|
|
|
assert {:ok, "ja", "I will crush you"} =
|
|
|
|
LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
|
2022-08-29 19:42:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should work with an API key" do
|
|
|
|
clear_config([:libre_translate, :api_key], "libre_translate_api_key")
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{method: :post, url: "http://libre.translate/translate"} = env ->
|
|
|
|
assert {:ok, %{"api_key" => "libre_translate_api_key"}} = Jason.decode(env.body)
|
|
|
|
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body:
|
|
|
|
Jason.encode!(%{
|
|
|
|
detectedLanguage: %{
|
|
|
|
confidence: 83,
|
|
|
|
language: "ja"
|
|
|
|
},
|
|
|
|
translatedText: "I will crush you"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2022-08-30 14:58:54 +00:00
|
|
|
assert {:ok, "ja", "I will crush you"} =
|
|
|
|
LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
|
2022-08-29 19:42:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should gracefully handle API key errors" do
|
|
|
|
clear_config([:libre_translate, :api_key], "")
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{method: :post, url: "http://libre.translate/translate"} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 403,
|
|
|
|
body:
|
|
|
|
Jason.encode!(%{
|
|
|
|
error: "Please contact the server operator to obtain an API key"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert {:error, "libre_translate: request failed (code 403)"} =
|
2022-08-30 14:58:54 +00:00
|
|
|
LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
|
|
|
|
end
|
|
|
|
|
|
|
|
test "should set a source language if requested" do
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{method: :post, url: "http://libre.translate/translate"} = env ->
|
|
|
|
assert {:ok, %{"api_key" => nil, "source" => "ja"}} = Jason.decode(env.body)
|
|
|
|
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body:
|
|
|
|
Jason.encode!(%{
|
|
|
|
translatedText: "I will crush you"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert {:ok, "ja", "I will crush you"} =
|
|
|
|
LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", "ja", "en")
|
2022-08-29 19:42:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "should gracefully handle an unsupported language" do
|
|
|
|
clear_config([:libre_translate, :api_key], "")
|
|
|
|
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{method: :post, url: "http://libre.translate/translate"} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 400,
|
|
|
|
body:
|
|
|
|
Jason.encode!(%{
|
|
|
|
error: "zoop is not supported"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
|
|
|
assert {:error, "libre_translate: request failed (code 400)"} =
|
2022-08-30 14:58:54 +00:00
|
|
|
LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "zoop")
|
2022-08-29 19:42:22 +00:00
|
|
|
end
|
2023-05-08 22:52:19 +00:00
|
|
|
|
|
|
|
test "should work when no detected language is received" do
|
|
|
|
Tesla.Mock.mock(fn
|
|
|
|
%{method: :post, url: "http://libre.translate/translate"} ->
|
|
|
|
%Tesla.Env{
|
|
|
|
status: 200,
|
|
|
|
body:
|
|
|
|
Jason.encode!(%{
|
|
|
|
translatedText: "I will crush you"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
|
2023-05-22 22:53:44 +00:00
|
|
|
assert {:ok, "", "I will crush you"} = LibreTranslate.translate("ギュギュ握りつぶしちゃうぞ", nil, "en")
|
2023-05-08 22:52:19 +00:00
|
|
|
end
|
2022-08-29 19:42:22 +00:00
|
|
|
end
|
|
|
|
end
|