event_formatting_agent_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. require 'spec_helper'
  2. describe Agents::EventFormattingAgent do
  3. before do
  4. @valid_params = {
  5. :name => "somename",
  6. :options => {
  7. :instructions => {
  8. :message => "Received {{content.text}} from {{content.name}} .",
  9. :subject => "Weather looks like {{conditions}} according to the forecast at {{pretty_date.time}}",
  10. :agent => "{{agent.type}}",
  11. :created_at => "{{created_at}}",
  12. :created_at_iso => "{{created_at | date:'%FT%T%:z'}}",
  13. },
  14. :mode => "clean",
  15. :matchers => [
  16. {
  17. :path => "{{date.pretty}}",
  18. :regexp => "\\A(?<time>\\d\\d:\\d\\d [AP]M [A-Z]+)",
  19. :to => "pretty_date",
  20. },
  21. ],
  22. }
  23. }
  24. @checker = Agents::EventFormattingAgent.new(@valid_params)
  25. @checker.user = users(:jane)
  26. @checker.save!
  27. @event = Event.new
  28. @event.agent = agents(:jane_weather_agent)
  29. @event.created_at = Time.now
  30. @event.payload = {
  31. :content => {
  32. :text => "Some Lorem Ipsum",
  33. :name => "somevalue",
  34. },
  35. :date => {
  36. :epoch => "1357959600",
  37. :pretty => "10:00 PM EST on January 11, 2013"
  38. },
  39. :conditions => "someothervalue"
  40. }
  41. end
  42. describe "#receive" do
  43. it "should accept clean mode" do
  44. @checker.receive([@event])
  45. expect(Event.last.payload[:content]).to eq(nil)
  46. end
  47. it "should accept merge mode" do
  48. @checker.options[:mode] = "merge"
  49. @checker.receive([@event])
  50. expect(Event.last.payload[:content]).not_to eq(nil)
  51. end
  52. it "should handle Liquid templating in instructions" do
  53. @checker.receive([@event])
  54. expect(Event.last.payload[:message]).to eq("Received Some Lorem Ipsum from somevalue .")
  55. expect(Event.last.payload[:agent]).to eq("WeatherAgent")
  56. expect(Event.last.payload[:created_at]).to eq(@event.created_at.to_s)
  57. expect(Event.last.payload[:created_at_iso]).to eq(@event.created_at.iso8601)
  58. end
  59. it "should handle matchers and Liquid templating in instructions" do
  60. @checker.receive([@event])
  61. expect(Event.last.payload[:subject]).to eq("Weather looks like someothervalue according to the forecast at 10:00 PM EST")
  62. end
  63. it "should allow escaping" do
  64. @event.payload[:content][:name] = "escape this!?"
  65. @event.save!
  66. @checker.options[:instructions][:message] = "Escaped: {{content.name | uri_escape}}\nNot escaped: {{content.name}}"
  67. @checker.save!
  68. @checker.receive([@event])
  69. expect(Event.last.payload[:message]).to eq("Escaped: escape+this%21%3F\nNot escaped: escape this!?")
  70. end
  71. it "should handle multiple events" do
  72. event1 = Event.new
  73. event1.agent = agents(:bob_weather_agent)
  74. event1.payload = {
  75. :content => {
  76. :text => "Some Lorem Ipsum",
  77. :name => "somevalue"
  78. },
  79. :conditions => "someothervalue"
  80. }
  81. event2 = Event.new
  82. event2.agent = agents(:bob_weather_agent)
  83. event2.payload = {
  84. :content => {
  85. :text => "Some Lorem Ipsum",
  86. :name => "somevalue"
  87. },
  88. :conditions => "someothervalue"
  89. }
  90. expect {
  91. @checker.receive([event2, event1])
  92. }.to change { Event.count }.by(2)
  93. end
  94. end
  95. describe "validation" do
  96. before do
  97. expect(@checker).to be_valid
  98. end
  99. it "should validate presence of instructions" do
  100. @checker.options[:instructions] = ""
  101. expect(@checker).not_to be_valid
  102. end
  103. it "should validate type of matchers" do
  104. @checker.options[:matchers] = ""
  105. expect(@checker).not_to be_valid
  106. @checker.options[:matchers] = {}
  107. expect(@checker).not_to be_valid
  108. end
  109. it "should validate the contents of matchers" do
  110. @checker.options[:matchers] = [
  111. {}
  112. ]
  113. expect(@checker).not_to be_valid
  114. @checker.options[:matchers] = [
  115. { :regexp => "(not closed", :path => "text" }
  116. ]
  117. expect(@checker).not_to be_valid
  118. @checker.options[:matchers] = [
  119. { :regexp => "(closed)", :path => "text", :to => "foo" }
  120. ]
  121. expect(@checker).to be_valid
  122. end
  123. it "should validate presence of mode" do
  124. @checker.options[:mode] = ""
  125. expect(@checker).not_to be_valid
  126. end
  127. end
  128. end