UNPKG

meadowbrook

Version:

Alternative meyda cli

269 lines (266 loc) 11.1 kB
const test = require("tape"); const fs = require("fs"); const path = require("path"); const jsyaml = require("js-yaml"); const child_process = require("child_process"); const ajv = new (require("ajv"))(); const validator = ajv.compile( jsyaml.load( fs.readFileSync(path.join(__dirname, "../docs/out-schema.yml"), "utf8") ) ); const exec = command => new Promise((resolve, reject) => { child_process.exec( command, { cwd: __dirname }, (error, stdout, stderr) => { resolve({ error, stdout, stderr }); } ); }); exec( `node ../index.js horse.mp3 all --o=${path.join( __dirname, "./out/default-all.json" )}` ) .then( () => JSON.parse( fs.readFileSync(path.join(__dirname, "./out/default-all.json")) ).extracted ) .then(ALL => { test("invalid input path", async t => { t.plan(4); const outFile = path.join(__dirname, "./out/invalid-input-path.json"); const command = `node ../index.js pony.mp3 all --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.ok(error); t.ok(stdout.length == 0, "No output on stdout"); t.ok(stderr.includes("Not a valid input file"), "Relevent error message"); t.notOk(fs.existsSync(outFile), "Should not produce file"); }); test("invalid feature", async t => { t.plan(4); const outFile = path.join(__dirname, "./out/invalid-feature.json"); const command = `node ../index.js horse.mp3 smell --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.ok(error); t.ok(stdout.length == 0, "No output on stdout"); t.ok(stderr.includes("Not a valid feature"), "Relevent error message"); t.notOk(fs.existsSync(outFile), "Should not produce file"); }); test("single feature", async t => { t.plan(6); const outFile = path.join(__dirname, "./out/single-feature.json"); const command = `node ../index.js horse.mp3 rms --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.equal(Object.keys(jsonContent.extracted).length, 1, "Only one feature"); }); test("multiple features", async t => { t.plan(6); const outFile = path.join(__dirname, "./out/multiple-feature.json"); const command = `node ../index.js horse.mp3 rms energy --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.equal( Object.keys(jsonContent.extracted).length, 2, "Only two features" ); }); test("all features", async t => { t.plan(9); const outFile = path.join(__dirname, "./out/all-features.json"); const command = `node ../index.js horse.mp3 all --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.equal(Object.keys(jsonContent.extracted).length, 16, "All features"); t.ok( Object.values(jsonContent.extracted).every( v => typeof v === "object" && Array.isArray(v) ), "Array content" ); t.ok( [ ["fps", 60], ["bufferSize", 512], ["windowingFunction", "hanning"] ].every(([a, b]) => jsonContent[a] === b) ); t.deepEqual(jsonContent.extracted, ALL, "Matches test out"); }); test("all features: options in front", async t => { t.plan(6); const outFile = path.join(__dirname, "./out/all-features.json"); const command = `node ../index.js horse.mp3 all --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.equal(Object.keys(jsonContent.extracted).length, 16, "All features"); }); test("buffer size", async t => { t.plan(7); const outFile = path.join(__dirname, "./out/buffer-size.json"); const command = `node ../index.js horse.mp3 all --bs=256 --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.equal(jsonContent.bufferSize, 256, "Correct buffer size"); t.notDeepEqual(jsonContent.extracted, ALL, "Does not match test out"); }); test("buffer size: not power of two", async t => { t.plan(4); const outFile = path.join(__dirname, "./out/buffer-size-not-2.json"); const command = `node ../index.js horse.mp3 all --bs=69 --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.ok(error); t.ok(stdout.length == 0, "No output on stdout"); t.ok( stderr.includes("Buffer size must be a power of 2"), "Relevent error message" ); t.notOk(fs.existsSync(outFile), "Should not produce file"); }); test("windowing function", async t => { t.plan(7); const outFile = path.join(__dirname, "./out/windowing-function.json"); const command = `node ../index.js horse.mp3 all --w=blackman --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.equal(jsonContent.windowingFunction, "blackman", "Correct function"); t.notDeepEqual(jsonContent.extracted, ALL, "Does not match test out"); }); test("invalid windowing function", async t => { t.plan(4); const outFile = path.join( __dirname, "./out/invalid-windowing-function.json" ); const command = `node ../index.js horse.mp3 all --w=sandman --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.ok(error); t.ok(stdout.length == 0, "No output on stdout"); t.ok( stderr.includes("Invalid windowing function"), "Relevent error message" ); t.notOk(fs.existsSync(outFile), "Should not produce file"); }); test("frames per second", async t => { t.plan(7); const outFile = path.join(__dirname, "./out/frames-per-second.json"); const command = `node ../index.js horse.mp3 all --f=42 --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.equal(jsonContent.fps, 42, "Correct fps"); t.notDeepEqual(jsonContent.extracted, ALL, "Does not match test out"); }); test("format object", async t => { t.plan(6); const outFile = path.join(__dirname, "./out/format-object.json"); const command = `node ../index.js horse.mp3 all --format=object --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.ok( Object.values(jsonContent.extracted).every( v => typeof v === "object" && !Array.isArray(v) ), "Object content" ); }); test("invalid format object", async t => { t.plan(4); const outFile = path.join(__dirname, "./out/invalid-format-object.json"); const command = `node ../index.js horse.mp3 all --format=hamburger --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.ok(error, "Error code"); t.ok(stdout.length == 0, "No output on stdout"); t.ok( stderr.includes("Invalid collection format"), "Relevent error message" ); t.notOk(fs.existsSync(outFile), "Should not produce file"); }); test("pipe mode", async t => { t.plan(7); const outFile = path.join(__dirname, "./out/pipe-mode.json"); const command = `node ../index.js horse.mp3 all > ${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error"); t.ok(fs.existsSync(outFile), "Out file exists"); t.ok(stdout.length == 0, "No output on stdout"); const fileContent = fs.readFileSync(outFile); const jsonContent = JSON.parse(fileContent); t.ok(jsonContent, "Valid json"); t.ok(validator(jsonContent), "Matches schema"); t.equal(Object.keys(jsonContent.extracted).length, 16, "All features"); t.ok( Object.values(jsonContent.extracted).every( v => typeof v === "object" && Array.isArray(v) ), "Array content" ); }); test("help", async t => { t.plan(3); const outFile = path.join(__dirname, "./out/help.json"); const command = `node ../index.js horse.mp3 all -h --o=${outFile}`; const { error, stdout, stderr } = await exec(command); t.notOk(error, "No error code"); t.ok(stdout.length == 0, "No output on stdout"); t.notOk(fs.existsSync(outFile), "Should not produce file"); }); });