agents_exporter_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. require 'rails_helper'
  2. describe AgentsExporter do
  3. describe "#as_json" do
  4. let(:name) { "My set of Agents" }
  5. let(:description) { "These Agents work together nicely!" }
  6. let(:guid) { "some-guid" }
  7. let(:tag_fg_color) { "#ffffff" }
  8. let(:tag_bg_color) { "#000000" }
  9. let(:icon) { 'Camera' }
  10. let(:source_url) { "http://yourhuginn.com/scenarios/2/export.json" }
  11. let(:agent_list) { [agents(:jane_weather_agent), agents(:jane_rain_notifier_agent)] }
  12. let(:exporter) { AgentsExporter.new(
  13. agents: agent_list, name: name, description: description,
  14. source_url: source_url, guid: guid, tag_fg_color: tag_fg_color,
  15. tag_bg_color: tag_bg_color, icon: icon) }
  16. it "outputs a structure containing name, description, the date, all agents & their links" do
  17. data = exporter.as_json
  18. expect(data[:name]).to eq(name)
  19. expect(data[:description]).to eq(description)
  20. expect(data[:source_url]).to eq(source_url)
  21. expect(data[:guid]).to eq(guid)
  22. expect(data[:schema_version]).to eq(1)
  23. expect(data[:tag_fg_color]).to eq(tag_fg_color)
  24. expect(data[:tag_bg_color]).to eq(tag_bg_color)
  25. expect(data[:icon]).to eq(icon)
  26. expect(Time.parse(data[:exported_at])).to be_within(2).of(Time.now.utc)
  27. expect(data[:links]).to eq([{ :source => guid_order(agent_list, :jane_weather_agent), :receiver => guid_order(agent_list, :jane_rain_notifier_agent)}])
  28. expect(data[:control_links]).to eq([])
  29. expect(data[:agents]).to eq(agent_list.sort_by{|a| a.guid}.map { |agent| exporter.agent_as_json(agent) })
  30. expect(data[:agents].all? { |agent_json| agent_json[:guid].present? && agent_json[:type].present? && agent_json[:name].present? }).to be_truthy
  31. expect(data[:agents][guid_order(agent_list, :jane_weather_agent)]).not_to have_key(:propagate_immediately) # can't receive events
  32. expect(data[:agents][guid_order(agent_list, :jane_rain_notifier_agent)]).not_to have_key(:schedule) # can't be scheduled
  33. end
  34. it "does not output links to other agents outside of the incoming set" do
  35. Link.create!(:source_id => agents(:jane_weather_agent).id, :receiver_id => agents(:jane_website_agent).id)
  36. Link.create!(:source_id => agents(:jane_website_agent).id, :receiver_id => agents(:jane_rain_notifier_agent).id)
  37. expect(exporter.as_json[:links]).to eq([{ :source => guid_order(agent_list, :jane_weather_agent), :receiver => guid_order(agent_list, :jane_rain_notifier_agent) }])
  38. end
  39. it "outputs control links to agents within the incoming set, but not outside it" do
  40. agents(:jane_rain_notifier_agent).control_targets = [agents(:jane_weather_agent), agents(:jane_basecamp_agent)]
  41. agents(:jane_rain_notifier_agent).save!
  42. expect(exporter.as_json[:control_links]).to eq([{ :controller => guid_order(agent_list, :jane_rain_notifier_agent), :control_target => guid_order(agent_list, :jane_weather_agent) }])
  43. end
  44. end
  45. describe "#filename" do
  46. it "strips special characters" do
  47. expect(AgentsExporter.new(:name => "ƏfooƐƕƺbar").filename).to eq("foo-bar.json")
  48. end
  49. it "strips punctuation" do
  50. expect(AgentsExporter.new(:name => "foo,bar").filename).to eq("foo-bar.json")
  51. end
  52. it "strips leading and trailing dashes" do
  53. expect(AgentsExporter.new(:name => ",foo,").filename).to eq("foo.json")
  54. end
  55. it "has a default when options[:name] is nil" do
  56. expect(AgentsExporter.new(:name => nil).filename).to eq("exported-agents.json")
  57. end
  58. it "has a default when the result is empty" do
  59. expect(AgentsExporter.new(:name => "").filename).to eq("exported-agents.json")
  60. expect(AgentsExporter.new(:name => "Ə").filename).to eq("exported-agents.json")
  61. expect(AgentsExporter.new(:name => "-").filename).to eq("exported-agents.json")
  62. expect(AgentsExporter.new(:name => ",,").filename).to eq("exported-agents.json")
  63. end
  64. end
  65. def guid_order(agent_list, agent_name)
  66. agent_list.map{|a|a.guid}.sort.find_index(agents(agent_name).guid)
  67. end
  68. end