<commit_before>require "spec_helper"

describe Lita::Adapters::Slack::MessageHandler, lita: true do
  subject { described_class.new(robot, robot_id, data) }

  let(:robot) { instance_double('Lita::Robot', name: 'Lita', mention_name: 'lita') }
  let(:robot_id) { 'U12345678' }

  describe "#handle" do
    context "with a hello message" do
      let(:data) { { "type" => "hello" } }

      it "triggers a connected event" do
        expect(robot).to receive(:trigger).with(:connected)

        subject.handle
      end
    end

    context "with a normal message" do
      let(:data) do
        {
          "type" => "message",
          "channel" => "C2147483705",
          "user" => "U023BECGF",
          "text" => "Hello"
        }
      end
      let(:message) { instance_double('Lita::Message', command!: false) }
      let(:source) { instance_double('Lita::Source', private_message?: false) }
      let(:user) { instance_double('Lita::User', id: 'U023BECGF') }

      before do
        allow(Lita::User).to receive(:find_by_id).and_return(user)
        allow(Lita::Source).to receive(:new).with(
          user: user,
          room: "C2147483705"
        ).and_return(source)
        allow(Lita::Message).to receive(:new).with(robot, "Hello", source).and_return(message)
        allow(robot).to receive(:receive).with(message)
      end

      it "dispatches the message to Lita" do
        expect(robot).to receive(:receive).with(message)

        subject.handle
      end

      context "when the message is a direct message" do
        let(:data) do
          {
            "type" => "message",
            "channel" => "D2147483705",
            "user" => "U023BECGF",
            "text" => "Hello"
          }
        end

        before do
          allow(Lita::Source).to receive(:new).with(
            user: user,
            room: "D2147483705"
          ).and_return(source)
          allow(source).to receive(:private_message!).and_return(true)
          allow(source).to receive(:private_message?).and_return(true)
        end

        it "marks the source as a private message" do
          expect(source).to receive(:private_message!)

          subject.handle
        end

        it "marks the message as a command" do
          expect(message).to receive(:command!)

          subject.handle
        end
      end

      context "when the message starts with a Slack-style @-mention" do
        let(:data) do
          {
            "type" => "message",
            "channel" => "C2147483705",
            "user" => "U023BECGF",
            "text" => "<@#{robot_id}>: Hello"
          }
        end

        it "converts it to a Lita-style @-mention" do
          expect(Lita::Message).to receive(:new).with(
            robot,
            "@lita: Hello",
            source
          ).and_return(message)

          subject.handle
        end
      end

      context "when the message has attach" do
        let(:data) do
          {
            "type" => "message",
            "channel" => "C2147483705",
            "user" => "U023BECGF",
            "text" => "Hello",
            "attachments" => [{"text" => "attached hello"}]
          }
        end

        it "recives attachment text" do
          expect(Lita::Message).to receive(:new).with(
            robot,
            "Hello\nattached hello",
            source
          ).and_return(message)

          subject.handle
        end
      end

      context "when the message is nil" do
        let(:data) do
          {
            "type" => "message",
            "channel" => "C2147483705",
            "user" => "U023BECGF",
          }
        end

        it "dispatches an empty message to Lita" do
          expect(Lita::Message).to receive(:new).with(
            robot,
            "",
            source
          ).and_return(message)

          subject.handle
        end
      end

      describe "Removing message formatting" do
        context "does nothing if there are no user links" do
          let(:data) do
            {
                "type"    => "message",
                "channel" => "C2147483705",
                "text"    => "foo",
            }
          end

          it "removes formatting" do
            expect(Lita::Message).to receive(:new).with(
                                         robot,
                                         "foo",
                                         source
                                     ).and_return(message)

            subject.handle
          end

        end
        context "decodes entities" do
          let(:data) do
            {
                "type"    => "message",
                "channel" => "C2147483705",
                "text"    => "foo &gt; &amp; &lt; &gt;&amp;&lt;",
            }
          end

          it "removes formatting" do
            expect(Lita::Message).to receive(:new).with(
                                         robot,
                                         "foo > & < >&<",
                                         source
                                     ).and_return(message)

            subject.handle
          end

        end
      end
    end

    context "with a message with an unsupported subtype" do
      let(:data) do
        {
          "type" => "message",
          "subtype" => "???"
        }<commit_msg>Tests for @names<commit_after>require "spec_helper"

