-
Notifications
You must be signed in to change notification settings - Fork 723
/
info-extras-test.js
180 lines (158 loc) · 5.7 KB
/
info-extras-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
177
178
179
180
const assert = require('assert-diff');
const extras = require('../lib/info-extras');
const assertURL = url => {
assert.ok(/^https?:\/\//.test(url), `Not a URL: ${url}`);
};
const assertChannelURL = url => {
assert.ok(/^https?:\/\/www\.youtube\.com\/channel\/[a-zA-Z0-9_-]+$/.test(url), `Not a channel URL: ${url}`);
};
const assertUserID = str => {
assert.ok(/^[a-zA-Z0-9_-]+$/.test(str), `Not a user id: ${str}`);
};
const assertUserName = str => {
assert.ok(/^[a-zA-Z0-9_-]+$/.test(str), `Not a username: ${str}`);
};
const assertUserURL = url => {
assert.ok(/^https?:\/\/www\.youtube\.com\/(user|channel)\/[a-zA-Z0-9_-]+$/.test(url), `Not a user URL: ${url}`);
};
describe('extras.getAuthor()', () => {
it('Returns video author object', () => {
const info = require('./files/videos/vevo/expected-info.json');
const author = extras.getAuthor(info);
assert.ok(author);
assertURL(author.avatar);
assertChannelURL(author.channel_url);
assertChannelURL(author.external_channel_url);
assertUserID(author.id);
assertUserName(author.user);
assert.ok(author.name);
assertUserURL(author.user_url);
assert.strictEqual(typeof author.verified, 'boolean');
assert.number(author.subscriber_count);
});
describe('watch page without author', () => {
it('Returns empty object if author not found', () => {
const info = require(
'./files/videos/regular/watch-no-extras.json');
const author = extras.getAuthor(info);
assert.deepEqual(author, {});
});
});
describe('from a rental', () => {
it('Returns video author object', () => {
const info = require('./files/videos/rental/expected-info.json');
const author = extras.getAuthor(info);
assert.ok(author);
assertURL(author.avatar);
assertChannelURL(author.channel_url);
assertChannelURL(author.external_channel_url);
assertUserID(author.id);
assertUserName(author.user);
assert.ok(author.name);
assertUserURL(author.user_url);
assert.strictEqual(typeof author.verified, 'boolean');
assert.ok(!author.subscriber_count);
});
});
});
describe('extras.getMedia()', () => {
it('Returns media object', () => {
const info = require('./files/videos/vevo/expected-info.json');
const media = extras.getMedia(info);
assert.ok(media);
assert.strictEqual(media.artist, 'Wu-Tang Clan');
assertChannelURL(media.artist_url);
assert.strictEqual(media.category, 'Music');
assertURL(media.category_url);
});
describe('On a video associated with a game', () => {
it('Returns media object', () => {
const info = require('./files/videos/game/expected-info.json');
const media = extras.getMedia(info);
assert.ok(media);
assert.strictEqual(media.category, 'Gaming');
assertURL(media.category_url);
assert.strictEqual(media.game, 'Pokémon Snap');
assertURL(media.game_url);
assert.strictEqual(media.year, '1999');
});
});
describe('With invalid input', () => {
it('Should return an empty object', () => {
const media = extras.getMedia({ invalidObject: '' });
assert.ok(media);
assert.deepEqual(media, {});
});
});
});
describe('extras.getRelatedVideos()', () => {
it('Returns related videos', () => {
const info = require('./files/videos/related2/expected-info.json');
const relatedVideos = extras.getRelatedVideos(info);
assert.ok(relatedVideos && relatedVideos.length > 0);
for (let video of relatedVideos) {
assert.ok(video.id);
assert.ok(video.author);
assert.ok(video.title);
assert.ok(video.length_seconds);
assertURL(video.video_thumbnail);
}
});
describe('Without `rvs` params', () => {
it('Still able to find video params', () => {
const info = require('./files/videos/related/expected-info-no-rvs.json');
const relatedVideos = extras.getRelatedVideos(info);
for (let video of relatedVideos) {
assert.ok(video.id);
assert.ok(video.author);
assert.ok(video.title);
assert.ok(video.length_seconds);
assertURL(video.video_thumbnail);
}
});
});
describe('Without `secondaryResults`', () => {
it('Unable to find any videos', () => {
const info = require('./files/videos/related/expected-info-no-results.json');
const relatedVideos = extras.getRelatedVideos(info);
assert.ok(relatedVideos);
assert.deepEqual(relatedVideos, []);
});
});
describe('With an unparseable video', () => {
it('Catches errors', () => {
const info = require(
'./files/videos/related/watch-bad-details.json');
const relatedVideos = extras.getRelatedVideos(info);
assert.deepEqual(relatedVideos, []);
});
});
});
describe('extras.getLikes()', () => {
it('Returns like count', () => {
const info = require('./files/videos/regular/watch.json')[3];
const likes = extras.getLikes(info);
assert.strictEqual(typeof likes, 'number');
});
describe('With no likes', () => {
it('Does not return likes', () => {
const info = require('./files/videos/no-likes-or-dislikes/watch.json')[3];
const likes = extras.getLikes(info);
assert.strictEqual(likes, null);
});
});
});
describe('extras.getDislikes()', () => {
it('Returns dislike count', () => {
const info = require('./files/videos/regular/watch.json')[3];
const dislikes = extras.getDislikes(info);
assert.strictEqual(typeof dislikes, 'number');
});
describe('With no dislikes', () => {
it('Does not return dislikes', () => {
const info = require('./files/videos/no-likes-or-dislikes/watch.json')[3];
const dislikes = extras.getDislikes(info);
assert.strictEqual(dislikes, null);
});
});
});