<commit_before># frozen_string_literal: true

require "rails_helper"

describe Commands::DirectCommand, type: :service do
  let(:character)   { create(:character) }
  let(:instance)    { described_class.new("/direct #{target_name} #{message}", character: character) }
  let(:message)     { "Hello." }
  let(:target)      { create(:character, room: character.room) }
  let(:target_name) { target.name.upcase }

  describe "#call" do
    subject(:call) { instance.call }

    before do
      allow(Turbo::StreamsChannel).to receive(:broadcast_append_later_to)
    end

    context "with a valid target" do
      it "broadcasts direct partial to the room" do
        call

        expect(Turbo::StreamsChannel).to have_received(:broadcast_append_later_to)
          .with(
            character.room,
            target:  "messages",
            partial: "commands/direct",
            locals:  {
              character:   character,
              message:     message,
              target:      target,
              target_name: target_name
            }
          )
      end
    end

    context "with character as target" do
      let(:target) { character }

      it "does not broadcast" do
        call

        expect(Turbo::StreamsChannel).not_to have_received(:broadcast_append_later_to)
      end
    end

    context "with inactive character as target" do
      let(:target) { create(:character, :inactive, room: character.room) }

      it "does not broadcast" do
        call

        expect(Turbo::StreamsChannel).not_to have_received(:broadcast_append_later_to)
      end
    end

    context "with invalid target" do
      let(:target_name) { "Nobody" }

      it "does not broadcast" do
        call<commit_msg>Add test for direct message to character in different room.<commit_after># frozen_string_literal: true

require "rails_helper"

describe Commands::DirectCommand, type: :service do
  let(:character)   { create(:character) }
  let(:instance)    { described_class.new("/direct #{target_name} #{message}", character: character) }
  let(:message)     { "Hello." }
  let(:target)      { create(:character, room: character.room) }
  let(:target_name) { target.name.upcase }

  describe "#call" do
    subject(:call) { instance.call }

    before do
      allow(Turbo::StreamsChannel).to receive(:broadcast_append_later_to)
    end

    context "with a valid target" do
      it "broadcasts direct partial to the room" do
        call

        expect(Turbo::StreamsChannel).to have_received(:broadcast_append_later_to)
          .with(
            character.room,
            target:  "messages",
            partial: "commands/direct",
            locals:  {
              character:   character,
              message:     message,
              target:      target,
              target_name: target_name
            }
          )
      end
    end

    context "with target in a different room" do
      let(:target) { create(:character) }

      it "does not broadcast" do
        call

        expect(Turbo::StreamsChannel).not_to have_received(:broadcast_append_later_to)
      end
    end

    context "with character as target" do
      let(:target) { character }

      it "does not broadcast" do
        call

        expect(Turbo::StreamsChannel).not_to have_received(:broadcast_append_later_to)
      end
    end

    context "with inactive character as target" do
      let(:target) { create(:character, :inactive, room: character.room) }

      it "does not broadcast" do
        call

        expect(Turbo::StreamsChannel).not_to have_received(:broadcast_append_later_to)
      end
    end

    context "with invalid target" do
      let(:target_name) { "Nobody" }

      it "does not broadcast" do
        call
{'fraction_non_alphanumeric': 0.09687750200160128, 'fraction_numerical': 0.0, 'mean_word_length': 2.416590701914312, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7311620', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1034, 'n_words': 274}