-
Notifications
You must be signed in to change notification settings - Fork 726
/
utils-test.js
176 lines (159 loc) · 6.14 KB
/
utils-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const utils = require('../lib/utils');
const assert = require('assert-diff');
const sinon = require('sinon');
const nock = require('nock');
describe('utils.between()', () => {
it('`left` positioned at the start', () => {
const rs = utils.between('<b>hello there friend</b>', '<b>', '</b>');
assert.strictEqual(rs, 'hello there friend');
});
it('somewhere in the middle', () => {
const rs = utils.between('something everything nothing', ' ', ' ');
assert.strictEqual(rs, 'everything');
});
it('not found', () => {
const rs = utils.between('oh oh _where_ is it', '<b>', '</b>');
assert.strictEqual(rs, '');
});
it('`right` before `left`', () => {
const rs = utils.between('>>> a <this> and that', '<', '>');
assert.strictEqual(rs, 'this');
});
it('`right` not found', () => {
const rs = utils.between('something [around[ somewhere', '[', ']');
assert.strictEqual(rs, '');
});
});
describe('utils.cutAfterJSON()', () => {
it('Works with simple JSON', () => {
assert.strictEqual(utils.cutAfterJSON('{"a": 1, "b": 1}'), '{"a": 1, "b": 1}');
});
it('Cut extra characters after JSON', () => {
assert.strictEqual(utils.cutAfterJSON('{"a": 1, "b": 1}abcd'), '{"a": 1, "b": 1}');
});
it('Tolerant to string constants', () => {
assert.strictEqual(utils.cutAfterJSON('{"a": "}1", "b": 1}abcd'), '{"a": "}1", "b": 1}');
});
it('Tolerant to string with escaped quoting', () => {
assert.strictEqual(utils.cutAfterJSON('{"a": "\\"}1", "b": 1}abcd'), '{"a": "\\"}1", "b": 1}');
});
it('works with nested', () => {
assert.strictEqual(
utils.cutAfterJSON('{"a": "\\"1", "b": 1, "c": {"test": 1}}abcd'),
'{"a": "\\"1", "b": 1, "c": {"test": 1}}',
);
});
it('Works with utf', () => {
assert.strictEqual(
utils.cutAfterJSON('{"a": "\\"фыва", "b": 1, "c": {"test": 1}}abcd'),
'{"a": "\\"фыва", "b": 1, "c": {"test": 1}}',
);
});
it('Works with \\\\ in string', () => {
assert.strictEqual(
utils.cutAfterJSON('{"a": "\\\\фыва", "b": 1, "c": {"test": 1}}abcd'),
'{"a": "\\\\фыва", "b": 1, "c": {"test": 1}}',
);
});
it('Works with [ as start', () => {
assert.strictEqual(
utils.cutAfterJSON('[{"a": 1}, {"b": 2}]abcd'),
'[{"a": 1}, {"b": 2}]',
);
});
it('Returns an error when not beginning with [ or {', () => {
assert.throws(() => {
utils.cutAfterJSON('abcd]}');
}, /Can't cut unsupported JSON \(need to begin with \[ or { \) but got: ./);
});
it('Returns an error when missing closing bracket', () => {
assert.throws(() => {
utils.cutAfterJSON('{"a": 1,{ "b": 1}');
}, /Can't cut unsupported JSON \(no matching closing bracket found\)/);
});
});
describe('utils.parseAbbreviatedNumber', () => {
it('Parses abbreviated numbers', () => {
assert.strictEqual(utils.parseAbbreviatedNumber('41K'), 41000);
assert.strictEqual(utils.parseAbbreviatedNumber('1.5M'), 1500000);
assert.strictEqual(utils.parseAbbreviatedNumber('8.19K '), 8190);
});
it('Parses non-abbreviated numbers', () => {
assert.strictEqual(utils.parseAbbreviatedNumber('1234'), 1234);
assert.strictEqual(utils.parseAbbreviatedNumber('123.456'), 123);
});
it('Returns `null` when given non-number', () => {
assert.strictEqual(utils.parseAbbreviatedNumber('abc'), null);
});
});
describe('utils.checkForUpdates', () => {
beforeEach(() => delete process.env.YTDL_NO_UPDATE);
after(() => process.env.YTDL_NO_UPDATE = 'true');
beforeEach(() => utils.lastUpdateCheck = Date.now());
afterEach(() => sinon.restore());
describe('Already on latest', () => {
it('Does not warn the console', async() => {
const pkg = require('../package.json');
sinon.replace(pkg, 'version', 'v1.0.0');
const scope = nock('https://api.github.com')
.get('/repos/fent/node-ytdl-core/releases/latest')
.reply(200, { tag_name: `v${pkg.version}` });
const warnSpy = sinon.spy();
sinon.replace(console, 'warn', warnSpy);
sinon.replace(Date, 'now', sinon.stub().returns(Infinity));
await utils.checkForUpdates();
scope.done();
assert.ok(warnSpy.notCalled);
});
});
describe('When there is a new update', () => {
it('Warns the console about the update', async() => {
const pkg = require('../package.json');
sinon.replace(pkg, 'version', 'v1.0.0');
const scope = nock('https://api.github.com')
.get('/repos/fent/node-ytdl-core/releases/latest')
.reply(200, { tag_name: 'vInfinity.0.0' });
const warnSpy = sinon.spy();
sinon.replace(console, 'warn', warnSpy);
sinon.replace(Date, 'now', sinon.stub().returns(Infinity));
await utils.checkForUpdates();
scope.done();
assert.ok(warnSpy.called);
assert.ok(/is out of date!/.test(warnSpy.firstCall.args[0]));
});
});
describe('Already checked recently', () => {
it('Does not make request to check', async() => {
const warnSpy = sinon.spy();
sinon.replace(console, 'warn', warnSpy);
await utils.checkForUpdates();
assert.ok(warnSpy.notCalled);
});
});
describe('With `YTDL_NO_UPDATE` env variable set', () => {
it('Does not make request to check', async() => {
process.env.YTDL_NO_UPDATE = 'true';
const warnSpy = sinon.spy();
sinon.replace(console, 'warn', warnSpy);
await utils.checkForUpdates();
assert.ok(warnSpy.notCalled);
});
});
describe('When there is an error checking for updates', () => {
it('Warns the console', async() => {
const pkg = require('../package.json');
sinon.replace(pkg, 'version', 'v1.0.0');
const scope = nock('https://api.github.com')
.get('/repos/fent/node-ytdl-core/releases/latest')
.reply(403, 'slow down there');
const warnSpy = sinon.spy();
sinon.replace(console, 'warn', warnSpy);
sinon.replace(Date, 'now', sinon.stub().returns(Infinity));
await utils.checkForUpdates();
scope.done();
assert.ok(warnSpy.called);
assert.ok(/Error checking for updates/.test(warnSpy.firstCall.args[0]));
assert.ok(/Status code: 403/.test(warnSpy.firstCall.args[1]));
});
});
});