update dependencies
[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('axiosResponseLogData', function () {
27 it('covers', function () {
28 const response = {
29 status: 200,
30 statusText: 'OK',
31 headers: {
32 'Content-Type': 'text/plain',
33 },
34 otherData: 'blah',
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.”',
36 };
37 const expected = {
38 status: 200,
39 statusText: 'OK',
40 headers: {
41 'Content-Type': 'text/plain',
42 },
43 data: 'Old Mother West Wind had stopped to talk with the Slender Fir Tree. "I\'ve just come across the Green... (184 bytes)',
44 };
45 const result = common.axiosResponseLogData(response);
46 assert.deepStrictEqual(result, expected);
47 });
48 it('covers no data', function () {
49 const response = {
50 status: 200,
51 statusText: 'OK',
52 headers: {
53 'Content-Type': 'text/plain',
54 },
55 };
56 const expected = {
57 status: 200,
58 statusText: 'OK',
59 headers: {
60 'Content-Type': 'text/plain',
61 },
62 };
63 const result = common.axiosResponseLogData(response);
64 assert.deepStrictEqual(result, expected);
65 });
66 }); // axiosResponseLogData
67
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');
77 });
78 it('cannot be changed', function () {
79 const result = common.topicLeaseDefaults();
80 const origMin = result.leaseSecondsMin;
81 try {
82 result.leaseSecondsMin += 10;
83 assert.fail('assign should fail');
84 } catch (e) {
85 assert(e instanceof TypeError);
86 }
87 assert.strictEqual(result.leaseSecondsMin, origMin);
88 });
89 }); // topicLeaseDefaults
90
91 describe('attemptRetrySeconds', function () {
92 const retries = [0, 1, 2];
93 const jitter = 0;
94 it('defaults without a number', function () {
95 const result = common.attemptRetrySeconds('not a number', retries, jitter);
96 assert.strictEqual(result, retries[0]);
97 });
98 it('brackets lower range', function () {
99 const result = common.attemptRetrySeconds(-10, retries, jitter);
100 assert.strictEqual(result, retries[0]);
101 });
102 it('brackets upper range', function () {
103 const result = common.attemptRetrySeconds(10, retries, jitter);
104 assert.strictEqual(result, retries[retries.length - 1]);
105 });
106 it('covers middle', function () {
107 const result = common.attemptRetrySeconds(1, retries, jitter);
108 assert.strictEqual(result, retries[1]);
109 });
110 it('covers default', function () {
111 const result = common.attemptRetrySeconds(0);
112 assert(result >= 60);
113 assert(result <= 60 * 1.618)
114 });
115 }); // attemptRetrySeconds
116
117 describe('arrayChunk', function () {
118 it('covers default', function () {
119 const result = common.arrayChunk([1, 2, 3]);
120 assert.deepStrictEqual(result, [[1], [2], [3]]);
121 });
122 it('covers remainders', function () {
123 const result = common.arrayChunk([1, 2, 3], 2);
124 assert.deepStrictEqual(result, [[1, 2], [3]]);
125 });
126 }); // arrayChunk
127
128 describe('stackSafePush', function () {
129 it('pushes', function () {
130 const bigArray = new Array(2**18);
131 const dst = [];
132
133 common.stackSafePush(dst, bigArray);
134
135 assert.strictEqual(dst.length, bigArray.length);
136 });
137 }); // stackSafePush
138
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);
144 });
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);
149 });
150 }); // logTruncate
151
152 describe('validHash', function () {
153 it('should succeed', function () {
154 const result = common.validHash('sha256');
155 assert.strictEqual(result, true);
156 });
157 it('should fail', function () {
158 const result = common.validHash('md5');
159 assert.strictEqual(result, false);
160 });
161 }); // validHash
162
163 describe('ensureArray', function () {
164 it('returns empty array for no data', function () {
165 const result = common.ensureArray();
166 assert.deepStrictEqual(result, []);
167 });
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);
172 });
173 it('returns array containing non-array data', function () {
174 const data = 'bar';
175 const result = common.ensureArray(data);
176 assert.deepStrictEqual(result, [data]);
177 });
178 }); // ensureArray
179
180 }); // Common