-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
disable-coverage: true | ||
allow-empty-coverage: true | ||
allow-incomplete-coverage: true | ||
include: | ||
- '*.test.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const { spawn } = require('child_process') | ||
const t = require('tap') | ||
const fetch = require('node-fetch') | ||
const fs = require('fs/promises') | ||
|
||
const { runCli, waitForUrl } = require('./test-utils') | ||
|
||
t.test('API Server', async t => { | ||
let child | ||
const port = 23080 | ||
const url = `http://localhost:${port}` | ||
|
||
t.before(async () => { | ||
child = runCli('node', ['index.js'], { env: {PORT: '23080'}}) | ||
console.log(`Starting api server with pid ${child.pid}`) | ||
await waitForUrl(`${url}/health`) | ||
console.log(`Api server is up and running on ${url}`) | ||
}) | ||
|
||
t.after(async () => { | ||
return new Promise((resolve, rejects) => { | ||
console.log(`Stop server`) | ||
process.kill(child.pid) | ||
child.on('exit', code => code == 0 ? resolve() : rejects(new Error(`Server existed with code ${code}`))) | ||
}) | ||
}) | ||
|
||
t.test('embeddings', async t => { | ||
const body = await fs.readFile('sample.jpg') | ||
const data = await fetch(`${url}/embeddings`, { | ||
method: 'POST', | ||
body, | ||
headers: { | ||
'Content-Type': 'image/jpeg' | ||
} | ||
}).then(res => res.json()) | ||
t.match(data, { | ||
srcSha1sum: '17f5e000be30d7915fe161a03db2773de279df1f', | ||
model: 'mobilenet', | ||
version: 'v1_1.0', | ||
}) | ||
t.match(data.data.length, 1024) | ||
}) | ||
|
||
t.test('objects', async t => { | ||
const body = await fs.readFile('sample-2.jpg') | ||
const data = await fetch(`${url}/objects`, { | ||
method: 'POST', | ||
body, | ||
headers: { | ||
'Content-Type': 'image/jpeg' | ||
} | ||
}).then(res => res.json()) | ||
t.match(data, { | ||
srcSha1sum: 'c3328f60fd4fc6c85bfff3fdba9e2f067c135e5f', | ||
model: 'cocossd', | ||
version: 'mobilenet_v2', | ||
width: 300, | ||
height: 188, | ||
}) | ||
t.match(data.data.length, 20) | ||
t.same(data.data.map(o => o.class), [ | ||
'person', | ||
'person', | ||
'person', | ||
'person', | ||
'book', | ||
'book', | ||
'book', | ||
'book', | ||
'chair', | ||
'book', | ||
'dining table', | ||
'potted plant', | ||
'book', | ||
'person', | ||
'book', | ||
'book', | ||
'book', | ||
'potted plant', | ||
'book', | ||
'book', | ||
]) | ||
}) | ||
|
||
t.test('faces', async t => { | ||
const body = await fs.readFile('sample-2.jpg') | ||
const data = await fetch(`${url}/faces`, { | ||
method: 'POST', | ||
body, | ||
headers: { | ||
'Content-Type': 'image/jpeg' | ||
} | ||
}).then(res => res.json()) | ||
t.match(data, { | ||
srcSha1sum: 'c3328f60fd4fc6c85bfff3fdba9e2f067c135e5f', | ||
model: 'face-api', | ||
width: 300, | ||
height: 188, | ||
}) | ||
t.match(data.data.length, 6) | ||
t.same(data.data.map(o => o.gender), [ | ||
'male', | ||
'male', | ||
'male', | ||
'male', | ||
'male', | ||
'female' | ||
]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { spawn } = require('child_process') | ||
const fetch = require('node-fetch') | ||
|
||
const runCli = (command, args = [], options = {}, onExit = () => true) => { | ||
const env = { | ||
...process.env, | ||
...options.env | ||
} | ||
const child = spawn(command, args, { shell: false, stdio: 'pipe', ...options, env }) | ||
const stdout = [] | ||
const stderr = [] | ||
|
||
child.stdout.on('data', data => { | ||
console.log(data.toString('utf8')) | ||
stdout.push(data) | ||
}) | ||
child.stderr.on('data', data => stderr.push(data)) | ||
|
||
child.on('close', (code, signal) => { | ||
onExit({ | ||
code, | ||
signal, | ||
stdout: Buffer.concat(stdout).toString('utf8'), | ||
stderr: Buffer.concat(stdout).toString('utf8') | ||
}) | ||
}) | ||
|
||
return child | ||
} | ||
|
||
const delayAsync = async (delay = 0) => new Promise(resolve => setTimeout(resolve, delay)) | ||
|
||
const waitFor = async (fn, retryDelay = 0) => { | ||
return fn().catch(() => delayAsync(retryDelay).then(() => waitFor(fn, retryDelay))) | ||
} | ||
|
||
const waitForUrl = (url, retryDelay = 125) => { | ||
return waitFor(() => { | ||
const controller = new AbortController() | ||
const timer = setTimeout(() => { | ||
controller.abort() | ||
}, 1000) | ||
return fetch(url, { | ||
signal: controller.signal | ||
}).then(res => { | ||
clearTimeout(timer) | ||
if (!res.ok) { | ||
throw new Error(`Failed to fetch ${url}: ${res.status}`) | ||
} | ||
return res | ||
}) | ||
.catch(e => { | ||
clearTimeout(timer) | ||
throw e | ||
}) | ||
}, retryDelay) | ||
} | ||
|
||
module.exports = { | ||
runCli, | ||
waitFor, | ||
waitForUrl | ||
} |