openvibers/openvibe.markets testphoto.test.js
photo.test.js
| 1 | ; |
| 2 | /** |
| 3 | * Search by photo (v0.2; server/vision.js): Markets asks Vision with its own token (client_credentials, audience |
| 4 | * openvibe.vision, scope vision.image.ask), turns the answer into words and searches them; "none" and a broken photo |
| 5 | * are said plainly; ten photos an hour per address and a daily total; a cross-site post is refused; without a secret |
| 6 | * the button is not shown and the route says so. |
| 7 | */ |
| 8 | const assert = require('assert'); |
| 9 | const http = require('node:http'); |
| 10 | const { createApp } = require('../server/app'); |
| 11 | const { createVision, queryOf } = require('../server/vision'); |
| 12 | |
| 13 | assert.strictEqual(queryOf('"Sony WH-1000XM5 wireless headphones."'), 'Sony WH-1000XM5 wireless headphones'); |
| 14 | assert.strictEqual(queryOf('none'), null); |
| 15 | assert.strictEqual(queryOf('None.'), null); |
| 16 | assert.strictEqual(queryOf(' \n Lego Technic 42115 Lamborghini \nextra'), 'Lego Technic 42115 Lamborghini'); |
| 17 | |
| 18 | const JPEG = `data:image/jpeg;base64,${Buffer.from('fake jpeg bytes').toString('base64')}`; |
| 19 | |
| 20 | (async () => { |
| 21 | const calls = []; |
| 22 | let answer = 'Sony WH-1000XM5 wireless headphones'; |
| 23 | const fake = http.createServer(async (req, res) => { |
| 24 | let body = ''; |
| 25 | for await (const c of req) body += c; |
| 26 | calls.push({ path: req.url, auth: req.headers.authorization || null, body }); |
| 27 | res.setHeader('content-type', 'application/json'); |
| 28 | if (req.url === '/oauth/token') return res.end(JSON.stringify({ access_token: 'svc-markets', expires_in: 300 })); |
| 29 | if (req.url === '/api/v1/ask') { res.statusCode = 201; return res.end(JSON.stringify({ answer: { text: answer, cost_usd: 0.001 } })); } |
| 30 | res.statusCode = 404; res.end('{}'); |
| 31 | }); |
| 32 | await new Promise((r) => fake.listen(0, '127.0.0.1', r)); |
| 33 | const at = `http://127.0.0.1:${fake.address().port}`; |
| 34 | const deals = { search: async () => ({ ok: true, total: 0, results: [] }) }; |
| 35 | const vision = createVision({ visionUrl: at, networkUrl: at, clientSecret: 'shh' }); |
| 36 | const server = createApp({ deals, vision, photoLimits: { perHour: 3, perDay: 5 } }).listen(0, '127.0.0.1'); |
| 37 | await new Promise((r) => server.once('listening', r)); |
| 38 | const base = `http://127.0.0.1:${server.address().port}`; |
| 39 | const send = (image, site = 'same-origin') => fetch(`${base}/photo`, { method: 'POST', redirect: 'manual', headers: { 'content-type': 'application/x-www-form-urlencoded', 'sec-fetch-site': site }, body: new URLSearchParams({ image }).toString() }) |
| 40 | .then(async (r) => ({ status: r.status, location: r.headers.get('location'), text: await r.text() })); |
| 41 | try { |
| 42 | const home = await fetch(`${base}/`).then((r) => r.text()); |
| 43 | assert.match(home, /Search by photo/); |
| 44 | assert.match(home, /src="\/markets\.js\?v=[0-9a-f]{10}"/); |
| 45 | |
| 46 | let r = await send(JPEG); |
| 47 | assert.deepStrictEqual([r.status, r.location], [303, '/search?q=Sony%20WH-1000XM5%20wireless%20headphones&from=photo']); |
| 48 | const tok = calls.find((c) => c.path === '/oauth/token'); |
| 49 | assert.deepStrictEqual(Object.fromEntries(new URLSearchParams(tok.body)), { grant_type: 'client_credentials', client_id: 'markets', client_secret: 'shh', audience: 'openvibe.vision', scope: 'vision.image.ask' }); |
| 50 | const ask = calls.find((c) => c.path === '/api/v1/ask'); |
| 51 | assert.strictEqual(ask.auth, 'Bearer svc-markets'); |
| 52 | const sent = JSON.parse(ask.body); |
| 53 | assert.deepStrictEqual([sent.task, sent.mode, sent.image], ['identify', 'auto', JPEG]); |
| 54 | const page = await fetch(`${base}${r.location}`).then((x) => x.text()); |
| 55 | assert.match(page, /From your photo: <b>Sony WH-1000XM5 wireless headphones<\/b>/); |
| 56 | |
| 57 | answer = 'none'; |
| 58 | r = await send(JPEG); |
| 59 | assert.strictEqual(r.status, 422); |
| 60 | assert.match(r.text, /does not seem to show something to buy/); |
| 61 | r = await send('data:text/html;base64,PGI+'); |
| 62 | assert.match(r.text, /not a photo Markets can read/); |
| 63 | r = await send(JPEG); |
| 64 | assert.strictEqual(r.status, 429, 'three an hour here (ten in production)'); |
| 65 | assert.match(r.text, /photos this hour/); |
| 66 | r = await send(JPEG, 'cross-site'); |
| 67 | assert.strictEqual(r.status, 403); |
| 68 | assert.strictEqual(calls.filter((c) => c.path === '/oauth/token').length, 1, 'the token is kept for its life'); |
| 69 | |
| 70 | // No secret: no button, and the route says so. |
| 71 | const off = createApp({ deals, vision: createVision({ visionUrl: at, networkUrl: at, clientSecret: '' }) }).listen(0, '127.0.0.1'); |
| 72 | await new Promise((x) => off.once('listening', x)); |
| 73 | const offBase = `http://127.0.0.1:${off.address().port}`; |
| 74 | assert.doesNotMatch(await fetch(`${offBase}/`).then((x) => x.text()), /Search by photo/); |
| 75 | const o = await fetch(`${offBase}/photo`, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded', 'sec-fetch-site': 'same-origin' }, body: `image=${encodeURIComponent(JPEG)}` }); |
| 76 | assert.strictEqual(o.status, 503); |
| 77 | off.close(); |
| 78 | } finally { |
| 79 | server.close(); |
| 80 | fake.close(); |
| 81 | } |
| 82 | console.log('photo: Vision names it with Markets\' own token, the words are searched, "none" and limits said plainly'); |
| 83 | })().catch((err) => { console.error(err); process.exit(1); }); |
| 84 |