Skip to content

Commit

Permalink
Adding tests and fixing CC issues
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Jan 27, 2024
1 parent 4a44ab0 commit 53b1498
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
4 changes: 1 addition & 3 deletions app/lib/smooch_nlu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ def self.disambiguation_threshold
def self.alegre_matches_from_message(message, language, context, alegre_result_key, uid)
# FIXME: Raise exception if not in a tipline context (so, if Bot::Smooch.config is nil)
matches = []
team_slug = Team.find(Bot::Smooch.config['team_id']).slug
params = nil
response = nil
unless Bot::Smooch.config.to_h['nlu_enabled']
return []
end
Expand All @@ -80,6 +77,7 @@ def self.alegre_matches_from_message(message, language, context, alegre_result_k
end
begin
self.increment_global_counter
team_slug = Team.find(Bot::Smooch.config['team_id']).slug
# FIXME: In the future we could consider matches across all languages when options is nil
# FIXME: No need to call Alegre if it's an exact match to one of the keywords
# FIXME: No need to call Alegre if message has no word characters
Expand Down
25 changes: 25 additions & 0 deletions test/lib/smooch_nlu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,29 @@ def create_team_with_smooch_bot_installed
Bot::Smooch.get_installation('smooch_id', 'test')
assert_not_nil SmoochNlu.menu_options_from_message('I want to subscribe to the newsletter', 'en', @menu_options, random_string)
end

test 'should return empty list of matches if global rate limit is reached' do
Bot::Smooch.stubs(:config).returns({ 'nlu_enabled' => true })
Bot::Alegre.stubs(:request).never
SmoochNlu.increment_global_counter
SmoochNlu.increment_global_counter
stub_configs({ 'nlu_global_rate_limit' => 1 }) do
assert_equal [], SmoochNlu.alegre_matches_from_message('test', 'en', {}, 'test', 'test')
end
end

test 'should return empty list of matches if user rate limit is reached' do
Bot::Smooch.stubs(:config).returns({ 'nlu_enabled' => true })
Bot::Alegre.stubs(:request).never
2.times { create_tipline_message uid: '123', state: 'received' }
stub_configs({ 'nlu_user_rate_limit' => 1 }) do
assert_equal [], SmoochNlu.alegre_matches_from_message('test', 'en', {}, 'test', '123')
end
end

test 'should return empty list of matches if exception happens' do
Bot::Smooch.stubs(:config).returns({ 'nlu_enabled' => true })
Bot::Alegre.stubs(:request).never
assert_equal [], SmoochNlu.alegre_matches_from_message('test', 'en', {}, 'test', 'test')
end
end

0 comments on commit 53b1498

Please sign in to comment.