spec/slack/real_time/client_spec.rb in slack-ruby-client-1.1.0 vs spec/slack/real_time/client_spec.rb in slack-ruby-client-2.0.0

- old
+ new

@@ -23,10 +23,11 @@ it 'defaults token to global default' do client = described_class.new expect(client.token).to eq '<SLACK_API_TOKEN>' expect(client.web_client.token).to eq '<SLACK_API_TOKEN>' end + context 'with real time config' do before do described_class.configure do |config| config.token = 'custom real time token' end @@ -35,10 +36,11 @@ it 'overrides token to real time config' do client = described_class.new expect(client.token).to eq 'custom real time token' expect(client.web_client.token).to eq 'custom real time token' end + it 'overrides token to specific token' do client = described_class.new(token: 'local token') expect(client.token).to eq 'local token' expect(client.web_client.token).to eq 'local token' end @@ -62,11 +64,11 @@ end end end context 'client with a full store', - vcr: { cassette_name: 'web/rtm_start', allow_playback_repeats: true } do + vcr: { cassette_name: 'web/rtm_connect', allow_playback_repeats: true } do let(:client) { described_class.new(store_class: Slack::RealTime::Stores::Store) } let(:url) { 'wss://cerberus-xxxx.lb.slack-msgs.com/websocket/uid' } describe '#start!' do let(:socket) { double(Slack::RealTime::Socket, connected?: true) } @@ -82,57 +84,50 @@ describe 'properties provided upon connection' do it 'sets url' do expect(client.url).to eq url end + it 'sets team' do expect(client.team.domain).to eq 'dblockdotorg' end + it 'sets teams' do expect(client.teams.count).to eq 1 expect(client.teams.values.first).to eq client.team end + it 'sets self' do expect(client.self.id).to eq 'U0J1GAHN1' end - it 'sets users' do - expect(client.users.count).to eq 35 + + it 'sets user' do + expect(client.users.count).to eq 1 expect(client.users.values.first['id']).to eq 'U0J1GAHN1' end - it 'sets channels' do - expect(client.channels.count).to eq 156 - expect(client.channels.values.first['name']).to eq 'general' - end - it 'sets ims' do - expect(client.ims.count).to eq 10 - expect(client.ims.values.first['user']).to eq 'U04KB5WQR' - end - it 'sets bots' do - expect(client.bots.count).to eq 83 - expect(client.bots.values.first['name']).to eq 'bot' - end - it 'sets groups' do - expect(client.groups.count).to eq 1 - end + it 'includes team name in to_s' do expect(client.to_s).to eq( "id=#{client.team.id}, name=#{client.team.name}, domain=#{client.team.domain}" ) end end it 'uses web client to fetch url' do expect(client.web_client).to be_a Slack::Web::Client end + it 'remembers socket' do expect(client.instance_variable_get(:@socket)).to eq socket end + it 'cannot be invoked twice' do expect do client.start! end.to raise_error Slack::RealTime::Client::ClientAlreadyStartedError end + describe '#stop!' do before do allow(socket).to receive(:disconnect!) client.stop! end @@ -162,22 +157,22 @@ expect(client.store.team.name).to eq 'New Team Name Inc.' end end describe '#run_handlers' do - describe 'empty events' do + context 'when store has no event hooks' do before do - @e = client.store.class.events - client.store.class.events = nil + @events = client.store.class.events.dup + client.store.class.events.clear end after do - client.store.class.events = @e + client.store.class.events.merge!(@events) end - it 'returns false when event is nil' do - expect(client.send(:run_handlers, 'example', {})).to be_nil + it 'returns empty array of handlers' do + expect(client.send(:run_handlers, 'example', {})).to be_empty end end end end @@ -197,17 +192,19 @@ it 'sends ping messages when the websocket connection is idle' do allow(socket).to receive(:time_since_last_message).and_return(30) expect(socket).to receive(:send_data).with('{"type":"ping","id":1}') client.run_ping! end + it 'reconnects the websocket if it has been idle for too long' do allow(socket).to receive(:time_since_last_message).and_return(75) allow(socket).to receive(:connected?).and_return(true) expect(socket).to receive(:close) expect(socket).to receive(:restart_async) client.run_ping! end + [ EOFError, Errno::ECONNRESET, Errno::EPIPE, Faraday::ClientError, @@ -264,52 +261,68 @@ describe 'properties provided upon connection' do it 'sets url' do expect(client.url).to eq url end + it 'sets team' do expect(client.team.domain).to eq 'dblockdotorg' end + it 'sets self' do expect(client.self.id).to eq 'U0J1GAHN1' end + + it 'no teams' do + expect(client.teams).to be_nil + end + it 'no users' do expect(client.users).to be_nil end - it 'no teams' do - expect(client.teams).to be_nil + + it 'no bots' do + expect(client.bots).to be_nil end - it 'no channels' do - expect(client.channels).to be_nil + + it 'no public channels' do + expect(client.public_channels).to be_nil end + + it 'no private channels' do + expect(client.private_channels).to be_nil + end + it 'no ims' do expect(client.ims).to be_nil end - it 'no bots' do - expect(client.bots).to be_nil + + it 'no mpims' do + expect(client.mpims).to be_nil end - it 'no groups' do - expect(client.groups).to be_nil - end + it 'includes team name in to_s' do expect(client.to_s).to eq( "id=#{client.team.id}, name=#{client.team.name}, domain=#{client.team.domain}" ) end end it 'uses web client to fetch url' do expect(client.web_client).to be_a Slack::Web::Client end + it 'remembers socket' do expect(client.instance_variable_get(:@socket)).to eq socket end + it 'cannot be invoked twice' do expect do client.start! end.to raise_error Slack::RealTime::Client::ClientAlreadyStartedError end + describe '#stop!' do before do allow(socket).to receive(:disconnect!) client.stop! end @@ -336,24 +349,28 @@ let(:url) { 'wss://mpmulti-w5tz.slack-msgs.com/websocket/uid' } it 'sets store to nil' do expect(client.store).to be_nil end + it "doesn't handle events" do event = Slack::RealTime::Event.new( 'type' => 'team_rename', 'name' => 'New Team Name Inc.' ) expect(client).not_to receive(:run_handlers) client.send(:dispatch, event) end + it 'self' do expect(client.self).to be_nil end + it 'team' do expect(client.team).to be_nil end + describe 'to_s' do it 'defaults to class instance' do expect(client.to_s).to match(/^#<Slack::RealTime::Client:0x\h+>$/) end end @@ -364,19 +381,23 @@ describe '#initialize' do it 'sets ping' do expect(client.websocket_ping).to eq 30 end + it "doesn't set proxy" do expect(client.websocket_proxy).to be_nil end + it 'defaults logger' do expect(client.send(:logger)).to be_a ::Logger end + it 'sets default store_class' do - expect(client.send(:store_class)).to eq Slack::RealTime::Store + expect(client.send(:store_class)).to eq Slack::RealTime::Stores::Starter end + (Slack::RealTime::Config::ATTRIBUTES - %i[logger store_class token]).each do |key| it "sets #{key}" do expect(client.send(key)).to eq Slack::RealTime::Config.send(key) end end @@ -385,10 +406,11 @@ describe '#run_ping?' do it 'returns true when websocket_ping is greater than 0' do client.websocket_ping = 30 expect(client.run_ping?).to be true end + it 'returns false when websocket_ping is less than 1' do client.websocket_ping = 0 expect(client.run_ping?).to be false client.websocket_ping = nil expect(client.run_ping?).to be false @@ -414,10 +436,11 @@ let(:client) { described_class.new } it 'exposes public logger' do expect(client.logger).to be_a(::Logger) end + it 'exposes public logger=' do expect { client.logger = nil }.not_to raise_error(NoMethodError) end end end @@ -439,16 +462,18 @@ describe '#initialize' do it 'sets ping' do expect(client.websocket_ping).to eq 15 end - it 'creates a connection with custom ping', vcr: { cassette_name: 'web/rtm_start' } do + + it 'creates a connection with custom ping', vcr: { cassette_name: 'web/rtm_connect' } do expect(Slack::RealTime::Concurrency::Mock::WebSocket).to( receive(:new).with(url, nil, { ping: 15 }).and_return(ws) ) client.start! end + it 'sets start_options' do expect(client.start_options).to eq(request: { timeout: 180 }) end end end @@ -468,11 +493,12 @@ expect(client.websocket_proxy).to eq( origin: 'http://username:password@proxy.example.com', headers: { 'User-Agent' => 'ruby' } ) end - it 'creates a connection with custom proxy', vcr: { cassette_name: 'web/rtm_start' } do + + it 'creates a connection with custom proxy', vcr: { cassette_name: 'web/rtm_connect' } do expect(Slack::RealTime::Concurrency::Mock::WebSocket).to receive(:new).with( url, nil, { ping: 30, @@ -488,53 +514,56 @@ end context 'start_options' do before do described_class.configure do |config| - config.start_options = { simple_latest: true } + config.start_options = { presence_sub: true } end end describe '#initialize' do it 'sets start_options' do - expect(client.start_options).to eq(simple_latest: true) + expect(client.start_options).to eq(presence_sub: true) end + context 'start!' do let(:socket) { double(Slack::RealTime::Socket, connected?: true) } before do allow(Slack::RealTime::Socket).to receive(:new).and_return(socket) allow(socket).to receive(:connect!) allow(socket).to receive(:start_sync) end - it 'calls rtm_start with start options', vcr: { cassette_name: 'web/rtm_start' } do + it 'calls rtm.connect with start options', vcr: { cassette_name: 'web/rtm_connect' } do expect(client.web_client).to( - receive(:rtm_start).with({ simple_latest: true }).and_call_original + receive(:rtm_connect).with({ presence_sub: true }).and_call_original ) client.start! end end end end context 'store_class' do - context 'starter' do + context 'when configured with Starter class' do before do described_class.configure do |config| config.store_class = Slack::RealTime::Stores::Starter end end describe '#initialize' do - it 'can be overriden explicitly' do - client = described_class.new(store_class: Slack::RealTime::Store) - expect(client.send(:store_class)).to eq Slack::RealTime::Store + it 'can override the configured store class' do + client = described_class.new(store_class: Slack::RealTime::Stores::Store) + expect(client.send(:store_class)).to eq Slack::RealTime::Stores::Store end + it 'sets store_class' do expect(client.send(:store_class)).to eq(Slack::RealTime::Stores::Starter) end + context 'start!' do let(:socket) { double(Slack::RealTime::Socket, connected?: true) } before do allow(Slack::RealTime::Socket).to receive(:new).and_return(socket) @@ -544,76 +573,9 @@ it 'instantiates the correct store class', vcr: { cassette_name: 'web/rtm_connect' } do client.start! expect(client.store).to be_a Slack::RealTime::Stores::Starter end - end - end - end - - context 'store' do - before do - described_class.configure do |config| - config.store_class = Slack::RealTime::Stores::Store - end - end - - describe '#initialize' do - context 'start!' do - let(:socket) { double(Slack::RealTime::Socket, connected?: true) } - - before do - allow(Slack::RealTime::Socket).to receive(:new).and_return(socket) - allow(socket).to receive(:connect!) - allow(socket).to receive(:start_sync) - end - - it 'calls rtm_start and not rtm_connect', vcr: { cassette_name: 'web/rtm_start' } do - expect(client.web_client).to receive(:rtm_start).and_call_original - client.start! - end - end - end - end - end - - context 'start_method' do - describe '#initialize' do - it 'can be overriden explicitly' do - client = described_class.new(start_method: :overriden) - expect(client.send(:start_method)).to eq :overriden - end - context 'with start_method' do - before do - described_class.configure do |config| - config.start_method = :overriden - end - end - - it 'sets start_method' do - expect(client.send(:start_method)).to eq :overriden - end - it 'calls the overriden method' do - expect(client.web_client).to receive(:overriden).and_raise('overriden') - expect do - client.start! - end.to raise_error RuntimeError, 'overriden' - end - end - - context 'start!' do - let(:socket) { double(Slack::RealTime::Socket, connected?: true) } - - before do - allow(Slack::RealTime::Socket).to receive(:new).and_return(socket) - allow(socket).to receive(:connect!) - allow(socket).to receive(:start_sync) - end - - it 'defaults to :rtm_start when using full store', - vcr: { cassette_name: 'web/rtm_start' } do - expect(client.web_client).to receive(:rtm_start).and_call_original - client.start! end end end end end