UNPKG

dpaw-brocket-prs-plugin

Version:

PRS plugin for the brocket network

665 lines (633 loc) 18.5 kB
'use strict'; let u = require('dpaw-brocket-utility'); let Kefir = require('kefir'); let log = u.log; let chai = require('chai'); let assert = chai.assert; let eq = chai.assert.equal; let ok = chai.assert.ok; let _throw = chai.assert.throw; let Plugin = require('../src/index'); let Mod = Plugin.module; var Suite = describe('Plugin', function() { describe('export', function () { it('should have a module', () => assert.ok(Plugin)); it('is not Bizarro', () => eq(1, 1, 'Or maybe its expected')); }); describe('setupRelay', () => { it('module should have a setupRelay function', () => ok(Mod.setupRelay)); it('should raise a TypeError if self object not provided', () => _throw(() => Mod.setupRelay(), /_self/)); it('should attach a relay if one is needed', () => { let self = {}; Mod.setupRelay(self, {autoConnect: false}); ok(self.relay); }); it('should use an injected relay if provided', () => { let _self = {ackRegistry: new Map()}; let relay = mock_relay(); Mod.setupRelay(_self, {autoConnect: false}, relay); eq(relay, _self.relay); }); }); describe('#init', () => { let $ = require('jquery'); var e; let defaultOpts = { baseAddr: "wss://brocket.dpaw.wa.gov.au", channel: 'dendrite', appID: 'prs', userID: 'gavinco', autoConnect: false }; beforeEach('create export object', () => { e = Object.create(Plugin); }); // afterEach('destroy export object', () => { // e.destroy(); // }); it('should have an init method', () => ok(e.init, 'no method')); // it('should have a relay object', (done) => { ok(e.relay); }); // it('should fail without opts', () => _throw(u.partial())) // it('should attach a relay', () => {ok(e.relay);}); }); describe('#destroy', () => { function mock_relay() { let relay = {called: false}; relay.destroy = function (){this.called = true;}; return relay; } it('should have a destroy method', () => ok(Plugin.destroy, 'no method')); it('should call destroy on its relay', () => { let e = Object.create(Plugin); e.init({autoConnect: false}); e.relay = mock_relay(); e.destroy(); assert(e.relay.called); }); }); describe('#harvest', () => { it('should have a private _harvest method', () => assert.ok(Plugin._harvest, 'no method')); it('should fail with no map', () => { assert.throw(u.partial(Plugin._harvest, [null, {}]), TypeError); }); it('should fail with no geojson', () => { _throw(u.partial(Plugin._harvest, [{getBounds(){}}, null]), TypeError); }); }); describe('#transform', () => { it('should have a private _transform method', () => ok(Plugin._transform, 'no method')); it('should fail with no appMap', () => _throw(u.partial(Plugin._transform, [null]), TypeError)); }); describe('acknowledgement', () => { var e; let defaultOpts = { baseAddr: "wss://brocket.dpaw.wa.gov.au", channel: 'dendrite', appID: 'prs', userID: 'gavinco', autoConnect: false }; function mock_relay() { let relay = {}; relay.destroy = function () {}; relay.connect = function () {}; relay.dispatch = function(msg) { this.receive(msg); }; relay.receive = function(msg) { this.received = true; }; return relay; } beforeEach('create export object', () => { e = Object.create(Plugin); e.init(defaultOpts); e.relay = mock_relay(); e.connect(); }); afterEach('destroy export object', () => { e.destroy(); }); it('should listen for messages', () => { e.dispatch(1); assert(e.relay.received); }); it('should distinguish own messages'); it('should popup on echo'); it('should popup on timeout'); }); describe('plugin streams', () => { it('should have a message stream', (done) => { /* NB. Using Kefir streams */ function mock_relay() { let relay = {called: false}; let msg = { tag: 'qgis:map:transport_map', payload: null, ack: 'abc123' }; relay.destroy = function () {}; relay.connect = function () {}; relay.dispatch = function(msg) { this.receive(msg); }; relay.receive = function(msg) { this.received = true; }; relay.message$ = Kefir.later(0, msg); return relay; } let p = Object.create(Plugin); p.init({autoConnect: false}); p.relay = mock_relay(); p.connect(); p.message$ = p.relay.message$; ok(p.message$); p.message$.onValue( msg => done() ); }); }); describe('layer names', () => { it('should use referral id for layer id', () => { const appMap = {bounds: example.one.bounds, features: example.one.features}; let urmap = Plugin._transform(appMap); eq(urmap.layout.layers[0], 3); }); it('should add the correct layer projection', () => { const appMap = { bounds: example.one.bounds, features: example.one.features }; let urmap = Plugin._transform(appMap); let layer_id = urmap.layout.layers[0]; let layer = urmap.featureLayers[layer_id]; let proj = layer.projection; eq(proj, "EPSG:4326"); }); it('should maintain the correct path reference', () =>{ const appMap = {bounds: example.one.bounds, features: example.one.features}; let urmap = Plugin._transform(appMap); let id = urmap.layout.layers[0]; let path = urmap.featureLayers[id].path; assert.deepEqual(path, ['root', 'data', 3]); }); it('should use referral id in name', () => { const appMap = {bounds: example.one.bounds, features: example.one.features}; let urmap = Plugin._transform(appMap); let id = urmap.layout.layers[0]; let name = urmap.featureLayers[id].layer_name; eq(name, 'referral_3'); }); }); describe('acknowledgements', () => { it('should assoc a sync request', () => { let msg = Mod.assocSyn({}); ok(msg.syn); }); it('should register an ack', () => { let reg = new Map(); let msg = {cuid: 'abc', syn: true}; Mod.registerAck(reg, msg); ok(reg.has('abc')); }); it('should callback on ack', (done) => { let reg = new Map([ ['abc', [() => done()]] ]); let msg = {ack: 'abc'}; Mod.acknowledge(reg, msg); }); it('should set an alert timeout', (done) => { let id = Mod.setCallbackTimeout(done, 0); }); it('should clear a timeout', (done) => { let reg = new Map(); let msg = {cuid: 'abc', syn: true}; let id = Mod.setCallbackTimeout( () => done('timeout was not cleared'), 0); Mod.registerAck(reg, msg, Mod.makeClearTimeout(id)); Mod.acknowledge(reg, {ack: 'abc'}); setTimeout(done, 1); }); }); }); var example = { one: { bounds: { "_southWest": { "lat":-32.04106762168929, "lng":115.96337020397185 }, "_northEast": { "lat":-32.03888491769794, "lng":115.96822500228882 } }, features: { "type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "EPSG:4326" } }, "features": [ { "geometry": { "type": "Polygon", "coordinates": [ [ [ 115.96568155, -32.03995522 ], [ 115.9653605, -32.04041725 ], [ 115.96562452000002, -32.04055368 ], [ 115.96577269999999, -32.04034545 ], [ 115.9657812, -32.04033352 ], [ 115.966057, -32.04047597 ], [ 115.96616872, -32.04031887 ], [ 115.96614987, -32.04024062 ], [ 115.96593883, -32.040112 ], [ 115.96568155, -32.03995522 ] ] ] }, "type": "Feature", "properties": { "cadastre_obj_id": null, "road_name": "RUPERT", "address_suffix": "", "landuse": null, "effective_to": null, "lot_desc": null, "created": "2015-01-11T15:51:24.232Z", "creator": 237, "address_no": 40, "modified": "2015-01-11T15:51:24.236Z", "address_string": "40 (lot 181) rupert st kenwick 6107", "lot_no": "181", "reserve": null, "locality": "KENWICK", "strata_lot_no": null, "referral": 3, "road_suffix": "ST", "modifier": 237, "strata_lot_desc": null, "postcode": "6107" } }, { "geometry": { "type": "Polygon", "coordinates": [ [ [ 115.96527488, -32.04004355 ], [ 115.96510921999999, -32.0402874 ], [ 115.9653605, -32.04041725 ], [ 115.96568155, -32.03995522 ], [ 115.96573602, -32.03987687 ], [ 115.96549, -32.0397269 ], [ 115.96527488, -32.04004355 ] ] ] }, "type": "Feature", "properties": { "cadastre_obj_id": null, "road_name": "RUPERT", "address_suffix": "", "landuse": null, "effective_to": null, "lot_desc": null, "created": "2015-01-11T15:41:58.154Z", "creator": 237, "address_no": 36, "modified": "2015-01-11T15:41:58.160Z", "address_string": "36 (lot 17) rupert st kenwick 6107", "lot_no": "17", "reserve": null, "locality": "KENWICK", "strata_lot_no": null, "referral": 3, "road_suffix": "ST", "modifier": 237, "strata_lot_desc": null, "postcode": "6107" } }, { "geometry": { "type": "Polygon", "coordinates": [ [ [ 115.96705298, -32.04067973 ], [ 115.96715663, -32.04035573 ], [ 115.96558411999999, -32.03927577 ], [ 115.96522898, -32.03956777 ], [ 115.96533497, -32.03963238 ], [ 115.96549, -32.0397269 ], [ 115.96573602, -32.03987687 ], [ 115.96599417, -32.04003427 ], [ 115.96625713, -32.04019457 ], [ 115.96705298, -32.04067973 ] ] ] }, "type": "Feature", "properties": { "cadastre_obj_id": null, "road_name": "ROYAL", "address_suffix": "", "landuse": null, "effective_to": null, "lot_desc": null, "created": "2015-01-06T22:32:53.344Z", "creator": 237, "address_no": 39, "modified": "2015-01-06T22:32:53.348Z", "address_string": "39 (lot 56) royal st kenwick 6107", "lot_no": "56", "reserve": null, "locality": "KENWICK", "strata_lot_no": null, "referral": 3, "road_suffix": "ST", "modifier": 237, "strata_lot_desc": null, "postcode": "6107" } }, { "geometry": { "type": "Polygon", "coordinates": [ [ [ 115.96504928, -32.0399294 ], [ 115.96487907, -32.04016845 ], [ 115.96510921999999, -32.0402874 ], [ 115.96527488, -32.04004355 ], [ 115.96549, -32.0397269 ], [ 115.96533497, -32.03963238 ], [ 115.9651036, -32.03995745 ], [ 115.96504928, -32.0399294 ] ] ] }, "type": "Feature", "properties": { "cadastre_obj_id": null, "road_name": "RUPERT", "address_suffix": "", "landuse": null, "effective_to": null, "lot_desc": null, "created": "2014-12-17T16:01:17.015Z", "creator": 237, "address_no": 34, "modified": "2014-12-17T16:01:17.017Z", "address_string": "34 (lot 21) rupert st kenwick 6107", "lot_no": "21", "reserve": null, "locality": "KENWICK", "strata_lot_no": null, "referral": 3, "road_suffix": "ST", "modifier": 237, "strata_lot_desc": null, "postcode": "6107" } }, { "geometry": { "type": "Polygon", "coordinates": [ [ [ 115.96468815, -32.03973203 ], [ 115.96443598, -32.03993948 ], [ 115.96464532999998, -32.04004767 ], [ 115.96489753, -32.03984028 ], [ 115.96468815, -32.03973203 ] ] ] }, "type": "Feature", "properties": { "cadastre_obj_id": null, "road_name": "RUPERT", "address_suffix": "", "landuse": null, "effective_to": null, "lot_desc": null, "created": "2014-12-02T22:43:08.158Z", "creator": 237, "address_no": 30, "modified": "2014-12-02T22:43:08.161Z", "address_string": "30 (lot 58) rupert st kenwick 6107", "lot_no": "58", "reserve": null, "locality": "KENWICK", "strata_lot_no": null, "referral": 3, "road_suffix": "ST", "modifier": 237, "strata_lot_desc": null, "postcode": "6107" } }, { "geometry": { "type": "Polygon", "coordinates": [ [ [ 115.96464532999998, -32.04004767 ], [ 115.96487907, -32.04016845 ], [ 115.96504928, -32.0399294 ], [ 115.9651036, -32.03995745 ], [ 115.96533497, -32.03963238 ], [ 115.96522898, -32.03956777 ], [ 115.96489753, -32.03984028 ], [ 115.96464532999998, -32.04004767 ] ] ] }, "type": "Feature", "properties": { "cadastre_obj_id": null, "road_name": "RUPERT", "address_suffix": "", "landuse": null, "effective_to": null, "lot_desc": null, "created": "2013-02-03T20:25:50.931Z", "creator": 1, "address_no": 32, "modified": "2013-02-03T20:25:50.934Z", "address_string": "32 (lot 20) rupert st kenwick 6107", "lot_no": "20", "reserve": null, "locality": "KENWICK", "strata_lot_no": null, "referral": 3, "road_suffix": "ST", "modifier": 1, "strata_lot_desc": null, "postcode": "6107" } } ] } } }; function mock_relay() { let relay = {called: false}; let msg = { tag: 'qgis:map:transport_map', payload: null, ack: 'abc123' }; relay.init = function () {}; relay.on = function () {}; relay.inject_kefir = function () {}; relay.destroy = function () {}; relay.connect = function () {}; relay.dispatch = function(msg) { this.receive(msg); }; relay.receive = function(msg) { this.received = true; }; relay.message$ = Kefir.later(0, msg); return relay; }