-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest.js
98 lines (84 loc) · 2.91 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
var test = require('tape')
var Sox = require('./')
var fs = require('fs')
var cp = require('child_process')
var concat = require('concat-stream')
var osTmpdir = require('os-tmpdir')
var testAudio = require('test-audio')()
var tmpFilesThen = getNumOfTmpFiles()
function getNumOfTmpFiles() {
return fs.readdirSync(osTmpdir()).length
}
function closeEnough(t, got, expect, within, whats) {
var msg = got + ' ' + whats + ' is close enough to ' + expect + ' ' + whats
t.ok(Math.abs(got - expect) <= within, msg)
}
function makeTest(description, soxOpts, file, endSize) {
test(description, function (t) {
var soxTransform = Sox(soxOpts)
soxTransform.on('error', function handler(err) {
var isWarn = /WARN/.test(err.message)
t.fail(err.message)
if (!isWarn) t.end()
})
var finished = false
soxTransform.on('finish', function handler() {
finished = true
})
fs.createReadStream(file.path)
.pipe(soxTransform)
.pipe(concat(function close(buf) {
closeEnough(t, buf.length, endSize, endSize / 200, 'bytes')
t.notOk(finished, 'stream should not be finished yet')
setTimeout(function() {
t.ok(finished, 'stream should be finished')
t.end()
}, 200)
}))
})
}
test('no options: throw', function (t) {
t.throws(Sox)
t.throws(Sox.bind(null, {}))
t.throws(Sox.bind(null, { output: {}}))
t.throws(Sox.bind(null, { output: { type: null }}))
t.end()
})
test('sox is installed on your computer', function (t) {
t.plan(3)
cp.exec('sox --version', function (err, stdout, stderr) {
t.ifError(err)
t.ifError(stderr)
t.ok(stdout)
t.end()
})
})
makeTest('ogg > ogg', { output: { type: 'ogg' }}, testAudio.ogg, 66031)
makeTest('ogg > wav', { input: { type: 'ogg' }, output: { type: 'wav' }}, testAudio.ogg, 542884)
makeTest('ogg > wav - no inputOpts', { output: { t: 'wav' }}, testAudio.ogg, 542884)
var soxWithManyOptions = {
input: { type: 'ogg', v: 0.9 },
output: { t: 'wav', b: 16, c: 1, r: 44100, C: 5 }
}
makeTest('ogg > wav - options - adjusted volume', soxWithManyOptions, testAudio.ogg, 271464) // { timeout: 3000 },
makeTest('wav > flac', { output: { type: 'flac' }}, testAudio.wav, 4711)
makeTest('wav > ogg', { input: { type: 'wav' }, output: { type: 'ogg' }}, testAudio.wav, 5792)
makeTest('flac > ogg', { output: { type: 'ogg' }}, testAudio.flac, 5086)
// github issue #4
test('An incorrect input type does not cause an infinite loop', function (t) {
var errorCount = 0
fs.createReadStream(testAudio.wav.path)
.pipe(Sox({ input: { type: 'ogg' }, output: { type: 'wav' }}))
.on('error', function (err) {
errorCount++
})
setTimeout(function () {
t.ok(errorCount >= 1, 'Received a few errors')
t.ok(errorCount < 10, 'Did not receive *lots* of errors')
t.end()
}, 200)
})
test('cleans up tmp files', function (t) {
closeEnough(t, tmpFilesThen, getNumOfTmpFiles(), 1, 'files')
t.end()
})