1 defmodule Pleroma.Akkoma.Translators.ArgosTranslateTest do
2 alias Pleroma.Akkoma.Translators.ArgosTranslate
6 use Pleroma.DataCase, async: true
9 clear_config([:argos_translate, :command_argos_translate], "argos-translate_test")
10 clear_config([:argos_translate, :command_argospm], "argospm_test")
13 test "it lists available languages" do
15 with_mock System, [:passthrough],
16 cmd: fn "argospm_test", ["list"], _ ->
17 {"translate-nl_en\ntranslate-en_nl\ntranslate-ja_en\n", 0}
19 ArgosTranslate.languages()
22 assert {:ok, source_langs, dest_langs} = languages
24 assert [%{code: "en", name: "en"}, %{code: "ja", name: "ja"}, %{code: "nl", name: "nl"}] =
25 source_langs |> Enum.sort()
27 assert [%{code: "en", name: "en"}, %{code: "nl", name: "nl"}] = dest_langs |> Enum.sort()
30 test "it translates from the to language when no language is set and returns the text unchanged" do
31 assert {:ok, "nl", "blabla"} = ArgosTranslate.translate("blabla", nil, "nl")
34 test "it translates from the provided language if provided" do
35 translation_response =
36 with_mock System, [:passthrough],
37 cmd: fn "argos-translate_test", ["--from-lang", "nl", "--to-lang", "en", "blabla"], _ ->
40 ArgosTranslate.translate("blabla", "nl", "en")
43 assert {:ok, "nl", "yadayada"} = translation_response
46 test "it returns a proper error when the executable can't be found" do
47 non_existing_command = "sfqsfgqsefd"
48 clear_config([:argos_translate, :command_argos_translate], non_existing_command)
49 clear_config([:argos_translate, :command_argospm], non_existing_command)
51 assert nil == System.find_executable(non_existing_command)
53 assert {:error, "ArgosTranslate failed to fetch languages" <> _} = ArgosTranslate.languages()
55 assert {:error, "ArgosTranslate failed to translate" <> _} =
56 ArgosTranslate.translate("blabla", "nl", "en")
59 test "it can strip html" do
61 ~s[<p>What's up my fellow fedizens?</p><p>So anyway</p><ul><li><a class="hashtag" data-tag="cofe" href="https://suya.space/tag/cofe">#cofe</a></li><li><a class="hashtag" data-tag="suya" href="https://cofe.space/tag/suya">#Suya</a></li></ul><p>ammiright!<br/>:ablobfoxhyper:</p>]
64 "\nWhat's up my fellow fedizens?\n\nSo anyway\n\n#cofe\n#Suya\nammiright!\n:ablobfoxhyper:\n"
66 expected_response_strip_html =
67 "<br/>What's up my fellow fedizens?<br/><br/>So anyway<br/><br/>#cofe<br/>#Suya<br/>ammiright!<br/>:ablobfoxhyper:<br/>"
70 with_mock System, [:passthrough],
71 cmd: fn "argos-translate_test",
72 ["--from-lang", _, "--to-lang", _, ^stripped_content],
76 ArgosTranslate.translate(content, "nl", "en")
79 clear_config([:argos_translate, :strip_html], false)
81 response_no_strip_html =
82 with_mock System, [:passthrough],
83 cmd: fn "argos-translate_test", ["--from-lang", _, "--to-lang", _, string], _ ->
86 ArgosTranslate.translate(content, "nl", "en")
89 assert {:ok, "nl", content} == response_no_strip_html
91 assert {:ok, "nl", expected_response_strip_html} == response_strip_html