Skip to content

Commit

Permalink
Fixing code and fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Oct 6, 2023
1 parent c2e1818 commit 41406f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
7 changes: 4 additions & 3 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,12 @@ def self.save_text_message(message)
extra = {}
if link.nil?
claim = self.extract_claim(text).gsub(/\s+/, ' ').strip
if message['archived'] == CheckArchivedFlags::FlagCodes::UNCONFIRMED && ::Bot::Alegre.get_number_of_words(claim) < CheckConfig.get('min_number_of_words_for_tipline_submit_shortcut', 10, :integer)
return team
end
extra = { quote: claim }
pm = ProjectMedia.joins(:media).where('trim(lower(quote)) = ?', claim.downcase).where('project_medias.team_id' => team.id).last
# Don't create a new text media if it's an unconfirmed request with just a few words
if pm.nil? && message['archived'] == CheckArchivedFlags::FlagCodes::UNCONFIRMED && ::Bot::Alegre.get_number_of_words(claim) < CheckConfig.get('min_number_of_words_for_tipline_submit_shortcut', 10, :integer)
return team
end
else
extra = { url: link.url }
pm = ProjectMedia.joins(:media).where('medias.url' => link.url, 'project_medias.team_id' => team.id).last
Expand Down
4 changes: 2 additions & 2 deletions app/models/concerns/smooch_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ def save_message(message_json, app_id, author = nil, request_type = 'default_req
self.get_installation(self.installation_setting_id_keys, app_id)
Team.current = Team.where(id: self.config['team_id']).last
annotated = nil
if ['default_requests', 'timeout_requests', 'resource_requests', 'irrelevant_search_result_requests'].include?(request_type)
if ['default_requests', 'timeout_requests', 'irrelevant_search_result_requests'].include?(request_type)
message['archived'] = ['default_requests', 'irrelevant_search_result_requests'].include?(request_type) ? self.default_archived_flag : CheckArchivedFlags::FlagCodes::UNCONFIRMED
annotated = self.create_project_media_from_message(message)
elsif ['menu_options_requests', 'relevant_search_result_requests', 'timeout_search_requests'].include?(request_type)
elsif ['menu_options_requests', 'relevant_search_result_requests', 'timeout_search_requests', 'resource_requests'].include?(request_type)
annotated = annotated_obj
end

Expand Down
61 changes: 46 additions & 15 deletions test/models/bot/smooch_4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -476,50 +476,81 @@ def teardown

test "should request resource" do
setup_smooch_bot(true)
RequestStore.store[:skip_cached_field_update] = false
uid = random_string
rss = '<rss version="1"><channel><title>x</title><link>x</link><description>x</description><item><title>x</title><link>x</link></item></channel></rss>'
WebMock.stub_request(:get, 'http://test.com/feed.rss').to_return(status: 200, body: rss)
Sidekiq::Testing.fake! do
send_message_to_smooch_bot('Hello', uid)
send_message_to_smooch_bot('1', uid)
send_message_to_smooch_bot('4', uid)
end
Sidekiq::Worker.drain_all
a = Dynamic.where(annotation_type: 'smooch').last
assert_equal 'TiplineResource', a.annotated_type
assert_not_nil a.get_field('smooch_resource_id')
end

test "should submit short unconfirmed request" do
setup_smooch_bot(true)
RequestStore.store[:skip_cached_field_update] = false
uid = random_string
message = "Hey ho, let's go"
Sidekiq::Testing.fake! do
send_message_to_smooch_bot('Hello', uid)
send_message_to_smooch_bot(message, uid)
end
assert_no_difference 'ProjectMedia.count' do
Sidekiq::Worker.drain_all
end
a = Dynamic.where(annotation_type: 'smooch').last
annotated = a.annotated
assert_equal 'Team', a.annotated_type
end

test "should submit long unconfirmed request" do
setup_smooch_bot(true)
RequestStore.store[:skip_cached_field_update] = false
uid = random_string
message = 'This is a message that has enough words to be considered a media'
Sidekiq::Testing.fake! do
send_message_to_smooch_bot('Hello', uid)
send_message_to_smooch_bot(message, uid)
end
assert_difference 'ProjectMedia.count' do
Sidekiq::Worker.drain_all
end
Rails.cache.stubs(:read).returns(nil)
Rails.cache.stubs(:read).with("smooch:last_message_from_user:#{uid}").returns(Time.now + 10.seconds)
send_message_to_smooch_bot('4', uid)
a = Dynamic.where(annotation_type: 'smooch').last
annotated = a.annotated
assert_equal 'ProjectMedia', a.annotated_type
assert_equal CheckArchivedFlags::FlagCodes::UNCONFIRMED, annotated.archived
# verify requests_count & demand count
# Verify requests count & demand
assert_equal 1, annotated.requests_count
assert_equal 1, annotated.demand
assert_not_nil a.get_field('smooch_resource_id')
# Test auto confirm the media if resend same media as a default request
# Auto confirm the media if the same media is sent as a default request
Sidekiq::Testing.fake! do
send_message_to_smooch_bot('Hello', uid)
send_message_to_smooch_bot(message, uid)
send_message_to_smooch_bot('1', uid)
send_message_to_smooch_bot('2', uid)
end
Rails.cache.stubs(:read).returns(nil)
Rails.cache.stubs(:read).with("smooch:last_message_from_user:#{uid}").returns(Time.now + 10.seconds)
assert_no_difference 'ProjectMedia.count' do
send_message_to_smooch_bot('2', uid)
send_message_to_smooch_bot('Query', uid)
end
Rails.cache.unstub(:read)
Sidekiq::Worker.drain_all
assert_equal CheckArchivedFlags::FlagCodes::NONE, annotated.reload.archived
assert_equal 2, annotated.reload.requests_count
# Test resend same media (should not update archived cloumn)
# Test resend the same media (should not update archived column)
Sidekiq::Testing.fake! do
send_message_to_smooch_bot('Hello', uid)
send_message_to_smooch_bot('1', uid)
send_message_to_smooch_bot(message, uid)
end
Rails.cache.stubs(:read).returns(nil)
Rails.cache.stubs(:read).with("smooch:last_message_from_user:#{uid}").returns(Time.now + 10.seconds)
assert_no_difference 'ProjectMedia.count' do
send_message_to_smooch_bot('2', uid)
Sidekiq::Worker.drain_all
end
assert_equal CheckArchivedFlags::FlagCodes::NONE, annotated.reload.archived
assert_equal 3, annotated.reload.requests_count
Rails.cache.unstub(:read)
end

test "should get default TOS message" do
Expand Down

0 comments on commit 41406f5

Please sign in to comment.