This repository has been archived by the owner on Sep 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 281
/
live-stream.spec.ts
213 lines (194 loc) · 6.61 KB
/
live-stream.spec.ts
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import MockAdapter from 'axios-mock-adapter';
import { assert } from 'chai';
import { describe, it } from 'mocha';
import TikTokAPI, {
BaseResponseData,
CanStartLiveStreamResponse,
CommonUserDetails,
CreateLiveStreamRoomResponse,
JoinLiveStreamResponse,
UpdateLiveStreamStatusResponse,
} from '../src';
import {
loadTestData,
mockConfig,
mockParams,
} from './util';
describe('#joinLiveStream()', () => {
it('a successful response should match the interface', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onGet(new RegExp('aweme/v1/room/enter/\?.*'))
.reply(200, loadTestData('joinLiveStream.json'), {});
const roomId = '9999999999999999999';
const streamId = '9000000000000000000';
const res = await api.joinLiveStream(roomId);
const expected: JoinLiveStreamResponse = {
extra: {
now: 1000000000000,
},
room: {
create_time: 1000000000,
finish_time: 1000000001,
owner: {} as CommonUserDetails,
room_id: roomId,
status: 0,
stream_id: streamId,
stream_url: {
rtmp_pull_url: `http://pull-flv-l1-mus.pstatp.com/hudong/stream-${streamId}.flv`,
sid: streamId,
},
title: 'Example',
user_count: 1000,
},
status_code: 0,
};
assert.deepStrictEqual(res.data, expected);
});
});
describe('#leaveLiveStream()', () => {
it('a successful response should match the interface', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onGet(new RegExp('aweme/v1/room/leave/\?.*'))
.reply(200, loadTestData('leaveLiveStream.json'), {});
const roomId = '9999999999999999999';
const res = await api.leaveLiveStream(roomId);
const expected: BaseResponseData = {
extra: {
now: 1000000000000,
},
status_code: 0,
};
assert.deepStrictEqual(res.data, expected);
});
});
describe('#canStartLiveStream()', () => {
it('a successful response should match the interface', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onGet(new RegExp('aweme/v1/live/podcast/\?.*'))
.reply(200, loadTestData('canStartLiveStream.json'), {});
const res = await api.canStartLiveStream();
const expected: CanStartLiveStreamResponse = {
extra: {
now: 1000000000000,
},
can_be_live_podcast: true,
status_code: 0,
};
assert.deepStrictEqual(res.data, expected);
});
});
describe('#createLiveStreamRoom()', () => {
it('a successful response should match the interface', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onPost(new RegExp('aweme/v1/room/create/\?.*'))
.reply(200, loadTestData('createLiveStreamRoom.json'), {});
const roomId = '9999999999999999999';
const streamId = '9000000000000000000';
const title = 'TITLE';
const res = await api.createLiveStreamRoom(title);
const expected: CreateLiveStreamRoomResponse = {
extra: {
now: 1000000000000,
},
room: {
title,
create_time: 1000000000,
finish_time: 1000000000,
owner: {} as CommonUserDetails,
room_id: roomId,
status: 0,
stream_id: streamId,
stream_url: {
rtmp_pull_url: `http://pull-flv-l1-mus.pstatp.com/hudong/stream-${streamId}.flv`,
rtmp_push_url: `rtmp://push-rtmp-l1-mus.pstatp.com/hudong/stream-${streamId}?wsSecret=1234&wsTime=1a1a1a1a`,
sid: streamId,
},
user_count: 0,
},
status_code: 0,
};
assert.deepStrictEqual(res.data, expected);
});
});
describe('#startLiveStream()', () => {
it('a successful response should match the interface', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onPost(new RegExp('aweme/v1/room/create/\?.*'))
.reply(200, loadTestData('createLiveStreamRoom.json'), {});
mock
.onGet(new RegExp('aweme/v1/room/update/status/\?.*'))
.reply(200, loadTestData('updateLiveStreamStatus.json'), {});
const roomId = '9999999999999999999';
const streamId = '9000000000000000000';
const title = 'TITLE';
const res = await api.startLiveStream(title);
const expected: CreateLiveStreamRoomResponse = {
extra: {
now: 1000000000000,
},
room: {
title,
create_time: 1000000000,
finish_time: 1000000000,
owner: {} as CommonUserDetails,
room_id: roomId,
status: 0,
stream_id: streamId,
stream_url: {
rtmp_pull_url: `http://pull-flv-l1-mus.pstatp.com/hudong/stream-${streamId}.flv`,
rtmp_push_url: `rtmp://push-rtmp-l1-mus.pstatp.com/hudong/stream-${streamId}?wsSecret=1234&wsTime=1a1a1a1a`,
sid: streamId,
},
user_count: 0,
},
status_code: 0,
};
assert.deepStrictEqual(res.data, expected);
});
it('should throw an error if the live stream room could not be created', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onPost(new RegExp('aweme/v1/room/create/\?.*'))
.reply(200, { status_code: 3 }, {});
assert.isRejected(api.startLiveStream('TITLE'), /could not be created/);
});
it('should throw an error if the live stream could not be started', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onPost(new RegExp('aweme/v1/room/create/\?.*'))
.reply(200, loadTestData('createLiveStreamRoom.json'), {});
mock
.onGet(new RegExp('aweme/v1/room/update/status/\?.*'))
.reply(200, { status_code: 3 }, {});
assert.isRejected(api.startLiveStream('TITLE'), /could not be started/);
});
});
describe('#endLiveStream()', () => {
it('a successful response should match the interface', async () => {
const api = new TikTokAPI(mockParams, mockConfig);
const mock = new MockAdapter(api.request);
mock
.onGet(new RegExp('aweme/v1/room/update/status/\?.*'))
.reply(200, loadTestData('updateLiveStreamStatus.json'), {});
const res = await api.endLiveStream('9999999999999999999', '9000000000000000000');
const expected: UpdateLiveStreamStatusResponse = {
extra: {
now: 1000000000000,
},
status_code: 0,
};
assert.deepStrictEqual(res.data, expected);
});
});