update dependencies and devDependencies, fix lint issues
[websub-hub] / test / src / common.js
1 'use strict';
2
3 const assert = require('node:assert');
4 const common = require('../../src/common');
5
6 describe('Common', function () {
7
8 describe('freezeDeep', function () {
9 it('freezes things', function () {
10 const obj = {
11 sub1: {
12 sub2: {
13 foo: 'blah',
14 },
15 },
16 };
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));
22 });
23 }); // freezeDeep
24
25 describe('gotResponseLogData', function () {
26 it('covers', function () {
27 const response = {
28 statusCode: 200,
29 statusMessage: 'OK',
30 headers: {
31 'Content-Type': 'text/plain',
32 },
33 otherData: 'blah',
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.”',
35 timings: {
36 phases: {
37 total: 87,
38 },
39 },
40 retryCount: 2,
41 redirectUrls: ['https://example.com/clip/Thornton_Burgess'],
42 };
43 const expected = {
44 statusCode: 200,
45 statusMessage: 'OK',
46 headers: {
47 'Content-Type': 'text/plain',
48 },
49 body: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
50 elapsedTimeMs: 87,
51 retryCount: 2,
52 redirectUrls: ['https://example.com/clip/Thornton_Burgess'],
53 };
54 const result = common.gotResponseLogData(response);
55 assert.deepStrictEqual(result, expected);
56 });
57 it('covers buffer data', function () {
58 const response = {
59 statusCode: 200,
60 statusMessage: 'OK',
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.”'),
62 };
63 const expected = {
64 statusCode: 200,
65 statusMessage: 'OK',
66 body: '<Buffer 188 bytes>',
67 };
68 const result = common.gotResponseLogData(response);
69 assert.deepStrictEqual(result, expected);
70 });
71 it('covers no data', function () {
72 const response = {
73 statusCode: 200,
74 statusMessage: 'OK',
75 headers: {
76 'Content-Type': 'text/plain',
77 },
78 };
79 const expected = {
80 statusCode: 200,
81 statusMessage: 'OK',
82 headers: {
83 'Content-Type': 'text/plain',
84 },
85 };
86 const result = common.gotResponseLogData(response);
87 assert.deepStrictEqual(result, expected);
88 });
89 }); // gotResponseLogData
90
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');
100 });
101 it('cannot be changed', function () {
102 const result = common.topicLeaseDefaults();
103 const origMin = result.leaseSecondsMin;
104 try {
105 result.leaseSecondsMin += 10;
106 assert.fail('assign should fail');
107 } catch (e) {
108 assert(e instanceof TypeError);
109 }
110 assert.strictEqual(result.leaseSecondsMin, origMin);
111 });
112 }); // topicLeaseDefaults
113
114 describe('attemptRetrySeconds', function () {
115 const retries = [0, 1, 2];
116 const jitter = 0;
117 it('defaults without a number', function () {
118 const result = common.attemptRetrySeconds('not a number', retries, jitter);
119 assert.strictEqual(result, retries[0]);
120 });
121 it('brackets lower range', function () {
122 const result = common.attemptRetrySeconds(-10, retries, jitter);
123 assert.strictEqual(result, retries[0]);
124 });
125 it('brackets upper range', function () {
126 const result = common.attemptRetrySeconds(10, retries, jitter);
127 assert.strictEqual(result, retries[retries.length - 1]);
128 });
129 it('covers middle', function () {
130 const result = common.attemptRetrySeconds(1, retries, jitter);
131 assert.strictEqual(result, retries[1]);
132 });
133 it('covers default', function () {
134 const result = common.attemptRetrySeconds(0);
135 assert(result >= 60);
136 assert(result <= 60 * 1.618);
137 });
138 }); // attemptRetrySeconds
139
140 describe('arrayChunk', function () {
141 it('covers default', function () {
142 const result = common.arrayChunk([1, 2, 3]);
143 assert.deepStrictEqual(result, [[1], [2], [3]]);
144 });
145 it('covers remainders', function () {
146 const result = common.arrayChunk([1, 2, 3], 2);
147 assert.deepStrictEqual(result, [[1, 2], [3]]);
148 });
149 }); // arrayChunk
150
151 describe('stackSafePush', function () {
152 it('pushes', function () {
153 const bigArray = new Array(2**18);
154 const dst = [];
155
156 common.stackSafePush(dst, bigArray);
157
158 assert.strictEqual(dst.length, bigArray.length);
159 });
160 }); // stackSafePush
161
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);
167 });
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);
172 });
173 }); // logTruncate
174
175 describe('validHash', function () {
176 it('should succeed', function () {
177 const result = common.validHash('sha256');
178 assert.strictEqual(result, true);
179 });
180 it('should fail', function () {
181 const result = common.validHash('md5');
182 assert.strictEqual(result, false);
183 });
184 }); // validHash
185
186 describe('ensureArray', function () {
187 it('returns empty array for no data', function () {
188 const result = common.ensureArray();
189 assert.deepStrictEqual(result, []);
190 });
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);
195 });
196 it('returns array containing non-array data', function () {
197 const data = 'bar';
198 const result = common.ensureArray(data);
199 assert.deepStrictEqual(result, [data]);
200 });
201 }); // ensureArray
202
203 }); // Common