require "spec_helper" module Boty RSpec.describe "happy pathy with integration", :project, :session do Boty::Logger.adapter = Boty::Logger::Memory.new def bot_with_memory_logger(bot_code) original_logger = %{Boty::Logger.adapter = Boty::Logger::Multi.new([ ::Logger.new(STDOUT), ::Logger.new("log/output.log", "daily") ])} memory_logger = %{Boty::Logger.adapter = Boty::Logger::Memory.new} bot_code.dup.tap { |code| code.sub! original_logger, memory_logger } end def start_session(only_knows: nil) executable_bot = bot_with_memory_logger File.read("bot") @session = instance_eval executable_bot, "bot", 1 return unless only_knows bot.know_how.each { |action| unless only_knows.include? action.desc.command bot.no action.desc.command end } end def message(message) faye.message(message, user: current_user_id) end let(:bot_name) { "boty" } # I know =(, hard to understand, check the spec/support/slack_support # TODO: enhance this =) let(:current_user) { user_name } let(:current_user_id) { user_id } after do Boty.locale = :en end context "creating a new project from template" do it "creates the project" do capture(:stdout) do expect(cli).to receive(:run).with "chmod +x bot" expect(cli).to receive(:run).with "bundle install" allow(cli).to receive(:ask).and_return "omg", "lol" cli.new bot_name end expect(Dir.exist? bot_name).to eq true Dir.chdir bot_name expect(File.read ".env.local").to eq %(SLACK_COMPANY=omg SLACK_BOT_API_TOKEN=lol ) end end context "on the bot directory" do before do capture(:stdout) do dry_run create_bot bot_name, company: "omg", api_key: "lol" end Dir.chdir bot_name end it "connects to the server and hear it's name" do expect(Faye::WebSocket::Client).to receive(:new).with(rtm_ws_url) start_session expect(bot).to receive(:say) .with "Ohay <@#{current_user}>! I'm here, that's for sure." message "hey boty!" end describe "knows" do before do start_session only_knows: ["knows"] end it "responds in particular with the known commands and handlers" do # check that a im arrived to the current user with knows result message = "```\nknows: List all the commands known by this bot.\n```" expect { message "<@boty>: knows" } .to send_im(current_user_id, message) end end it "overrides default locale with the project specific one" do File.open("locale/xpto.yml", "w") do |file| file.write(%(xpto: scripts: knows: "Wow! Such commands!")) end start_session only_knows: ["knows"] ::Boty.locale = :xpto message = "```\nknows: List all the commands known by this bot.\n```" expect { message "<@boty>: knows" } .to send_im(current_user_id, message) end end end end