3 const assert
= require('node:assert');
4 const common
= require('../../src/common');
6 describe('Common', function () {
8 describe('freezeDeep', function () {
9 it('freezes things', function () {
17 const result
= common
.freezeDeep(obj
);
18 assert(Object
.isFrozen(result
));
19 assert(Object
.isFrozen(result
.sub1
));
20 assert(Object
.isFrozen(result
.sub1
.sub2
));
21 assert(Object
.isFrozen(result
.sub1
.sub2
.foo
));
25 describe('gotResponseLogData', function () {
26 it('covers', function () {
31 'Content-Type': 'text/plain',
34 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.”',
41 redirectUrls: ['https://example.com/clip/Thornton_Burgess'],
47 'Content-Type': 'text/plain',
49 body: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
52 redirectUrls: ['https://example.com/clip/Thornton_Burgess'],
54 const result
= common
.gotResponseLogData(response
);
55 assert
.deepStrictEqual(result
, expected
);
57 it('covers buffer data', function () {
61 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.”'),
66 body: '<Buffer 188 bytes>',
68 const result
= common
.gotResponseLogData(response
);
69 assert
.deepStrictEqual(result
, expected
);
71 it('covers no data', function () {
76 'Content-Type': 'text/plain',
83 'Content-Type': 'text/plain',
86 const result
= common
.gotResponseLogData(response
);
87 assert
.deepStrictEqual(result
, expected
);
89 }); // gotResponseLogData
91 describe('topicLeaseDefaults', function () {
92 it('supplies necessary properties', function () {
93 const result
= common
.topicLeaseDefaults();
94 assert('leaseSecondsPreferred' in result
);
95 assert
.strictEqual(typeof result
.leaseSecondsPreferred
, 'number');
96 assert('leaseSecondsMax' in result
);
97 assert
.strictEqual(typeof result
.leaseSecondsMax
, 'number');
98 assert('leaseSecondsMin' in result
);
99 assert
.strictEqual(typeof result
.leaseSecondsMin
, 'number');
101 it('cannot be changed', function () {
102 const result
= common
.topicLeaseDefaults();
103 const origMin
= result
.leaseSecondsMin
;
105 result
.leaseSecondsMin
+= 10;
106 assert
.fail('assign should fail');
108 assert(e
instanceof TypeError
);
110 assert
.strictEqual(result
.leaseSecondsMin
, origMin
);
112 }); // topicLeaseDefaults
114 describe('attemptRetrySeconds', function () {
115 const retries
= [0, 1, 2];
117 it('defaults without a number', function () {
118 const result
= common
.attemptRetrySeconds('not a number', retries
, jitter
);
119 assert
.strictEqual(result
, retries
[0]);
121 it('brackets lower range', function () {
122 const result
= common
.attemptRetrySeconds(-10, retries
, jitter
);
123 assert
.strictEqual(result
, retries
[0]);
125 it('brackets upper range', function () {
126 const result
= common
.attemptRetrySeconds(10, retries
, jitter
);
127 assert
.strictEqual(result
, retries
[retries
.length
- 1]);
129 it('covers middle', function () {
130 const result
= common
.attemptRetrySeconds(1, retries
, jitter
);
131 assert
.strictEqual(result
, retries
[1]);
133 it('covers default', function () {
134 const result
= common
.attemptRetrySeconds(0);
135 assert(result
>= 60);
136 assert(result
<= 60 * 1.618);
138 }); // attemptRetrySeconds
140 describe('arrayChunk', function () {
141 it('covers default', function () {
142 const result
= common
.arrayChunk([1, 2, 3]);
143 assert
.deepStrictEqual(result
, [[1], [2], [3]]);
145 it('covers remainders', function () {
146 const result
= common
.arrayChunk([1, 2, 3], 2);
147 assert
.deepStrictEqual(result
, [[1, 2], [3]]);
151 describe('stackSafePush', function () {
152 it('pushes', function () {
153 const bigArray
= new Array(2**18);
156 common
.stackSafePush(dst
, bigArray
);
158 assert
.strictEqual(dst
.length
, bigArray
.length
);
162 describe('logTruncate', function () {
163 it('returns short string', function () {
164 const str
= 'this is a short string';
165 const result
= common
.logTruncate(str
, 100);
166 assert
.strictEqual(result
, str
);
168 it('truncates long string', function () {
169 const str
= 'this is not really a very long string but it is long enough for this test';
170 const result
= common
.logTruncate(str
, 10);
171 assert(result
.length
< str
.length
);
175 describe('validHash', function () {
176 it('should succeed', function () {
177 const result
= common
.validHash('sha256');
178 assert
.strictEqual(result
, true);
180 it('should fail', function () {
181 const result
= common
.validHash('md5');
182 assert
.strictEqual(result
, false);
186 describe('ensureArray', function () {
187 it('returns empty array for no data', function () {
188 const result
= common
.ensureArray();
189 assert
.deepStrictEqual(result
, []);
191 it('returns same array passed in', function () {
192 const expected
= [1, 2, 3, 'foo'];
193 const result
= common
.ensureArray(expected
);
194 assert
.deepStrictEqual(result
, expected
);
196 it('returns array containing non-array data', function () {
198 const result
= common
.ensureArray(data
);
199 assert
.deepStrictEqual(result
, [data
]);