<commit_before>require 'rails_helper'

RSpec.describe Translation do

  context 'default translations' do
    Translation.reset('de-de')
    Translation.sync('de-de')

    it 'en with existing word' do
      expect(Translation.translate('en', 'New')).to eq('New')
    end

    it 'en-us with existing word' do
      expect(Translation.translate('en-us', 'New')).to eq('New')
    end

    it 'en with not existing word' do
      expect(Translation.translate('en', 'Some Not Existing Word')).to eq('Some Not Existing Word')
    end

    it 'de-de with existing word' do
      expect(Translation.translate('de-de', 'New')).to eq('Neu')
    end

    it 'de-de with existing word' do
      expect(Translation.translate('de-de', 'Some Not Existing Word')).to eq('Some Not Existing Word')
    end

  end

  context 'custom translation tests' do
    Translation.where(locale: 'de-de').destroy_all
    Translation.sync('de-de')

    locale = 'de-de'

    it 'cycle of change and reload translation' do

      # check for non existing custom changes
      list = Translation.lang(locale)
      list['list'].each { |item|
        translation = Translation.find_by(source: item[1], locale: locale)
        expect(translation.class).to be(Translation)
        expect(locale).to eq(translation.locale)
        expect(translation.target).to eq(translation.target_initial)
      }

      # add custom changes
      translation = Translation.find_by(locale: locale, source: 'open')
      expect(translation.class).to be(Translation)
      expect(translation.target).to eq('offen')
      expect(translation.target_initial).to eq('offen')
      translation.target = 'offen2'
      translation.save!

      list = Translation.lang(locale)
      list['list'].each { |item|
        translation = Translation.find_by(source: item[1], locale: locale)
        expect(translation.class).to be(Translation)
        expect(locale).to eq(translation.locale)
        if translation.source == 'open'
          expect(translation.target).to eq('offen2')
          expect(translation.target_initial).to eq('offen')
        else
          expect(translation.target).to eq(translation.target_initial)
        end
      }

      # check for existing custom changes after new translations are loaded
      Translation.load(locale)
      list = Translation.lang(locale)
      list['list'].each { |item|
        translation = Translation.find_by(source: item[1], locale: locale)
        expect(translation.class).to be(Translation)
        expect(locale).to eq(translation.locale)
        if translation.source == 'open'
          expect(translation.target).to eq('offen2')
          expect(translation.target_initial).to eq('offen')
        else
          expect(translation.target).to eq(translation.target_initial)
        end
      }

      # reset custom translations and check for non existing custom changes
      Translation.reset(locale)
      list = Translation.lang(locale)
      list['list'].each { |item|
        translation = Translation.find_by(source: item[1], locale: locale)
        expect(translation.class).to be(Translation)
        expect(locale).to eq(translation.locale)
        expect(translation.target).to eq(translation.target_initial)
      }
    end

  end

  context 'file based import' do

    it 'check download of locales' do
      directory = Rails.root.join('config')
      file = Rails.root.join("#{directory}/locales.yml")
      if File.exist?(file)
        File.delete(file)
      end
      expect(File.exist?(file)).to be false
      Locale.fetch
      expect(File.exist?(file)).to be true
    end

    it 'check download of translations' do
      locale = 'de-de'
      directory = Rails.root.join('config/translations')
      if File.directory?(directory)
        FileUtils.rm_rf(directory)
      end
      file = Rails.root.join("#{directory}/#{locale}.yml")
      expect(File.exist?(file)).to be false
      Translation.fetch(locale)
      expect(File.exist?(file)).to be true
    end

  end

end<commit_msg>Improved support for different version of translation cache files. Fixed test.<commit_after>require 'rails_helper'

RSpec.describe Translation do

  context 'default translations' do
    Translation.reset('de-de')
    Translation.sync('de-de')

    it 'en with existing word' do
      expect(Translation.translate('en', 'New')).to eq('New')
    end

    it 'en-us with existing word' do
      expect(Translation.translate('en-us', 'New')).to eq('New')
    end

    it 'en with not existing word' do
      expect(Translation.translate('en', 'Some Not Existing Word')).to eq('Some Not Existing Word')
    end

    it 'de-de with existing word' do
      expect(Translation.translate('de-de', 'New')).to eq('Neu')
    end

    it 'de-de with existing word' do
      expect(Translation.translate('de-de', 'Some Not Existing Word')).to eq('Some Not Existing Word')
    end

  end

  context 'custom translation tests' do
    Translation.where(locale: 'de-de').destroy_all
    Translation.sync('de-de')

    locale = 'de-de'

    it 'cycle of change and reload translation' do

      # check for non existing custom changes
      list = Translation.lang(locale)
      list['list'].each { |item|
        translation = Translation.find_by(source: item[1], locale: locale)
        expect(translation.class).to be(Translation)
        expect(locale).to eq(translation.locale)
        expect(translation.target).to eq(translation.target_initial)
      }

      # add custom changes
      translation = Translation.find_by(locale: locale, source: 'open')
      expect(translation.class).to be(Translation)
      expect(translation.target).to eq('offen')
      expect(translation.target_initial).to eq('offen')
      translation.target = 'offen2'
      translation.save!

      list = Translation.lang(locale)
      list['list'].each { |item|
        translation = Translation.find_by(source: item[1], locale: locale)
        expect(translation.class).to be(Translation)
        expect(locale).to eq(translation.locale)
        if translation.source == 'open'
          expect(translation.target).to eq('offen2')
          expect(translation.target_initial).to eq('offen')
        else
          expect(translation.target).to eq(translation.target_initial)
        end
      }

      # check for existing custom changes after new translations are loaded
      Translation.load(locale)
      list = Translation.lang(locale)
      list['list'].each { |item|
        translation = Translation.find_by(source: item[1], locale: locale)
        expect(translation.class).to be(Translation)
        expect(locale).to eq(translation.locale)
        if translation.source == 'open'
          expect(translation.target).to eq('offen2')
          expect(translation.target_initial).to eq('offen')
        else
          expect(translation.target).to eq(translation.target_initial)
        end
      }

      # reset custom translations and check for non existing custom changes
      Translation.reset(locale)
      list = Translation.lang(locale)
      list['list'].each { |item|
        translation = Translation.find_by(source: item[1], locale: locale)
        expect(translation.class).to be(Translation)
        expect(locale).to eq(translation.locale)
        expect(translation.target).to eq(translation.target_initial)
      }
    end

  end

  context 'file based import' do

    it 'check download of locales' do
      version = Version.get
      directory = Rails.root.join('config')
      file = Rails.root.join("#{directory}/locales-#{version}.yml")
      if File.exist?(file)
        File.delete(file)
      end
      expect(File.exist?(file)).to be false
      Locale.fetch
      expect(File.exist?(file)).to be true
    end

    it 'check download of translations' do
      locale = 'de-de'
      directory = Rails.root.join('config/translations')
      if File.directory?(directory)
        FileUtils.rm_rf(directory)
      end
      file = Rails.root.join("#{directory}/#{locale}-#{version}.yml")
      expect(File.exist?(file)).to be false
      Translation.fetch(locale)
      expect(File.exist?(file)).to be true
    end

  end

end
{'fraction_non_alphanumeric': 0.12037833190025796, 'fraction_numerical': 0.0017196904557179708, 'mean_word_length': 2.89010989010989, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2739800', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2251, 'n_words': 553}