describe Lita::Adapters::Slack::MessageHandler, lita: true do
  subject { described_class.new(robot, robot_id, data) }

  let(:robot) { instance_double('Lita::Robot', name: 'Lita', mention_name: 'lita') }
  let(:robot_id) { 'U12345678' }

  describe "#handle" do
    context "with a hello message" do
      let(:data) { { "type" => "hello" } }

      it "triggers a connected event" do
        expect(robot).to receive(:trigger).with(:connected)

        subject.handle
      end
    end

    context "with a normal message" do
      let(:data) do
        {
          "type" => "message",
          "channel" => "C2147483705",
          "user" => "U023BECGF",
          "text" => "Hello"
        }
      end
      let(:message) { instance_double('Lita::Message', command!: false) }
      let(:source) { instance_double('Lita::Source', private_message?: false) }
      let(:user) { instance_double('Lita::User', id: 'U023BECGF') }

      before do
        allow(Lita::User).to receive(:find_by_id).and_return(user)
        allow(Lita::Source).to receive(:new).with(
          user: user,
          room: "C2147483705"
        ).and_return(source)
        allow(Lita::Message).to receive(:new).with(robot, "Hello", source).and_return(message)
        allow(robot).to receive(:receive).with(message)
      end

      it "dispatches the message to Lita" do
        expect(robot).to receive(:receive).with(message)

        subject.handle
      end

      context "when the message is a direct message" do
        let(:data) do
          {
            "type" => "message",
            "channel" => "D2147483705",
            "user" => "U023BECGF",
            "text" => "Hello"
          }
        end

        before do
          allow(Lita::Source).to receive(:new).with(
            user: user,
            room: "D2147483705"
          ).and_return(source)
          allow(source).to receive(:private_message!).and_return(true)
          allow(source).to receive(:private_message?).and_return(true)
        end

        it "marks the source as a private message" do
          expect(source).to receive(:private_message!)

          subject.handle
        end

        it "marks the message as a command" do
          expect(message).to receive(:command!)

          subject.handle
        end
      end

      context "when the message starts with a Slack-style @-mention" do
        let(:data) do
          {
            "type" => "message",
            "channel" => "C2147483705",
            "user" => "U023BECGF",
            "text" => "<@#{robot_id}>: Hello"
          }
        end

        it "converts it to a Lita-style @-mention" do
          expect(Lita::Message).to receive(:new).with(
            robot,
            "@lita: Hello",
            source
          ).and_return(message)

          subject.handle
        end
      end

      context "when the message has attach" do
        let(:data) do
          {
            "type" => "message",
            "channel" => "C2147483705",
            "user" => "U023BECGF",
            "text" => "Hello",
            "attachments" => [{"text" => "attached hello"}]
          }
        end

        it "recives attachment text" do
          expect(Lita::Message).to receive(:new).with(
            robot,
            "Hello\nattached hello",
            source
          ).and_return(message)

          subject.handle
        end
      end

      context "when the message is nil" do
        let(:data) do
          {
            "type" => "message",
            "channel" => "C2147483705",
            "user" => "U023BECGF",
          }
        end

        it "dispatches an empty message to Lita" do
          expect(Lita::Message).to receive(:new).with(
            robot,
            "",
            source
          ).and_return(message)

          subject.handle
        end
      end

      describe "Removing message formatting" do

        let(:user) { instance_double('Lita::User', id: 'U123',name: 'name', mention_name: 'label') }

        context "does nothing if there are no user links" do
          let(:data) do
            {
                "type"    => "message",
                "channel" => "C2147483705",
                "text"    => "foo",
            }
          end

          it "removes formatting" do
            expect(Lita::Message).to receive(:new).with(
                                         robot,
                                         "foo",
                                         source
                                     ).and_return(message)

            subject.handle
          end

        end
        context "decodes entities" do
          let(:data) do
            {
                "type"    => "message",
                "channel" => "C2147483705",
                "text"    => "foo &gt; &amp; &lt; &gt;&amp;&lt;",
            }
          end

          it "removes formatting" do
            expect(Lita::Message).to receive(:new).with(
                                         robot,
                                         "foo > & < >&<",
                                         source
                                     ).and_return(message)

            subject.handle
          end

        end

        context "changes <@123> links to @name" do
          let(:data) do
            {
                "type"    => "message",
                "channel" => "C2147483705",
                "text"    => "foo <@123> bar",
            }
          end
          it "removes formatting" do
            expect(Lita::Message).to receive(:new).with(
                                         robot,
                                         "foo @name bar",
                                         source
                                     ).and_return(message)

            subject.handle
          end
        end

        context "changes <@U123|label> links to label" do
          let(:data) do
            {
                "type"    => "message",
                "channel" => "C2147483705",
                "text"    => "foo <@123|label> bar",
            }
          end
          it "removes formatting" do
            expect(Lita::Message).to receive(:new).with(
                                         robot,
                                         "foo label bar",
                                         source
                                     ).and_return(message)

            subject.handle
          end
        end
      end
    end

    context "with a message with an unsupported subtype" do
      let(:data) do
        {
          "type" => "message",
          "subtype" => "???"
        }
{'fraction_non_alphanumeric': 0.11904563970404855, 'fraction_numerical': 0.02219635879956771, 'mean_word_length': 1.3161340007701194, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3100350', 'n_tokens_mistral': 3480, 'n_tokens_neox': 3222, 'n_words': 779}