<commit_before>require 'spec_helper'

describe ChatNames::FindUserService, services: true do
  describe '#execute' do
    let(:service) { create(:service) }

    subject { described_class.new(service, params).execute }

    context 'find user mapping' do
      let(:user) { create(:user) }
      let!(:chat_name) { create(:chat_name, user: user, service: service) }

      context 'when existing user is requested' do
        let(:params) { { team_id: chat_name.team_id, user_id: chat_name.chat_id } }

        it 'returns the existing user' do
          is_expected.to eq(user)
        end

        it 'updates when last time chat name was used' do
          subject

          expect(chat_name.reload.last_used_at).to be_like_time(Time.now)
        end
      end

      context 'when different user is requested' do
        let(:params) { { team_id: chat_name.team_id, user_id: 'non-existing-user' } }

        it 'returns existing user' do
          is_expected.to be_nil
        end
      end
    end
  end
end<commit_msg>Make ChatNames::FindUserService stable by not checking a precise time<commit_after>require 'spec_helper'

describe ChatNames::FindUserService, services: true do
  describe '#execute' do
    let(:service) { create(:service) }

    subject { described_class.new(service, params).execute }

    context 'find user mapping' do
      let(:user) { create(:user) }
      let!(:chat_name) { create(:chat_name, user: user, service: service) }

      context 'when existing user is requested' do
        let(:params) { { team_id: chat_name.team_id, user_id: chat_name.chat_id } }

        it 'returns the existing user' do
          is_expected.to eq(user)
        end

        it 'updates when last time chat name was used' do
          expect(chat_name.last_used_at).to be_nil

          subject

          initial_last_used = chat_name.reload.last_used_at
          expect(initial_last_used).to be_present

          Timecop.travel(2.days.from_now) { described_class.new(service, params).execute }

          expect(chat_name.reload.last_used_at).to be > initial_last_used
        end
      end

      context 'when different user is requested' do
        let(:params) { { team_id: chat_name.team_id, user_id: 'non-existing-user' } }

        it 'returns existing user' do
          is_expected.to be_nil
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09310198899703766, 'fraction_numerical': 0.00042319085907744394, 'mean_word_length': 2.6651162790697676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4886330', 'n_tokens_mistral': 722, 'n_tokens_neox': 699, 'n_words': 193}