4 const assert
= require('assert');
5 const common
= require('../../src/common');
7 describe('Common', function () {
9 describe('freezeDeep', function () {
10 it('freezes things', function () {
18 const result
= common
.freezeDeep(obj
);
19 assert(Object
.isFrozen(result
));
20 assert(Object
.isFrozen(result
.sub1
));
21 assert(Object
.isFrozen(result
.sub1
.sub2
));
22 assert(Object
.isFrozen(result
.sub1
.sub2
.foo
));
26 describe('axiosResponseLogData', function () {
27 it('covers', function () {
32 'Content-Type': 'text/plain',
35 data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green Meadows," said Old Mother West Wind, “and there I saw the Best Thing in the World.”',
41 'Content-Type': 'text/plain',
43 data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
45 const result
= common
.axiosResponseLogData(response
);
46 assert
.deepStrictEqual(result
, expected
);
48 it('covers no data', function () {
53 'Content-Type': 'text/plain',
60 'Content-Type': 'text/plain',
63 const result
= common
.axiosResponseLogData(response
);
64 assert
.deepStrictEqual(result
, expected
);
66 }); // axiosResponseLogData
68 describe('topicLeaseDefaults', function () {
69 it('supplies necessary properties', function () {
70 const result
= common
.topicLeaseDefaults();
71 assert('leaseSecondsPreferred' in result
);
72 assert
.strictEqual(typeof result
.leaseSecondsPreferred
, 'number');
73 assert('leaseSecondsMax' in result
);
74 assert
.strictEqual(typeof result
.leaseSecondsMax
, 'number');
75 assert('leaseSecondsMin' in result
);
76 assert
.strictEqual(typeof result
.leaseSecondsMin
, 'number');
78 it('cannot be changed', function () {
79 const result
= common
.topicLeaseDefaults();
80 const origMin
= result
.leaseSecondsMin
;
82 result
.leaseSecondsMin
+= 10;
83 assert
.fail('assign should fail');
85 assert(e
instanceof TypeError
);
87 assert
.strictEqual(result
.leaseSecondsMin
, origMin
);
89 }); // topicLeaseDefaults
91 describe('attemptRetrySeconds', function () {
92 const retries
= [0, 1, 2];
94 it('defaults without a number', function () {
95 const result
= common
.attemptRetrySeconds('not a number', retries
, jitter
);
96 assert
.strictEqual(result
, retries
[0]);
98 it('brackets lower range', function () {
99 const result
= common
.attemptRetrySeconds(-10, retries
, jitter
);
100 assert
.strictEqual(result
, retries
[0]);
102 it('brackets upper range', function () {
103 const result
= common
.attemptRetrySeconds(10, retries
, jitter
);
104 assert
.strictEqual(result
, retries
[retries
.length
- 1]);
106 it('covers middle', function () {
107 const result
= common
.attemptRetrySeconds(1, retries
, jitter
);
108 assert
.strictEqual(result
, retries
[1]);
110 it('covers default', function () {
111 const result
= common
.attemptRetrySeconds(0);
112 assert(result
>= 60);
113 assert(result
<= 60 * 1.618)
115 }); // attemptRetrySeconds
117 describe('arrayChunk', function () {
118 it('covers default', function () {
119 const result
= common
.arrayChunk([1, 2, 3]);
120 assert
.deepStrictEqual(result
, [[1], [2], [3]]);
122 it('covers remainders', function () {
123 const result
= common
.arrayChunk([1, 2, 3], 2);
124 assert
.deepStrictEqual(result
, [[1, 2], [3]]);
128 describe('stackSafePush', function () {
129 it('pushes', function () {
130 const bigArray
= new Array(2**18);
133 common
.stackSafePush(dst
, bigArray
);
135 assert
.strictEqual(dst
.length
, bigArray
.length
);
139 describe('logTruncate', function () {
140 it('returns short string', function () {
141 const str
= 'this is a short string';
142 const result
= common
.logTruncate(str
, 100);
143 assert
.strictEqual(result
, str
);
145 it('truncates long string', function () {
146 const str
= 'this is not really a very long string but it is long enough for this test';
147 const result
= common
.logTruncate(str
, 10);
148 assert(result
.length
< str
.length
);
152 describe('validHash', function () {
153 it('should succeed', function () {
154 const result
= common
.validHash('sha256');
155 assert
.strictEqual(result
, true);
157 it('should fail', function () {
158 const result
= common
.validHash('md5');
159 assert
.strictEqual(result
, false);
163 describe('ensureArray', function () {
164 it('returns empty array for no data', function () {
165 const result
= common
.ensureArray();
166 assert
.deepStrictEqual(result
, []);
168 it('returns same array passed in', function () {
169 const expected
= [1, 2, 3, 'foo'];
170 const result
= common
.ensureArray(expected
);
171 assert
.deepStrictEqual(result
, expected
);
173 it('returns array containing non-array data', function () {
175 const result
= common
.ensureArray(data
);
176 assert
.deepStrictEqual(result
, [data
]);