-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.js
248 lines (189 loc) · 6.72 KB
/
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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
'use strict'
const path = require('path')
const util = require('util')
const fs = require('fs-extra')
const liburl = require('url')
const genThumbnail = require('../')
const test = require('ava')
const ffmpeg = require('ffmpeg-static')
const looksSame = util.promisify(require('looks-same'))
const nock = require('nock')
const absPath = rel => path.join(__dirname, rel)
async function badSizeStringMacro (t, sizeString, expectedErrMsg) {
const filePath = absPath('./this/should/not/matter')
const outPath = absPath('./foo/bar')
try {
await genThumbnail(filePath, outPath, sizeString)
t.fail()
} catch (err) {
t.is(err.message, expectedErrMsg)
}
}
async function imageTestMacro (t, { input, size, title, config }, pathToExpected) {
if (!title) {
throw new Error('Missing image title')
}
input = input || absPath('./data/bunny.webm')
size = size || '50x?'
config = config || {}
pathToExpected = pathToExpected || absPath('./expected/tiny.png')
const output = absPath(`./out/${title}.png`)
await genThumbnail(input, output, size, config)
const isSame = await looksSame(pathToExpected, output, { tolerance: 5 })
t.true(isSame)
}
function imageCreationMacro (t, { input, output, size, config }) {
input = input || absPath('./data/bunny.webm')
size = size || '50x?'
config = config || {}
return genThumbnail(input, output, size, config)
.then(t.pass)
.catch(t.fail)
}
async function httpMediaTestMacro (t, { url, title }) {
if (!title) {
throw new Error('Missing image title')
}
const parsedUrl = liburl.parse(url)
const output = absPath(`./out/${title}.png`)
nock(`${parsedUrl.protocol}//${parsedUrl.host}`)
.get(parsedUrl.path)
.replyWithFile(200, absPath('./data/bunny.webm'), { 'Content-Type': 'video/webm' })
await genThumbnail(url, output, '50x?')
const isSame = await looksSame(absPath('./expected/tiny.png'), output, { tolerance: 5 })
t.true(isSame)
}
async function withArgsMacro (t, { config, output }) {
await genThumbnail(null, null, null, config)
const textImageIsSame = await looksSame(absPath('./expected/args-text.png'), output, { tolerance: 5 })
t.true(textImageIsSame)
}
function streamReturnMacro (t, { input, title }) {
const output = absPath(`./out/${title}.png`)
const write = fs.createWriteStream(output)
genThumbnail(input, null, '50x?').then(stream => {
stream.pipe(write)
write.on('finish', t.end)
write.on('error', t.end)
})
}
test.before(async t => {
await fs.mkdirp(absPath('./out'))
})
test('throws error on malformed size string', badSizeStringMacro, 'bad size', 'Invalid size string')
test('throws error given a percentage string with no value', badSizeStringMacro, '%', 'Invalid size string')
test('throws error given a "?x?" size string', badSizeStringMacro, '?x?', 'Invalid size string')
test('throws a ffmpeg stderr dumb on non-zero exit', async t => {
const promise = genThumbnail('not real path', './foo/bar', '200x200')
const err = await t.throwsAsync(promise)
const lines = err.message.split('\n')
t.is(lines[0], 'ffmpeg exited 1')
t.is(lines[1], 'ffmpeg stderr:')
})
test('creates thumbnails for files saved on disk', imageTestMacro, {
input: absPath('./data/bunny.webm'),
title: 'disk'
})
test('creates thumbnails for streams', imageTestMacro, {
input: fs.createReadStream(absPath('./data/bunny.webm')),
title: 'stream'
})
;(function () {
const title = 'args'
const text = absPath('./data/text.txt')
const output = absPath(`./out/${title}.png`)
const fontPath = absPath('./utils/opensans.ttf')
test('can pass args to ffmpeg via config', withArgsMacro, {
config: {
args: [
'-f lavfi',
'-i color=c=white:s=640x480:d=5.396',
`-filter_complex "drawtext=textfile='${text}':x=0:y=0:fontsize=30:fontcolor=ff00ff:fontfile=${fontPath}"`,
'-y',
output
],
path: ffmpeg.path
},
output
})
})()
test('creates thumbnails for http', httpMediaTestMacro, {
url: 'http://www.w3schools.com/html/mov_bbb.webm',
title: 'http'
})
test('creates thumbnails for https', httpMediaTestMacro, {
url: 'https://www.w3schools.com/html/mov_bbb.webm',
title: 'https'
})
test('can create thumbnails with spaces, from files with spaces', imageTestMacro, {
input: absPath('./data/File With A Space.webm'),
title: 'File With A Space'
})
test('can create thumbnails from webm', imageTestMacro, {
input: absPath('./data/bunny.webm'),
title: 'webm'
})
test('can create thumbnails from mp4', imageTestMacro, {
input: absPath('./data/bunny.mp4'),
title: 'mp4'
})
test('can seek to an arbitrary time', imageTestMacro, {
config: { seek: '00:00:00.900' },
title: 'seek'
}, absPath('./expected/seek.png'))
test('operates when ffmpeg path prevent via config', imageTestMacro, {
config: { path: ffmpeg.path },
title: 'path'
})
test('operates when ffmpeg path specified via env var', async t => {
process.env.FFMPEG_PATH = ffmpeg.path
const input = absPath('./data/bunny.webm')
const output = absPath('./out/envvar.png')
await genThumbnail(input, output, '50x?')
const isSame = await looksSame(absPath('./expected/tiny.png'), output, { tolerance: 5 })
t.true(isSame)
process.env.FFMPEG_PATH = ''
})
test('throws error when seek is outside range', async t => {
const input = absPath('./data/bunny.webm')
const output = absPath('./out/doesnotmatter.png')
try {
await genThumbnail(input, output, '50x?', { seek: '00:05:00' })
t.fail()
} catch (err) {
console.log(err)
t.pass()
}
})
// Currently doesn't save in out folder since there's some weird race condition
test('writes to a file via a write-stream', imageCreationMacro, {
output: fs.createWriteStream(absPath('./write.png'))
})
test.cb('returns a duplex stream for single argument', t => {
const readStream = fs.createReadStream(absPath('./data/bunny.webm'))
const writeStream = fs.createWriteStream(absPath('./duplex.jpg'))
readStream
.pipe(genThumbnail(null, null, '250x?'))
.pipe(writeStream)
.on('finish', t.end)
})
test.cb('returns a read-stream on null', streamReturnMacro, {
input: absPath('./data/bunny.mp4'),
title: 'null'
})
test.cb('returns a read-stream on null, with stream input', streamReturnMacro, {
input: fs.createReadStream(absPath('./data/bunny.mp4')),
title: 'null-stream'
})
;['25%', '101%', '50x?', '?x50', '100x50'].forEach((size) => {
const sanitizeSize = str => str.replace('%', '').replace('?', 'X')
test(`handles sizes of the form ${size}`, imageTestMacro, {
size,
title: `size-${sanitizeSize(size)}`
}, absPath(`./expected/size-${sanitizeSize(size)}.png`))
})
;['gif', 'jpg', 'png'].forEach(format => {
test(`it can create ${format} images`, imageCreationMacro, {
output: absPath(`./out/${format}.${format}`)
})
})