sportident.js 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/.dependency-cruiser.js +233 -0
  2. package/.editorconfig +12 -0
  3. package/.eslintignore +6 -0
  4. package/.github/ISSUE_TEMPLATE/bug_report.md +35 -0
  5. package/.github/ISSUE_TEMPLATE/feature_request.md +17 -0
  6. package/.github/workflows/npm.yml +17 -0
  7. package/LICENSE +21 -0
  8. package/README.md +31 -0
  9. package/babel.config.js +21 -0
  10. package/build-docs.sh +25 -0
  11. package/docs/index.md +9 -0
  12. package/docs/typedoc/index.md +11 -0
  13. package/eslint.base.js +232 -0
  14. package/install.sh +6 -0
  15. package/jest.config.ts +49 -0
  16. package/nx.json +39 -0
  17. package/package.json +51 -0
  18. package/src/SiCard/BaseSiCard.test.ts +187 -0
  19. package/src/SiCard/BaseSiCard.ts +101 -0
  20. package/src/SiCard/IRaceResultData.ts +16 -0
  21. package/src/SiCard/ISiCard.ts +23 -0
  22. package/src/SiCard/ISiCardExamples.ts +4 -0
  23. package/src/SiCard/index.ts +2 -0
  24. package/src/SiCard/raceResultTools.test.ts +260 -0
  25. package/src/SiCard/raceResultTools.ts +150 -0
  26. package/src/SiCard/types/FCard.test.ts +19 -0
  27. package/src/SiCard/types/FCard.ts +14 -0
  28. package/src/SiCard/types/ModernSiCard.test.ts +186 -0
  29. package/src/SiCard/types/ModernSiCard.ts +241 -0
  30. package/src/SiCard/types/PCard.test.ts +19 -0
  31. package/src/SiCard/types/PCard.ts +14 -0
  32. package/src/SiCard/types/SIAC.test.ts +84 -0
  33. package/src/SiCard/types/SIAC.ts +19 -0
  34. package/src/SiCard/types/SiCard10.test.ts +85 -0
  35. package/src/SiCard/types/SiCard10.ts +17 -0
  36. package/src/SiCard/types/SiCard11.test.ts +84 -0
  37. package/src/SiCard/types/SiCard11.ts +19 -0
  38. package/src/SiCard/types/SiCard5.test.ts +149 -0
  39. package/src/SiCard/types/SiCard5.ts +129 -0
  40. package/src/SiCard/types/SiCard6.test.ts +179 -0
  41. package/src/SiCard/types/SiCard6.ts +222 -0
  42. package/src/SiCard/types/SiCard8.test.ts +137 -0
  43. package/src/SiCard/types/SiCard8.ts +129 -0
  44. package/src/SiCard/types/SiCard9.test.ts +132 -0
  45. package/src/SiCard/types/SiCard9.ts +128 -0
  46. package/src/SiCard/types/TCard.test.ts +19 -0
  47. package/src/SiCard/types/TCard.ts +14 -0
  48. package/src/SiCard/types/index.test.ts +26 -0
  49. package/src/SiCard/types/index.ts +15 -0
  50. package/src/SiCard/types/modernSiCardExamples.ts +364 -0
  51. package/src/SiCard/types/siCard5Examples.ts +73 -0
  52. package/src/SiCard/types/siCard6Examples.ts +262 -0
  53. package/src/SiCard/types/siCard8Examples.ts +152 -0
  54. package/src/SiCard/types/siCard9Examples.ts +143 -0
  55. package/src/SiDevice/INavigatorWebSerial.ts +78 -0
  56. package/src/SiDevice/INavigatorWebUsb.ts +62 -0
  57. package/src/SiDevice/ISiDevice.ts +48 -0
  58. package/src/SiDevice/ISiDeviceDriver.ts +35 -0
  59. package/src/SiDevice/README.md +13 -0
  60. package/src/SiDevice/SiDevice.test.ts +354 -0
  61. package/src/SiDevice/SiDevice.ts +132 -0
  62. package/src/SiDevice/WebSerialSiDeviceDriver.ts +146 -0
  63. package/src/SiDevice/WebUsbSiDeviceDriver.ts +343 -0
  64. package/src/SiDevice/index.ts +3 -0
  65. package/src/SiDevice/testUtils/index.ts +2 -0
  66. package/src/SiDevice/testUtils/testISiDeviceDriver.ts +63 -0
  67. package/src/SiDevice/testUtils/testISiDeviceDriverWithAutodetection.ts +72 -0
  68. package/src/SiStation/BaseSiStation.test.ts +221 -0
  69. package/src/SiStation/BaseSiStation.ts +253 -0
  70. package/src/SiStation/CoupledSiStation.test.ts +41 -0
  71. package/src/SiStation/CoupledSiStation.ts +130 -0
  72. package/src/SiStation/ISiMainStation.ts +29 -0
  73. package/src/SiStation/ISiSendTask.ts +9 -0
  74. package/src/SiStation/ISiStation.ts +88 -0
  75. package/src/SiStation/ISiTargetMultiplexer.ts +51 -0
  76. package/src/SiStation/SiMainStation.test.ts +222 -0
  77. package/src/SiStation/SiMainStation.ts +133 -0
  78. package/src/SiStation/SiSendTask.ts +50 -0
  79. package/src/SiStation/SiTargetMultiplexer.targeting.test.ts +112 -0
  80. package/src/SiStation/SiTargetMultiplexer.test.ts +605 -0
  81. package/src/SiStation/SiTargetMultiplexer.ts +241 -0
  82. package/src/SiStation/index.ts +5 -0
  83. package/src/SiStation/siStationExamples.ts +103 -0
  84. package/src/constants.test.ts +17 -0
  85. package/src/constants.ts +92 -0
  86. package/src/fakes/FakeSiCard/BaseFakeSiCard.test.ts +11 -0
  87. package/src/fakes/FakeSiCard/BaseFakeSiCard.ts +10 -0
  88. package/src/fakes/FakeSiCard/IFakeSiCard.ts +6 -0
  89. package/src/fakes/FakeSiCard/index.ts +2 -0
  90. package/src/fakes/FakeSiCard/types/FakeModernSiCard.test.ts +62 -0
  91. package/src/fakes/FakeSiCard/types/FakeModernSiCard.ts +43 -0
  92. package/src/fakes/FakeSiCard/types/FakeSIAC.ts +17 -0
  93. package/src/fakes/FakeSiCard/types/FakeSiCard10.ts +17 -0
  94. package/src/fakes/FakeSiCard/types/FakeSiCard11.ts +17 -0
  95. package/src/fakes/FakeSiCard/types/FakeSiCard5.test.ts +42 -0
  96. package/src/fakes/FakeSiCard/types/FakeSiCard5.ts +40 -0
  97. package/src/fakes/FakeSiCard/types/FakeSiCard6.test.ts +62 -0
  98. package/src/fakes/FakeSiCard/types/FakeSiCard6.ts +44 -0
  99. package/src/fakes/FakeSiCard/types/FakeSiCard8.ts +16 -0
  100. package/src/fakes/FakeSiCard/types/FakeSiCard9.ts +16 -0
  101. package/src/fakes/FakeSiCard/types/index.ts +7 -0
  102. package/src/fakes/FakeSiDeviceDriver.ts +148 -0
  103. package/src/fakes/FakeSiMainStation.test.ts +141 -0
  104. package/src/fakes/FakeSiMainStation.ts +118 -0
  105. package/src/fakes/IFakeSiMainStation.ts +15 -0
  106. package/src/fakes/index.ts +2 -0
  107. package/src/index.ts +24 -0
  108. package/src/siProtocol.test.ts +509 -0
  109. package/src/siProtocol.ts +417 -0
  110. package/src/storage/SiArray.test.ts +103 -0
  111. package/src/storage/SiArray.ts +56 -0
  112. package/src/storage/SiBool.test.ts +81 -0
  113. package/src/storage/SiBool.ts +47 -0
  114. package/src/storage/SiDataType.test.ts +81 -0
  115. package/src/storage/SiDataType.ts +60 -0
  116. package/src/storage/SiDict.test.ts +115 -0
  117. package/src/storage/SiDict.ts +60 -0
  118. package/src/storage/SiEnum.test.ts +77 -0
  119. package/src/storage/SiEnum.ts +48 -0
  120. package/src/storage/SiFieldValue.test.ts +58 -0
  121. package/src/storage/SiFieldValue.ts +23 -0
  122. package/src/storage/SiInt.test.ts +80 -0
  123. package/src/storage/SiInt.ts +84 -0
  124. package/src/storage/SiModified.test.ts +135 -0
  125. package/src/storage/SiModified.ts +59 -0
  126. package/src/storage/SiStorage.test.ts +51 -0
  127. package/src/storage/SiStorage.ts +44 -0
  128. package/src/storage/index.test.ts +222 -0
  129. package/src/storage/index.ts +12 -0
  130. package/src/storage/interfaces.ts +41 -0
  131. package/src/storage/siStringEncoding.ts +1361 -0
  132. package/src/testUtils.test.ts +266 -0
  133. package/src/testUtils.ts +75 -0
  134. package/src/utils/NumberRange.test.ts +66 -0
  135. package/src/utils/NumberRange.ts +46 -0
  136. package/src/utils/NumberRangeRegistry.test.ts +49 -0
  137. package/src/utils/NumberRangeRegistry.ts +43 -0
  138. package/src/utils/bytes.test.ts +126 -0
  139. package/src/utils/bytes.ts +69 -0
  140. package/src/utils/errors.test.ts +29 -0
  141. package/src/utils/errors.ts +20 -0
  142. package/src/utils/events.test.ts +112 -0
  143. package/src/utils/events.ts +68 -0
  144. package/src/utils/general.test.ts +139 -0
  145. package/src/utils/general.ts +69 -0
  146. package/src/utils/index.ts +8 -0
  147. package/src/utils/mixins.test.ts +40 -0
  148. package/src/utils/mixins.ts +13 -0
  149. package/src/utils/typed.ts +3 -0
  150. package/tsconfig.base.json +22 -0
  151. package/tsconfig.json +15 -0
