openvibers/openvibe.finance testreceipt.test.js
receipt.test.js
| 1 | ; |
| 2 | /** |
| 3 | * Receipt photos (v0.12; server/receipt.js): the total (never a subtotal, tax or change), the day, the store; what was |
| 4 | * not clear is said; a real picture read by Tesseract (where it is installed); the page: a photo in, the add form |
| 5 | * filled in, nothing saved until Add it; refusals (not a photo, unreadable, another person's account, cross-site). |
| 6 | */ |
| 7 | const assert = require('assert'); |
| 8 | const fs = require('node:fs'); |
| 9 | const path = require('node:path'); |
| 10 | const crypto = require('node:crypto'); |
| 11 | const { readReceipt, ocr, ReceiptError } = require('../server/receipt'); |
| 12 | |
| 13 | const TODAY = '2026-11-18'; |
| 14 | let r = readReceipt('CORNER GROCER\n123 Main St, Springfield\nTel 555-0100\n11/14/2026 18:42\nBananas 1.29\nMilk 2% 3.49\nSUBTOTAL 7.77\nTAX 0.62\nTOTAL 8.39\nVISA 8.39\nCHANGE 0.00\nThank you!', { today: TODAY }); |
| 15 | assert.deepStrictEqual(r, { payee: 'CORNER GROCER', posted_on: '2026-11-14', amount_cents: -839, found: { payee: true, date: true, total: true } }); |
| 16 | r = readReceipt('Welcome to\nBean There Coffee\nLatte 4.75\nMuffin 3.25\nNov 17, 2026', { today: TODAY }); |
| 17 | assert.deepStrictEqual([r.payee, r.posted_on, r.found.total], ['Bean There Coffee', '2026-11-17', false], 'no total line: the largest amount, said to check'); |
| 18 | r = readReceipt('THE HARDWARE PLACE\n18/11/2026\nGRAND TOTAL £ 1,204.50', { today: TODAY }); |
| 19 | assert.deepStrictEqual([r.posted_on, r.amount_cents], ['2026-11-18', -120450], 'day first when the day is over 12'); |
| 20 | r = readReceipt('Shop\n12/31/2027\nTOTAL 3.00', { today: TODAY }); |
| 21 | assert.deepStrictEqual([r.posted_on, r.found.date], [TODAY, false], 'a day in the future is not the receipt\'s'); |
| 22 | assert.deepStrictEqual(readReceipt('', { today: TODAY }), { payee: '', posted_on: TODAY, amount_cents: null, found: { payee: false, date: false, total: false } }); |
| 23 | |
| 24 | (async () => { |
| 25 | if (fs.existsSync('/usr/bin/tesseract')) { |
| 26 | const text = await ocr(fs.readFileSync(path.join(__dirname, 'fixtures', 'receipt.png'))); |
| 27 | assert.deepStrictEqual(readReceipt(text, { today: TODAY }), { payee: 'CORNER GROCER', posted_on: '2026-11-14', amount_cents: -839, found: { payee: true, date: true, total: true } }, 'a real picture, read'); |
| 28 | await assert.rejects(ocr(Buffer.from('not an image')), ReceiptError); |
| 29 | } else { |
| 30 | console.log('receipt: tesseract is not installed here, the real picture is skipped'); |
| 31 | } |
| 32 | |
| 33 | const { createTestDb } = require('openvibe-sdk/testing'); |
| 34 | const { createVault } = require('../server/crypto'); |
| 35 | const { createStore } = require('../server/store'); |
| 36 | const { createApp } = require('../server/app'); |
| 37 | const { db, close } = await createTestDb({ migrations: path.join(__dirname, '..', 'migrations'), store: process.env.FINANCE_TEST_STORE || 'pglite', service: 'finance' }); |
| 38 | const ADA = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAW'; |
| 39 | const BO = 'usr_01JCZ3NDEKTSV4RRFFQ69G5FAX'; |
| 40 | const vault = createVault(crypto.randomBytes(32).toString('base64')); |
| 41 | const store = createStore({ db, vault }); |
| 42 | const auth = { router: (express) => express.Router(), optionalAuth: () => (req, _res, next) => { const u = req.get('x-test-user'); if (u) req.user = { subject_id: u }; next(); } }; |
| 43 | let seen = null; |
| 44 | const readImage = async (bytes) => { |
| 45 | seen = bytes; |
| 46 | if (bytes.toString() === 'blurry') throw new ReceiptError('That photo could not be read. A JPEG or PNG of the whole receipt works best.'); |
| 47 | return 'CORNER GROCER\n11/14/2026\nTOTAL 8.39'; |
| 48 | }; |
| 49 | const server = createApp({ db, vault, auth, readImage, now: () => new Date('2026-11-18T12:00:00Z') }).listen(0, '127.0.0.1'); |
| 50 | await new Promise((x) => server.once('listening', x)); |
| 51 | const base = `http://127.0.0.1:${server.address().port}`; |
| 52 | const post = (p, form, { as = ADA, site = 'same-origin' } = {}) => fetch(base + p, { method: 'POST', redirect: 'manual', headers: { 'sec-fetch-site': site, 'x-test-user': as, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams(form).toString() }) |
| 53 | .then(async (x) => ({ status: x.status, location: x.headers.get('location'), text: await x.text() })); |
| 54 | const photo = (s) => `data:image/jpeg;base64,${Buffer.from(s).toString('base64')}`; |
| 55 | try { |
| 56 | const acc = await store.addAccount(ADA, { name: 'Checking' }); |
| 57 | const page = await fetch(`${base}/accounts/${acc.id}`, { headers: { 'x-test-user': ADA } }).then((x) => x.text()); |
| 58 | assert.match(page, /From a receipt photo/); |
| 59 | assert.match(page, /action="\/accounts\/[^"]+\/receipt"/); |
| 60 | |
| 61 | let res = await post(`/accounts/${acc.id}/receipt`, { image: photo('jpeg bytes') }); |
| 62 | assert.strictEqual(res.status, 200, res.text.slice(0, 200)); |
| 63 | assert.strictEqual(seen.toString(), 'jpeg bytes', 'the photo reached the reader as bytes'); |
| 64 | assert.match(res.text, /Check the receipt/); |
| 65 | assert.match(res.text, /name="posted_on" value="2026-11-14"/); |
| 66 | assert.match(res.text, /name="amount"[^>]*value="8\.39"/); |
| 67 | assert.match(res.text, /name="payee"[^>]*value="CORNER GROCER"/); |
| 68 | assert.match(res.text, /action="\/accounts\/[^"]+\/transactions"/, 'the usual add form'); |
| 69 | assert.strictEqual(Number(await db.value('SELECT count(*) FROM finance_transactions')), 0, 'nothing is saved before Add it'); |
| 70 | |
| 71 | res = await post(`/accounts/${acc.id}/receipt`, { image: photo('blurry') }); |
| 72 | assert.strictEqual(res.status, 422); |
| 73 | assert.match(res.text, /could not be read/); |
| 74 | res = await post(`/accounts/${acc.id}/receipt`, { image: 'data:text/html;base64,PGI+' }); |
| 75 | assert.match(res.text, /Choose a photo of the receipt/); |
| 76 | res = await post(`/accounts/${acc.id}/receipt`, { image: photo('x') }, { as: BO }); |
| 77 | assert.strictEqual(res.status, 404, 'never another person\'s account'); |
| 78 | res = await post(`/accounts/${acc.id}/receipt`, { image: photo('x') }, { site: 'cross-site' }); |
| 79 | assert.strictEqual(res.status, 403); |
| 80 | } finally { |
| 81 | server.close(); |
| 82 | await close(); |
| 83 | } |
| 84 | console.log('receipt: the total, the day and the store read; a real picture through Tesseract; checked before it is added'); |
| 85 | })().catch((err) => { console.error(err); process.exit(1); }); |
| 86 |