From 572a3d3cc7e756d5c5c0d44bad4d46ad47906af5 Mon Sep 17 00:00:00 2001 From: Adam Magaluk Date: Tue, 19 Jan 2016 10:48:41 -0700 Subject: [PATCH 1/2] Added failing test for virtual_device for _update called with proper formatted args --- test/test_virtual_device.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/test_virtual_device.js b/test/test_virtual_device.js index c5c5080..3b7b03f 100644 --- a/test/test_virtual_device.js +++ b/test/test_virtual_device.js @@ -91,6 +91,31 @@ describe('Virtual Device', function() { }); }); + it('_update should always be called with data.actions in proper format', function(done) { + var called = 0; + var orig = vdevice._update; + vdevice._update = function(data) { + called++; + assert(Array.isArray(data.actions)); + data.actions.forEach(function(action) { + assert(action.class); + assert(action.name); + assert(action.method); + assert(action.href); + assert(action.fields); + }); + orig.apply(vdevice, arguments); + + // _update is called twice on transitions. Once for the return of the transition http POST and again + // for the log topic update. + if (called === 2) { + done(); + } + }; + + vdevice.call('change'); + }); + it('call should work without arguments', function(done) { vdevice.call('change', function(err) { assert.equal(err, null); From a7088a10144d080776181871666964aa258e50dd Mon Sep 17 00:00:00 2001 From: Adam Magaluk Date: Tue, 19 Jan 2016 10:48:53 -0700 Subject: [PATCH 2/2] virtual_device log update properly formats actions before updating state --- lib/http_server.js | 4 ++++ lib/virtual_device.js | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/http_server.js b/lib/http_server.js index f1b764f..924f9bf 100644 --- a/lib/http_server.js +++ b/lib/http_server.js @@ -84,6 +84,10 @@ var ZettaHttpServer = module.exports = function(zettaInstance, options) { name = decodeURI(name); self.zetta.log.emit('log', 'http_server', 'Websocket connection for peer "' + name + '" established.'); + // Include ._env and ._loader on websocket, allows argo formatters to work used in virtual_device build actions. + var host = ws.upgradeReq.headers['host'] + self.wireUpWebSocketForEvent(ws, host, '/servers/' + name); + if (self.peers[name] && self.peers[name].state !== PeerSocket.DISCONNECTED) { // peer already connected or connecting ws.close(4000, 'peer already connected'); diff --git a/lib/virtual_device.js b/lib/virtual_device.js index f1c81a0..dbcd57f 100644 --- a/lib/virtual_device.js +++ b/lib/virtual_device.js @@ -3,6 +3,7 @@ var util = require('util'); var ReadableStream = require('stream').Readable; var EventEmitter = require('events').EventEmitter; var rels = require('zetta-rels'); +var buildDeviceActions = require('./api_formats/siren/device.siren').buildActions; var VirtualStream = module.exports = function(topic, socket, options) { ReadableStream.call(this, options); @@ -41,6 +42,10 @@ var VirtualDevice = module.exports = function(entity, peerSocket) { }); this._socket.on(logTopic, function(data) { + // Format data.actions to siren action format + data.actions = buildDeviceActions(data.properties.id, self._socket.ws._env, self._socket.ws._loader, data.transitions); + delete data.transitions; + self._update(data); self._eventEmitter.emit(data.transition); });