|
4 | 4 |
|
5 | 5 | RSpec.describe DiscourseTranslator::LibreTranslate do |
6 | 6 | let(:mock_response) { Struct.new(:status, :body) } |
| 7 | + let(:api_key) { "12345" } |
| 8 | + |
| 9 | + before { SiteSetting.translator_libretranslate_endpoint = "http://localhost:5000" } |
7 | 10 |
|
8 | 11 | describe ".access_token" do |
9 | 12 | describe "when set" do |
|
17 | 20 |
|
18 | 21 | describe ".translate_supported?" do |
19 | 22 | it "should equate source language to target" do |
20 | | - SiteSetting.translator_libretranslate_endpoint = "http://localhost:5000" |
21 | 23 | source = "en" |
22 | 24 | target = :fr |
23 | 25 |
|
|
27 | 29 | expect(described_class.translate_supported?(source, target)).to be true |
28 | 30 | end |
29 | 31 | end |
| 32 | + |
| 33 | + describe ".translate" do |
| 34 | + fab!(:post) |
| 35 | + |
| 36 | + before do |
| 37 | + SiteSetting.translator_libretranslate_api_key = api_key |
| 38 | + Excon |
| 39 | + .expects(:get) |
| 40 | + .with(SiteSetting.translator_libretranslate_endpoint + "/languages") |
| 41 | + .returns(mock_response.new(200, [{ code: "de" }, { code: "en" }].to_json)) |
| 42 | + end |
| 43 | + |
| 44 | + it "truncates text for translation to max_characters_per_translation setting" do |
| 45 | + SiteSetting.max_characters_per_translation = 50 |
| 46 | + post.set_detected_locale("de") |
| 47 | + body = { q: post.cooked, source: "de", target: "en", format: "html", api_key: api_key } |
| 48 | + |
| 49 | + translated_text = "hur dur hur dur" |
| 50 | + # https://publicapi.dev/libre-translate-api |
| 51 | + Excon |
| 52 | + .expects(:post) |
| 53 | + .with( |
| 54 | + SiteSetting.translator_libretranslate_endpoint + "/translate", |
| 55 | + body: URI.encode_www_form(body), |
| 56 | + headers: { |
| 57 | + "Content-Type" => "application/x-www-form-urlencoded", |
| 58 | + }, |
| 59 | + ) |
| 60 | + .returns(mock_response.new(200, %{ { "translatedText": "#{translated_text}"} })) |
| 61 | + .once |
| 62 | + |
| 63 | + expect(described_class.translate(post)).to eq(["de", translated_text]) |
| 64 | + end |
| 65 | + end |
30 | 66 | end |
0 commit comments