@@ -0,0 +1,509 @@
1
+ import { describe, expect, test } from '@jest/globals';
2
+ import _ from 'lodash';
3
+ import Immutable from 'immutable';
4
+ import { proto } from './constants';
5
+ import * as utils from './utils';
6
+ import * as storage from './storage';
7
+ import * as testUtils from './testUtils';
8
+ import * as siProtocol from './siProtocol';
9
+
10
+ const json2date = (str: string): Date | undefined => {
11
+ const res = /^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})\.([0-9]{3})Z$/.exec(str);
12
+ if (!res) {
13
+ return undefined;
14
+ }
15
+ return new Date(Number(res[1]!), Number(res[2]!) - 1, Number(res[3]!), Number(res[4]!), Number(res[5]!), Number(res[6]!), Number(res[7]!));
16
+ };
17
+
18
+ const date2json = (date: Date | undefined): string | undefined => {
19
+ if (!date) {
20
+ return undefined;
21
+ }
22
+ const year = date.getFullYear();
23
+ const month = date.getMonth();
24
+ const day = date.getDate();
25
+ const hours = date.getHours();
26
+ const minutes = date.getMinutes();
27
+ const seconds = date.getSeconds();
28
+ const milliseconds = date.getMilliseconds();
29
+ return new Date(Date.UTC(year, month, day, hours, minutes, seconds, milliseconds)).toJSON();
30
+ };
31
+
32
+ export const getValidButIncompleteMessageBytes = (): number[][] => {
33
+ const cutOffCommands: number[][] = [];
34
+ const appendCutOffCommands = (command: number[]) => {
35
+ _.range(command.length).forEach((cutOffLength) => {
36
+ cutOffCommands.push(command.slice(0, cutOffLength));
37
+ });
38
+ };
39
+ const getRandomMessageBytes = (numParameters: number): number[] => siProtocol.render(testUtils.getRandomMessage({ numParameters: numParameters }));
40
+ appendCutOffCommands(getRandomMessageBytes(0));
41
+ appendCutOffCommands(getRandomMessageBytes(1));
42
+ appendCutOffCommands(getRandomMessageBytes(2));
43
+ return cutOffCommands;
44
+ };
45
+
46
+ describe('siProtocol', () => {
47
+ const asOf = new Date('2020-01-01T00:00:00.000Z');
48
+ test('arr2date works', () => {
49
+ expect(date2json(siProtocol.arr2date([0x00, 0x01, 0x01], asOf))).toBe('2000-01-01T00:00:00.000Z');
50
+ expect(date2json(siProtocol.arr2date([0x01, 0x02, 0x03], asOf))).toBe('2001-02-03T00:00:00.000Z');
51
+ expect(date2json(siProtocol.arr2date([0x01, 0x0c, 0x1f], asOf))).toBe('2001-12-31T00:00:00.000Z');
52
+ expect(date2json(siProtocol.arr2date([0x14, 0x0c, 0x1f], asOf))).toBe('2020-12-31T00:00:00.000Z');
53
+ expect(date2json(siProtocol.arr2date([0x15, 0x01, 0x01], asOf))).toBe('1921-01-01T00:00:00.000Z');
54
+ expect(date2json(siProtocol.arr2date([0x63, 0x0c, 0x1f], asOf))).toBe('1999-12-31T00:00:00.000Z');
55
+ expect(date2json(siProtocol.arr2date([0x00, 0x01, 0x01, 0x00, 0x00, 0x00], asOf))).toBe('2000-01-01T00:00:00.000Z');
56
+ expect(date2json(siProtocol.arr2date([0x14, 0x0c, 0x1f, 0x01, 0x00, 0x00], asOf))).toBe('2020-12-31T12:00:00.000Z');
57
+ expect(date2json(siProtocol.arr2date([0x15, 0x0c, 0x1f, 0x01, 0x00, 0x00], asOf))).toBe('1921-12-31T12:00:00.000Z');
58
+ expect(date2json(siProtocol.arr2date([0x63, 0x0c, 0x1f, 0x01, 0x00, 0x00], asOf))).toBe('1999-12-31T12:00:00.000Z');
59
+ expect(date2json(siProtocol.arr2date([0x00, 0x01, 0x01, 0x00, 0xa8, 0xbf], asOf))).toBe('2000-01-01T11:59:59.000Z');
60
+ expect(date2json(siProtocol.arr2date([0x63, 0x0c, 0x1f, 0x01, 0xa8, 0xbf], asOf))).toBe('1999-12-31T23:59:59.000Z');
61
+ expect(date2json(siProtocol.arr2date([0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00], asOf))).toBe('2000-01-01T00:00:00.000Z');
62
+ expect(date2json(siProtocol.arr2date([0x14, 0x0c, 0x1f, 0x01, 0x00, 0x00, 0x20], asOf))).toBe('2020-12-31T12:00:00.125Z');
63
+ expect(date2json(siProtocol.arr2date([0x15, 0x0c, 0x1f, 0x01, 0x00, 0x00, 0x60], asOf))).toBe('1921-12-31T12:00:00.375Z');
64
+ expect(date2json(siProtocol.arr2date([0x63, 0x0c, 0x1f, 0x01, 0x00, 0x00, 0x80], asOf))).toBe('1999-12-31T12:00:00.500Z');
65
+ expect(date2json(siProtocol.arr2date([0x00, 0x01, 0x01, 0x00, 0xa8, 0xbf, 0x40], asOf))).toBe('2000-01-01T11:59:59.250Z');
66
+ expect(date2json(siProtocol.arr2date([0x63, 0x0c, 0x1f, 0x01, 0xa8, 0xbf, 0xc0], asOf))).toBe('1999-12-31T23:59:59.750Z');
67
+ });
68
+ test('arr2date sanitizes', () => {
69
+ expect(() => siProtocol.arr2date([], asOf)).toThrow();
70
+ expect(() => siProtocol.arr2date([0x100], asOf)).toThrow();
71
+ expect(() => siProtocol.arr2date([0x123, 0x123], asOf)).toThrow();
72
+ expect(() => siProtocol.arr2date([1, 2, 3, 4], asOf)).toThrow();
73
+ expect(() => siProtocol.arr2date([1, 2, 3, 4, 5], asOf)).toThrow();
74
+ expect(() => siProtocol.arr2date([1, 2, 3, 4, 5, 6, 7, 8], asOf)).toThrow();
75
+ expect(siProtocol.arr2date([100, 1, 1], asOf)).toBe(undefined);
76
+ expect(siProtocol.arr2date([0xff, 1, 1], asOf)).toBe(undefined);
77
+ expect(siProtocol.arr2date([12, 0, 1], asOf)).toBe(undefined);
78
+ expect(siProtocol.arr2date([12, 13, 1], asOf)).toBe(undefined);
79
+ expect(siProtocol.arr2date([12, 0xff, 1], asOf)).toBe(undefined);
80
+ });
81
+ test('arr2date without asOf', () => {
82
+ expect(date2json(siProtocol.arr2date([0x00, 0x01, 0x01]))).toBe('2000-01-01T00:00:00.000Z');
83
+ });
84
+ test('date2arr', () => {
85
+ expect(siProtocol.date2arr(json2date('2000-01-01T00:00:00.000Z')!)).toEqual([0x00, 0x01, 0x01, 0x0c, 0x00, 0x00, 0x00]);
86
+ expect(siProtocol.date2arr(json2date('2000-01-01T12:00:00.000Z')!)).toEqual([0x00, 0x01, 0x01, 0x0d, 0x00, 0x00, 0x00]);
87
+ expect(siProtocol.date2arr(json2date('2000-01-02T00:00:00.000Z')!)).toEqual([0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00]);
88
+ expect(siProtocol.date2arr(json2date('2000-01-02T12:00:00.000Z')!)).toEqual([0x00, 0x01, 0x02, 0x01, 0x00, 0x00, 0x00]);
89
+ expect(siProtocol.date2arr(json2date('2000-01-03T00:00:00.000Z')!)).toEqual([0x00, 0x01, 0x03, 0x02, 0x00, 0x00, 0x00]);
90
+ expect(siProtocol.date2arr(json2date('2000-01-03T12:00:00.000Z')!)).toEqual([0x00, 0x01, 0x03, 0x03, 0x00, 0x00, 0x00]);
91
+ expect(siProtocol.date2arr(json2date('2000-01-04T00:00:00.000Z')!)).toEqual([0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x00]);
92
+ expect(siProtocol.date2arr(json2date('2000-01-04T12:00:00.000Z')!)).toEqual([0x00, 0x01, 0x04, 0x05, 0x00, 0x00, 0x00]);
93
+ expect(siProtocol.date2arr(json2date('2000-01-05T00:00:00.000Z')!)).toEqual([0x00, 0x01, 0x05, 0x06, 0x00, 0x00, 0x00]);
94
+ expect(siProtocol.date2arr(json2date('2000-01-05T12:00:00.000Z')!)).toEqual([0x00, 0x01, 0x05, 0x07, 0x00, 0x00, 0x00]);
95
+ expect(siProtocol.date2arr(json2date('2000-01-06T00:00:00.000Z')!)).toEqual([0x00, 0x01, 0x06, 0x08, 0x00, 0x00, 0x00]);
96
+ expect(siProtocol.date2arr(json2date('2000-01-06T12:00:00.000Z')!)).toEqual([0x00, 0x01, 0x06, 0x09, 0x00, 0x00, 0x00]);
97
+ expect(siProtocol.date2arr(json2date('2000-01-07T00:00:00.000Z')!)).toEqual([0x00, 0x01, 0x07, 0x0a, 0x00, 0x00, 0x00]);
98
+ expect(siProtocol.date2arr(json2date('2000-01-07T12:00:00.000Z')!)).toEqual([0x00, 0x01, 0x07, 0x0b, 0x00, 0x00, 0x00]);
99
+ expect(siProtocol.date2arr(json2date('2099-12-31T12:00:00.500Z')!)).toEqual([0x63, 0x0c, 0x1f, 0x09, 0x00, 0x00, 0x80]);
100
+ expect(siProtocol.date2arr(json2date('2000-01-01T11:59:59.250Z')!)).toEqual([0x00, 0x01, 0x01, 0x0c, 0xa8, 0xbf, 0x40]);
101
+ expect(siProtocol.date2arr(json2date('1999-12-31T23:59:59.750Z')!)).toEqual([0x63, 0x0c, 0x1f, 0x0b, 0xa8, 0xbf, 0xc0]);
102
+ });
103
+ test('date2arr and arr2date do the reverse', () => {
104
+ const forthAndBack = (date: Date) => siProtocol.arr2date(siProtocol.date2arr(date), asOf);
105
+ const forthAndBackAsJson = (str: string) => date2json(forthAndBack(json2date(str)!));
106
+ expect(forthAndBackAsJson('2000-01-01T00:00:00.000Z')).toBe('2000-01-01T00:00:00.000Z');
107
+ expect(forthAndBackAsJson('2001-02-03T00:00:00.000Z')).toBe('2001-02-03T00:00:00.000Z');
108
+ expect(forthAndBackAsJson('2001-12-31T00:00:00.000Z')).toBe('2001-12-31T00:00:00.000Z');
109
+ expect(forthAndBackAsJson('2020-12-31T00:00:00.000Z')).toBe('2020-12-31T00:00:00.000Z');
110
+ expect(forthAndBackAsJson('1921-01-01T00:00:00.000Z')).toBe('1921-01-01T00:00:00.000Z');
111
+ expect(forthAndBackAsJson('1999-12-31T00:00:00.000Z')).toBe('1999-12-31T00:00:00.000Z');
112
+ expect(forthAndBackAsJson('2000-01-01T06:30:00.000Z')).toBe('2000-01-01T06:30:00.000Z');
113
+ expect(forthAndBackAsJson('2020-12-31T13:27:30.000Z')).toBe('2020-12-31T13:27:30.000Z');
114
+ expect(forthAndBackAsJson('1921-12-31T12:00:00.000Z')).toBe('1921-12-31T12:00:00.000Z');
115
+ expect(forthAndBackAsJson('1999-12-31T12:00:00.000Z')).toBe('1999-12-31T12:00:00.000Z');
116
+ expect(forthAndBackAsJson('2000-01-01T11:59:59.000Z')).toBe('2000-01-01T11:59:59.000Z');
117
+ expect(forthAndBackAsJson('1999-12-31T23:59:59.000Z')).toBe('1999-12-31T23:59:59.000Z');
118
+ expect(forthAndBackAsJson('2000-01-01T00:00:00.000Z')).toBe('2000-01-01T00:00:00.000Z');
119
+ expect(forthAndBackAsJson('2020-12-31T12:00:00.125Z')).toBe('2020-12-31T12:00:00.125Z');
120
+ expect(forthAndBackAsJson('1921-12-31T12:00:00.375Z')).toBe('1921-12-31T12:00:00.375Z');
121
+ expect(forthAndBackAsJson('1999-12-31T12:00:00.500Z')).toBe('1999-12-31T12:00:00.500Z');
122
+ expect(forthAndBackAsJson('2000-01-01T11:59:59.250Z')).toBe('2000-01-01T11:59:59.250Z');
123
+ expect(forthAndBackAsJson('1999-12-31T23:59:59.750Z')).toBe('1999-12-31T23:59:59.750Z');
124
+ });
125
+ test('arr2cardNumber works', () => {
126
+ expect(siProtocol.arr2cardNumber([0x00, 0x00, 0x00])).toBe(0x000000);
127
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x00])).toBe(0x003412);
128
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x01])).toBe(0x003412 + 1 * 100000);
129
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x02])).toBe(0x003412 + 2 * 100000);
130
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x03])).toBe(0x003412 + 3 * 100000);
131
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x04])).toBe(0x003412 + 4 * 100000);
132
+ // the following should actually never appear, as 0x053412 = 341010, which would be represented differently
133
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x05])).toBe(0x053412);
134
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x08])).toBe(0x083412);
135
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x56])).toBe(0x563412);
136
+ expect(siProtocol.arr2cardNumber([0x00, 0x00, 0x00, 0x00])).toBe(0x00000000);
137
+ expect(siProtocol.arr2cardNumber([0x00, 0x00, 0x00, 0x01])).toBe(0x01000000);
138
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x56, 0x78])).toBe(0x78563412);
139
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, 0x56, undefined])).toBe(undefined);
140
+ expect(siProtocol.arr2cardNumber([0x12, 0x34, undefined, 0x78])).toBe(undefined);
141
+ expect(siProtocol.arr2cardNumber([0x12, undefined, 0x56, 0x78])).toBe(undefined);
142
+ expect(siProtocol.arr2cardNumber([undefined, 0x34, 0x56, 0x78])).toBe(undefined);
143
+ });
144
+ test('arr2cardNumber sanitizes', () => {
145
+ expect(() => siProtocol.arr2cardNumber([])).toThrow();
146
+ expect(() => siProtocol.arr2cardNumber([1])).toThrow();
147
+ expect(() => siProtocol.arr2cardNumber([1, 2])).toThrow();
148
+ expect(() => siProtocol.arr2cardNumber([1, 2, 3, 4, 5])).toThrow();
149
+ expect(() => siProtocol.arr2cardNumber([1, 2, 3, 4, 5, 6, 7, 8])).toThrow();
150
+ });
151
+ test('cardNumber2arr works', () => {
152
+ expect(siProtocol.cardNumber2arr(0x000000)).toEqual([0x00, 0x00, 0x00, 0x00]);
153
+ expect(siProtocol.cardNumber2arr(0x003412)).toEqual([0x12, 0x34, 0x00, 0x00]);
154
+ expect(siProtocol.cardNumber2arr(0x003412 + 1 * 100000)).toEqual([0x12, 0x34, 0x01, 0x00]);
155
+ expect(siProtocol.cardNumber2arr(0x003412 + 2 * 100000)).toEqual([0x12, 0x34, 0x02, 0x00]);
156
+ expect(siProtocol.cardNumber2arr(0x003412 + 3 * 100000)).toEqual([0x12, 0x34, 0x03, 0x00]);
157
+ expect(siProtocol.cardNumber2arr(0x003412 + 4 * 100000)).toEqual([0x12, 0x34, 0x04, 0x00]);
158
+ expect(siProtocol.cardNumber2arr(0x083412)).toEqual([0x12, 0x34, 0x08, 0x00]);
159
+ expect(siProtocol.cardNumber2arr(0x563412)).toEqual([0x12, 0x34, 0x56, 0x00]);
160
+ expect(siProtocol.cardNumber2arr(0x00000000)).toEqual([0x00, 0x00, 0x00, 0x00]);
161
+ expect(siProtocol.cardNumber2arr(0x01000000)).toEqual([0x00, 0x00, 0x00, 0x01]);
162
+ expect(siProtocol.cardNumber2arr(0x78563412)).toEqual([0x12, 0x34, 0x56, 0x78]);
163
+ expect(siProtocol.cardNumber2arr(undefined)).toEqual([undefined, undefined, undefined, undefined]);
164
+ });
165
+ test('consistent cardNumber <=> arr conversion', () => {
166
+ const cardNumbers = [1000, 10000, 65535, 100000, 165535, 200000, 265535, 300000, 365535, 400000, 465535, 500000, 599999, 999999, 1000000, 1999999, 2000000, 2999999];
167
+ cardNumbers.forEach((cardNumber) => {
168
+ const arr = siProtocol.cardNumber2arr(cardNumber);
169
+ const restoredCardNumber = siProtocol.arr2cardNumber(arr);
170
+ expect(restoredCardNumber).toEqual(cardNumber);
171
+ });
172
+ });
173
+ test('prettyMessage', () => {
174
+ expect(siProtocol.prettyMessage({ command: proto.cmd.GET_MS, parameters: [] }).length > 3).toBe(true);
175
+ expect(siProtocol.prettyMessage({ mode: proto.ACK }).length > 3).toBe(true);
176
+ });
177
+ test('CRC16', () => {
178
+ expect(siProtocol.CRC16([])).toEqual([0x00, 0x00]);
179
+ expect(siProtocol.CRC16([0x01])).toEqual([0x01, 0x00]);
180
+ expect(siProtocol.CRC16([0x12])).toEqual([0x12, 0x00]);
181
+ expect(siProtocol.CRC16([0xff])).toEqual([0xff, 0x00]);
182
+ expect(siProtocol.CRC16([0x01, 0x02])).toEqual([0x01, 0x02]);
183
+ expect(siProtocol.CRC16([0x12, 0x34])).toEqual([0x12, 0x34]);
184
+ expect(siProtocol.CRC16([0x12, 0x34, 0x56])).toEqual([0xba, 0xbb]);
185
+ expect(siProtocol.CRC16([0x12, 0x32, 0x56])).toEqual([0xba, 0xaf]);
186
+ expect(siProtocol.CRC16([0x12, 0x34, 0x56, 0x78])).toEqual([0x1e, 0x83]);
187
+ expect(siProtocol.CRC16([0x12, 0x32, 0x56, 0x78])).toEqual([0x1e, 0xfb]);
188
+ });
189
+
190
+ test('parse WAKEUP', () => {
191
+ expect(siProtocol.parse([proto.WAKEUP])).toEqual({
192
+ message: { mode: proto.WAKEUP },
193
+ remainder: []
194
+ });
195
+ const randomByte = testUtils.getRandomByte();
196
+ expect(siProtocol.parse([proto.WAKEUP, randomByte])).toEqual({
197
+ message: { mode: proto.WAKEUP },
198
+ remainder: [randomByte]
199
+ });
200
+ });
201
+ test('parse ACK', () => {
202
+ expect(siProtocol.parse([proto.ACK])).toEqual({
203
+ message: { mode: proto.ACK },
204
+ remainder: []
205
+ });
206
+ const randomByte = testUtils.getRandomByte();
207
+ expect(siProtocol.parse([proto.ACK, randomByte])).toEqual({
208
+ message: { mode: proto.ACK },
209
+ remainder: [randomByte]
210
+ });
211
+ });
212
+ test('parse NAK', () => {
213
+ expect(siProtocol.parse([proto.NAK])).toEqual({
214
+ message: { mode: proto.NAK },
215
+ remainder: []
216
+ });
217
+ const randomByte = testUtils.getRandomByte();
218
+ expect(siProtocol.parse([proto.NAK, randomByte])).toEqual({
219
+ message: { mode: proto.NAK },
220
+ remainder: [randomByte]
221
+ });
222
+ });
223
+ test('parse with invalid start byte', () => {
224
+ const invalidStartByte = testUtils.getRandomByteExcept([proto.STX, proto.WAKEUP, proto.NAK]);
225
+ console.debug(`Chosen invalid start byte: ${utils.prettyHex([invalidStartByte])}`);
226
+ expect(siProtocol.parse([invalidStartByte])).toEqual({ message: null, remainder: [] });
227
+ const randomByte = testUtils.getRandomByte();
228
+ expect(siProtocol.parse([invalidStartByte, randomByte])).toEqual({
229
+ message: null,
230
+ remainder: [randomByte]
231
+ });
232
+ });
233
+
234
+ test('parse command without remainder', () => {
235
+ const parseForMessage = (message: siProtocol.SiMessage) => siProtocol.parse(siProtocol.render(message)).message;
236
+ expect(parseForMessage({ command: 0x00, parameters: [] })).toEqual({
237
+ command: 0x00,
238
+ parameters: []
239
+ });
240
+ expect(parseForMessage({ command: 0xff, parameters: [0xee] })).toEqual({
241
+ command: 0xff,
242
+ parameters: [0xee]
243
+ });
244
+ });
245
+ test('parse command with remainder', () => {
246
+ const parseForMessageWithRemainder = (message: siProtocol.SiMessage) => siProtocol.parse([...siProtocol.render(message), 0xdd]);
247
+ expect(parseForMessageWithRemainder({ command: 0x00, parameters: [] })).toEqual({
248
+ message: { command: 0x00, parameters: [] },
249
+ remainder: [0xdd]
250
+ });
251
+ expect(parseForMessageWithRemainder({ command: 0xff, parameters: [0xee] })).toEqual({
252
+ message: { command: 0xff, parameters: [0xee] },
253
+ remainder: [0xdd]
254
+ });
255
+ });
256
+ test('parse incomplete but valid command', () => {
257
+ getValidButIncompleteMessageBytes().forEach((cutOffCommand) => {
258
+ expect(siProtocol.parse(cutOffCommand)).toEqual({ message: null, remainder: cutOffCommand });
259
+ });
260
+ });
261
+ test('parse command with invalid ETX', () => {
262
+ const invalidETX = testUtils.getRandomByteExcept([proto.ETX]);
263
+ console.debug(`Chosen invalid ETX: ${utils.prettyHex([invalidETX])}`);
264
+ expect(siProtocol.parse([proto.STX, 0x00, 0x00, 0x00, 0x00, invalidETX])).toEqual({
265
+ message: null,
266
+ remainder: [0x00, 0x00, 0x00, 0x00, invalidETX]
267
+ });
268
+ expect(siProtocol.parse([proto.STX, 0xff, 0x01, 0xee, 0x00, 0x01, invalidETX])).toEqual({
269
+ message: null,
270
+ remainder: [0xff, 0x01, 0xee, 0x00, 0x01, invalidETX]
271
+ });
272
+ expect(siProtocol.parse([proto.STX, 0x00, 0x00, 0x00, 0x01, invalidETX, 0xdd])).toEqual({
273
+ message: null,
274
+ remainder: [0x00, 0x00, 0x00, 0x01, invalidETX, 0xdd]
275
+ });
276
+ expect(siProtocol.parse([proto.STX, 0xff, 0x01, 0xee, 0x00, 0x01, invalidETX, 0xdd])).toEqual({
277
+ message: null,
278
+ remainder: [0xff, 0x01, 0xee, 0x00, 0x01, invalidETX, 0xdd]
279
+ });
280
+ });
281
+ test('parse command with invalid CRC', () => {
282
+ expect(siProtocol.parse([proto.STX, 0x00, 0x00, 0x00, 0x01, proto.ETX])).toEqual({
283
+ message: null,
284
+ remainder: []
285
+ });
286
+ expect(siProtocol.parse([proto.STX, 0xff, 0x01, 0xee, 0x00, 0x01, proto.ETX])).toEqual({
287
+ message: null,
288
+ remainder: []
289
+ });
290
+ expect(siProtocol.parse([proto.STX, 0x00, 0x00, 0x00, 0x01, proto.ETX, 0xdd])).toEqual({
291
+ message: null,
292
+ remainder: [0xdd]
293
+ });
294
+ expect(siProtocol.parse([proto.STX, 0xff, 0x01, 0xee, 0x00, 0x01, proto.ETX, 0xdd])).toEqual({
295
+ message: null,
296
+ remainder: [0xdd]
297
+ });
298
+ });
299
+
300
+ test('parseAll without remainder', () => {
301
+ const stream0 = [] as number[];
302
+ expect(siProtocol.parseAll(stream0)).toEqual({
303
+ messages: [],
304
+ remainder: []
305
+ });
306
+
307
+ const stream1 = siProtocol.render({ command: 0x00, parameters: [] });
308
+ expect(siProtocol.parseAll(stream1)).toEqual({
309
+ messages: [{ command: 0x00, parameters: [] }],
310
+ remainder: []
311
+ });
312
+
313
+ const stream2 = [...siProtocol.render({ command: 0x00, parameters: [] }), proto.WAKEUP, ...siProtocol.render({ command: 0xff, parameters: [0xee] })];
314
+ expect(siProtocol.parseAll(stream2)).toEqual({
315
+ messages: [{ command: 0x00, parameters: [] }, { mode: proto.WAKEUP }, { command: 0xff, parameters: [0xee] }],
316
+ remainder: []
317
+ });
318
+ });
319
+ test('parseAll with valid remainder', () => {
320
+ getValidButIncompleteMessageBytes().forEach((cutOffCommand) => {
321
+ const stream0 = [...cutOffCommand];
322
+ expect(siProtocol.parseAll(stream0)).toEqual({
323
+ messages: [],
324
+ remainder: cutOffCommand
325
+ });
326
+
327
+ const stream1 = [...siProtocol.render({ command: 0x00, parameters: [] }), proto.NAK, ...cutOffCommand];
328
+ expect(siProtocol.parseAll(stream1)).toEqual({
329
+ messages: [{ command: 0x00, parameters: [] }, { mode: proto.NAK }],
330
+ remainder: cutOffCommand
331
+ });
332
+
333
+ const stream2 = [proto.WAKEUP, ...siProtocol.render({ command: 0x00, parameters: [] }), ...siProtocol.render({ command: 0xff, parameters: [0xee] }), ...cutOffCommand];
334
+ expect(siProtocol.parseAll(stream2)).toEqual({
335
+ messages: [{ mode: proto.WAKEUP }, { command: 0x00, parameters: [] }, { command: 0xff, parameters: [0xee] }],
336
+ remainder: cutOffCommand
337
+ });
338
+ });
339
+ });
340
+
341
+ test('render ACK', () => {
342
+ expect(siProtocol.render({ mode: proto.ACK })).toEqual([proto.ACK]);
343
+ });
344
+ test('render NAK', () => {
345
+ expect(siProtocol.render({ mode: proto.NAK })).toEqual([proto.NAK]);
346
+ });
347
+ test('render WAKEUP', () => {
348
+ expect(siProtocol.render({ mode: proto.WAKEUP })).toEqual([proto.WAKEUP]);
349
+ });
350
+ test('render command', () => {
351
+ expect(siProtocol.render({ command: 0x00, parameters: [] })).toEqual([proto.STX, 0x00, 0x00, 0x00, 0x00, proto.ETX]);
352
+ expect(siProtocol.render({ command: 0xff, parameters: [0xee] })).toEqual([proto.STX, 0xff, 0x01, 0xee, 0xec, 0x0a, proto.ETX]);
353
+ expect(siProtocol.render({ mode: undefined, command: 0x00, parameters: [] })).toEqual([proto.STX, 0x00, 0x00, 0x00, 0x00, proto.ETX]);
354
+ expect(siProtocol.render({ mode: undefined, command: 0xff, parameters: [0xee] })).toEqual([proto.STX, 0xff, 0x01, 0xee, 0xec, 0x0a, proto.ETX]);
355
+ });
356
+ test('render invalid mode', () => {
357
+ const invalidMode = testUtils.getRandomByteExcept([proto.WAKEUP, proto.NAK, proto.ACK]);
358
+ expect(() => siProtocol.render({ mode: invalidMode })).toThrow();
359
+ });
360
+
361
+ test('SiDate SiStorage integration', () => {
362
+ const weirdStorage = storage.defineStorage(0x09, {
363
+ weirdDate: new siProtocol.SiDate(3, (i) => i),
364
+ crazyDate: new siProtocol.SiDate(6, (i) => 0x03 + i)
365
+ });
366
+
367
+ const myWeirdStorage = weirdStorage(utils.unPrettyHex('0F 03 07 00 00 00 00 00 00'));
368
+
369
+ expect(myWeirdStorage.get('weirdDate')!.value).toEqual(new Date(2015, 2, 7));
370
+ myWeirdStorage.set('weirdDate', new Date(2017, 12, 30));
371
+ expect(myWeirdStorage.data.toJS()).toEqual(utils.unPrettyHex('12 01 1E 00 00 00 00 00 00'));
372
+ expect(myWeirdStorage.get('weirdDate')!.value).toEqual(new Date(2017, 12, 30));
373
+
374
+ expect(myWeirdStorage.get('crazyDate')).toEqual(undefined);
375
+ myWeirdStorage.set('crazyDate', new Date(2003, 1, 30, 3, 7, 5));
376
+ expect(myWeirdStorage.data.toJS()).toEqual(utils.unPrettyHex('12 01 1E 03 03 02 00 2B D9'));
377
+ expect(myWeirdStorage.get('crazyDate')!.value).toEqual(new Date(2003, 1, 30, 3, 7, 5));
378
+
379
+ const unknownWeirdStorage = weirdStorage();
380
+ const ModifyUndefinedException = storage.ModifyUndefinedException;
381
+
382
+ expect(unknownWeirdStorage.get('weirdDate')).toBe(undefined);
383
+ expect(() => unknownWeirdStorage.set('weirdDate', new Date(2017, 12, 30))).toThrow(ModifyUndefinedException);
384
+
385
+ expect(unknownWeirdStorage.get('crazyDate')).toBe(undefined);
386
+ expect(() => unknownWeirdStorage.set('crazyDate', new Date(2003, 1, 30, 3, 7, 5))).toThrow(ModifyUndefinedException);
387
+ });
388
+
389
+ describe('SiDate', () => {
390
+ const mySiDate = new siProtocol.SiDate(3, (i) => i);
391
+ const fieldValueOf = (value: Date) => new storage.SiFieldValue(mySiDate, value);
392
+ test('typeSpecificIsValueValid', () => {
393
+ expect(mySiDate.typeSpecificIsValueValid(new Date())).toBe(true);
394
+ });
395
+ test('valueToString', () => {
396
+ expect(mySiDate.valueToString(json2date('2000-01-01T00:00:00.000Z')!)).toBe('2000-01-01T00:00:00.000Z');
397
+ expect(mySiDate.valueToString(json2date('2020-12-31T13:27:30.000Z')!)).toBe('2020-12-31T13:27:30.000Z');
398
+ });
399
+ test('valueFromString', () => {
400
+ expect(mySiDate.valueFromString('2000-01-01T00:00:00.000Z')).toEqual(json2date('2000-01-01T00:00:00.000Z')!);
401
+ expect(mySiDate.valueFromString('2020-12-31T13:27:30.000Z')).toEqual(json2date('2020-12-31T13:27:30.000Z')!);
402
+ expect(mySiDate.valueFromString('2020-12-31T13:27:30.000') instanceof storage.ValueFromStringError).toBe(true);
403
+ expect(mySiDate.valueFromString('2020-12-31T13:27:30Z') instanceof storage.ValueFromStringError).toBe(true);
404
+ expect(mySiDate.valueFromString('2020-12-31Z') instanceof storage.ValueFromStringError).toBe(true);
405
+ expect(mySiDate.valueFromString('2020-12-31') instanceof storage.ValueFromStringError).toBe(true);
406
+ expect(mySiDate.valueFromString('test') instanceof storage.ValueFromStringError).toBe(true);
407
+ });
408
+ test('extractFromData gives field value', () => {
409
+ const data = Immutable.List([0x00, 0x01, 0x01]);
410
+ const fieldValue = mySiDate.extractFromData(data);
411
+ expect(fieldValue instanceof storage.SiFieldValue).toBe(true);
412
+ expect(fieldValue!.field).toBe(mySiDate);
413
+ expect(fieldValue!.value).toEqual(json2date('2000-01-01T00:00:00.000Z')!);
414
+ });
415
+ test('extractFromData', () => {
416
+ const getExtractedFieldValue = (bytes: (number | undefined)[]) => mySiDate.extractFromData(Immutable.List(bytes));
417
+ expect(getExtractedFieldValue([0x00, 0x01, 0x01])!.value instanceof Date).toBe(true);
418
+ expect(getExtractedFieldValue([0x00, 0x01, undefined])).toBe(undefined);
419
+ expect(getExtractedFieldValue([0x00, undefined, 0x01])).toBe(undefined);
420
+ expect(getExtractedFieldValue([undefined, 0x00, 0x01])).toBe(undefined);
421
+ expect(getExtractedFieldValue([0x00])).toBe(undefined);
422
+ expect(getExtractedFieldValue([])).toBe(undefined);
423
+ });
424
+ test('updateData', () => {
425
+ const initialData = Immutable.List([0x00, 0x00, 0x00]);
426
+ const updateInitialData = (newValue: Date | storage.SiFieldValue<Date>) => mySiDate.updateData(initialData, newValue).toJS();
427
+
428
+ expect(updateInitialData(json2date('2000-01-01T00:00:00.000Z')!)).toEqual([0x00, 0x01, 0x01]);
429
+ expect(updateInitialData(fieldValueOf(json2date('2000-01-01T00:00:00.000Z')!))).toEqual([0x00, 0x01, 0x01]);
430
+ });
431
+ test('updateData modify undefined', () => {
432
+ const updateData = (data: (number | undefined)[], newValue: Date | storage.SiFieldValue<Date>) => mySiDate.updateData(Immutable.List(data), newValue).toJS();
433
+
434
+ expect(() => updateData([], json2date('2000-01-01T00:00:00.000Z')!)).toThrow(storage.ModifyUndefinedException);
435
+ expect(() => updateData([], fieldValueOf(json2date('2000-01-01T00:00:00.000Z')!))).toThrow(storage.ModifyUndefinedException);
436
+ expect(() => updateData([0x00, 0x00, undefined], json2date('2000-01-01T00:00:00.000Z')!)).toThrow(storage.ModifyUndefinedException);
437
+ expect(() => updateData([0x00, undefined, 0x00], json2date('2000-01-01T00:00:00.000Z')!)).toThrow(storage.ModifyUndefinedException);
438
+ expect(() => updateData([undefined, 0x00, 0x00], json2date('2000-01-01T00:00:00.000Z')!)).toThrow(storage.ModifyUndefinedException);
439
+ });
440
+ });
441
+
442
+ describe('SiTime', () => {
443
+ const mySiTime = new siProtocol.SiTime([[0x01], [0x00]]);
444
+ const myInexistentSiTime = new siProtocol.SiTime(undefined);
445
+ const fieldValueOf = (value: siProtocol.SiTimestamp) => new storage.SiFieldValue(mySiTime, value);
446
+ test('typeSpecificIsValueValid', () => {
447
+ expect(mySiTime.typeSpecificIsValueValid({time:0})).toBe(true);
448
+ expect(mySiTime.typeSpecificIsValueValid({time:43199})).toBe(true);
449
+ expect(mySiTime.typeSpecificIsValueValid({time:86400})).toBe(false);
450
+ expect(mySiTime.typeSpecificIsValueValid(null)).toBe(true);
451
+ });
452
+ test('valueToString', () => {
453
+ expect(mySiTime.valueToString({time:0})).toBe('00:00:00');
454
+ expect(mySiTime.valueToString({time:43199})).toBe('11:59:59');
455
+ expect(mySiTime.valueToString(null)).toBe('NO_TIME');
456
+ });
457
+ test('valueFromString', () => {
458
+ expect(mySiTime.valueFromString('00:00:00')).toEqual({time:0});
459
+ expect(mySiTime.valueFromString('11:59:59')).toEqual({time:43199});
460
+ expect(mySiTime.valueFromString('NO_TIME')).toBe(null);
461
+ expect(mySiTime.valueFromString('06:12') instanceof storage.ValueFromStringError).toBe(true);
462
+ expect(mySiTime.valueFromString('test') instanceof storage.ValueFromStringError).toBe(true);
463
+ });
464
+ test('extractFromData gives field value', () => {
465
+ const data = Immutable.List([0x01, 0x01]);
466
+ const fieldValue = mySiTime.extractFromData(data);
467
+ expect(fieldValue instanceof storage.SiFieldValue).toBe(true);
468
+ expect(fieldValue!.field).toBe(mySiTime);
469
+ expect(JSON.stringify(fieldValue!.value)).toBe(JSON.stringify({time:257, weekcounter: undefined, weekday: undefined}));
470
+ });
471
+ test('extractFromData', () => {
472
+ const getExtractedFieldValue = (bytes: (number | undefined)[]) => mySiTime.extractFromData(Immutable.List(bytes));
473
+ expect(JSON.stringify(getExtractedFieldValue([0x00, 0x01])!.value)).toBe(JSON.stringify({time:1, weekcounter: undefined, weekday: undefined}));
474
+ expect(getExtractedFieldValue([0xee, 0xee])!.value).toBe(null);
475
+ expect(getExtractedFieldValue([0x00, undefined])).toBe(undefined);
476
+ expect(getExtractedFieldValue([undefined, 0x01])).toBe(undefined);
477
+ expect(getExtractedFieldValue([0x00])).toBe(undefined);
478
+ expect(getExtractedFieldValue([])).toBe(undefined);
479
+ });
480
+ test('extractFromData for inexistent', () => {
481
+ const getExtractedFieldValue = (bytes: (number | undefined)[]) => myInexistentSiTime.extractFromData(Immutable.List(bytes));
482
+ expect(getExtractedFieldValue([0x00, 0x01])!.value).toBe(null);
483
+ });
484
+ test('updateData', () => {
485
+ const initialData = Immutable.List([0x00, 0x00]);
486
+ const updateInitialData = (newValue: siProtocol.SiTimestamp | storage.SiFieldValue<siProtocol.SiTimestamp>) => mySiTime.updateData(initialData, newValue).toJS();
487
+
488
+ expect(updateInitialData({time:257})).toEqual([0x01, 0x01]);
489
+ expect(updateInitialData(fieldValueOf({time:257}))).toEqual([0x01, 0x01]);
490
+ expect(updateInitialData(null)).toEqual([0xee, 0xee]);
491
+ expect(updateInitialData(fieldValueOf(null))).toEqual([0xee, 0xee]);
492
+ });
493
+ test('updateData for inexistent', () => {
494
+ const initialData = Immutable.List([0x00, 0x00]);
495
+ const updateInitialData = (newValue: siProtocol.SiTimestamp | storage.SiFieldValue<siProtocol.SiTimestamp>) => myInexistentSiTime.updateData(initialData, newValue).toJS();
496
+
497
+ expect(updateInitialData({time:257})).toEqual([0x00, 0x00]);
498
+ expect(updateInitialData(fieldValueOf({time:257}))).toEqual([0x00, 0x00]);
499
+ });
500
+ test('updateData modify undefined', () => {
501
+ const updateData = (data: (number | undefined)[], newValue: siProtocol.SiTimestamp | storage.SiFieldValue<siProtocol.SiTimestamp>) => mySiTime.updateData(Immutable.List(data), newValue).toJS();
502
+
503
+ expect(() => updateData([], {time:257})).toThrow(storage.ModifyUndefinedException);
504
+ expect(() => updateData([], fieldValueOf({time:257}))).toThrow(storage.ModifyUndefinedException);
505
+ expect(() => updateData([0x00, undefined], {time:257})).toThrow(storage.ModifyUndefinedException);
506
+ expect(() => updateData([undefined, 0x01], {time:257})).toThrow(storage.ModifyUndefinedException);
507
+ });
508
+ });
509
+ });