Add tests

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-10-30 21:06:31 +01:00
parent 90f91168f7
commit 90f590788c
5 changed files with 97 additions and 4 deletions

View file

@ -3,7 +3,6 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Translation do
@cache_ttl 86_400_000
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
def configured? do
@ -45,7 +44,7 @@ defmodule Pleroma.Translation do
end
defp store_result({:ok, result}, cache_key) do
@cachex.put(:translations_cache, cache_key, result, ttl: @cache_ttl)
@cachex.put(:translations_cache, cache_key, result)
end
defp store_result(_, _), do: nil

View file

@ -129,7 +129,10 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do
"profile_directory"
end,
"pleroma:get:main/ostatus",
"pleroma:group_actors"
"pleroma:group_actors",
if Pleroma.Translation.configured?() do
"translation"
end
]
|> Enum.filter(& &1)
end
@ -203,7 +206,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceView do
vapid: %{
public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
},
translation: %{enabled: Pleroma.Translation.configured?}
translation: %{enabled: Pleroma.Translation.configured?()}
})
end

View file

@ -0,0 +1,29 @@
defmodule Pleroma.TranslationTest do
use Pleroma.Web.ConnCase
alias Pleroma.Translation
# use Oban.Testing, repo: Pleroma.Repo
setup do: clear_config([Pleroma.Translation, :service], TranslationMock)
test "it translates text" do
assert {:ok,
%{
content: "txet emos",
detected_source_language: _,
provider: _
}} = Translation.translate("some text", "en", "uk")
end
test "it stores translation result in cache" do
Translation.translate("some text", "en", "uk")
assert {:ok, result} =
Cachex.get(
:translations_cache,
"en/uk/#{:crypto.hash(:sha256, "some text") |> Base.encode64()}"
)
assert result.content == "txet emos"
end
end

View file

@ -2550,4 +2550,44 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
|> json_response_and_validate_schema(:not_found)
end
end
describe "translating statuses" do
setup do: clear_config([Pleroma.Translation, :service], TranslationMock)
test "it translates a status to user language" do
user = insert(:user, language: "fr")
%{conn: conn, user: user} = oauth_access(["read:statuses"], user: user)
another_user = insert(:user)
{:ok, activity} =
CommonAPI.post(another_user, %{
status: "Cześć!",
visibility: "public",
language: "pl"
})
response =
conn
|> post("/api/v1/statuses/#{activity.id}/translate")
|> json_response_and_validate_schema(200)
assert response == %{"content" => "!ćśezC", "detected_source_language" => "pl", "provider" => "TranslationMock"}
end
test "it returns an error if no target language provided" do
%{conn: conn, user: user} = oauth_access(["read:statuses"])
another_user = insert(:user)
{:ok, activity} =
CommonAPI.post(another_user, %{
status: "Cześć!",
language: "pl"
})
response =
conn
|> post("/api/v1/statuses/#{activity.id}/translate")
|> json_response_and_validate_schema(400)
end
end
end

View file

@ -0,0 +1,22 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule TranslationMock do
alias Pleroma.Translation.Service
@behaviour Service
@impl Service
def configured?, do: true
@impl Service
def translate(content, source_language, _target_language) do
{:ok,
%{
content: content |> String.reverse(),
detected_source_language: source_language,
provider: "TranslationMock"
}}
end
end