agents_exporter_spec.rb 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # encoding: utf-8
  2. require 'spec_helper'
  3. describe AgentsExporter do
  4. describe "#as_json" do
  5. let(:name) { "My set of Agents" }
  6. let(:description) { "These Agents work together nicely!" }
  7. let(:guid) { "some-guid" }
  8. let(:tag_fg_color) { "#ffffff" }
  9. let(:tag_bg_color) { "#000000" }
  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, :source_url => source_url,
  14. :guid => guid, :tag_fg_color => tag_fg_color, :tag_bg_color => tag_bg_color) }
  15. it "outputs a structure containing name, description, the date, all agents & their links" do
  16. data = exporter.as_json
  17. expect(data[:name]).to eq(name)
  18. expect(data[:description]).to eq(description)
  19. expect(data[:source_url]).to eq(source_url)
  20. expect(data[:guid]).to eq(guid)
  21. expect(data[:tag_fg_color]).to eq(tag_fg_color)
  22. expect(data[:tag_bg_color]).to eq(tag_bg_color)
  23. expect(Time.parse(data[:exported_at])).to be_within(2).of(Time.now.utc)
  24. expect(data[:links]).to eq([{ :source => 0, :receiver => 1 }])
  25. expect(data[:agents]).to eq(agent_list.map { |agent| exporter.agent_as_json(agent) })
  26. expect(data[:agents].all? { |agent_json| agent_json[:guid].present? && agent_json[:type].present? && agent_json[:name].present? }).to be_truthy
  27. expect(data[:agents][0]).not_to have_key(:propagate_immediately) # can't receive events
  28. expect(data[:agents][1]).not_to have_key(:schedule) # can't be scheduled
  29. end
  30. it "does not output links to other agents outside of the incoming set" do
  31. Link.create!(:source_id => agents(:jane_weather_agent).id, :receiver_id => agents(:jane_website_agent).id)
  32. Link.create!(:source_id => agents(:jane_website_agent).id, :receiver_id => agents(:jane_rain_notifier_agent).id)
  33. expect(exporter.as_json[:links]).to eq([{ :source => 0, :receiver => 1 }])
  34. end
  35. end
  36. describe "#filename" do
  37. it "strips special characters" do
  38. expect(AgentsExporter.new(:name => "ƏfooƐƕƺbar").filename).to eq("foo-bar.json")
  39. end
  40. it "strips punctuation" do
  41. expect(AgentsExporter.new(:name => "foo,bar").filename).to eq("foo-bar.json")
  42. end
  43. it "strips leading and trailing dashes" do
  44. expect(AgentsExporter.new(:name => ",foo,").filename).to eq("foo.json")
  45. end
  46. it "has a default when options[:name] is nil" do
  47. expect(AgentsExporter.new(:name => nil).filename).to eq("exported-agents.json")
  48. end
  49. it "has a default when the result is empty" do
  50. expect(AgentsExporter.new(:name => "").filename).to eq("exported-agents.json")
  51. expect(AgentsExporter.new(:name => "Ə").filename).to eq("exported-agents.json")
  52. expect(AgentsExporter.new(:name => "-").filename).to eq("exported-agents.json")
  53. expect(AgentsExporter.new(:name => ",,").filename).to eq("exported-agents.json")
  54. end
  55. end
  56. end