gap_detector_agent_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require 'rails_helper'
  2. describe Agents::GapDetectorAgent do
  3. let(:valid_params) {
  4. {
  5. 'name' => "my gap detector agent",
  6. 'options' => {
  7. 'window_duration_in_days' => "2",
  8. 'message' => "A gap was found!"
  9. }
  10. }
  11. }
  12. let(:agent) {
  13. _agent = Agents::GapDetectorAgent.new(valid_params)
  14. _agent.user = users(:bob)
  15. _agent.save!
  16. _agent
  17. }
  18. describe 'validation' do
  19. before do
  20. expect(agent).to be_valid
  21. end
  22. it 'should validate presence of message' do
  23. agent.options['message'] = nil
  24. expect(agent).not_to be_valid
  25. end
  26. it 'should validate presence of window_duration_in_days' do
  27. agent.options['window_duration_in_days'] = ""
  28. expect(agent).not_to be_valid
  29. agent.options['window_duration_in_days'] = "wrong"
  30. expect(agent).not_to be_valid
  31. agent.options['window_duration_in_days'] = "1"
  32. expect(agent).to be_valid
  33. agent.options['window_duration_in_days'] = "0.5"
  34. expect(agent).to be_valid
  35. end
  36. end
  37. describe '#receive' do
  38. it 'records the event if it has a created_at newer than the last seen' do
  39. agent.receive([events(:bob_website_agent_event)])
  40. expect(agent.memory['newest_event_created_at']).to eq events(:bob_website_agent_event).created_at.to_i
  41. events(:bob_website_agent_event).created_at = 2.days.ago
  42. expect {
  43. agent.receive([events(:bob_website_agent_event)])
  44. }.to_not change { agent.memory['newest_event_created_at'] }
  45. events(:bob_website_agent_event).created_at = 2.days.from_now
  46. expect {
  47. agent.receive([events(:bob_website_agent_event)])
  48. }.to change { agent.memory['newest_event_created_at'] }.to(events(:bob_website_agent_event).created_at.to_i)
  49. end
  50. it 'ignores the event if value_path is present and the value at the path is blank' do
  51. agent.options['value_path'] = 'title'
  52. agent.receive([events(:bob_website_agent_event)])
  53. expect(agent.memory['newest_event_created_at']).to eq events(:bob_website_agent_event).created_at.to_i
  54. events(:bob_website_agent_event).created_at = 2.days.from_now
  55. events(:bob_website_agent_event).payload['title'] = ''
  56. expect {
  57. agent.receive([events(:bob_website_agent_event)])
  58. }.to_not change { agent.memory['newest_event_created_at'] }
  59. events(:bob_website_agent_event).payload['title'] = 'present!'
  60. expect {
  61. agent.receive([events(:bob_website_agent_event)])
  62. }.to change { agent.memory['newest_event_created_at'] }.to(events(:bob_website_agent_event).created_at.to_i)
  63. end
  64. it 'clears any previous alert' do
  65. agent.memory['alerted_at'] = 2.days.ago.to_i
  66. agent.receive([events(:bob_website_agent_event)])
  67. expect(agent.memory).to_not have_key('alerted_at')
  68. end
  69. end
  70. describe '#check' do
  71. it 'alerts once if no data has been received during window_duration_in_days' do
  72. agent.memory['newest_event_created_at'] = 1.days.ago.to_i
  73. expect {
  74. agent.check
  75. }.to_not change { agent.events.count }
  76. agent.memory['newest_event_created_at'] = 3.days.ago.to_i
  77. expect {
  78. agent.check
  79. }.to change { agent.events.count }.by(1)
  80. expect(agent.events.last.payload).to eq ({ 'message' => 'A gap was found!',
  81. 'gap_started_at' => agent.memory['newest_event_created_at'] })
  82. expect {
  83. agent.check
  84. }.not_to change { agent.events.count }
  85. end
  86. end
  87. end