stealth 1.1.5 → 2.0.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +27 -11
  3. data/CHANGELOG.md +77 -0
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +53 -49
  6. data/LICENSE +4 -17
  7. data/README.md +9 -17
  8. data/VERSION +1 -1
  9. data/lib/stealth/base.rb +72 -21
  10. data/lib/stealth/cli.rb +1 -2
  11. data/lib/stealth/commands/console.rb +1 -1
  12. data/lib/stealth/configuration.rb +6 -3
  13. data/lib/stealth/controller/callbacks.rb +1 -1
  14. data/lib/stealth/controller/catch_all.rb +27 -4
  15. data/lib/stealth/controller/controller.rb +168 -49
  16. data/lib/stealth/controller/dev_jumps.rb +41 -0
  17. data/lib/stealth/controller/dynamic_delay.rb +4 -6
  18. data/lib/stealth/controller/interrupt_detect.rb +100 -0
  19. data/lib/stealth/controller/messages.rb +283 -0
  20. data/lib/stealth/controller/nlp.rb +50 -0
  21. data/lib/stealth/controller/replies.rb +183 -40
  22. data/lib/stealth/controller/unrecognized_message.rb +62 -0
  23. data/lib/stealth/{flow/core_ext.rb → core_ext/numeric.rb} +0 -1
  24. data/lib/stealth/core_ext/string.rb +18 -0
  25. data/lib/stealth/core_ext.rb +5 -0
  26. data/lib/stealth/dispatcher.rb +21 -0
  27. data/lib/stealth/errors.rb +12 -0
  28. data/lib/stealth/flow/base.rb +1 -2
  29. data/lib/stealth/flow/specification.rb +3 -2
  30. data/lib/stealth/flow/state.rb +3 -3
  31. data/lib/stealth/generators/builder/Gemfile +4 -3
  32. data/lib/stealth/generators/builder/bot/controllers/bot_controller.rb +42 -0
  33. data/lib/stealth/generators/builder/bot/controllers/catch_alls_controller.rb +2 -0
  34. data/lib/stealth/generators/builder/bot/controllers/goodbyes_controller.rb +2 -0
  35. data/lib/stealth/generators/builder/bot/controllers/hellos_controller.rb +2 -0
  36. data/lib/stealth/generators/builder/bot/controllers/interrupts_controller.rb +9 -0
  37. data/lib/stealth/generators/builder/bot/controllers/unrecognized_messages_controller.rb +9 -0
  38. data/lib/stealth/generators/builder/config/flow_map.rb +8 -0
  39. data/lib/stealth/generators/builder/config/initializers/autoload.rb +8 -0
  40. data/lib/stealth/generators/builder/config/initializers/inflections.rb +16 -0
  41. data/lib/stealth/generators/builder/config/puma.rb +15 -0
  42. data/lib/stealth/helpers/redis.rb +40 -0
  43. data/lib/stealth/lock.rb +83 -0
  44. data/lib/stealth/logger.rb +27 -18
  45. data/lib/stealth/nlp/client.rb +22 -0
  46. data/lib/stealth/nlp/result.rb +57 -0
  47. data/lib/stealth/reloader.rb +90 -0
  48. data/lib/stealth/reply.rb +17 -0
  49. data/lib/stealth/scheduled_reply.rb +3 -3
  50. data/lib/stealth/server.rb +8 -3
  51. data/lib/stealth/service_message.rb +3 -2
  52. data/lib/stealth/service_reply.rb +5 -1
  53. data/lib/stealth/services/base_reply_handler.rb +10 -2
  54. data/lib/stealth/session.rb +106 -53
  55. data/spec/configuration_spec.rb +42 -2
  56. data/spec/controller/callbacks_spec.rb +23 -28
  57. data/spec/controller/catch_all_spec.rb +87 -29
  58. data/spec/controller/controller_spec.rb +444 -43
  59. data/spec/controller/dynamic_delay_spec.rb +16 -18
  60. data/spec/controller/helpers_spec.rb +1 -2
  61. data/spec/controller/interrupt_detect_spec.rb +171 -0
  62. data/spec/controller/messages_spec.rb +744 -0
  63. data/spec/controller/nlp_spec.rb +93 -0
  64. data/spec/controller/replies_spec.rb +446 -11
  65. data/spec/controller/unrecognized_message_spec.rb +168 -0
  66. data/spec/dispatcher_spec.rb +79 -0
  67. data/spec/flow/flow_spec.rb +1 -2
  68. data/spec/flow/state_spec.rb +14 -3
  69. data/spec/helpers/redis_spec.rb +77 -0
  70. data/spec/lock_spec.rb +100 -0
  71. data/spec/nlp/client_spec.rb +23 -0
  72. data/spec/nlp/result_spec.rb +57 -0
  73. data/spec/replies/messages/say_msgs_without_breaks.yml +4 -0
  74. data/spec/replies/messages/say_randomize_speech.yml +10 -0
  75. data/spec/replies/messages/say_randomize_text.yml +10 -0
  76. data/spec/replies/messages/sub1/sub2/say_nested.yml +10 -0
  77. data/spec/reply_spec.rb +61 -0
  78. data/spec/scheduled_reply_spec.rb +23 -0
  79. data/spec/service_reply_spec.rb +1 -2
  80. data/spec/session_spec.rb +251 -12
  81. data/spec/spec_helper.rb +21 -0
  82. data/spec/support/controllers/vaders_controller.rb +24 -0
  83. data/spec/support/nlp_clients/dialogflow.rb +9 -0
  84. data/spec/support/nlp_clients/luis.rb +9 -0
  85. data/spec/support/nlp_results/luis_result.rb +163 -0
  86. data/spec/version_spec.rb +1 -2
  87. data/stealth.gemspec +6 -6
  88. metadata +83 -38
  89. data/docs/00-introduction.md +0 -37
  90. data/docs/01-getting-started.md +0 -21
  91. data/docs/02-local-development.md +0 -40
  92. data/docs/03-basics.md +0 -171
  93. data/docs/04-sessions.md +0 -29
  94. data/docs/05-controllers.md +0 -179
  95. data/docs/06-models.md +0 -39
  96. data/docs/07-replies.md +0 -114
  97. data/docs/08-catchalls.md +0 -49
  98. data/docs/09-messaging-integrations.md +0 -80
  99. data/docs/10-nlp-integrations.md +0 -13
  100. data/docs/11-analytics.md +0 -13
  101. data/docs/12-commands.md +0 -62
  102. data/docs/13-deployment.md +0 -50
  103. data/lib/stealth/generators/builder/config/initializers/.keep +0 -0
