Class: SlackSocketModeBot::TestMode

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_socket_mode_bot/test_mode.rb,
sig/slack_socket_mode_bot.rbs

Overview

The offline test double exposed as bot.test_mode (bot built with token: "test"). It drives the real dispatch without touching the network, and stubs the Web API.

Instance Method Summary collapse

Constructor Details

#initialize(bot) ⇒ TestMode

Returns a new instance of TestMode.



7
8
9
10
11
# File 'lib/slack_socket_mode_bot/test_mode.rb', line 7

def initialize(bot)
  @bot = bot
  @web_api_stubs = {}
  @seq = 0
end

Instance Method Details

#call(method, data) ⇒ Object

Called in place of the real Web API request; returns the stub's value.



27
28
29
30
31
32
# File 'lib/slack_socket_mode_bot/test_mode.rb', line 27

def call(method, data)
  stub = @web_api_stubs[method]
  return stub.call(data) if stub
  warn "unstubbed Web API call: #{ method }" if $VERBOSE
  { ok: true }
end

#resetself

Forget stubs and dedup state. Returns self.

Returns:

  • (self)


14
15
16
17
18
# File 'lib/slack_socket_mode_bot/test_mode.rb', line 14

def reset
  @web_api_stubs.clear
  @bot.instance_variable_get(:@events).clear
  self
end

#simulate_envelope(envelope) ⇒ Array[untyped]

Deliver an envelope as if it arrived over the socket; returns the ACK frames.

Parameters:

  • envelope (Object)

Returns:

  • (Array[untyped])


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/slack_socket_mode_bot/test_mode.rb', line 35

def simulate_envelope(envelope)
  envelope = JSON.parse(JSON.generate(envelope), symbolize_names: true)
  envelope[:envelope_id] ||= "test-envelope-#{ @seq += 1 }"
  produced = []
  @bot.send(
    :handle_message,
    "test",
    JSON.generate(envelope),
    send: ->(text) { produced << JSON.parse(text, symbolize_names: true) },
    close: -> {},
  )
  produced
end

#stub_web_api(method) {|arg0| ... } ⇒ self

Stub the Web API response for a method; the block receives the call data.

Parameters:

  • method (String)

Yields:

Yield Parameters:

  • arg0 (Object)

Yield Returns:

  • (Object)

Returns:

  • (self)


21
22
23
24
# File 'lib/slack_socket_mode_bot/test_mode.rb', line 21

def stub_web_api(method, &block)
  @web_api_stubs[method] = block
  self
end