Comparing version 0.1.0 to 1.0.1
@@ -9,3 +9,3 @@ { | ||
"description": "CloudSight image recognition API (unofficial)", | ||
"version": "0.1.0", | ||
"version": "1.0.1", | ||
"repository": { | ||
@@ -12,0 +12,0 @@ "type": "git", |
287
test.js
@@ -8,7 +8,16 @@ /* | ||
var app = require ('./'); | ||
var path = require ('path'); | ||
var dir = path.dirname (module.filename); | ||
var pkg = require (path.join (dir, 'package.json')); | ||
var app = require (path.join (dir)); | ||
var cloudsight; | ||
var errors = 0; | ||
var warnings = 0; | ||
var queue = []; | ||
var next = 0; | ||
var cache = { | ||
token: null | ||
}; | ||
@@ -31,10 +40,91 @@ | ||
/** | ||
* ANSI colorize a string | ||
* | ||
* @param color {String} - The color to add | ||
* @param str {String} - The string to alter | ||
* @returns {String} | ||
*/ | ||
function colorStr (color, str) { | ||
var colors = { | ||
red: '\u001b[31m', | ||
green: '\u001b[32m', | ||
yellow: '\u001b[33m', | ||
blue: '\u001b[34m', | ||
magenta: '\u001b[35m', | ||
cyan: '\u001b[36m', | ||
gray: '\u001b[37m', | ||
bold: '\u001b[1m', | ||
plain: '\u001b[0m' | ||
}; | ||
return colors [color] + str + colors.plain; | ||
} | ||
/** | ||
* console.log with style | ||
* | ||
* @param [type] {String=plain} - Formatting style | ||
* @param str {String} - The string to alter | ||
* @returns {void} | ||
*/ | ||
function log (type, str) { | ||
var types = { | ||
fail: ['red', 'FAIL'], | ||
good: ['green', 'good'], | ||
warn: ['yellow', 'warn'], | ||
info: ['cyan', 'info'] | ||
}; | ||
if (!str) { | ||
str = type; | ||
type = 'plain'; | ||
} | ||
switch (type) { | ||
case 'error': console.log (colorStr ('red', colorStr ('bold', 'ERROR ')) + str + '\n'); break; | ||
case 'note': console.log (colorStr ('bold', str)); break; | ||
case 'plain': console.log (str); break; | ||
default: | ||
console.log (colorStr (types[type][0], types[type][1]) + ' ' + str); | ||
break; | ||
} | ||
} | ||
/** | ||
* Detect and wrap string type | ||
* | ||
* @param str {String} - The string | ||
* @returns {String} | ||
*/ | ||
function typeStr (str) { | ||
if (typeof str === 'string') { | ||
str = '"' + str + '"'; | ||
} else if (str instanceof Object) { | ||
str = 'Object'; | ||
} else if (str instanceof Array) { | ||
str = 'Array'; | ||
} else if (str instanceof Error) { | ||
str = 'Error'; | ||
} | ||
return colorStr ('magenta', str); | ||
} | ||
// handle exits | ||
process.on ('exit', function () { | ||
if (errors === 0) { | ||
console.log ('\n\u001b[1mDONE, no errors.\u001b[0m\n'); | ||
process.on ('exit', function processExit () { | ||
console.log (); | ||
log ('info', errors + ' errors'); | ||
log ('info', warnings + ' warnings'); | ||
console.log (); | ||
if (errors) { | ||
process.exit (1); | ||
} else { | ||
process.exit (0); | ||
} else { | ||
console.log ('\n\u001b[1mFAIL, ' + errors + ' error' + (errors > 1 ? 's' : '') + ' occurred!\u001b[0m\n'); | ||
process.exit (1); | ||
} | ||
@@ -44,5 +134,6 @@ }); | ||
// prevent errors from killing the process | ||
process.on ('uncaughtException', function (err) { | ||
process.on ('uncaughtException', function uncaughtException (err) { | ||
console.log (err); | ||
console.log (); | ||
console.error (err.stack); | ||
console.log (err.stack); | ||
console.log (); | ||
@@ -52,36 +143,80 @@ errors++; | ||
// Queue to prevent flooding | ||
/** | ||
* Queue to prevent flooding | ||
* | ||
* @returns {void} | ||
*/ | ||
function doNext () { | ||
next++; | ||
if (queue[next]) { | ||
queue[next] (); | ||
if (queue [next]) { | ||
console.log (); | ||
queue [next] (); | ||
} | ||
} | ||
// doTest (passErr, 'methods', [ | ||
// ['feeds', typeof feeds === 'object'] | ||
// ]) | ||
/** | ||
* doTest checks for error | ||
* else runs specified tests | ||
* | ||
* @param {Error} err | ||
* @param {String} label | ||
* @param {Array} tests | ||
* @returns {void} | ||
* | ||
* doTest(err, 'label text', [ | ||
* ['fail', 'feeds', typeof feeds, 'object'], | ||
* ['warn', 'music', music instanceof Array, true], | ||
* ['info', 'tracks', music.length] | ||
* ]); | ||
*/ | ||
function doTest (err, label, tests) { | ||
var testErrors = []; | ||
var level = 'good'; | ||
var test; | ||
var i; | ||
if (err instanceof Error) { | ||
console.error (label + ': \u001b[1m\u001b[31mERROR\u001b[0m\n'); | ||
log ('error', label); | ||
console.dir (err, { depth: null, colors: true }); | ||
console.log (); | ||
console.error (err.stack); | ||
console.log (err.stack); | ||
console.log (); | ||
errors++; | ||
} else { | ||
tests.forEach (function (test) { | ||
if (test[1] !== true) { | ||
testErrors.push (test[0]); | ||
errors++; | ||
} | ||
}); | ||
if (testErrors.length === 0) { | ||
console.log (label + ': \u001b[1m\u001b[32mok\u001b[0m'); | ||
} else { | ||
console.error (label + ': \u001b[1m\u001b[31mfailed\u001b[0m (' + testErrors.join (', ') + ')'); | ||
doNext (); | ||
return; | ||
} | ||
log ('note', colorStr ('blue', '(' + (next + 1) + '/' + queue.length + ') ') + label); | ||
for (i = 0; i < tests.length; i++) { | ||
test = { | ||
level: tests [i] [0], | ||
label: tests [i] [1], | ||
result: tests [i] [2], | ||
expect: tests [i] [3] | ||
}; | ||
if (test.result === test.expect) { | ||
log ('good', colorStr ('blue', test.label) + ': ' + typeStr (test.result) + ' is exactly ' + typeStr (test.expect)); | ||
} | ||
if (test.level === 'fail' && test.result !== test.expect) { | ||
errors++; | ||
level = 'fail'; | ||
log ('fail', colorStr ('blue', test.label) + ': ' + typeStr (test.result) + ' is not ' + typeStr (test.expect)); | ||
} | ||
if (test.level === 'warn' && test.result !== test.expect) { | ||
warnings++; | ||
level = level !== 'fail' && 'warn'; | ||
log ('warn', colorStr ('blue', test.label) + ': ' + typeStr (test.result) + ' is not ' + typeStr (test.expect)); | ||
} | ||
if (test.level === 'info') { | ||
log ('info', colorStr ('blue', test.label) + ': ' + typeStr (test.result)); | ||
} | ||
} | ||
@@ -93,12 +228,98 @@ | ||
// TESTS | ||
// module basics | ||
queue.push (function () { | ||
doTest (null, 'module', [ | ||
['exports', typeof app === 'function'], | ||
['interface', cloudsight instanceof Object] | ||
['fail', 'exports', typeof app, 'function'], | ||
['interface', cloudsight instanceof Object, true] | ||
]); | ||
}); | ||
// upload image without status polling | ||
queue.push (function () { | ||
var image = { | ||
image: path.join (dir, 'test_image.png'), | ||
locale: 'nl-NL' | ||
}; | ||
cloudsight.request (image, false, function (err, data) { | ||
cache.token = data && data.token; | ||
doTest (err, '.request - upload without polling', [ | ||
['fail', 'type', data instanceof Object, true], | ||
['warn', 'url', data && typeof data.url, 'string'] | ||
]); | ||
}); | ||
}); | ||
// upload image with status polling | ||
queue.push (function () { | ||
var image = { | ||
image: path.join (dir, 'test_image.png'), | ||
locale: 'nl-NL' | ||
}; | ||
cloudsight.request (image, true, function (err, data) { | ||
cache.token = data && data.token; | ||
doTest (err, '.request - upload with polling', [ | ||
['fail', 'type', data instanceof Object, true], | ||
['warn', 'status', data && data.status, 'completed'] | ||
]); | ||
}); | ||
}); | ||
// send image from url without status polling | ||
queue.push (function () { | ||
var image = { | ||
remote_image_url: 'https://frankl.in/u/test_image.png', | ||
locale: 'nl-NL' | ||
}; | ||
cloudsight.request (image, false, function (err, data) { | ||
cache.token = data && data.token; | ||
doTest (err, '.request - url without polling', [ | ||
['fail', 'type', data instanceof Object, true], | ||
['warn', 'url', data && typeof data.url, 'string'] | ||
]); | ||
}); | ||
}); | ||
// send image from url with status polling | ||
queue.push (function () { | ||
var image = { | ||
remote_image_url: 'https://frankl.in/u/test_image.png', | ||
locale: 'nl-NL' | ||
}; | ||
cloudsight.request (image, true, function (err, data) { | ||
cache.token = data && data.token; | ||
doTest (err, '.request - url with polling', [ | ||
['fail', 'type', data instanceof Object, true], | ||
['warn', 'status', data && data.status, 'completed'] | ||
]); | ||
}); | ||
}); | ||
// get image status | ||
queue.push (function () { | ||
cloudsight.response (cache.token, function (err, data) { | ||
cache.token = data && data.token; | ||
doTest (err, '.response', [ | ||
['fail', 'type', data instanceof Object, true], | ||
['fail', 'token', data && data.token, cache.token] | ||
]); | ||
}); | ||
}); | ||
// Start the tests | ||
queue[0] (); | ||
log ('note', 'Running tests...\n'); | ||
log ('note', 'API endpoint: ' + (config.apikey ? 'LIVE' : 'CUSTOM')); | ||
log ('note', 'Node.js: ' + process.versions.node); | ||
log ('note', 'Module: ' + pkg.version); | ||
console.log (); | ||
queue [0] (); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
29097
9
470
1
5