1
0

agents_exporter_spec.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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[:schema_version]).to eq(1)
  22. expect(data[:tag_fg_color]).to eq(tag_fg_color)
  23. expect(data[:tag_bg_color]).to eq(tag_bg_color)
  24. expect(Time.parse(data[:exported_at])).to be_within(2).of(Time.now.utc)
  25. expect(data[:links]).to eq([{ :source => 0, :receiver => 1 }])
  26. expect(data[:agents]).to eq(agent_list.map { |agent| exporter.agent_as_json(agent) })
  27. expect(data[:agents].all? { |agent_json| agent_json[:guid].present? && agent_json[:type].present? && agent_json[:name].present? }).to be_truthy
  28. expect(data[:agents][0]).not_to have_key(:propagate_immediately) # can't receive events
  29. expect(data[:agents][1]).not_to have_key(:schedule) # can't be scheduled
  30. end
  31. it "does not output links to other agents outside of the incoming set" do
  32. Link.create!(:source_id => agents(:jane_weather_agent).id, :receiver_id => agents(:jane_website_agent).id)
  33. Link.create!(:source_id => agents(:jane_website_agent).id, :receiver_id => agents(:jane_rain_notifier_agent).id)
  34. expect(exporter.as_json[:links]).to eq([{ :source => 0, :receiver => 1 }])
  35. end
  36. end
  37. describe "#filename" do
  38. it "strips special characters" do
  39. expect(AgentsExporter.new(:name => "ƏfooƐƕƺbar").filename).to eq("foo-bar.json")
  40. end
  41. it "strips punctuation" do
  42. expect(AgentsExporter.new(:name => "foo,bar").filename).to eq("foo-bar.json")
  43. end
  44. it "strips leading and trailing dashes" do
  45. expect(AgentsExporter.new(:name => ",foo,").filename).to eq("foo.json")
  46. end
  47. it "has a default when options[:name] is nil" do
  48. expect(AgentsExporter.new(:name => nil).filename).to eq("exported-agents.json")
  49. end
  50. it "has a default when the result is empty" do
  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. expect(AgentsExporter.new(:name => ",,").filename).to eq("exported-agents.json")
  55. end
  56. end
  57. end