Merge pull request 'Add YunoHost to installation guides' (#518) from ilja/akkoma...
[akkoma] / test / pleroma / akkoma / translators / argos_translate_test.exs
1 defmodule Pleroma.Akkoma.Translators.ArgosTranslateTest do
2 alias Pleroma.Akkoma.Translators.ArgosTranslate
3
4 import Mock
5
6 use Pleroma.DataCase, async: true
7
8 setup do
9 clear_config([:argos_translate, :command_argos_translate], "argos-translate_test")
10 clear_config([:argos_translate, :command_argospm], "argospm_test")
11 end
12
13 test "it lists available languages" do
14 languages =
15 with_mock System, [:passthrough],
16 cmd: fn "argospm_test", ["list"], _ ->
17 {"translate-nl_en\ntranslate-en_nl\ntranslate-ja_en\n", 0}
18 end do
19 ArgosTranslate.languages()
20 end
21
22 assert {:ok, source_langs, dest_langs} = languages
23
24 assert [%{code: "en", name: "en"}, %{code: "ja", name: "ja"}, %{code: "nl", name: "nl"}] =
25 source_langs |> Enum.sort()
26
27 assert [%{code: "en", name: "en"}, %{code: "nl", name: "nl"}] = dest_langs |> Enum.sort()
28 end
29
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")
32 end
33
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"], _ ->
38 {"yadayada", 0}
39 end do
40 ArgosTranslate.translate("blabla", "nl", "en")
41 end
42
43 assert {:ok, "nl", "yadayada"} = translation_response
44 end
45
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)
50
51 assert nil == System.find_executable(non_existing_command)
52
53 assert {:error, "ArgosTranslate failed to fetch languages" <> _} = ArgosTranslate.languages()
54
55 assert {:error, "ArgosTranslate failed to translate" <> _} =
56 ArgosTranslate.translate("blabla", "nl", "en")
57 end
58
59 test "it can strip html" do
60 content =
61 ~s[<p>What&#39;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>]
62
63 stripped_content =
64 "\nWhat's up my fellow fedizens?\n\nSo anyway\n\n#cofe\n#Suya\nammiright!\n:ablobfoxhyper:\n"
65
66 expected_response_strip_html =
67 "<br/>What&#39;s up my fellow fedizens?<br/><br/>So anyway<br/><br/>#cofe<br/>#Suya<br/>ammiright!<br/>:ablobfoxhyper:<br/>"
68
69 response_strip_html =
70 with_mock System, [:passthrough],
71 cmd: fn "argos-translate_test",
72 ["--from-lang", _, "--to-lang", _, ^stripped_content],
73 _ ->
74 {stripped_content, 0}
75 end do
76 ArgosTranslate.translate(content, "nl", "en")
77 end
78
79 clear_config([:argos_translate, :strip_html], false)
80
81 response_no_strip_html =
82 with_mock System, [:passthrough],
83 cmd: fn "argos-translate_test", ["--from-lang", _, "--to-lang", _, string], _ ->
84 {string, 0}
85 end do
86 ArgosTranslate.translate(content, "nl", "en")
87 end
88
89 assert {:ok, "nl", content} == response_no_strip_html
90
91 assert {:ok, "nl", expected_response_strip_html} == response_strip_html
92 end
93 end