1
0

agent_log_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. require 'rails_helper'
  3. describe AgentLog do
  4. describe "validations" do
  5. before do
  6. @log = AgentLog.new(:agent => agents(:jane_website_agent), :message => "The agent did something", :level => 3)
  7. expect(@log).to be_valid
  8. end
  9. it "requires an agent" do
  10. @log.agent = nil
  11. expect(@log).not_to be_valid
  12. expect(@log).to have(1).error_on(:agent)
  13. end
  14. it "requires a message" do
  15. @log.message = ""
  16. expect(@log).not_to be_valid
  17. @log.message = nil
  18. expect(@log).not_to be_valid
  19. expect(@log).to have(1).error_on(:message)
  20. end
  21. it "requires a valid log level" do
  22. @log.level = nil
  23. expect(@log).not_to be_valid
  24. expect(@log).to have(1).error_on(:level)
  25. @log.level = -1
  26. expect(@log).not_to be_valid
  27. expect(@log).to have(1).error_on(:level)
  28. @log.level = 5
  29. expect(@log).not_to be_valid
  30. expect(@log).to have(1).error_on(:level)
  31. @log.level = 4
  32. expect(@log).to be_valid
  33. @log.level = 0
  34. expect(@log).to be_valid
  35. end
  36. end
  37. it "replaces invalid byte sequences in a message" do
  38. log = AgentLog.new(:agent => agents(:jane_website_agent), level: 3)
  39. log.message = "\u{3042}\xffA\x95"
  40. expect { log.save! }.not_to raise_error
  41. expect(log.message).to eq("\u{3042}<ff>A\<95>")
  42. end
  43. it "truncates message to a reasonable length" do
  44. log = AgentLog.new(:agent => agents(:jane_website_agent), :level => 3)
  45. log.message = "a" * 11_000
  46. log.save!
  47. expect(log.message.length).to eq(10_000)
  48. end
  49. describe "#log_for_agent" do
  50. it "creates AgentLogs" do
  51. log = AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 4, :outbound_event => events(:jane_website_agent_event))
  52. expect(log).not_to be_new_record
  53. expect(log.agent).to eq(agents(:jane_website_agent))
  54. expect(log.outbound_event).to eq(events(:jane_website_agent_event))
  55. expect(log.message).to eq("some message")
  56. expect(log.level).to eq(4)
  57. end
  58. it "cleans up old logs when there are more than log_length" do
  59. allow(AgentLog).to receive(:log_length) { 4 }
  60. AgentLog.log_for_agent(agents(:jane_website_agent), "message 1")
  61. AgentLog.log_for_agent(agents(:jane_website_agent), "message 2")
  62. AgentLog.log_for_agent(agents(:jane_website_agent), "message 3")
  63. AgentLog.log_for_agent(agents(:jane_website_agent), "message 4")
  64. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message).to eq("message 4")
  65. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message).to eq("message 1")
  66. AgentLog.log_for_agent(agents(:jane_website_agent), "message 5")
  67. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message).to eq("message 5")
  68. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message).to eq("message 2")
  69. AgentLog.log_for_agent(agents(:jane_website_agent), "message 6")
  70. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").first.message).to eq("message 6")
  71. expect(agents(:jane_website_agent).logs.order("agent_logs.id desc").last.message).to eq("message 3")
  72. end
  73. it "updates Agents' last_error_log_at when an error is logged" do
  74. AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 3, :outbound_event => events(:jane_website_agent_event))
  75. expect(agents(:jane_website_agent).reload.last_error_log_at).to be_nil
  76. AgentLog.log_for_agent(agents(:jane_website_agent), "some message", :level => 4, :outbound_event => events(:jane_website_agent_event))
  77. expect(agents(:jane_website_agent).reload.last_error_log_at.to_i).to be_within(2).of(Time.now.to_i)
  78. end
  79. it "accepts objects as well as strings" do
  80. log = AgentLog.log_for_agent(agents(:jane_website_agent), events(:bob_website_agent_event).payload)
  81. expect(log.message).to include('"title"=>"foo"')
  82. end
  83. end
  84. describe "#log_length" do
  85. it "defaults to 200" do
  86. expect(AgentLog.log_length).to eq(200)
  87. end
  88. end
  89. end