Issues (7)

test/fullsignalk.js (1 issue)

1
/* global describe, it */
2
const chai = require('chai')
3
const dirtyChai = require('dirty-chai')
4
chai.use(dirtyChai)
5
chai.should()
6
chai.use(require('../dist/').chaiModule)
7
8
const FullSignalK = require('../dist/fullsignalk')
9
const skApi = require('../dist/index.js')
10
11
var deltaA = {
12
  'updates': [{
13
    'source': {
14
      'label': 'n2kFromFile',
15
      'type': 'NMEA2000',
16
      'pgn': 129038,
17
      'src': '43'
18
    },
19
    'timestamp': '2014-08-15T19:03:21.532Z',
20
    'values': [{
21
      'path': 'navigation.speedOverGround',
22
      'value': 7.09
23
    }, {
24
      'path': 'navigation.courseOverGroundTrue',
25
      'value': 4.8171
26
    }, {
27
      'path': 'navigation.position',
28
      'value': {
29
        'longitude': 25.4398883,
30
        'latitude': 59.969895
31
      }
32
    }]
33
  }],
34
  'context': 'vessels.urn:mrn:imo:mmsi:200000000'
35
}
36
37
describe('FullSignalK', function () {
38
  describe('validations', function () {
39
    it('.validSignalK passes valid signalK', function () {
40
      var validSk = {
41
        'vessels': {
42
          'urn:mrn:imo:mmsi:200000000': {
43
            'mmsi': '200000000'}},
44
        'version': '1.0.0',
45
        'self': 'urn:mrn:imo:mmsi:200000000'}
46
47
      validSk.should.be.validSignalK()
48
    })
49
    it('.validSignalK fails invalid signalK', function () {
50
      var invalidSk = {
51
        'vessels': {
52
          'urn:mrn:imo:mmXX:200000000': {
53
            'mmsi': '200000000'}},
54
        'version': '1.0.0',
55
        'self': 'urn:mrn:imo:mmsi:200000000'}
56
57
      invalidSk.should.not.be.validSignalK()
58
    })
59
    it('.validSignalKDelta passes valid delta', function () {
60
      var validDelta = {
61
        'context': 'bar',
62
        'updates': [{
63
          'source': {
64
            'label': '',
65
            'type': 'NMEA0183',
66
            'sentence': 'GLL',
67
            'talker': 'II'
68
          },
69
          'timestamp': '2013-10-08T15:47:28.263Z',
70
          'values': [{'path': 'a.b.c', 'value': 1234}]
71
        }]
72
      }
73
      validDelta.should.be.validSignalKDelta()
74
    })
75
  })
76
  // TODO add full set of validation tests in here
77
78
  it('deltaToFull with object value should produce valid SignalK', function () {
79
    var full = skApi.deltaToFull(deltaA)
80
    console.log(JSON.stringify(full))
0 ignored issues
show
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
81
    full.vessels['urn:mrn:imo:mmsi:200000000'].navigation.position.value.should.have.property('longitude')
82
    full.vessels['urn:mrn:imo:mmsi:200000000'].navigation.position.should.have.property('$source')
83
    full.should.be.validSignalK()
84
  })
85
86
  it('addDelta with object value should produce full tree leaf without the .value', function () {
87
    var fullSignalK = new FullSignalK()
88
    fullSignalK.addDelta(deltaA)
89
    fullSignalK.retrieve().vessels['urn:mrn:imo:mmsi:200000000'].navigation.position.value.should.have.property('longitude')
90
    fullSignalK.retrieve().vessels['urn:mrn:imo:mmsi:200000000'].navigation.position.should.have.property('$source')
91
  })
92
93
  it('Two deltas from different sources results in values structure', function () {
94
    var delta = {
95
      'updates': [{
96
        'source': {
97
          'label': 'n2kFromFile',
98
          'type': 'NMEA2000',
99
          'pgn': 129038,
100
          'src': '43'
101
        },
102
        'timestamp': '2014-08-15T19:03:21.532Z',
103
        'values': [{
104
          'path': 'navigation.speedOverGround',
105
          'value': 7.09
106
        }]
107
      }],
108
      'context': 'vessels.foo'
109
    }
110
    var fullSignalK = new FullSignalK()
111
    fullSignalK.addDelta(delta)
112
    delta.updates[0].source.src = 48
113
    delta.updates[0].values[0].value = 8
114
    fullSignalK.addDelta(delta)
115
    fullSignalK.retrieve().vessels.foo.navigation.speedOverGround.should.have.property('value', 8)
116
    fullSignalK.retrieve().vessels.foo.navigation.speedOverGround.should.have.property('$source')
117
    fullSignalK.retrieve().vessels.foo.navigation.speedOverGround.values['n2kFromFile.43'].should.have.property('value', 7.09)
118
    fullSignalK.retrieve().vessels.foo.navigation.speedOverGround.values['n2kFromFile.48'].should.have.property('value', 8)
119
  })
120
121
  it('AIS delta produces valid Signal K', function () {
122
    this.timeout(4000) // first use of schema validation may have to download schemas so increase timeout from 2 to 4 seconds
123
    var aisDelta = {
124
      'updates': [{
125
        'source': {
126
          'label': 'N2K-1',
127
          'type': 'NMEA2000',
128
          'pgn': 129038,
129
          'src': '43'
130
        },
131
        'timestamp': '2014-08-15T19:00:15.402Z',
132
        'values': [{
133
          'path': 'navigation.speedOverGround',
134
          'value': 14.81
135
        }, {
136
          'path': 'navigation.courseOverGroundTrue',
137
          'value': 3.4889
138
        }, {
139
          'path': 'navigation.position',
140
          'value': {
141
            'longitude': 24.8142433,
142
            'latitude': 59.865655
143
          }
144
        }]
145
      }],
146
      'context': 'vessels.urn:mrn:imo:mmsi:276780000'
147
    }
148
    var fullSignalK = new FullSignalK('urn:mrn:imo:mmsi:276799999', 'mmsi')
149
    fullSignalK.addDelta(aisDelta)
150
    fullSignalK.retrieve().should.be.validSignalK()
151
  })
152
153
  it('Delta with empty path sets content under root', function () {
154
    var msg = {
155
      'updates': [{
156
        'source': {
157
          'label': 'n2kFromFile',
158
          'type': 'NMEA2000',
159
          'pgn': 129794,
160
          'src': '43'
161
        },
162
        'timestamp': '2014-08-15T19:02:31.507Z',
163
        'values': [{
164
          'path': '',
165
          'value': {
166
            'name': 'WRANGO'
167
          }
168
        }]
169
      }],
170
      'context': 'vessels.urn:mrn:imo:mmsi:276810000'
171
    }
172
    var fullSignalK = new FullSignalK()
173
    fullSignalK.addDelta(msg)
174
    var vessel = fullSignalK.retrieve().vessels['urn:mrn:imo:mmsi:276810000']
175
    vessel.should.have.property('name', 'WRANGO')
176
    vessel.should.not.have.property('$source')
177
    vessel.should.not.have.property('timestamp')
178
    vessel.should.not.have.property('pgn')
179
  })
180
181
  it('Delta with instance produces proper sources hierarchy', function () {
182
    var msg = {
183
      'updates': [{
184
        'source': {
185
          'label': 'N2K',
186
          'type': 'NMEA2000',
187
          'pgn': 130312,
188
          'src': '36',
189
          'instance': '0'
190
        },
191
        'timestamp': '2015-01-15T16:15:19.628Z',
192
        'values': [{
193
          'path': 'environment.water.temperature',
194
          'value': 15.2
195
        }]
196
      }],
197
      'context': 'vessels.urn:mrn:imo:mmsi:200000000'
198
    }
199
    var fullSignalK = new FullSignalK()
200
    fullSignalK.addDelta(msg)
201
    var full = fullSignalK.retrieve()
202
    var vessel = full.vessels['urn:mrn:imo:mmsi:200000000']
203
    vessel.environment.water.temperature.should.have.property('value', 15.2)
204
    full.sources.should.have.property('N2K')
205
    full.sources['N2K'].should.have.property('36')
206
    full.sources['N2K']['36'].should.have.property('0')
207
  })
208
209
  it('Delta with $source produces sources hierarchy and correct $source reference', function () {
210
    var msg = {
211
      'context': 'vessels.urn:mrn:imo:mmsi:200000000',
212
      'updates': [{
213
        '$source': '1W.0316013faeff',
214
        'values': [{
215
          'path': 'propulsion.engine1.temperature',
216
          'value': 301.837
217
        }]
218
      }]
219
    }
220
221
    var fullSignalK = new FullSignalK('urn:mrn:imo:mmsi:200000000')
222
    fullSignalK.addDelta(msg)
223
    var full = fullSignalK.retrieve()
224
    full.sources.should.have.property('1W')
225
    full.sources['1W'].should.have.property('0316013faeff')
226
    var vessel = full.vessels['urn:mrn:imo:mmsi:200000000']
227
    vessel.propulsion.engine1.temperature.should.have.property('$source', '1W.0316013faeff')
228
    // full.should.be.validSignalK
229
    // TODO make this work!
230
  })
231
232
  it('MMSI self is set correctly in full tree', function () {
233
    var fullSignalK = new FullSignalK('urn:mrn:imo:mmsi:200000000', null, {})
234
    fullSignalK.retrieve().self.should.equal('vessels.urn:mrn:imo:mmsi:200000000')
235
  })
236
237
  it('UUID self is set correctly in full tree', function () {
238
    var fullSignalK = new FullSignalK('urn:mrn:signalk:uuid:00000000-0000-4000-800000000000', null, {})
239
    fullSignalK.retrieve().self.should.equal('vessels.urn:mrn:signalk:uuid:00000000-0000-4000-800000000000')
240
  })
241
242
  // bulk testing all variant inputs valid and not to check initialisatino works as it should
243
  var ids = [
244
    {
245
      types: ['', 'nonesense', 'a.b', {}, [], 5],
246
      id: 'http://something.com',
247
      expect: {group: ['vessels', 'vessels', 'vessels', 'vessels', 'vessels', 'vessels'], idType: 'url', idValue: 'http://something.com'}
248
    }, {
249
      types: ['vessels', 'aton', 'sar', 'aircraft', 'vessel'],
250
      id: 'urn:mrn:signalk:uuid:00000000-0000-4000-800000000000',
251
      expect: {group: [null, null, null, null, null]}
252
    }, {
253
      types: ['vessels', 'aton', 'sar', 'aircraft', 'vessel'], // the types to test - '' and vessel should default to vessels
254
      id: 'http://something.com',
255
      expect: {group: ['vessels', 'aton', 'sar', 'aircraft', 'vessels'], idType: 'url', idValue: 'http://something.com'}
256
    }, {
257
      types: ['vessels', 'aton', 'sar', 'aircraft', 'vessel'],
258
      id: 'https://aurl.com/something#w=3',
259
      expect: {group: ['vessels', 'aton', 'sar', 'aircraft', 'vessels'], idField: 'url', idFieldValue: 'https://aurl.com/something#w=3'}
260
    }, {
261
      types: ['vessels', 'aton', 'sar', 'aircraft', 'vessel'],
262
      id: 'mailto:[email protected]',
263
      expect: {group: ['vessels', 'aton', 'sar', 'aircraft', 'vessels'], idField: 'url', idFieldValue: 'mailto:[email protected]'}
264
    }, {
265
      types: ['vessels', 'aton', 'sar', 'aircraft', 'vessel'],
266
      id: 'tel: +11 (0) 1111 111 111',
267
      expect: {group: ['vessels', 'aton', 'sar', 'aircraft', 'vessels'], idField: 'url', idFieldValue: 'tel: +11 (0) 1111 111 111'}
268
    }, {
269
      types: ['vessels', 'aton', 'sar', 'aircraft', 'vessel'],
270
      id: 'urn:mrn:signalk:uuid:00000000-0000-4000-800000000000',
271
      expect: {group: ['vessels', 'aton', 'sar', 'aircraft', 'vessels'], idField: 'uuid', idFieldValue: 'urn:mrn:signalk:uuid:00000000-0000-4000-800000000000'}
272
    }, {
273
      types: ['vessels', 'vessel'],
274
      id: 'urn:mrn:imo:mmsi:200000000',
275
      expect: {group: ['vessels', 'vessels'], idField: 'mmsi', idFieldValue: '200000000'}
276
    }, {
277
      types: ['aircraft'],
278
      id: 'urn:mrn:imo:mmsi:100000000',
279
      expect: {group: ['aircraft'], idField: 'mmsi', idFieldValue: '100000000'}
280
    }, {
281
      types: ['sar'],
282
      id: 'urn:mrn:imo:mmsi:970000000',
283
      expect: {group: ['sar'], idField: 'mmsi', idFieldValue: '970000000'}
284
    }, {
285
      types: ['aton'],
286
      id: 'urn:mrn:imo:mmsi:990000000',
287
      expect: {group: ['aton'], idField: 'mmsi', idFieldValue: '990000000'}
288
    }
289
  ]
290
291
  var id
292
  for (var i = 0; i < ids.length; i++) {
293
    id = ids[i]
294
    for (var t = 0; t < id.types.length; t++) {
295
      var thisId = id
296
      var thisType = id.types[t]
297
      var thisGroup = id.expect.group[t]
298
      var sk = new skApi.FullSignalK(thisId.id, thisType)
299
      it('Initialisation with id: ' + thisId.id + ' and type: ' + thisType, function () {
300
        sk.retrieve()[thisGroup][thisId.id][thisId.expect.idField].should.equal(thisId.expect.idFieldValue)
301
      })
302
      it('... and is valid full Signal K', function () {
303
        sk.retrieve().should.be.validSignalK()
304
      })
305
    }
306
  }
307
/*
308
  it('Initialisation with id: "nonesense" and type: "vessel" should produce {}', function () {
309
    var sk = new skApi.FullSignalK("nonesense", "vessel")
310
    sk.retrieve().should.deep.equal({})
311
  })
312
313
  it('Initialisation with id: "urn:mrn:imo:mmsi:230099999" and type: "V3553L" should produce {}', function () {
314
    var sk = new skApi.FullSignalK("urn:mrn:imo:mmsi:230099999", "V3553L")
315
    sk.retrieve().should.deep.equal({})
316
  })
317
318
  it('Initialisation with id: "" and type: "vessel" should produce {}', function () {
319
    var sk = new skApi.FullSignalK("", "vessel")
320
    sk.retrieve().should.deep.equal({})
321
  })
322
323
  it('Initialisation with id: "" and type: "vessel" should produce {}', function () {
324
    var sk = new skApi.FullSignalK("urn:mrn:imo:mmsi:230099999", "V3553L")
325
    sk.retrieve().should.deep.equal({})
326
  })
327
328
  it('Initialisation with id: "urn:mrn:imo:mmsi:230099999" and type: "aton" should produce {} (incorrect mmsi number zone)', function () {
329
    var sk = new skApi.FullSignalK("urn:mrn:imo:mmsi:230099999", "aton")
330
    sk.retrieve().should.deep.equal({})
331
  })
332
333
  it('Initialisation with id: "ftp:my.site.co.uk" and type: "vessel" should produce {} (invalid id)', function () {
334
    var sk = new skApi.FullSignalK("ftp:my.site.co.uk", "vessel")
335
    sk.retrieve().should.deep.equal({})
336
  })
337
*/
338
})
339