1 | 'use strict'
|
2 |
|
3 | const DownloadYTAudio = require('../lib/download-audio')
|
4 | const fue = require('file-utils-easy')
|
5 | const path = require('path')
|
6 |
|
7 | const OUT_DIR = 'test/out'
|
8 | const TEST_PLAYLIST = {
|
9 | singleSong: { id: 'ZIyyj2FrVI0', length: 1 },
|
10 | twoSongs: { id: 'PLAv2aQ9JgGbVcUtDpuiTB9WgaMljCUpa_', length: 2 },
|
11 | twoSongsDuplicated: { id: 'PLAv2aQ9JgGbVEzvdzyoigNgnJcvOtMmU0', length: 2 },
|
12 | hundredSongs: { id: 'PLzCxunOM5WFKZuBXTe8EobD6Dwi4qV-kO', length: 100 }
|
13 | }
|
14 |
|
15 | describe('Core lib', () => {
|
16 | let downloader
|
17 |
|
18 | beforeAll(() => {
|
19 | downloader = new DownloadYTAudio({ outputPath: OUT_DIR })
|
20 | return fue.deleteDirectoryFiles(OUT_DIR)
|
21 | })
|
22 | afterAll(() => {
|
23 | downloader.removeAllListeners()
|
24 | return fue.deleteDirectoryFiles(OUT_DIR)
|
25 | })
|
26 |
|
27 | async function downloadPlaylist (testPlaylist) {
|
28 | let startEvents = 0
|
29 | let progressEvents = 0
|
30 | let completeEvents = 0
|
31 | let errorEvents = 0
|
32 |
|
33 | downloader.on('start', () => startEvents++)
|
34 | downloader.on('progress', () => progressEvents++)
|
35 | downloader.on('complete', () => completeEvents++)
|
36 | downloader.on('error', () => errorEvents++)
|
37 |
|
38 | const result = await downloader.downloadPlaylist(testPlaylist.id)
|
39 |
|
40 | expect(result).toHaveLength(testPlaylist.length)
|
41 | expect(startEvents).toEqual(testPlaylist.length)
|
42 | expect(completeEvents + errorEvents).toEqual(testPlaylist.length)
|
43 | expect(progressEvents).toBeGreaterThanOrEqual(1)
|
44 |
|
45 | result.forEach((song) => {
|
46 | expect(song.id).toBeDefined()
|
47 | expect(song.fileName || song.error).toBeDefined()
|
48 | expect(song.ref).toBeDefined()
|
49 | if (song.path) {
|
50 | expect(song.path).toEqual(OUT_DIR)
|
51 | }
|
52 | })
|
53 | }
|
54 |
|
55 | it('download small playlist', () => downloadPlaylist(TEST_PLAYLIST.twoSongs), 120000)
|
56 |
|
57 |
|
58 | it.skip('download huge playlist', () => downloadPlaylist(TEST_PLAYLIST.hundredSongs), 1800000)
|
59 |
|
60 | it('download single song', async () => {
|
61 | let startEvents = 0
|
62 | let progressEvents = 0
|
63 | let completeEvents = 0
|
64 | let errorEvents = 0
|
65 |
|
66 | downloader.on('start', () => startEvents++)
|
67 | downloader.on('progress', () => progressEvents++)
|
68 | downloader.on('complete', () => completeEvents++)
|
69 | downloader.on('error', () => errorEvents++)
|
70 |
|
71 | const result = await downloader.download(TEST_PLAYLIST.singleSong.id)
|
72 |
|
73 | expect(result).toMatchObject({ id: TEST_PLAYLIST.singleSong.id, path: OUT_DIR, result: 'conversionFileComplete' })
|
74 | expect(result.fileName).toMatch(`${result.ref.title}.mp3`)
|
75 | expect(startEvents).toEqual(TEST_PLAYLIST.singleSong.length)
|
76 | expect(completeEvents).toEqual(TEST_PLAYLIST.singleSong.length)
|
77 | expect(progressEvents).toBeGreaterThanOrEqual(TEST_PLAYLIST.singleSong.length)
|
78 | expect(errorEvents).toEqual(0)
|
79 | }, 120000)
|
80 |
|
81 | it('get video info', async () => {
|
82 | const info = await downloader.getVideoInfo(TEST_PLAYLIST.singleSong.id)
|
83 | const compareTo = {
|
84 | id: 'ZIyyj2FrVI0',
|
85 | url_simple: 'http://youtube.com/watch?v=ZIyyj2FrVI0',
|
86 | title: '[No Copyright Music] Cloudy - KODOMOi',
|
87 | duration: '3:24',
|
88 | author: null
|
89 | }
|
90 | expect(info).toMatchObject(compareTo)
|
91 | expect(info.url).toBeDefined()
|
92 | expect(info.thumbnail).toBeDefined()
|
93 | }, 15000)
|
94 |
|
95 | it('get video info error', async () => {
|
96 | let error
|
97 | try {
|
98 | await downloader.getVideoInfo('error')
|
99 | } catch (err) {
|
100 | error = err
|
101 | }
|
102 | expect(error).toBeDefined()
|
103 | })
|
104 |
|
105 | it('get playlist info', async () => {
|
106 | const info = await downloader.getPlaylistInfo(TEST_PLAYLIST.hundredSongs.id)
|
107 | expect(info.id).toEqual(TEST_PLAYLIST.hundredSongs.id)
|
108 | expect(info.items).toHaveLength(TEST_PLAYLIST.hundredSongs.length)
|
109 | info.items.forEach((i) => {
|
110 | expect(i.id).toBeDefined()
|
111 | expect(i.title).toBeDefined()
|
112 | })
|
113 | }, 10000)
|
114 |
|
115 | it('get playlist info error', async () => {
|
116 | let error
|
117 | try {
|
118 | await downloader.getPlaylistInfo('error')
|
119 | } catch (err) {
|
120 | error = err
|
121 | }
|
122 | expect(error).toBeDefined()
|
123 | })
|
124 |
|
125 | it('overwrite configuration', async () => {
|
126 | let startEvents = 0
|
127 | let progressEvents = 0
|
128 | let completeEvents = 0
|
129 | let errorEvents = 0
|
130 |
|
131 | downloader.on('start', () => startEvents++)
|
132 | downloader.on('progress', () => progressEvents++)
|
133 | downloader.on('complete', () => completeEvents++)
|
134 | downloader.on('error', () => errorEvents++)
|
135 |
|
136 | const mockCustomFilename = jest.fn((title) => {
|
137 | return 'overwrite.mp3'
|
138 | })
|
139 |
|
140 | downloader.overwriteExistingFiles = false
|
141 | downloader.fileNameGenerator = mockCustomFilename
|
142 | const result = await downloader.download(TEST_PLAYLIST.singleSong.id)
|
143 |
|
144 | let overwriteError
|
145 | try {
|
146 | await downloader.download(TEST_PLAYLIST.singleSong.id)
|
147 | } catch (error) {
|
148 | overwriteError = error
|
149 | }
|
150 | expect(overwriteError).toBeDefined()
|
151 | expect(mockCustomFilename).toHaveBeenCalledTimes(2)
|
152 | expect(result).toMatchObject({ fileName: mockCustomFilename(), id: TEST_PLAYLIST.singleSong.id, path: OUT_DIR, result: 'conversionFileComplete' })
|
153 | expect(startEvents).toEqual(TEST_PLAYLIST.singleSong.length)
|
154 | expect(completeEvents).toEqual(TEST_PLAYLIST.singleSong.length)
|
155 | expect(errorEvents).toEqual(1)
|
156 | expect(progressEvents).toBeGreaterThanOrEqual(1)
|
157 | }, 120000)
|
158 |
|
159 | it('download playlist with errors', async () => {
|
160 | let startEvents = 0
|
161 | let progressEvents = 0
|
162 | let completeEvents = 0
|
163 | let errorEvents = 0
|
164 |
|
165 | downloader.on('start', () => startEvents++)
|
166 | downloader.on('progress', () => progressEvents++)
|
167 | downloader.on('complete', () => completeEvents++)
|
168 | downloader.on('error', () => errorEvents++)
|
169 |
|
170 | const mockCustomFilename = jest.fn(() => 'error-test.mp3')
|
171 | downloader.overwriteExistingFiles = false
|
172 | downloader.fileNameGenerator = mockCustomFilename
|
173 | await fue.writeToFile('', path.join(OUT_DIR, 'error-test.mp3'))
|
174 | const result = await downloader.downloadPlaylist(TEST_PLAYLIST.twoSongsDuplicated.id)
|
175 |
|
176 | expect(result).toHaveLength(TEST_PLAYLIST.twoSongsDuplicated.length)
|
177 | expect(startEvents).toEqual(0)
|
178 | expect(completeEvents).toEqual(0)
|
179 | expect(errorEvents).toEqual(2)
|
180 | expect(result.find((song) => song.error !== undefined)).toBeDefined()
|
181 | }, 120000)
|
182 |
|
183 | it('ffmpeg error', async () => {
|
184 | process.env.PATH = ``
|
185 | let errorEvents = 0
|
186 | downloader.on('error', (errorEvent) => {
|
187 | errorEvents++
|
188 | expect(errorEvent.result).toBeDefined()
|
189 | })
|
190 |
|
191 | let conversionError
|
192 | try {
|
193 | await downloader.download(TEST_PLAYLIST.singleSong.id)
|
194 | } catch (error) {
|
195 | conversionError = error
|
196 | }
|
197 | expect(errorEvents).toEqual(1)
|
198 | expect(conversionError).toBeDefined()
|
199 | expect(conversionError.result).toEqual('internalServerErrorOnProcessingVideo.')
|
200 | }, 15000)
|
201 | })
|