From 9a1dfecc5e5c2a3a89e2b912d678bfbd5e51b194 Mon Sep 17 00:00:00 2001 From: Caio <117518+caiosba@users.noreply.github.com> Date: Wed, 21 Aug 2024 11:30:07 -0300 Subject: [PATCH] Use different query normalization settings for different BSPs (CAPI/Smooch-Zendesk) --- app/models/concerns/smooch_search.rb | 7 ++++++- test/controllers/graphql_controller_11_test.rb | 6 +++--- test/models/bot/smooch_3_test.rb | 2 ++ test/models/bot/smooch_4_test.rb | 2 ++ test/models/bot/smooch_5_test.rb | 4 ++++ test/models/bot/smooch_7_test.rb | 2 ++ 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/concerns/smooch_search.rb b/app/models/concerns/smooch_search.rb index 8c66f2c1c0..f4f63708a3 100644 --- a/app/models/concerns/smooch_search.rb +++ b/app/models/concerns/smooch_search.rb @@ -142,7 +142,12 @@ def get_search_results(uid, message, team_id, language) end def normalized_query_hash(type, query, team_ids, after, feed_id, language) - normalized_query = query.downcase.chomp.strip unless query.nil? + normalized_query = nil + if RequestStore.store[:smooch_bot_provider] == 'CAPI' + normalized_query = query.to_s.downcase.chomp.strip.truncate(4096) + elsif RequestStore.store[:smooch_bot_provider] == 'SMOOCH' + normalized_query = query.to_s.downcase.chomp.strip.truncate(1024) + end Digest::MD5.hexdigest([type.to_s, normalized_query, [team_ids].flatten.join(','), after.to_s, feed_id.to_i, language.to_s].join(':')) end diff --git a/test/controllers/graphql_controller_11_test.rb b/test/controllers/graphql_controller_11_test.rb index af6670ab50..43995dc5f1 100644 --- a/test/controllers/graphql_controller_11_test.rb +++ b/test/controllers/graphql_controller_11_test.rb @@ -132,10 +132,10 @@ def teardown post :create, params: { query: query } assert_response :success response = JSON.parse(@response.body)['data']['me'] - data = response['accessible_teams']['edges'] + data = response['accessible_teams']['edges'].collect{ |edge| edge['node']['dbid'] }.sort assert_equal 2, data.size - assert_equal team1.id, data[0]['node']['dbid'] - assert_equal team2.id, data[1]['node']['dbid'] + assert_equal team1.id, data[0] + assert_equal team2.id, data[1] assert_equal 2, response['accessible_teams_count'] end diff --git a/test/models/bot/smooch_3_test.rb b/test/models/bot/smooch_3_test.rb index acef2b3e66..883b89b9e9 100644 --- a/test/models/bot/smooch_3_test.rb +++ b/test/models/bot/smooch_3_test.rb @@ -558,9 +558,11 @@ def teardown 'Segurança das urna', 'Seguranca das urnas' ].each do |query| + Rails.cache.clear assert_equal [pm1.id], Bot::Smooch.search_for_similar_published_fact_checks('text', query, [t.id]).to_a.map(&:id) end + Rails.cache.clear assert_equal [], Bot::Smooch.search_for_similar_published_fact_checks('text', 'Segurando', [t.id]).to_a.map(&:id) end diff --git a/test/models/bot/smooch_4_test.rb b/test/models/bot/smooch_4_test.rb index 34167fa5f6..ff17eca751 100644 --- a/test/models/bot/smooch_4_test.rb +++ b/test/models/bot/smooch_4_test.rb @@ -671,9 +671,11 @@ def teardown uid = random_string query = Bot::Smooch.get_search_query(uid, {}) + Rails.cache.clear assert_equal [pm2], Bot::Smooch.get_search_results(uid, query, t.id, 'en') Bot::Smooch.stubs(:bundle_list_of_messages).returns({ 'type' => 'text', 'text' => "Test #{url}" }) query = Bot::Smooch.get_search_query(uid, {}) + Rails.cache.clear assert_equal [pm1], Bot::Smooch.get_search_results(uid, query, t.id, 'en') ProjectMedia.any_instance.unstub(:report_status) diff --git a/test/models/bot/smooch_5_test.rb b/test/models/bot/smooch_5_test.rb index 0321dca79f..befb70b9a7 100644 --- a/test/models/bot/smooch_5_test.rb +++ b/test/models/bot/smooch_5_test.rb @@ -69,15 +69,19 @@ def teardown with_current_user_and_team(u, t1) do # Keyword search + Rails.cache.clear assert_equal [pm1a, pm1f, pm2a].sort, Bot::Smooch.search_for_similar_published_fact_checks('text', 'Test', [t1.id, t2.id, t3.id, t4.id], nil, f1.id).to_a.sort # Text similarity search + Rails.cache.clear assert_equal [pm1a, pm1d, pm2a], Bot::Smooch.search_for_similar_published_fact_checks('text', 'This is a test', [t1.id, t2.id, t3.id, t4.id], nil, f1.id).to_a # Media similarity search + Rails.cache.clear assert_equal [pm1a, pm1d, pm2a], Bot::Smooch.search_for_similar_published_fact_checks('image', random_url, [t1.id, t2.id, t3.id, t4.id], nil, f1.id).to_a # URL search + Rails.cache.clear assert_equal [pm1g, pm2b].sort, Bot::Smooch.search_for_similar_published_fact_checks('text', "Test with URL: #{url}", [t1.id, t2.id, t3.id, t4.id], nil, f1.id).to_a.sort end diff --git a/test/models/bot/smooch_7_test.rb b/test/models/bot/smooch_7_test.rb index 4fd46ac40e..c4e4426866 100644 --- a/test/models/bot/smooch_7_test.rb +++ b/test/models/bot/smooch_7_test.rb @@ -389,6 +389,7 @@ def teardown 'ward', #Fuzzy match (non-emoji) '🤣 ward', #Fuzzy match (non-emoji) ].each do |query| + Rails.cache.clear assert_equal [pm.id], Bot::Smooch.search_for_similar_published_fact_checks('text', query, [t.id]).to_a.map(&:id) end @@ -397,6 +398,7 @@ def teardown '🌞', #No match '🤣 🌞' #No match (we only perform AND) ].each do |query| + Rails.cache.clear assert_equal [], Bot::Smooch.search_for_similar_published_fact_checks('text', query, [t.id]).to_a.map(&:id) end end