witai_agent_spec.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. require 'rails_helper'
  2. describe Agents::WitaiAgent do
  3. before do
  4. stub_request(:get, /wit/).to_return(:body => File.read(Rails.root.join('spec/data_fixtures/witai.json')), :status => 200, :headers => {'Content-Type' => 'text/json'})
  5. @valid_params = {
  6. :server_access_token => 'x',
  7. :expected_receive_period_in_days => '2',
  8. :query => '{{message.content}}'
  9. }
  10. @checker = Agents::WitaiAgent.new :name => 'wit.ai agent',
  11. :options => @valid_params
  12. @checker.user = users :jane
  13. @checker.save!
  14. @event = Event.new
  15. @event.agent = agents :jane_weather_agent
  16. @event.payload = {:message => {
  17. :content => 'set the temperature to 22 degrees at 7 PM'
  18. }}
  19. @event.save!
  20. end
  21. describe '#validation' do
  22. before do
  23. expect(@checker).to be_valid
  24. end
  25. it 'validates presence of server access token' do
  26. @checker.options[:server_access_token] = nil
  27. expect(@checker).not_to be_valid
  28. end
  29. it 'validates presence of query' do
  30. @checker.options[:query] = nil
  31. expect(@checker).not_to be_valid
  32. end
  33. it 'validates presence of expected receive period in days key' do
  34. @checker.options[:expected_receive_period_in_days] = nil
  35. expect(@checker).not_to be_valid
  36. end
  37. end
  38. describe '#working' do
  39. it 'checks if agent is working when event is received withing expected number of days' do
  40. expect(@checker).not_to be_working
  41. Agents::WitaiAgent.async_receive @checker.id, [@event.id]
  42. expect(@checker.reload).to be_working
  43. two_days_from_now = 2.days.from_now
  44. allow(Time).to receive(:now) { two_days_from_now }
  45. expect(@checker.reload).not_to be_working
  46. end
  47. end
  48. describe '#receive' do
  49. it 'checks that a new event is created after receiving one' do
  50. expect {
  51. @checker.receive([@event])
  52. }.to change { Event.count }.by(1)
  53. end
  54. it 'checks the integrity of new event' do
  55. @checker.receive([@event])
  56. expect(Event.last.payload[:outcomes][0][:_text]).to eq(@event.payload[:message][:content])
  57. end
  58. end
  59. end