agent_log_spec.rb 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. require 'spec_helper'
  2. describe AgentLog do
  3. describe "validations" do
  4. before do
  5. @log = AgentLog.new(:agent => agents(:jane_website_agent), :message => "The agent did something", :level => 3)
  6. expect(@log).to be_valid
  7. end
  8. it "requires an agent" do
  9. @log.agent = nil
  10. expect(@log).not_to be_valid
  11. expect(@log).to have(1).error_on(:agent)
  12. end
  13. it "requires a message" do
  14. @log.message = ""
  15. expect(@log).not_to be_valid
  16. @log.message = nil
  17. expect(@log).not_to be_valid
  18. expect(@log).to have(1).error_on(:message)
  19. end
  20. it "requires a valid log level" do
  21. @log.level = nil
  22. expect(@log).not_to be_valid
  23. expect(@log).to have(1).error_on(:level)
  24. @log.level = -1
  25. expect(@log).not_to be_valid
  26. expect(@log).to have(1).error_on(:level)
  27. @log.level = 5
  28. expect(@log).not_to be_valid
  29. expect(@log).to have(1).error_on(:level)
  30. @log.level = 4
  31. expect(@log).to be_valid
  32. @log.level = 0
  33. expect(@log).to be_valid
  34. end
  35. end
  36. it "truncates message to a reasonable length" do
  37. log = AgentLog.new(:agent => agents(:jane_website_agent), :level => 3)
  38. log.message = "a" * 3000
  39. log.save!
  40. expect(log.message.length).to eq(2048)
  41. end
  42. describe "#log_for_agent" do
  43. it "creates AgentLogs" do
  44. log = AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 4, :outbound_event => events(:jane_website_agent_event))
  45. expect(log).not_to be_new_record
  46. expect(log.agent).to eq(agents(:jane_website_agent))
  47. expect(log.outbound_event).to eq(events(:jane_website_agent_event))
  48. expect(log.message).to eq("some message")
  49. expect(log.level).to eq(4)
  50. end
  51. it "cleans up old logs when there are more than log_length" do
  52. stub(AgentLog).log_length { 4 }
  53. AgentLog.log_for_agent(agents(:jane_website_agent), "message 1")
  54. AgentLog.log_for_agent(agents(:jane_website_agent), "message 2")
  55. AgentLog.log_for_agent(agents(:jane_website_agent), "message 3")
  56. AgentLog.log_for_agent(agents(:jane_website_agent), "message 4")
  57. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message).to eq("message 4")
  58. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message).to eq("message 1")
  59. AgentLog.log_for_agent(agents(:jane_website_agent), "message 5")
  60. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message).to eq("message 5")
  61. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message).to eq("message 2")
  62. AgentLog.log_for_agent(agents(:jane_website_agent), "message 6")
  63. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message).to eq("message 6")
  64. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message).to eq("message 3")
  65. end
  66. it "updates Agents' last_error_log_at when an error is logged" do
  67. AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 3, :outbound_event => events(:jane_website_agent_event))
  68. expect(agents(:jane_website_agent).reload.last_error_log_at).to be_nil
  69. AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 4, :outbound_event => events(:jane_website_agent_event))
  70. expect(agents(:jane_website_agent).reload.last_error_log_at.to_i).to be_within(2).of(Time.now.to_i)
  71. end
  72. end
  73. describe "#log_length" do
  74. it "defaults to 200" do
  75. expect(AgentLog.log_length).to eq(200)
  76. end
  77. end
  78. end