0
  7   Cˆl–Ãa¾”ªi?u8 ¸Û71hß�þE(YÇÌc!Z(^ùÏ„€ËÿÒÌ¿Õ /    C# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe LibreTranslate::Client do
  let(:url)                { 'https://translate.example.com' }
  let(:endpoint)           { "#{url}/translate" }
  let(:languages_endpoint) { "#{url}/languages" }
  let(:api_key)            { nil }
  let(:client)             { described_class.new(url:, api_key:) }

  before { stub_hostname_resolution(url) }

  # What the instance answers, in the shape UserAgent parses as JSON.
  def stub_translate(text)
    stub_request(:post, endpoint)
      .to_return(status: 200, body: { translatedText: text }.to_json, headers: { 'Content-Type' => 'application/json' })
  end

  def stub_languages(codes)
    stub_request(:get, languages_endpoint)
      .to_return(status: 200, body: codes.map { |code| { code: } }.to_json, headers: { 'Content-Type' => 'application/json' })
  end

  def stub_refusal(status, error)
    stub_request(:post, endpoint)
      .to_return(status:, body: { error: }.to_json, headers: { 'Content-Type' => 'application/json' })
  end

  def translate(format: 'text')
    client.translate(text: 'Hello world.', target: 'de', format:)
  end

  describe '#translate' do
    before { stub_translate('Hallo Welt.') }

    it 'returns the translated text' do
      expect(translate).to eq('Hallo Welt.')
    end

    it 'lets the instance detect the source language' do
      translate

      expect(WebMock).to have_requested(:post, endpoint).with(body: hash_including('source' => 'auto'))
    end

    it 'asks for the given target and format' do
      translate(format: 'html')

      expect(WebMock).to have_requested(:post, endpoint).with(body: hash_including('target' => 'de', 'format' => 'html'))
    end

    it 'sends no API key while none is configured' do
      translate

      expect(WebMock).to have_requested(:post, endpoint).with { |request| !JSON.parse(request.body).key?('api_key') }
    end

    context 'with an API key configured' do
      let(:api_key) { 'secret-key' }

      it 'sends it' do
        translate

        expect(WebMock).to have_requested(:post, endpoint).with(body: hash_including('api_key' => api_key))
      end
    end

    # What a URL pointing at something else than a LibreTranslate instance answers with. Taking it
    # for a translation would hand the caller an empty article.
    context 'when the answer carries no translated text' do
      it 'raises for an answer that is no object' do
        stub_request(:post, endpoint)
          .to_return(status: 200, body: [].to_json, headers: { 'Content-Type' => 'application/json' })

        expect { translate }.to raise_error(described_class::UnreachableError)
      end

      it 'raises for an object without the text' do
        stub_request(:post, endpoint)
          .to_return(status: 200, body: { message: 'hello' }.to_json, headers: { 'Content-Type' => 'application/json' })

        expect { translate }.to raise_error(described_class::UnreachableError)
      end
    end
  end

  describe 'mapping a refusal onto an error' do
    it 'maps a host that does not answer onto unreachable' do
      stub_request(:post, endpoint).to_timeout

      expect { translate }.to raise_error(described_class::UnreachableError)
    end

    it 'maps a URL no request can be built from onto unreachable' do
      expect { described_class.new(url: 'translate.example.com').translate(text: 'Hello', target: 'de', format: 'text') }
        .to raise_error(described_class::UnreachableError)
    end

    it 'maps a URL with another scheme than HTTP onto unreachable' do
      expect { described_class.new(url: 'ftp://translate.example.com').translate(text: 'Hello', target: 'de', format: 'text') }
        .to raise_error(described_class::UnreachableError)
    end

    it 'maps a server error onto unreachable' do
      stub_refusal(503, 'Service Unavailable')

      expect { translate }.to raise_error(described_class::UnreachableError)
    end

    it 'maps a refused API key