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('gotResponseLogData', function () {
27 it('covers', function () {
32 'Content-Type': 'text/plain',
35 body: '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.”',
42 redirectUrls: ['https://example.com/clip/Thornton_Burgess'],
48 'Content-Type': 'text/plain',
50 body: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
53 redirectUrls: ['https://example.com/clip/Thornton_Burgess'],
55 const result
= common
.gotResponseLogData(response
);
56 assert
.deepStrictEqual(result
, expected
);
58 it('covers buffer data', function () {
62 body: Buffer
.from('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.”'),
67 body: '<Buffer 188 bytes>',
69 const result
= common
.gotResponseLogData(response
);
70 assert
.deepStrictEqual(result
, expected
);
72 it('covers no data', function () {
77 'Content-Type': 'text/plain',
84 'Content-Type': 'text/plain',
87 const result
= common
.gotResponseLogData(response
);
88 assert
.deepStrictEqual(result
, expected
);
90 }); // gotResponseLogData
92 describe('topicLeaseDefaults', function () {
93 it('supplies necessary properties', function () {
94 const result
= common
.topicLeaseDefaults();
95 assert('leaseSecondsPreferred' in result
);
96 assert
.strictEqual(typeof result
.leaseSecondsPreferred
, 'number');
97 assert('leaseSecondsMax' in result
);
98 assert
.strictEqual(typeof result
.leaseSecondsMax
, 'number');
99 assert('leaseSecondsMin' in result
);
100 assert
.strictEqual(typeof result
.leaseSecondsMin
, 'number');
102 it('cannot be changed', function () {
103 const result
= common
.topicLeaseDefaults();
104 const origMin
= result
.leaseSecondsMin
;
106 result
.leaseSecondsMin
+= 10;
107 assert
.fail('assign should fail');
109 assert(e
instanceof TypeError
);
111 assert
.strictEqual(result
.leaseSecondsMin
, origMin
);
113 }); // topicLeaseDefaults
115 describe('attemptRetrySeconds', function () {
116 const retries
= [0, 1, 2];
118 it('defaults without a number', function () {
119 const result
= common
.attemptRetrySeconds('not a number', retries
, jitter
);
120 assert
.strictEqual(result
, retries
[0]);
122 it('brackets lower range', function () {
123 const result
= common
.attemptRetrySeconds(-10, retries
, jitter
);
124 assert
.strictEqual(result
, retries
[0]);
126 it('brackets upper range', function () {
127 const result
= common
.attemptRetrySeconds(10, retries
, jitter
);
128 assert
.strictEqual(result
, retries
[retries
.length
- 1]);
130 it('covers middle', function () {
131 const result
= common
.attemptRetrySeconds(1, retries
, jitter
);
132 assert
.strictEqual(result
, retries
[1]);
134 it('covers default', function () {
135 const result
= common
.attemptRetrySeconds(0);
136 assert(result
>= 60);
137 assert(result
<= 60 * 1.618)
139 }); // attemptRetrySeconds
141 describe('arrayChunk', function () {
142 it('covers default', function () {
143 const result
= common
.arrayChunk([1, 2, 3]);
144 assert
.deepStrictEqual(result
, [[1], [2], [3]]);
146 it('covers remainders', function () {
147 const result
= common
.arrayChunk([1, 2, 3], 2);
148 assert
.deepStrictEqual(result
, [[1, 2], [3]]);
152 describe('stackSafePush', function () {
153 it('pushes', function () {
154 const bigArray
= new Array(2**18);
157 common
.stackSafePush(dst
, bigArray
);
159 assert
.strictEqual(dst
.length
, bigArray
.length
);
163 describe('logTruncate', function () {
164 it('returns short string', function () {
165 const str
= 'this is a short string';
166 const result
= common
.logTruncate(str
, 100);
167 assert
.strictEqual(result
, str
);
169 it('truncates long string', function () {
170 const str
= 'this is not really a very long string but it is long enough for this test';
171 const result
= common
.logTruncate(str
, 10);
172 assert(result
.length
< str
.length
);
176 describe('validHash', function () {
177 it('should succeed', function () {
178 const result
= common
.validHash('sha256');
179 assert
.strictEqual(result
, true);
181 it('should fail', function () {
182 const result
= common
.validHash('md5');
183 assert
.strictEqual(result
, false);
187 describe('ensureArray', function () {
188 it('returns empty array for no data', function () {
189 const result
= common
.ensureArray();
190 assert
.deepStrictEqual(result
, []);
192 it('returns same array passed in', function () {
193 const expected
= [1, 2, 3, 'foo'];
194 const result
= common
.ensureArray(expected
);
195 assert
.deepStrictEqual(result
, expected
);
197 it('returns array containing non-array data', function () {
199 const result
= common
.ensureArray(data
);
200 assert
.deepStrictEqual(result
, [data
]);