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