@@ -1,35 +1,22 @@
1
- # coding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '/spec_helper'))
3
+ require 'spec_helper'
5
4
 
6
5
  describe "Stealth::Controller::CatchAll" do
7
-
8
- class VadersController < Stealth::Controller
9
- def my_action
10
- raise "oops"
11
- end
12
-
13
- def my_action2
14
-
15
- end
16
-
17
- def my_action3
18
-
19
- end
20
- end
6
+ $msg = nil
21
7
 
22
8
  class StubbedCatchAllsController < Stealth::Controller
23
9
  def level1
24
-
10
+ $msg = current_message
11
+ do_nothing
25
12
  end
26
13
 
27
14
  def level2
28
-
15
+ do_nothing
29
16
  end
30
17
 
31
18
  def level3
32
-
19
+ do_nothing
33
20
  end
34
21
  end
35
22
 
@@ -40,6 +27,8 @@ describe "Stealth::Controller::CatchAll" do
40
27
  state :my_action
41
28
  state :my_action2
42
29
  state :my_action3
30
+ state :action_with_unrecognized_msg
31
+ state :action_with_unrecognized_match
43
32
  end
44
33
 
45
34
  flow :catch_all do
@@ -64,30 +53,26 @@ describe "Stealth::Controller::CatchAll" do
64
53
  it "should step_to catch_all->level1 when a StandardError is raised" do
65
54
  controller.current_session.session = Stealth::Session.canonical_session_slug(flow: 'vader', state: 'my_action')
66
55
  controller.action(action: :my_action)
67
- expect(controller.current_session.flow_string).to eq("catch_all")
68
- expect(controller.current_session.state_string).to eq("level1")
56
+ expect($redis.get(controller.current_session.session_key)).to eq('catch_all->level1')
69
57
  end
70
58
 
71
59
  it "should step_to catch_all->level1 when an action doesn't progress the flow" do
72
60
  controller.current_session.session = Stealth::Session.canonical_session_slug(flow: 'vader', state: 'my_action2')
73
61
  controller.action(action: :my_action2)
74
- expect(controller.current_session.flow_string).to eq("catch_all")
75
- expect(controller.current_session.state_string).to eq("level1")
62
+ expect($redis.get(controller.current_session.session_key)).to eq('catch_all->level1')
76
63
  end
77
64
 
78
65
  it "should step_to catch_all->level2 when an action raises back to back" do
79
66
  controller.step_to flow: :vader, state: :my_action
80
67
  controller.step_to flow: :vader, state: :my_action
81
- expect(controller.current_session.flow_string).to eq("catch_all")
82
- expect(controller.current_session.state_string).to eq("level2")
68
+ expect($redis.get(controller.current_session.session_key)).to eq('catch_all->level2')
83
69
  end
84
70
 
