<commit_before>require 'test_helper'

class ElasticsearchActiveTest < ActiveSupport::TestCase
  include SearchindexHelper

  setup do

    configure_elasticsearch(required: true)

    rebuild_searchindex

    roles  = Role.where(name: 'Agent')
    groups = Group.where(name: 'Users')

    @agent = User.create!(
      login: '<EMAIL>',
      firstname: 'E',
      lastname: 'S',
      email: '<EMAIL>',
      password: '<PASSWORD>',
      active: true,
      roles: roles,
      updated_by_id: 1,
      created_by_id: 1,
    )

    roles = Role.where(name: 'Customer')

    (1..6).each do |i|
      name = i.even? ? "Active-#{i}" : "Inactive-#{i}"
      User.create!(
        login: <EMAIL>",
        firstname: 'ActiveTest',
        lastname: name,
        active: i.even?,
        roles: roles,
        updated_by_id: 1,
        created_by_id: 1,
      )
      Organization.create!(
        name: "TestOrg-#{name}",
        active: i.even?,
        updated_by_id: 1,
        created_by_id: 1,
      )
      travel 10.seconds
    end

    # execute background jobs to index created/changed objects
    Scheduler.worker(true)
    sleep 2 # for ES to come ready/indexed
  end

  test 'active users appear before inactive users in search results' do
    result = User.search(
      current_user: @agent,
      query: 'ActiveTest',
      limit: 15,
    )
    assert(result.present?, 'result should not be empty')

    names = result.map(&:lastname)
    correct_names = %w[Active-6 Active-4 Active-2 Inactive-5 Inactive-3 Inactive-1]
    assert_equal(correct_names, names)
  end

  test 'active organizations appear before inactive organizations in search results' do
    result = Organization.search(
      current_user: @agent,
      query: 'TestOrg',
      limit: 15,
    )
    assert(result.present?, 'result should not be empty')

    names = result.map(&:name)
    correct_names = %w[TestOrg-Active-2
                       TestOrg-Active-4
                       TestOrg-Active-6
                       TestOrg-Inactive-1
                       TestOrg-Inactive-3
                       TestOrg-Inactive-5]
    assert_equal(correct_names, names)
  end

  test 'ordering of tickets are not affected by the lack of active flags' do
    ticket_setup

    result = Ticket.search(
      current_user: User.find(1),
      query: 'ticket',
      limit: 15,
    )
    assert(result.present?, 'result should not be empty')

    names = result.map(&:title)<commit_msg>Fixed failing tests in test/integration/elasticsearch_active_test.rb<commit_after>require 'test_helper'

class ElasticsearchActiveTest < ActiveSupport::TestCase
  include SearchindexHelper

  setup do

    configure_elasticsearch(required: true)

    rebuild_searchindex

    roles  = Role.where(name: 'Agent')
    groups = Group.where(name: 'Users')

    @agent = User.create!(
      login: '<EMAIL>',
      firstname: 'E',
      lastname: 'S',
      email: '<EMAIL>',
      password: '<PASSWORD>',
      active: true,
      roles: roles,
      updated_by_id: 1,
      created_by_id: 1,
    )

    roles = Role.where(name: 'Customer')

    (1..6).each do |i|
      name = i.even? ? "Active-#{i}" : "Inactive-#{i}"
      User.create!(
        login: "#{name}-<EMAIL>#{<EMAIL>",
        firstname: 'ActiveTest',
        lastname: name,
        active: i.even?,
        roles: roles,
        updated_by_id: 1,
        created_by_id: 1,
      )
      Organization.create!(
        name: "TestOrg-#{name}",
        active: i.even?,
        updated_by_id: 1,
        created_by_id: 1,
      )
      travel 10.seconds
    end

    # execute background jobs to index created/changed objects
    Scheduler.worker(true)
    sleep 2 # for ES to come ready/indexed
  end

  test 'active users appear before inactive users in search results' do
    result = User.search(
      current_user: @agent,
      query: 'ActiveTest',
      limit: 15,
    )
    assert(result.present?, 'result should not be empty')

    names = result.map(&:lastname)
    correct_names = %w[Active-2 Active-4 Active-6 Inactive-1 Inactive-3 Inactive-5]
    assert_equal(correct_names[0, 3], names[0, 3].sort)
    assert_equal(correct_names[3, 6], names[3, 6].sort)
  end

  test 'active organizations appear before inactive organizations in search results' do
    result = Organization.search(
      current_user: @agent,
      query: 'TestOrg',
      limit: 15,
    )
    assert(result.present?, 'result should not be empty')

    names = result.map(&:name)
    correct_names = %w[TestOrg-Active-2
                       TestOrg-Active-4
                       TestOrg-Active-6
                       TestOrg-Inactive-1
                       TestOrg-Inactive-3
                       TestOrg-Inactive-5]
    assert_equal(correct_names[0, 3], names[0, 3].sort)
    assert_equal(correct_names[3, 6], names[3, 6].sort)
  end

  test 'ordering of tickets are not affected by the lack of active flags' do
    ticket_setup

    result = Ticket.search(
      current_user: User.find(1),
      query: 'ticket',
      limit: 15,
    )
    assert(result.present?, 'result should not be empty')

    names = result.map(&:title)
{'fraction_non_alphanumeric': 0.11281851779809375, 'fraction_numerical': 0.014783116125267459, 'mean_word_length': 2.302504816955684, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 12, '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': '4144182', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1574, 'n_words': 407}