Nenhuma Descrição http://j1x-huginn.herokuapp.com

java_script_agent_spec.rb 7.3KB

    require 'spec_helper' describe Agents::JavaScriptAgent do before do @valid_params = { :name => "somename", :options => { :code => "Agent.check = function() { this.createEvent({ 'message': 'hi' }); };", } } @agent = Agents::JavaScriptAgent.new(@valid_params) @agent.user = users(:jane) @agent.save! end describe "validations" do it "requires 'code'" do expect(@agent).to be_valid @agent.options['code'] = '' expect(@agent).not_to be_valid @agent.options.delete('code') expect(@agent).not_to be_valid end it "accepts a credential, but it must exist" do expect(@agent).to be_valid @agent.options['code'] = 'credential:foo' expect(@agent).not_to be_valid users(:jane).user_credentials.create! :credential_name => "foo", :credential_value => "bar" expect(@agent.reload).to be_valid end end describe "#working?" do describe "when expected_update_period_in_days is set" do it "returns false when more than expected_update_period_in_days have passed since the last event creation" do @agent.options['expected_update_period_in_days'] = 1 @agent.save! expect(@agent).not_to be_working @agent.check expect(@agent.reload).to be_working three_days_from_now = 3.days.from_now stub(Time).now { three_days_from_now } expect(@agent).not_to be_working end end describe "when expected_receive_period_in_days is set" do it "returns false when more than expected_receive_period_in_days have passed since the last event was received" do @agent.options['expected_receive_period_in_days'] = 1 @agent.save! expect(@agent).not_to be_working Agents::JavaScriptAgent.async_receive @agent.id, [events(:bob_website_agent_event).id] expect(@agent.reload).to be_working two_days_from_now = 2.days.from_now stub(Time).now { two_days_from_now } expect(@agent.reload).not_to be_working end end end describe "executing code" do it "works by default" do @agent.options = @agent.default_options @agent.options['make_event'] = true @agent.save! expect { expect { @agent.receive([events(:bob_website_agent_event)]) @agent.check }.not_to change { AgentLog.count } }.to change { Event.count }.by(2) end describe "using credentials as code" do before do @agent.user.user_credentials.create :credential_name => 'code-foo', :credential_value => 'Agent.check = function() { this.log("ran it"); };' @agent.options['code'] = 'credential:code-foo' @agent.save! end it "accepts credentials" do @agent.check expect(AgentLog.last.message).to eq("ran it") end it "logs an error when the credential goes away" do @agent.user.user_credentials.delete_all @agent.reload.check expect(AgentLog.last.message).to eq("Unable to find credential") end end describe "error handling" do it "should log an error when V8 has issues" do @agent.options['code'] = 'syntax error!' @agent.save! expect { expect { @agent.check }.not_to raise_error }.to change { AgentLog.count }.by(1) expect(AgentLog.last.message).to match(/Unexpected identifier/) expect(AgentLog.last.level).to eq(4) end it "should log an error when JavaScript throws" do @agent.options['code'] = 'Agent.check = function() { throw "oh no"; };' @agent.save! expect { expect { @agent.check }.not_to raise_error }.to change { AgentLog.count }.by(1) expect(AgentLog.last.message).to match(/oh no/) expect(AgentLog.last.level).to eq(4) end it "won't store NaNs" do @agent.options['code'] = 'Agent.check = function() { this.memory("foo", NaN); };' @agent.save! @agent.check expect(@agent.memory['foo']).to eq('NaN') # string @agent.save! expect { @agent.reload.memory }.not_to raise_error end end describe "creating events" do it "creates events with this.createEvent in the JavaScript environment" do @agent.options['code'] = 'Agent.check = function() { this.createEvent({ message: "This is an event!", stuff: { foo: 5 } }); };' @agent.save! expect { expect { @agent.check }.not_to change { AgentLog.count } }.to change { Event.count }.by(1) created_event = @agent.events.last expect(created_event.payload).to eq({ 'message' => "This is an event!", 'stuff' => { 'foo' => 5 } }) end end describe "logging" do it "can output AgentLogs with this.log and this.error in the JavaScript environment" do @agent.options['code'] = 'Agent.check = function() { this.log("woah"); this.error("WOAH!"); };' @agent.save! expect { expect { @agent.check }.not_to raise_error }.to change { AgentLog.count }.by(2) log1, log2 = AgentLog.last(2) expect(log1.message).to eq("woah") expect(log1.level).to eq(3) expect(log2.message).to eq("WOAH!") expect(log2.level).to eq(4) end end describe "getting incoming events" do it "can access incoming events in the JavaScript enviroment via this.incomingEvents" do event = Event.new event.agent = agents(:bob_rain_notifier_agent) event.payload = { :data => "Something you should know about" } event.save! event.reload @agent.options['code'] = <<-JS Agent.receive = function() { var events = this.incomingEvents(); for(var i = 0; i < events.length; i++) { this.createEvent({ 'message': 'I got an event!', 'event_was': events[i].payload }); } } JS @agent.save! expect { expect { @agent.receive([events(:bob_website_agent_event), event]) }.not_to change { AgentLog.count } }.to change { Event.count }.by(2) created_event = @agent.events.first expect(created_event.payload).to eq({ 'message' => "I got an event!", 'event_was' => { 'data' => "Something you should know about" } }) end end describe "getting and setting memory, getting options" do it "can access options via this.options and work with memory via this.memory" do @agent.options['code'] = <<-JS Agent.check = function() { if (this.options('make_event')) { var callCount = this.memory('callCount') || 0; this.memory('callCount', callCount + 1); } }; JS @agent.save! expect { expect { @agent.check expect(@agent.memory['callCount']).not_to be_present @agent.options['make_event'] = true @agent.check expect(@agent.memory['callCount']).to eq(1) @agent.check expect(@agent.memory['callCount']).to eq(2) @agent.memory['callCount'] = 20 @agent.check expect(@agent.memory['callCount']).to eq(21) }.not_to change { AgentLog.count } }.not_to change { Event.count } end end end end