-
-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathsourceLabels.test.ts
More file actions
841 lines (778 loc) · 23.9 KB
/
Copy pathsourceLabels.test.ts
File metadata and controls
841 lines (778 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
import { describe, it, expect } from 'vitest'
import {
parseSourceRef,
getDeviceInfo,
buildSourceLabel,
buildSourceLabelMap,
canonicaliseSourceRef,
detectInstanceConflicts,
isProprietaryPGN,
isRemoveSourceFailure,
type SourcesData,
type N2kDeviceEntry
} from './sourceLabels'
// Realistic sources API response shape (subset of a real server)
const sourcesData: SourcesData = {
YDEN02: {
type: 'NMEA2000',
'44': {
n2k: {
src: '44',
manufacturerCode: 'Furuno',
canName: '00:11:22:33:44:55:66:77'
}
},
'37': {
n2k: {
src: '37',
manufacturerCode: 'Victron Energy',
modelId: 'SmartSolar Charger MPPT 100/50'
}
},
'73': {
n2k: {
src: '73',
manufacturerCode: 'Maretron'
}
},
'226': {
n2k: {
src: '226',
manufacturerCode: 'Victron Energy',
modelId: 'Quattro 24/8000/200-2x100'
}
},
'99': {
n2k: {
src: '99'
}
}
},
SERIAL1: {
type: 'NMEA0183',
sentences: ['RMC', 'GGA']
}
}
describe('parseSourceRef', () => {
it('parses N2K sourceRef into connection and src', () => {
expect(parseSourceRef('YDEN02.44')).toEqual({
connection: 'YDEN02',
src: '44'
})
})
it('splits on first dot only (connection names do not contain dots)', () => {
expect(parseSourceRef('some.host.99')).toEqual({
connection: 'some',
src: 'host.99'
})
})
it('returns null for plugin sources without a dot', () => {
expect(parseSourceRef('derived-data')).toBeNull()
expect(parseSourceRef('autopilot')).toBeNull()
})
it('returns null for empty string', () => {
expect(parseSourceRef('')).toBeNull()
})
})
describe('getDeviceInfo', () => {
it('returns n2k info for a known N2K device', () => {
const info = getDeviceInfo('YDEN02.44', sourcesData)
expect(info).toEqual({
src: '44',
manufacturerCode: 'Furuno',
canName: '00:11:22:33:44:55:66:77'
})
})
it('returns null for plugin sources', () => {
expect(getDeviceInfo('derived-data', sourcesData)).toBeNull()
})
it('returns null for unknown connection', () => {
expect(getDeviceInfo('UNKNOWN.44', sourcesData)).toBeNull()
})
it('returns null for unknown src address on known connection', () => {
expect(getDeviceInfo('YDEN02.999', sourcesData)).toBeNull()
})
it('returns null for NMEA0183 sources (no n2k sub-object)', () => {
expect(getDeviceInfo('SERIAL1.sentences', sourcesData)).toBeNull()
})
})
describe('buildSourceLabel', () => {
it('shows manufacturer + model when both available', () => {
expect(buildSourceLabel('YDEN02.37', sourcesData)).toBe(
'Victron Energy SmartSolar Charger MPPT 100/50 (YDEN02.37)'
)
expect(buildSourceLabel('YDEN02.226', sourcesData)).toBe(
'Victron Energy Quattro 24/8000/200-2x100 (YDEN02.226)'
)
})
it('shows manufacturer only when no model', () => {
expect(buildSourceLabel('YDEN02.44', sourcesData)).toBe(
'Furuno (YDEN02.44)'
)
expect(buildSourceLabel('YDEN02.73', sourcesData)).toBe(
'Maretron (YDEN02.73)'
)
})
it('returns raw sourceRef when no manufacturer and no model', () => {
expect(buildSourceLabel('YDEN02.99', sourcesData)).toBe('YDEN02.99')
})
it('returns raw sourceRef for plugin sources', () => {
expect(buildSourceLabel('derived-data', sourcesData)).toBe('derived-data')
})
it('returns raw sourceRef when sourcesData is null', () => {
expect(buildSourceLabel('YDEN02.44', null)).toBe('YDEN02.44')
})
it('returns raw sourceRef for unknown connections', () => {
expect(buildSourceLabel('UNKNOWN.1', sourcesData)).toBe('UNKNOWN.1')
})
it('uses sourceNames for ws device sources', () => {
const sourceNames = {
'ws.3d3e48a1-1185-2fe3-c494-1c1a9ee6f41f': 'sensesp-engines'
}
expect(
buildSourceLabel(
'ws.3d3e48a1-1185-2fe3-c494-1c1a9ee6f41f',
sourcesData,
sourceNames
)
).toBe('sensesp-engines (ws.3d3e48a1-1185-2fe3-c494-1c1a9ee6f41f)')
})
it('falls back to raw ref for ws sources missing from sourceNames', () => {
expect(buildSourceLabel('ws.unknown-device', sourcesData, {})).toBe(
'ws.unknown-device'
)
})
it('lets sourceNames override N2K-derived labels', () => {
expect(
buildSourceLabel('YDEN02.37', sourcesData, { 'YDEN02.37': 'My Charger' })
).toBe('My Charger (YDEN02.37)')
})
})
describe('canonicaliseSourceRef', () => {
it('translates numeric form to canName form when canName is known', () => {
expect(canonicaliseSourceRef('YDEN02.44', sourcesData)).toBe(
'YDEN02.00:11:22:33:44:55:66:77'
)
})
it('returns the ref unchanged when it is already in canName form', () => {
expect(
canonicaliseSourceRef('YDEN02.00:11:22:33:44:55:66:77', sourcesData)
).toBe('YDEN02.00:11:22:33:44:55:66:77')
})
it('returns the raw ref when the device has no canName', () => {
expect(canonicaliseSourceRef('YDEN02.37', sourcesData)).toBe('YDEN02.37')
})
it('returns the raw ref for unknown connections', () => {
expect(canonicaliseSourceRef('UNKNOWN.1', sourcesData)).toBe('UNKNOWN.1')
})
it('returns the raw ref for plugin sources (no dot)', () => {
expect(canonicaliseSourceRef('derived-data', sourcesData)).toBe(
'derived-data'
)
})
it('returns the raw ref when sourcesData is null', () => {
expect(canonicaliseSourceRef('YDEN02.44', null)).toBe('YDEN02.44')
})
})
describe('buildSourceLabelMap', () => {
it('builds a map of enriched labels for all known N2K devices', () => {
const map = buildSourceLabelMap(sourcesData)
// Device 44 has canName, so sourceRef uses it
expect(map.get('YDEN02.00:11:22:33:44:55:66:77')).toBe(
'Furuno (YDEN02.00:11:22:33:44:55:66:77)'
)
// Devices without canName fall back to numeric address
expect(map.get('YDEN02.37')).toBe(
'Victron Energy SmartSolar Charger MPPT 100/50 (YDEN02.37)'
)
expect(map.get('YDEN02.73')).toBe('Maretron (YDEN02.73)')
expect(map.get('YDEN02.226')).toBe(
'Victron Energy Quattro 24/8000/200-2x100 (YDEN02.226)'
)
})
it('excludes devices with no useful metadata', () => {
const map = buildSourceLabelMap(sourcesData)
expect(map.has('YDEN02.99')).toBe(false)
})
it('excludes non-N2K sources', () => {
const map = buildSourceLabelMap(sourcesData)
const serial1Keys = [...map.keys()].filter((k) => k.startsWith('SERIAL1'))
expect(serial1Keys).toHaveLength(0)
})
it('returns empty map for empty sourcesData', () => {
const map = buildSourceLabelMap({})
expect(map.size).toBe(0)
})
})
describe('detectInstanceConflicts', () => {
const makeDevice = (
overrides: Partial<N2kDeviceEntry> & {
sourceRef: string
connection: string
}
): N2kDeviceEntry => ({
src: '0',
...overrides
})
it('returns no conflicts when devices have different instances', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 0,
pgns: { '127505': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 1,
pgns: { '127505': '' }
})
]
expect(detectInstanceConflicts(devices)).toHaveLength(0)
})
it('detects conflict when two devices share instance and PGNs', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 0,
pgns: { '127505': '', '130312': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 0,
pgns: { '127505': '', '127506': '' }
})
]
const conflicts = detectInstanceConflicts(devices)
expect(conflicts).toHaveLength(1)
expect(conflicts[0].deviceInstance).toBe(0)
expect(conflicts[0].sharedPGNs).toEqual(['127505'])
expect(conflicts[0].deviceA.sourceRef).toBe('C.1')
expect(conflicts[0].deviceB.sourceRef).toBe('C.2')
})
it('returns no conflict when devices share instance but have disjoint PGNs', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 0,
pgns: { '127505': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 0,
pgns: { '130312': '' }
})
]
expect(detectInstanceConflicts(devices)).toHaveLength(0)
})
it('handles devices with no pgns', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 0
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 0
})
]
expect(detectInstanceConflicts(devices)).toHaveLength(0)
})
it('skips devices with undefined deviceInstance', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
pgns: { '127505': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
pgns: { '127505': '' }
})
]
expect(detectInstanceConflicts(devices)).toHaveLength(0)
})
it('ignores protocol/management PGNs (every device sends those)', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 0,
pgns: { '60928': '', '126996': '', '126993': '', '59904': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 0,
pgns: { '60928': '', '126996': '', '126993': '', '59904': '' }
})
]
expect(detectInstanceConflicts(devices)).toHaveLength(0)
})
it('detects conflict on data PGNs even when protocol PGNs also overlap', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 0,
pgns: { '60928': '', '126996': '', '127505': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 0,
pgns: { '60928': '', '126996': '', '127505': '' }
})
]
const conflicts = detectInstanceConflicts(devices)
expect(conflicts).toHaveLength(1)
// Only the data PGN should be listed, not protocol PGNs
expect(conflicts[0].sharedPGNs).toEqual(['127505'])
})
it('detects multiple conflicts in a group of three', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 5,
pgns: { '127505': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 5,
pgns: { '127505': '' }
}),
makeDevice({
sourceRef: 'C.3',
connection: 'C',
src: '3',
deviceInstance: 5,
pgns: { '127505': '' }
})
]
// 3 devices → 3 pairs: (1,2), (1,3), (2,3)
expect(detectInstanceConflicts(devices)).toHaveLength(3)
})
it('no conflict when devices share PGN but have different data instances', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 3,
pgns: { '127508': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 3,
pgns: { '127508': '' }
})
]
const pgnDataInstances = {
'C.1': { '127508': [0] },
'C.2': { '127508': [100, 101, 102, 103] }
}
expect(detectInstanceConflicts(devices, pgnDataInstances)).toHaveLength(0)
})
it('conflict when devices share PGN with overlapping data instances', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 3,
pgns: { '127508': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 3,
pgns: { '127508': '' }
})
]
const pgnDataInstances = {
'C.1': { '127508': [0, 1] },
'C.2': { '127508': [1, 2] }
}
const conflicts = detectInstanceConflicts(devices, pgnDataInstances)
expect(conflicts).toHaveLength(1)
expect(conflicts[0].sharedPGNs).toEqual(['127508'])
})
it('conservative fallback when pgnDataInstances not provided', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 3,
pgns: { '127508': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 3,
pgns: { '127508': '' }
})
]
// No pgnDataInstances → flags conservatively
expect(detectInstanceConflicts(devices)).toHaveLength(1)
})
it('mixed: PGN with non-overlapping instances excluded, PGN without data flagged', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 3,
pgns: { '127501': '', '127508': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 3,
pgns: { '127501': '', '127508': '' }
})
]
// 127501: different instances → no conflict
// 127508: no instance data → conservative flag
const pgnDataInstances = {
'C.1': { '127501': [20] },
'C.2': { '127501': [21] }
}
const conflicts = detectInstanceConflicts(devices, pgnDataInstances)
expect(conflicts).toHaveLength(1)
expect(conflicts[0].sharedPGNs).toEqual(['127508'])
})
it('uses pgnInstances from sources API over pgnDataInstances', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 3,
pgns: { '127505': '' },
pgnInstances: { '127505': [0] }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 3,
pgns: { '127505': '' },
pgnInstances: { '127505': [1] }
})
]
// pgnInstances on device takes priority — different instances → no conflict
expect(detectInstanceConflicts(devices)).toHaveLength(0)
})
it('no conflict when temp PGN has same instance but different source (compound key)', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '111',
deviceInstance: 0,
pgns: { '130312': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '109',
deviceInstance: 0,
pgns: { '130312': '' }
})
]
const pgnSourceKeys = {
'C.1': { '130312': ['0:Inside Temperature'] },
'C.2': { '130312': ['0:Main Cabin Temperature'] }
}
// Same instance 0 but different source → no conflict
expect(
detectInstanceConflicts(devices, undefined, pgnSourceKeys)
).toHaveLength(0)
})
it('conflict when temp PGN has same instance AND same source (compound key)', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '111',
deviceInstance: 0,
pgns: { '130312': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '109',
deviceInstance: 0,
pgns: { '130312': '' }
})
]
const pgnSourceKeys = {
'C.1': { '130312': ['0:Inside Temperature'] },
'C.2': { '130312': ['0:Inside Temperature'] }
}
const conflicts = detectInstanceConflicts(devices, undefined, pgnSourceKeys)
expect(conflicts).toHaveLength(1)
expect(conflicts[0].sharedPGNs).toEqual(['130312'])
})
it('temp PGN with no pgnSourceKeys data for either device clears the conflict', () => {
// Temp/humidity PGNs are keyed by full SK path. If neither device
// currently publishes the PGN to the SK tree, there is nothing to
// collide on — falling back to the bare device instance would
// re-introduce false positives for separately-keyed sources.
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '111',
deviceInstance: 0,
pgns: { '130312': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '109',
deviceInstance: 0,
pgns: { '130312': '' }
})
]
const pgnSourceKeys = {}
expect(
detectInstanceConflicts(devices, undefined, pgnSourceKeys)
).toHaveLength(0)
})
it('temp PGN with pgnSourceKeys for only one device clears the conflict', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '111',
deviceInstance: 0,
pgns: { '130312': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '109',
deviceInstance: 0,
pgns: { '130312': '' }
})
]
const pgnSourceKeys = {
'C.1': { '130312': ['environment.inside.temperature'] }
}
expect(
detectInstanceConflicts(devices, undefined, pgnSourceKeys)
).toHaveLength(0)
})
it('non-temp PGNs still use instance-only check even when pgnSourceKeys present', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'C.1',
connection: 'C',
src: '1',
deviceInstance: 0,
pgns: { '127505': '' }
}),
makeDevice({
sourceRef: 'C.2',
connection: 'C',
src: '2',
deviceInstance: 0,
pgns: { '127505': '' }
})
]
const pgnSourceKeys = {
'C.1': { '127505': ['0:Fuel'] },
'C.2': { '127505': ['0:Water'] }
}
// 127505 is NOT a compound key PGN — pgnSourceKeys ignored, falls through
// to conservative check (no pgnDataInstances → conflict flagged)
expect(
detectInstanceConflicts(devices, undefined, pgnSourceKeys)
).toHaveLength(1)
})
it('no conflict for same deviceInstance on different connections (multi-bus)', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'can0.1',
connection: 'can0',
src: '1',
deviceInstance: 0,
pgns: { '127505': '', '130312': '' }
}),
makeDevice({
sourceRef: 'can1.1',
connection: 'can1',
src: '1',
deviceInstance: 0,
pgns: { '127505': '', '130312': '' }
})
]
expect(detectInstanceConflicts(devices)).toHaveLength(0)
})
it('ignores proprietary PGNs when they are the only shared PGN', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'can0.189',
connection: 'can0',
src: '189',
deviceInstance: 0,
pgns: { '65284': '' }
}),
makeDevice({
sourceRef: 'can0.169',
connection: 'can0',
src: '169',
deviceInstance: 0,
pgns: { '65284': '' }
})
]
expect(detectInstanceConflicts(devices)).toHaveLength(0)
})
it('excludes proprietary PGNs from sharedPGNs but still reports real conflicts', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'can0.1',
connection: 'can0',
src: '1',
deviceInstance: 0,
pgns: { '65284': '', '127488': '' }
}),
makeDevice({
sourceRef: 'can0.2',
connection: 'can0',
src: '2',
deviceInstance: 0,
pgns: { '65284': '', '127488': '' }
})
]
const conflicts = detectInstanceConflicts(devices)
expect(conflicts).toHaveLength(1)
expect(conflicts[0].sharedPGNs).toEqual(['127488'])
})
// Reproduces the false-positive nmbath reported: a YDTC-13 (Main Cabin
// Temperature, instance 0) and a YDHS-01 (Inside Temperature,
// instance 0) share PGN 130312 but route to disjoint SK leaf paths,
// so they are not in conflict. The keys here are the same shape
// buildPgnSourceKeysFromTree produces server-side from the live tree.
it('YDTC-13 vs YDHS-01 on instance 0 is not a 130312 conflict', () => {
const devices: N2kDeviceEntry[] = [
makeDevice({
sourceRef: 'N2K.c096820059aabb99',
connection: 'N2K',
src: '109',
deviceInstance: 0,
pgns: { '130312': '', '130316': '' }
}),
makeDevice({
sourceRef: 'N2K.c096aa0059a164be',
connection: 'N2K',
src: '111',
deviceInstance: 0,
pgns: { '130312': '', '130316': '' }
})
]
const pgnSourceKeys = {
'N2K.c096820059aabb99': {
'130312': ['environment.inside.mainCabin.temperature'],
'130316': ['environment.inside.mainCabin.temperature']
},
'N2K.c096aa0059a164be': {
'130312': ['environment.inside.temperature'],
'130316': ['environment.inside.temperature']
}
}
expect(
detectInstanceConflicts(devices, undefined, pgnSourceKeys)
).toHaveLength(0)
})
})
describe('isProprietaryPGN', () => {
it('recognises the single-frame addressed slot', () => {
expect(isProprietaryPGN(61184)).toBe(true)
expect(isProprietaryPGN(61183)).toBe(false)
expect(isProprietaryPGN(61185)).toBe(false)
})
it('recognises the single-frame non-addressed range', () => {
expect(isProprietaryPGN(65279)).toBe(false)
expect(isProprietaryPGN(65280)).toBe(true)
expect(isProprietaryPGN(65284)).toBe(true)
expect(isProprietaryPGN(65535)).toBe(true)
expect(isProprietaryPGN(65536)).toBe(false)
})
it('recognises the fast-packet addressed slot', () => {
expect(isProprietaryPGN(126720)).toBe(true)
expect(isProprietaryPGN(126719)).toBe(false)
expect(isProprietaryPGN(126721)).toBe(false)
})
it('recognises the fast-packet non-addressed range', () => {
expect(isProprietaryPGN(130815)).toBe(false)
expect(isProprietaryPGN(130816)).toBe(true)
expect(isProprietaryPGN(131071)).toBe(true)
expect(isProprietaryPGN(131072)).toBe(false)
})
it('accepts string input', () => {
expect(isProprietaryPGN('65284')).toBe(true)
expect(isProprietaryPGN('127488')).toBe(false)
})
it('rejects non-numeric input', () => {
expect(isProprietaryPGN('not-a-number')).toBe(false)
})
})
describe('isRemoveSourceFailure', () => {
it('treats a 2xx response as success', () => {
expect(isRemoveSourceFailure(200, true, false)).toBe(false)
expect(isRemoveSourceFailure(200, true, true)).toBe(false)
})
it('treats a 404 on a missing-from-status ref as success', () => {
// The stale-saved-ref case: a device that re-registered under a new
// transport leaves an old ref in the saved group. The server 404s
// because that ref is genuinely gone — which is exactly the outcome
// the user wanted, so the optimistic removal must stick.
expect(isRemoveSourceFailure(404, false, true)).toBe(false)
})
it('treats a 404 on a still-tracked ref as a failure', () => {
// Not missing from status: a 404 here is unexpected, so revert.
expect(isRemoveSourceFailure(404, false, false)).toBe(true)
})
it('treats auth and server errors as failures regardless of status flag', () => {
expect(isRemoveSourceFailure(401, false, true)).toBe(true)
expect(isRemoveSourceFailure(403, false, true)).toBe(true)
expect(isRemoveSourceFailure(500, false, true)).toBe(true)
expect(isRemoveSourceFailure(401, false, false)).toBe(true)
expect(isRemoveSourceFailure(403, false, false)).toBe(true)
expect(isRemoveSourceFailure(500, false, false)).toBe(true)
})
})