Skip to content

Commit bb05c11

Browse files
committed
Write more tests and fix destructuring/viewer bugs
1 parent 9ad9328 commit bb05c11

File tree

5 files changed

+109
-23
lines changed

5 files changed

+109
-23
lines changed

lib/commands/implementations/live.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ function live(input, services) {
2222
};
2323

2424
const totalViewers = () => {
25-
return data.filter((s) => s?.live).reduce((acc, curr) => acc + curr, 0);
25+
return data.filter((s) => s?.live).reduce((acc, s) => acc + s.viewers, 0);
2626
};
2727

2828
if (!isLive()) {
29-
const { duration, ended_at } = newestEndedStream();
29+
const { duration, ended_at } = newestEndedStream() ?? {};
3030
if (duration && ended_at) {
3131
const endedAgo = formatDuration(moment.duration(now.diff(ended_at)));
3232
const friendlyDuration = formatDuration(moment.duration(duration, 'seconds'));

tests/lib/commands/implementations/live.test.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const live = require('../../../../lib/commands/implementations/live');
33
const CommandOutput = require('../../../../lib/commands/command-output');
44
const assert = require('assert');
55
const streamInfoOffline = require('./mocks/stream-info-offline.json');
6+
const streamInfoOfflineNull = require('./mocks/stream-info-offline-null.json');
7+
const streamInfoOnline = require('./mocks/stream-info-online.json');
68

79
describe('!live Test', () => {
810
const buildMockServices = (dggApiResponse) => {
@@ -18,10 +20,24 @@ describe('!live Test', () => {
1820
it('responds correctly when stream is offline', function () {
1921
const expected = new CommandOutput(
2022
null,
21-
`Stream was last online 2 days 11h ago. Time Streamed: 4h 34m.`,
23+
`Stream was last online 2 days 11h ago. Time Streamed: 4h 35m.`,
2224
);
2325
return live.work(null, buildMockServices(streamInfoOffline)).then((response) => {
2426
assert.deepStrictEqual(response, expected);
2527
});
2628
});
29+
30+
it('responds correctly when all streams are null', function () {
31+
const expected = new CommandOutput(null, 'Stream is offline.');
32+
return live.work(null, buildMockServices(streamInfoOfflineNull)).then((response) => {
33+
assert.deepStrictEqual(response, expected);
34+
});
35+
});
36+
37+
it('responds with start time of oldest stream and cumulative viewers', function () {
38+
const expected = new CommandOutput(null, `Viewers: 17018. Stream live as of 2 days 16h ago.`);
39+
return live.work(null, buildMockServices(streamInfoOnline)).then((response) => {
40+
assert.deepStrictEqual(response, expected);
41+
});
42+
});
2743
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"data": {
3+
"streams": {
4+
"twitch": null,
5+
"youtube": null,
6+
"facebook": null,
7+
"rumble": null,
8+
"kick": null
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
{
2-
"data": {
3-
"streams": {
4-
"twitch": null,
5-
"youtube": {
6-
"live": false,
7-
"game": null,
8-
"preview": "https://i.ytimg.com/vi/HmCHsE9LYUI/mqdefault.jpg",
9-
"status_text": "Okay I lied, second short stream today, leaving @ 6 PM | Random videos/articles/memes",
10-
"started_at": "2023-04-13T16:23:41+0000",
11-
"ended_at": "2023-04-13T20:58:13+0000",
12-
"duration": 16472,
13-
"viewers": null,
14-
"id": null,
15-
"platform": "youtube",
16-
"type": "livestream"
17-
},
18-
"facebook": null,
19-
"rumble": null,
20-
"kick": null
21-
}
2+
"data": {
3+
"streams": {
4+
"twitch": {
5+
"live": false,
6+
"game": "Just Chatting",
7+
"preview": null,
8+
"status_text": "Okay I lied, second short stream today, leaving @ 6 PM | Random videos/articles/memes",
9+
"started_at": "2023-04-13T16:23:41+0000",
10+
"ended_at": "2023-04-13T20:59:13+0000",
11+
"duration": 16532,
12+
"viewers": null,
13+
"id": null,
14+
"platform": "twitch",
15+
"type": null
16+
},
17+
"youtube": {
18+
"live": false,
19+
"game": null,
20+
"preview": "https://i.ytimg.com/vi/HmCHsE9LYUI/mqdefault.jpg",
21+
"status_text": "Okay I lied, second short stream today, leaving @ 6 PM | Random videos/articles/memes",
22+
"started_at": "2023-04-13T16:23:41+0000",
23+
"ended_at": "2023-04-13T20:58:13+0000",
24+
"duration": 16472,
25+
"viewers": null,
26+
"id": null,
27+
"platform": "youtube",
28+
"type": "livestream"
29+
},
30+
"facebook": null,
31+
"rumble": null,
32+
"kick": null
2233
}
34+
}
2335
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"data": {
3+
"streams": {
4+
"twitch": null,
5+
"youtube": {
6+
"live": true,
7+
"game": null,
8+
"preview": "https://i.ytimg.com/vi/HmCHsE9LYUI/mqdefault.jpg",
9+
"status_text": "Okay I lied, second short stream today, leaving @ 6 PM | Random videos/articles/memes",
10+
"started_at": "2023-04-13T16:23:41+0000",
11+
"ended_at": null,
12+
"duration": 16472,
13+
"viewers": 5183,
14+
"id": "zOhOMLCuCW0",
15+
"platform": "youtube",
16+
"type": "livestream"
17+
},
18+
"facebook": null,
19+
"rumble": {
20+
"live": false,
21+
"game": null,
22+
"preview": null,
23+
"status_text": "Obamna",
24+
"started_at": "2023-04-10T13:21:40+0000",
25+
"ended_at": "2023-04-10T16:23:48+0000",
26+
"duration": 10928,
27+
"viewers": null,
28+
"id": null,
29+
"platform": "rumble",
30+
"type": null
31+
},
32+
"kick": {
33+
"live": true,
34+
"game": null,
35+
"preview": null,
36+
"status_text": "Okay I lied, second short stream today, leaving @ 6 PM | Random videos/articles/memes",
37+
"started_at": "2023-04-13T16:20:41+0000",
38+
"ended_at": null,
39+
"duration": 16652,
40+
"viewers": 11835,
41+
"id": "https://fa723fc1b171.us-west-2.playback.live-video.net/api/video/v1/us-west-2.196233775518.channel.0PMYoN0I2p4i.m3u8",
42+
"platform": "kick",
43+
"type": null
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)