rita 0.1.0 → 5.0.0.alpha.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +19 -0
- data/.rubocop.yml +51 -0
- data/.ruby-version +1 -0
- data/.travis.yml +16 -0
- data/CONTRIBUTING.md +18 -0
- data/Gemfile +5 -0
- data/{LICENSE.txt → LICENSE} +1 -3
- data/README.md +17 -24
- data/Rakefile +5 -5
- data/bin/lita +7 -0
- data/lib/lita/adapter.rb +147 -0
- data/lib/lita/adapters/shell.rb +126 -0
- data/lib/lita/adapters/test.rb +62 -0
- data/lib/lita/authorization.rb +112 -0
- data/lib/lita/callback.rb +39 -0
- data/lib/lita/cli.rb +218 -0
- data/lib/lita/configurable.rb +47 -0
- data/lib/lita/configuration_builder.rb +247 -0
- data/lib/lita/configuration_validator.rb +95 -0
- data/lib/lita/default_configuration.rb +122 -0
- data/lib/lita/errors.rb +25 -0
- data/lib/lita/handler/chat_router.rb +141 -0
- data/lib/lita/handler/common.rb +208 -0
- data/lib/lita/handler/event_router.rb +84 -0
- data/lib/lita/handler/http_router.rb +31 -0
- data/lib/lita/handler.rb +15 -0
- data/lib/lita/handlers/authorization.rb +129 -0
- data/lib/lita/handlers/help.rb +171 -0
- data/lib/lita/handlers/info.rb +66 -0
- data/lib/lita/handlers/room.rb +36 -0
- data/lib/lita/handlers/users.rb +37 -0
- data/lib/lita/http_callback.rb +46 -0
- data/lib/lita/http_route.rb +83 -0
- data/lib/lita/logger.rb +43 -0
- data/lib/lita/message.rb +124 -0
- data/lib/lita/middleware_registry.rb +39 -0
- data/lib/lita/namespace.rb +29 -0
- data/lib/lita/plugin_builder.rb +43 -0
- data/lib/lita/rack_app.rb +100 -0
- data/lib/lita/registry.rb +164 -0
- data/lib/lita/response.rb +65 -0
- data/lib/lita/robot.rb +273 -0
- data/lib/lita/room.rb +119 -0
- data/lib/lita/route_validator.rb +82 -0
- data/lib/lita/rspec/handler.rb +127 -0
- data/lib/lita/rspec/matchers/chat_route_matcher.rb +53 -0
- data/lib/lita/rspec/matchers/event_route_matcher.rb +29 -0
- data/lib/lita/rspec/matchers/http_route_matcher.rb +34 -0
- data/lib/lita/rspec.rb +48 -0
- data/lib/lita/source.rb +81 -0
- data/lib/lita/store.rb +23 -0
- data/lib/lita/target.rb +3 -0
- data/lib/lita/template.rb +71 -0
- data/lib/lita/template_resolver.rb +52 -0
- data/lib/lita/timer.rb +49 -0
- data/lib/lita/user.rb +157 -0
- data/lib/lita/util.rb +31 -0
- data/lib/lita/version.rb +6 -0
- data/lib/lita.rb +166 -0
- data/rita.gemspec +50 -0
- data/spec/lita/adapter_spec.rb +54 -0
- data/spec/lita/adapters/shell_spec.rb +99 -0
- data/spec/lita/authorization_spec.rb +122 -0
- data/spec/lita/configuration_builder_spec.rb +247 -0
- data/spec/lita/configuration_validator_spec.rb +114 -0
- data/spec/lita/default_configuration_spec.rb +242 -0
- data/spec/lita/handler/chat_router_spec.rb +236 -0
- data/spec/lita/handler/common_spec.rb +289 -0
- data/spec/lita/handler/event_router_spec.rb +121 -0
- data/spec/lita/handler/http_router_spec.rb +155 -0
- data/spec/lita/handler_spec.rb +62 -0
- data/spec/lita/handlers/authorization_spec.rb +111 -0
- data/spec/lita/handlers/help_spec.rb +124 -0
- data/spec/lita/handlers/info_spec.rb +67 -0
- data/spec/lita/handlers/room_spec.rb +24 -0
- data/spec/lita/handlers/users_spec.rb +35 -0
- data/spec/lita/logger_spec.rb +28 -0
- data/spec/lita/message_spec.rb +178 -0
- data/spec/lita/plugin_builder_spec.rb +41 -0
- data/spec/lita/response_spec.rb +62 -0
- data/spec/lita/robot_spec.rb +285 -0
- data/spec/lita/room_spec.rb +136 -0
- data/spec/lita/rspec/handler_spec.rb +33 -0
- data/spec/lita/rspec_spec.rb +162 -0
- data/spec/lita/source_spec.rb +68 -0
- data/spec/lita/store_spec.rb +23 -0
- data/spec/lita/template_resolver_spec.rb +42 -0
- data/spec/lita/template_spec.rb +52 -0
- data/spec/lita/timer_spec.rb +32 -0
- data/spec/lita/user_spec.rb +167 -0
- data/spec/lita/util_spec.rb +18 -0
- data/spec/lita_spec.rb +227 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/templates/basic.erb +1 -0
- data/spec/templates/basic.irc.erb +1 -0
- data/spec/templates/helpers.erb +1 -0
- data/spec/templates/interpolated.erb +1 -0
- data/templates/locales/en.yml +137 -0
- data/templates/plugin/Gemfile +5 -0
- data/templates/plugin/README.tt +29 -0
- data/templates/plugin/Rakefile +8 -0
- data/templates/plugin/gemspec.tt +27 -0
- data/templates/plugin/gitignore +18 -0
- data/templates/plugin/lib/lita/plugin_type/plugin.tt +19 -0
- data/templates/plugin/lib/plugin.tt +16 -0
- data/templates/plugin/locales/en.yml.tt +4 -0
- data/templates/plugin/spec/lita/plugin_type/plugin_spec.tt +6 -0
- data/templates/plugin/spec/spec_helper.tt +8 -0
- data/templates/plugin/templates/gitkeep +0 -0
- data/templates/robot/Gemfile +5 -0
- data/templates/robot/lita_config.rb +28 -0
- metadata +386 -21
- data/.standard.yml +0 -3
- data/CHANGELOG.md +0 -5
- data/CODE_OF_CONDUCT.md +0 -132
- data/lib/rita/version.rb +0 -5
- data/lib/rita.rb +0 -8
- data/sig/rita.rbs +0 -4
@@ -0,0 +1,236 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
handler = Class.new do
|
6
|
+
extend Lita::Handler::ChatRouter
|
7
|
+
|
8
|
+
def self.name
|
9
|
+
"Test"
|
10
|
+
end
|
11
|
+
|
12
|
+
route(/message/, :message)
|
13
|
+
route(/command/, :command, command: true)
|
14
|
+
route(/admin/, :admin, restrict_to: :admins)
|
15
|
+
route(/error/, :error)
|
16
|
+
route(/validate route hook/, :validate_route_hook, code_word: true)
|
17
|
+
route(/trigger route hook/, :trigger_route_hook, custom_data: "trigger route hook")
|
18
|
+
|
19
|
+
def message(response)
|
20
|
+
response.reply("message")
|
21
|
+
end
|
22
|
+
|
23
|
+
def command(response)
|
24
|
+
response.reply("command")
|
25
|
+
end
|
26
|
+
|
27
|
+
def admin(response)
|
28
|
+
response.reply("admin")
|
29
|
+
end
|
30
|
+
|
31
|
+
def error(_response)
|
32
|
+
raise
|
33
|
+
end
|
34
|
+
|
35
|
+
def validate_route_hook(response)
|
36
|
+
response.reply("validate route hook")
|
37
|
+
end
|
38
|
+
|
39
|
+
def trigger_route_hook(response)
|
40
|
+
response.reply(response.extensions[:custom_data])
|
41
|
+
end
|
42
|
+
|
43
|
+
route(/block/) do |response|
|
44
|
+
response.reply("block")
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe handler, lita_handler: true do
|
49
|
+
describe ".dispatch" do
|
50
|
+
it "routes a matching message to the supplied method" do
|
51
|
+
send_message("message")
|
52
|
+
expect(replies.last).to eq("message")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "routes a matching message even if addressed to the robot" do
|
56
|
+
send_command("message")
|
57
|
+
expect(replies.last).to eq("message")
|
58
|
+
end
|
59
|
+
|
60
|
+
it "routes a command message to the supplied method" do
|
61
|
+
send_command("command")
|
62
|
+
expect(replies.last).to eq("command")
|
63
|
+
end
|
64
|
+
|
65
|
+
it "requires command routes to be addressed to the robot" do
|
66
|
+
send_message("command")
|
67
|
+
expect(replies).to be_empty
|
68
|
+
end
|
69
|
+
|
70
|
+
it "doesn't route messages that don't match anything" do
|
71
|
+
send_message("nothing")
|
72
|
+
expect(replies).to be_empty
|
73
|
+
end
|
74
|
+
|
75
|
+
it "dispatches to restricted routes if the user is in the auth group" do
|
76
|
+
allow(robot.auth).to receive(:user_is_admin?).with(user).and_return(true)
|
77
|
+
send_message("admin")
|
78
|
+
expect(replies.last).to eq("admin")
|
79
|
+
end
|
80
|
+
|
81
|
+
it "doesn't route unauthorized users' messages to restricted routes" do
|
82
|
+
send_message("admin")
|
83
|
+
expect(replies).to be_empty
|
84
|
+
end
|
85
|
+
|
86
|
+
it "triggers a :route_authorization_failed event when a user is not authorized" do
|
87
|
+
allow(robot).to receive(:trigger)
|
88
|
+
expect(robot).to receive(:trigger).with(:route_authorization_failed, anything)
|
89
|
+
send_message("admin")
|
90
|
+
end
|
91
|
+
|
92
|
+
it "ignores messages from itself" do
|
93
|
+
allow(user).to receive(:name).and_return(robot.name)
|
94
|
+
send_message("message")
|
95
|
+
expect(replies).to be_empty
|
96
|
+
end
|
97
|
+
|
98
|
+
it "allows route callbacks to be provided as blocks" do
|
99
|
+
send_message("block")
|
100
|
+
expect(replies.last).to eq("block")
|
101
|
+
end
|
102
|
+
|
103
|
+
it "logs exceptions without crashing" do
|
104
|
+
test_mode = Lita.test_mode?
|
105
|
+
|
106
|
+
begin
|
107
|
+
Lita.test_mode = false
|
108
|
+
expect(robot.logger).to receive(:error).with(/Test crashed/)
|
109
|
+
send_message("error")
|
110
|
+
ensure
|
111
|
+
Lita.test_mode = test_mode
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it "triggers a message_dispatched event" do
|
116
|
+
expect(robot).to receive(:trigger).with(:message_received, anything)
|
117
|
+
expect(robot).to receive(:trigger).with(:message_dispatched, anything)
|
118
|
+
send_message("message")
|
119
|
+
end
|
120
|
+
|
121
|
+
it "raises exceptions in test mode" do
|
122
|
+
expect { send_message("error") }.to raise_error(RuntimeError)
|
123
|
+
end
|
124
|
+
|
125
|
+
context "with a custom validate_route hook" do
|
126
|
+
let(:hook) do
|
127
|
+
proc do |payload|
|
128
|
+
if payload[:route].extensions[:code_word]
|
129
|
+
payload[:message].body.include?("code word")
|
130
|
+
else
|
131
|
+
true
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
before { registry.register_hook(:validate_route, hook) }
|
137
|
+
|
138
|
+
it "matches if the hook returns true" do
|
139
|
+
send_message("validate route hook - code word")
|
140
|
+
expect(replies.last).to eq("validate route hook")
|
141
|
+
end
|
142
|
+
|
143
|
+
it "does not match if the hook returns false" do
|
144
|
+
send_message("validate route hook")
|
145
|
+
expect(replies).to be_empty
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context "with a custom trigger_route hook" do
|
150
|
+
let(:hook) do
|
151
|
+
proc do |payload|
|
152
|
+
payload[:response].extensions[:custom_data] = payload[:route].extensions[:custom_data]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
before { registry.register_hook(:trigger_route, hook) }
|
157
|
+
|
158
|
+
it "adds data to the response's extensions" do
|
159
|
+
send_message("trigger route hook")
|
160
|
+
expect(replies.last).to eq("trigger route hook")
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context "with a custom post_route hook" do
|
165
|
+
let(:hook) do
|
166
|
+
proc do |payload|
|
167
|
+
Lita.logger.info(
|
168
|
+
"#{payload[:response].message.body} triggered /#{payload[:route].pattern.source}/"
|
169
|
+
)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
before { registry.register_hook(:post_route, hook) }
|
174
|
+
|
175
|
+
it "executes after the message has been routed" do
|
176
|
+
expect(Lita.logger).to receive(:info).with("message triggered /message/")
|
177
|
+
|
178
|
+
send_message("message")
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
handler = Class.new do
|
185
|
+
extend Lita::Handler::ChatRouter
|
186
|
+
extend Lita::Handler::EventRouter
|
187
|
+
|
188
|
+
def self.name
|
189
|
+
"Test"
|
190
|
+
end
|
191
|
+
|
192
|
+
route(/boom/) { |_response| 2 + nil }
|
193
|
+
|
194
|
+
route(/one/) { |response| response.reply "got one" }
|
195
|
+
route(/two/) { |response| response.reply "got two" }
|
196
|
+
|
197
|
+
on :unhandled_message do |payload|
|
198
|
+
message = payload[:message]
|
199
|
+
robot.send_message(message.source, message.body)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
describe handler, lita_handler: true do
|
204
|
+
it "triggers the unhandled message event if no route matches" do
|
205
|
+
send_message("this won't match any routes")
|
206
|
+
expect(replies.last).to eq("this won't match any routes")
|
207
|
+
end
|
208
|
+
|
209
|
+
it "doesn't stop checking routes when the first one matches" do
|
210
|
+
send_message("one two")
|
211
|
+
expect(replies.last).to eq("got two")
|
212
|
+
end
|
213
|
+
|
214
|
+
context "with another handler registered" do
|
215
|
+
before do
|
216
|
+
registry.register_handler(:test_2) do
|
217
|
+
route(/three/) { |response| response.reply "got three" }
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
it "doesn't stop dispatching to handlers when there is a matching route in one" do
|
222
|
+
send_message("two three")
|
223
|
+
expect(replies.last).to eq("got three")
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
context "when the handler raises an exception" do
|
228
|
+
it "calls the error handler with the exception as argument" do
|
229
|
+
expect(registry.config.robot.error_handler).to receive(:call) do |error, *_args|
|
230
|
+
expect(error).to be_an_instance_of(TypeError)
|
231
|
+
end
|
232
|
+
|
233
|
+
expect { send_message("boom!") }.to raise_error(TypeError)
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
@@ -0,0 +1,289 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Lita::Handler::Common, lita: true do
|
6
|
+
let(:robot) { Lita::Robot.new(registry) }
|
7
|
+
|
8
|
+
let(:handler) do
|
9
|
+
Class.new do
|
10
|
+
include Lita::Handler::Common
|
11
|
+
|
12
|
+
namespace "foo"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
subject { handler.new(robot) }
|
17
|
+
|
18
|
+
describe ".config" do
|
19
|
+
it "sets configuration attributes" do
|
20
|
+
handler.config :foo
|
21
|
+
|
22
|
+
config = handler.configuration_builder.build
|
23
|
+
|
24
|
+
expect(config.foo).to be_nil
|
25
|
+
config.foo = :bar
|
26
|
+
expect(config.foo).to eq(:bar)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe ".configuration_builder" do
|
31
|
+
it "returns a ConfigurationBuilder object" do
|
32
|
+
expect(handler.configuration_builder).to be_a(Lita::ConfigurationBuilder)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "is memoized" do
|
36
|
+
expect(handler.configuration_builder).to equal(handler.configuration_builder)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe ".namespace" do
|
41
|
+
it "returns a snake cased namesapce for the handler based on class name" do
|
42
|
+
handler = Class.new do
|
43
|
+
include Lita::Handler::Common
|
44
|
+
|
45
|
+
def self.name
|
46
|
+
"Lita::Handlers::FooBarBaz"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
expect(handler.namespace).to eq("foo_bar_baz")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "allows the namespace to be set with a simple string" do
|
54
|
+
handler = Class.new do
|
55
|
+
include Lita::Handler::Common
|
56
|
+
|
57
|
+
namespace "common"
|
58
|
+
end
|
59
|
+
|
60
|
+
expect(handler.namespace).to eq("common")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "allows the namespace to be set with the full path to an object as a string" do
|
64
|
+
handler = Class.new do
|
65
|
+
include Lita::Handler::Common
|
66
|
+
|
67
|
+
namespace "Lita::Handler::Common"
|
68
|
+
end
|
69
|
+
|
70
|
+
expect(handler.namespace).to eq("common")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "allows the namespace to be set with an object" do
|
74
|
+
handler = Class.new do
|
75
|
+
include Lita::Handler::Common
|
76
|
+
|
77
|
+
namespace Lita::Handler::Common
|
78
|
+
end
|
79
|
+
|
80
|
+
expect(handler.namespace).to eq("common")
|
81
|
+
end
|
82
|
+
|
83
|
+
it "raises an exception if the handler doesn't have a name to derive the namespace from" do
|
84
|
+
handler = Class.new { include Lita::Handler::Common }
|
85
|
+
expect { handler.namespace }.to raise_error(RuntimeError, /must set a namespace/)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "#config" do
|
90
|
+
before { registry.register_handler(handler) }
|
91
|
+
|
92
|
+
let(:handler) do
|
93
|
+
Class.new do
|
94
|
+
include Lita::Handler::Common
|
95
|
+
|
96
|
+
namespace "foo_bar_baz"
|
97
|
+
|
98
|
+
config :style, default: :new
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
it "returns the handler's config settings" do
|
103
|
+
expect(subject.config.style).to eq(:new)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "#http" do
|
108
|
+
it "returns a Faraday connection" do
|
109
|
+
expect(subject.http).to be_a(Faraday::Connection)
|
110
|
+
end
|
111
|
+
|
112
|
+
it "sets a default user agent" do
|
113
|
+
expect(subject.http.headers["User-Agent"]).to eq("Lita v#{Lita::VERSION}")
|
114
|
+
end
|
115
|
+
|
116
|
+
it "merges in user-supplied options" do
|
117
|
+
connection = subject.http(headers: {
|
118
|
+
"User-Agent" => "Foo", "X-Bar" => "Baz"
|
119
|
+
})
|
120
|
+
expect(connection.headers["User-Agent"]).to eq("Foo")
|
121
|
+
expect(connection.headers["X-Bar"]).to eq("Baz")
|
122
|
+
end
|
123
|
+
|
124
|
+
it "passes blocks on to Faraday" do
|
125
|
+
connection = subject.http { |builder| builder.response :logger }
|
126
|
+
expect(connection.builder.handlers).to include(Faraday::Response::Logger)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "#log" do
|
131
|
+
it "returns the robot's logger" do
|
132
|
+
expect(subject.log).to eq(robot.logger)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe "#render_template" do
|
137
|
+
context "with the template root set" do
|
138
|
+
before do
|
139
|
+
handler.template_root(File.expand_path(File.join("..", "..", "..", "templates"), __FILE__))
|
140
|
+
end
|
141
|
+
|
142
|
+
it "renders the given template to a string" do
|
143
|
+
expect(subject.render_template("basic")).to eq("Template rendered from a file!")
|
144
|
+
end
|
145
|
+
|
146
|
+
it "interpolates variables into the rendered template" do
|
147
|
+
result = subject.render_template("interpolated", first: "Carl", last: "Pug")
|
148
|
+
|
149
|
+
expect(result).to eq("I love Carl Pug!")
|
150
|
+
end
|
151
|
+
|
152
|
+
it "renders adapter-specific templates if available" do
|
153
|
+
robot.config.robot.adapter = :irc
|
154
|
+
expect(subject.render_template("basic")).to eq("IRC template rendered from a file!")
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
it "raises an exception if the template root hasn't been set" do
|
159
|
+
expect { subject.render_template("basic") }.to raise_error(Lita::MissingTemplateRootError)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe "#render_template_with_helpers" do
|
164
|
+
before do
|
165
|
+
handler.template_root(File.expand_path(File.join("..", "..", "..", "templates"), __FILE__))
|
166
|
+
end
|
167
|
+
|
168
|
+
it "extends custom helpers into the template evaluation context" do
|
169
|
+
helpers = Module.new do
|
170
|
+
def reverse_name(first, last)
|
171
|
+
"#{last}, #{first}"
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
result = subject.render_template_with_helpers(
|
176
|
+
"helpers",
|
177
|
+
[helpers],
|
178
|
+
first: "Carl",
|
179
|
+
last: "Pug",
|
180
|
+
)
|
181
|
+
|
182
|
+
expect(result).to eq("Pug, Carl")
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
describe "#store" do
|
187
|
+
it "shares data between instances" do
|
188
|
+
subject.store[:foo] = :bar
|
189
|
+
|
190
|
+
second_instance = handler.new(robot)
|
191
|
+
|
192
|
+
expect(second_instance.store[:foo]).to eq(:bar)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
describe "timer methods" do
|
197
|
+
let(:queue) { Queue.new }
|
198
|
+
|
199
|
+
subject { handler.new(robot) }
|
200
|
+
|
201
|
+
before do
|
202
|
+
# Don't wait for sleeps while testing.
|
203
|
+
allow_any_instance_of(Lita::Timer).to receive(:sleep)
|
204
|
+
|
205
|
+
# Since we're not using the `:lita_handler` RSpec metadata in this test, we need to do this
|
206
|
+
# manually or we will reach an `exit(false)` when `robot.run_concurrently` delegates to the
|
207
|
+
# adapter and there isn't one!
|
208
|
+
registry.register_adapter(:test, Lita::Adapters::Test)
|
209
|
+
registry.config.robot.adapter = :test
|
210
|
+
registry.config.adapters.test.blocking = false
|
211
|
+
end
|
212
|
+
|
213
|
+
describe "#after" do
|
214
|
+
let(:handler) do
|
215
|
+
Class.new do
|
216
|
+
include Lita::Handler::Common
|
217
|
+
|
218
|
+
namespace "foo"
|
219
|
+
|
220
|
+
def after_test(queue)
|
221
|
+
after(2) { queue.push("Waited 2 seconds!") }
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
it "triggers the block after the given number of seconds" do
|
227
|
+
subject.after_test(queue)
|
228
|
+
expect(queue.pop).to eq("Waited 2 seconds!")
|
229
|
+
expect { queue.pop(true) }.to raise_error(ThreadError)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
describe "#every" do
|
234
|
+
let(:handler) do
|
235
|
+
Class.new do
|
236
|
+
include Lita::Handler::Common
|
237
|
+
|
238
|
+
namespace "foo"
|
239
|
+
|
240
|
+
def every_test(queue)
|
241
|
+
array = [1, 2, 3]
|
242
|
+
|
243
|
+
every(2) do |timer|
|
244
|
+
value = array.shift
|
245
|
+
|
246
|
+
if value
|
247
|
+
queue.push(value)
|
248
|
+
else
|
249
|
+
timer.stop
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
it "triggers the block until the timer is stopped" do
|
257
|
+
subject.every_test(queue)
|
258
|
+
expect(queue.pop).to eq(1)
|
259
|
+
expect(queue.pop).to eq(2)
|
260
|
+
expect(queue.pop).to eq(3)
|
261
|
+
expect { queue.pop(true) }.to raise_error(ThreadError)
|
262
|
+
end
|
263
|
+
end
|
264
|
+
|
265
|
+
context "with an infinite timer" do
|
266
|
+
let(:response) { instance_double("Lita::Response") }
|
267
|
+
|
268
|
+
let(:handler) do
|
269
|
+
Class.new do
|
270
|
+
include Lita::Handler::Common
|
271
|
+
|
272
|
+
namespace "foo"
|
273
|
+
|
274
|
+
def infinite_every_test(response)
|
275
|
+
thread = every(5) { "Looping forever!" }
|
276
|
+
response.reply("Replying after timer!")
|
277
|
+
thread
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
it "doesn't block the handler's thread" do
|
283
|
+
expect(response).to receive(:reply)
|
284
|
+
thread = subject.infinite_every_test(response)
|
285
|
+
thread.kill
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Lita::Handler::EventRouter, lita: true do
|
6
|
+
let(:robot) { Lita::Robot.new(registry) }
|
7
|
+
|
8
|
+
subject do
|
9
|
+
Class.new do
|
10
|
+
extend Lita::Handler::EventRouter
|
11
|
+
|
12
|
+
def self.name
|
13
|
+
"Test"
|
14
|
+
end
|
15
|
+
|
16
|
+
on :connected, :greet
|
17
|
+
|
18
|
+
def greet(payload)
|
19
|
+
robot.send_message("Hi, #{payload[:name]}! Lita has started!")
|
20
|
+
end
|
21
|
+
|
22
|
+
on :block_test do |payload|
|
23
|
+
robot.send_message("#{payload[:data]} received via block!")
|
24
|
+
end
|
25
|
+
|
26
|
+
on :callable_test, lambda { |payload|
|
27
|
+
robot.send_message("#{payload[:data]} received via callable!")
|
28
|
+
}
|
29
|
+
|
30
|
+
on(:multiple_callbacks) { robot.send_message("first callback") }
|
31
|
+
on(:multiple_callbacks) { robot.send_message("second callback") }
|
32
|
+
|
33
|
+
on(:multiple_errors) do
|
34
|
+
robot.send_message("first error")
|
35
|
+
raise ArgumentError, "first"
|
36
|
+
end
|
37
|
+
|
38
|
+
on(:multiple_errors) do
|
39
|
+
robot.send_message("second error")
|
40
|
+
raise ArgumentError, "second"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe ".trigger" do
|
46
|
+
it "invokes methods registered with .on and passes an arbitrary payload" do
|
47
|
+
expect(robot).to receive(:send_message).with(
|
48
|
+
"Hi, Carl! Lita has started!"
|
49
|
+
)
|
50
|
+
subject.trigger(robot, :connected, name: "Carl")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "calls blocks that were passed to .on" do
|
54
|
+
expect(robot).to receive(:send_message).with("Data received via block!")
|
55
|
+
subject.trigger(robot, :block_test, data: "Data")
|
56
|
+
end
|
57
|
+
|
58
|
+
it "calls arbitrary callables that were passed to .on" do
|
59
|
+
expect(robot).to receive(:send_message).with("Data received via callable!")
|
60
|
+
subject.trigger(robot, :callable_test, data: "Data")
|
61
|
+
end
|
62
|
+
|
63
|
+
it "doesn't stop triggering callbacks after the first is triggered" do
|
64
|
+
allow(robot).to receive(:send_message)
|
65
|
+
|
66
|
+
expect(robot).to receive(:send_message).with("second callback")
|
67
|
+
|
68
|
+
subject.trigger(robot, :multiple_callbacks)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "normalizes the event name" do
|
72
|
+
expect(robot).to receive(:send_message).twice
|
73
|
+
subject.trigger(robot, "connected")
|
74
|
+
subject.trigger(robot, " ConNected ")
|
75
|
+
end
|
76
|
+
|
77
|
+
context "not in test mode" do
|
78
|
+
before { stub_const("STDERR", StringIO.new) }
|
79
|
+
|
80
|
+
around do |example|
|
81
|
+
test_mode = Lita.test_mode?
|
82
|
+
Lita.test_mode = false
|
83
|
+
begin
|
84
|
+
example.run
|
85
|
+
ensure
|
86
|
+
Lita.test_mode = test_mode
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it "doesn't stop triggering callbacks after an exception is raised" do
|
91
|
+
expect(robot).to receive(:send_message).with("first error").once
|
92
|
+
expect(robot).to receive(:send_message).with("second error").once
|
93
|
+
subject.trigger(robot, :multiple_errors)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "reports callback exceptions to the error handler" do
|
97
|
+
allow(robot).to receive(:send_message)
|
98
|
+
expect(registry.config.robot.error_handler).to receive(:call).twice
|
99
|
+
subject.trigger(robot, :multiple_errors)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context "in test mode" do
|
104
|
+
around do |example|
|
105
|
+
test_mode = Lita.test_mode?
|
106
|
+
Lita.test_mode = true
|
107
|
+
begin
|
108
|
+
example.run
|
109
|
+
ensure
|
110
|
+
Lita.test_mode = test_mode
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
it "re-raises callback exceptions immediately" do
|
115
|
+
allow(robot).to receive(:send_message)
|
116
|
+
expect(registry.config.robot.error_handler).to receive(:call).once
|
117
|
+
expect { subject.trigger(robot, :multiple_errors) }.to raise_error(ArgumentError, "first")
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|