<commit_before># frozen_string_literal: true

require 'test_helper'

class MessageTest < ActiveSupport::TestCase
  def setup
    super

    @user, @recipient = create_list(:user, 2)

    @conversation = create(:conversation, originator: @user,
                                          recipient: @recipient)
  end

  def test_enforces_non_nil_sender_on_creation
    message = @conversation.messages.build(sender: nil, body: 'hi!')

    assert_equal false, message.valid?
  end

  def test_allows_sender_becoming_orphan
    assert_equal true, @conversation.messages.first.update(sender: nil)
  end

  def test_unread_by_scope_returns_the_number_of_unread_messages
    assert_equal 0, messages.unread_by(@user).size
    assert_equal 1, messages.unread_by(@recipient).size

    @conversation.envelope_for(sender: @user,
                               recipient: @recipient,
                               body: 'You there, buddy?')
    @conversation.save!

    assert_equal 0, messages.unread_by(@user).size
    assert_equal 2, messages.unread_by(@recipient).size
  end

  def test_involving_includes_outgoing_messages
    assert_equal messages, Message.involving(@user)
  end

  def test_involving_includes_incoming_messages
    @conversation.envelope_for(sender: @recipient, recipient: @user, body: 'hi')
    @conversation.save!

    assert_equal messages, Message.involving(@user)
  end

  def test_involving_includes_orphan_messages
    @recipient.destroy

    assert_equal messages, Message.involving(@user)
  end

  private

  def messages
    @conversation.messages
  end
end<commit_msg>Hopefully fix some flaky test failures<commit_after># frozen_string_literal: true

require 'test_helper'

class MessageTest < ActiveSupport::TestCase
  def setup
    super

    @user, @recipient = create_list(:user, 2)

    @conversation = create(:conversation, originator: @user,
                                          recipient: @recipient)
  end

  def test_enforces_non_nil_sender_on_creation
    message = @conversation.messages.build(sender: nil, body: 'hi!')

    assert_equal false, message.valid?
  end

  def test_allows_sender_becoming_orphan
    assert_equal true, @conversation.messages.first.update(sender: nil)
  end

  def test_unread_by_scope_returns_the_number_of_unread_messages
    assert_equal 0, messages.unread_by(@user).size
    assert_equal 1, messages.unread_by(@recipient).size

    @conversation.envelope_for(sender: @user,
                               recipient: @recipient,
                               body: 'You there, buddy?')
    @conversation.save!

    assert_equal 0, messages.unread_by(@user).size
    assert_equal 2, messages.unread_by(@recipient).size
  end

  def test_involving_includes_outgoing_messages
    assert_equal messages.to_set, Message.involving(@user).to_set
  end

  def test_involving_includes_incoming_messages
    @conversation.envelope_for(sender: @recipient, recipient: @user, body: 'hi')
    @conversation.save!

    assert_equal messages.to_set, Message.involving(@user).to_set
  end

  def test_involving_includes_orphan_messages
    @recipient.destroy

    assert_equal messages.to_set, Message.involving(@user).to_set
  end

  private

  def messages
    @conversation.messages
  end
end
{'fraction_non_alphanumeric': 0.08120578283605044, 'fraction_numerical': 0.0030759766225776685, 'mean_word_length': 3.5292479108635098, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '331750', 'n_tokens_mistral': 1046, 'n_tokens_neox': 974, 'n_words': 196}