85
71
  it "should step_to catch_all->level3 when an action raises back to back to back" do
86
72
  controller.step_to flow: :vader, state: :my_action
87
73
  controller.step_to flow: :vader, state: :my_action
88
74
  controller.step_to flow: :vader, state: :my_action
89
- expect(controller.current_session.flow_string).to eq("catch_all")
90
- expect(controller.current_session.state_string).to eq("level3")
75
+ expect($redis.get(controller.current_session.session_key)).to eq('catch_all->level3')
91
76
  end
92
77
 
93
78
  it "should just stop after the maximum number of catch_all levels have been reached" do
@@ -95,8 +80,81 @@ describe "Stealth::Controller::CatchAll" do
95
80
  controller.step_to flow: :vader, state: :my_action
96
81
  controller.step_to flow: :vader, state: :my_action
97
82
  controller.step_to flow: :vader, state: :my_action
98
- expect(controller.current_session.flow_string).to eq("vader")
99
- expect(controller.current_session.state_string).to eq("my_action")
83
+ expect($redis.get(controller.current_session.session_key)).to eq('vader->my_action')
84
+ end
85
+
86
+ it "should NOT run the catch_all if do_nothing is called" do
87
+ controller.current_session.set_session(new_flow: 'vader', new_state: 'my_action3')
88
+ controller.action(action: :my_action3)
89
+ expect($redis.get(controller.current_session.session_key)).to eq('vader->my_action3')
90
+ end
91
+
92
+ describe "catch_alls from within catch_all flow" do
93
+ let(:e) {
94
+ e = OpenStruct.new
95
+ e.class = RuntimeError
96
+ e.message = 'oops'
97
+ e.backtrace = [
98
+ '/stealth/lib/stealth/controller/controller.rb',
99
+ '/stealth/lib/stealth/controller/catch_all.rb',
100
+ ]
101
+ e
102
+ }
103
+
104
+ before(:each) do
105
+ controller.current_session.session = Stealth::Session.canonical_session_slug(flow: 'catch_all', state: 'level1')
106
+ end
107
+
108
+ it "should not step_to to catch_all" do
109
+ expect(controller).to_not receive(:step_to)
110
+ controller.run_catch_all(err: e)
111
+ end
112
+
113
+ it "should return false" do
114
+ expect(controller.run_catch_all(err: e)).to be false
115
+ end
116
+
117
+ it "should log the error message" do
118
+ err_klass = if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
119
+ "RuntimeError"
120
+ else
121
+ "OpenStruct"
122
+ end
123
+
124
+ expect(Stealth::Logger).to receive(:l).with(topic: 'catch_all', message: "[Level 1] for user #{facebook_message.sender_id} #{err_klass}\noops\n/stealth/lib/stealth/controller/controller.rb\n/stealth/lib/stealth/controller/catch_all.rb")
125
+ expect(Stealth::Logger).to receive(:l).with(topic: 'catch_all', message: "CatchAll triggered for user #{facebook_message.sender_id} from within CatchAll; ignoring.")
126
+ controller.run_catch_all(err: e)
127
+ end
128
+ end
129
+
130
+ describe "catch_all_reason" do
131
+ before(:each) do
132
+ @session = Stealth::Session.new(id: controller.current_session_id)
133
+ @session.set_session(new_flow: 'vader', new_state: 'my_action2')
134
+ end
135
+
136
+ after(:each) do
137
+ $msg = nil
138
+ end
139
+
140
+ it 'should have access to the error raised in current_message.catch_all_reason' do
141
+ controller.action(action: :my_action)
142
+ expect($msg.catch_all_reason).to be_a(Hash)
143
+ expect($msg.catch_all_reason[:err]).to eq(RuntimeError)
144
+ expect($msg.catch_all_reason[:err_msg]).to eq('oops')
145
+ end
146
+
147
+ it 'should have the correct error when handle_message fails to recognize a message' do
148
+ controller.action(action: :action_with_unrecognized_msg)
149
+ expect($msg.catch_all_reason[:err]).to eq(Stealth::Errors::UnrecognizedMessage)
150
+ expect($msg.catch_all_reason[:err_msg]).to eq("The reply '#{facebook_message.message_with_text.message}' was not recognized.")
151
+ end
152
+
153
+ it 'should have the correct error when get_match fails to recognize a message' do
154
+ controller.action(action: :action_with_unrecognized_match)
155
+ expect($msg.catch_all_reason[:err]).to eq(Stealth::Errors::UnrecognizedMessage)
156
+ expect($msg.catch_all_reason[:err_msg]).to eq("The reply '#{facebook_message.message_with_text.message}' was not recognized.")
157
+ end
100
158
  end
101
159
  end
102
160
  end