openvibers/openvibe.finance testcsv.test.js
csv.test.js
| 1 | ; |
| 2 | // Reading a bank's CSV export (server/csv.js): the delimiter, quotes, the columns by their header, dates and amounts in |
| 3 | // the shapes banks send, debit/credit pairs, and rows that cannot be read said, never guessed. |
| 4 | const assert = require('assert'); |
| 5 | const { readCsv, readDate, readAmount } = require('../server/csv'); |
| 6 | |
| 7 | assert.strictEqual(readDate('2026-11-18'), '2026-11-18'); |
| 8 | assert.strictEqual(readDate('11/18/2026'), '2026-11-18'); |
| 9 | assert.strictEqual(readDate('18/11/2026'), '2026-11-18', 'a first number over 12 is a day'); |
| 10 | assert.strictEqual(readDate('01/02/2026', true), '2026-02-01', 'a day-first file'); |
| 11 | assert.strictEqual(readDate('18.11.2026'), '2026-11-18'); |
| 12 | assert.strictEqual(readDate('Nov 18, 2026'), '2026-11-18'); |
| 13 | assert.strictEqual(readDate('18 Nov 2026'), '2026-11-18'); |
| 14 | assert.strictEqual(readDate('2026-02-30'), null, 'no 30th of February'); |
| 15 | assert.strictEqual(readDate('soon'), null); |
| 16 | |
| 17 | assert.strictEqual(readAmount('-12.34'), -1234); |
| 18 | assert.strictEqual(readAmount('(12.34)'), -1234); |
| 19 | assert.strictEqual(readAmount('$1,234.56'), 123456); |
| 20 | assert.strictEqual(readAmount('1.234,56'), 123456, 'comma decimals'); |
| 21 | assert.strictEqual(readAmount('12,5'), 1250); |
| 22 | assert.strictEqual(readAmount('12.34 DR'), -1234); |
| 23 | assert.strictEqual(readAmount('12.34 CR'), 1234); |
| 24 | assert.strictEqual(readAmount('7'), 700); |
| 25 | assert.strictEqual(readAmount(''), null); |
| 26 | assert.strictEqual(readAmount('n/a'), null); |
| 27 | |
| 28 | // A typical export: quoted payees with commas, a signed amount. |
| 29 | let r = readCsv('Date,Description,Amount,Category\n2026-11-02,"Corner Grocer, Main St",-23.18,Groceries\n2026-11-01,Paycheck,2500.00,\n'); |
| 30 | assert.strictEqual(r.problem, null); |
| 31 | assert.deepStrictEqual(r.rows, [ |
| 32 | { posted_on: '2026-11-02', amount_cents: -2318, payee: 'Corner Grocer, Main St', memo: '', category: 'Groceries' }, |
| 33 | { posted_on: '2026-11-01', amount_cents: 250000, payee: 'Paycheck', memo: '', category: '' }, |
| 34 | ]); |
| 35 | |
| 36 | // Debit and credit columns, semicolons, a day-first file, a byte order mark, a row that cannot be read. |
| 37 | r = readCsv('Booking Date;Payee;Memo;Debit;Credit\r\n15/10/2026;Rent;October;1200,00;\r\n03/11/2026;Refund;;;"19,99"\r\nlater;Mystery;;5,00;\r\n'); |
| 38 | assert.deepStrictEqual(r.rows.map((x) => [x.posted_on, x.amount_cents, x.payee, x.memo]), [ |
| 39 | ['2026-10-15', -120000, 'Rent', 'October'], |
| 40 | ['2026-11-03', 1999, 'Refund', ''], |
| 41 | ]); |
| 42 | assert.deepStrictEqual(r.skipped.map((s) => s.line), [4]); |
| 43 | assert.match(r.skipped[0].why, /date/); |
| 44 | |
| 45 | // Tabs, and a description beside a payee is the memo. |
| 46 | r = readCsv('Posted Date\tPayee\tDescription\tAmount\n2026-11-05\tSTREAMCO\tMonthly plan\t-9.99\n'); |
| 47 | assert.deepStrictEqual(r.rows[0], { posted_on: '2026-11-05', amount_cents: -999, payee: 'STREAMCO', memo: 'Monthly plan', category: '' }); |
| 48 | |
| 49 | // What a file is missing is said. |
| 50 | assert.match(readCsv('Description,Amount\nx,1\n').problem, /No date column/); |
| 51 | assert.match(readCsv('Date,Description\n2026-11-01,x\n').problem, /No amount column/); |
| 52 | assert.match(readCsv('Date,Amount\n').problem, /no rows/); |
| 53 | |
| 54 | console.log('csv: dates, amounts, columns, delimiters and unreadable rows'); |
| 55 |