Files
token-circles/worker/test/import-csv-parse.test.ts
KomediruzeckiandGitHub 302f25453e fix(import): stop dropping rows that have no description (#392)
A Google Sheet sync rejected 479 of 5,194 rows. Replaying the sheet through the
real import path reproduced the reported rows exactly and pinned down three
defects; two more were found reviewing the fix.

1. A blank description dropped the row, and only locally. The IndexedDB path
   required a description; the Worker never has, so the same sheet imported
   clean in the cloud and silently lost 291 rows in local mode. Those rows
   carry a date, an amount, a category and both accounts — a missing
   description is not a reason to discard one. The message was wrong twice
   over: description is not a number, and it named a field the numeric parser
   never validates. A Worker test now pins that side of the contract.

2. The rejection label was cut mid-token. importRowLabel sliced the date to ten
   characters to drop a trailing time, but the frontend reads sheets through
   the Google Visualization API, which delivers `Date(2026,1,7)` — sliced to
   `Date(2026,`. It now trims only when the value leads with a calendar date,
   and the local path labels from the normalized date.

3. One CSV parser instead of two. Both runtimes split the text into lines
   before tracking quotes, so a quoted cell containing a newline — how Sheets
   exports every multi-line cell — was torn into fragments: the real row
   vanished and short, column-shifted junk rows took its place, silently. The
   parser now carries quote state across newlines, handles the RFC 4180 `""`
   escape the old copies dropped, and lives in shared/ so the two cannot drift.

4. A byte-order mark ahead of a quoted header left the header as `"description"`
   with the quotes attached, which silently unmaps a saved header→column
   mapping. Unquoted headers survived by accident, since trim() drops U+FEFF.

5. Padding before an opening quote (`a, "b"`) is accepted again, as the parsers
   this replaced did — the Worker ingests uploaded and emailed CSVs from
   arbitrary producers.

A missing date still rejects the row, now saying "No date — every transaction
needs one" rather than blaming the number format.
2026-08-09 01:51:19 +02:00

41 lines
1.5 KiB
TypeScript

/**
* The Worker's CSV entry points (published-sheet fetch, uploaded CSV, email-in attachment) all
* go through parseCsv. It used to split the text into lines before tracking quotes, so a cell
* containing a newline — which Google Sheets exports for any multi-line cell — was torn into
* fragments: the real row disappeared and short, column-shifted junk rows took its place, with
* no error to say so.
*/
import { describe, expect, it } from 'vitest';
import { parseCsv } from '../src/routes/imports';
describe('Worker parseCsv', () => {
it('keeps a quoted newline inside one record and does not shift the rows after it', () => {
const { headers, rows } = parseCsv(
'date,description,amount\n2026-01-01,"two\nlines",5\n2026-01-02,after,6\n'
);
expect(headers).toEqual(['date', 'description', 'amount']);
expect(rows).toEqual([
['2026-01-01', 'two\nlines', '5'],
['2026-01-02', 'after', '6'],
]);
});
it('keeps commas inside a quoted cell in one column', () => {
const { rows } = parseCsv('description,amount\n"CLOUDFLARE 10,46 USD, Dublin",9.45\n');
expect(rows).toEqual([['CLOUDFLARE 10,46 USD, Dublin', '9.45']]);
});
it('unescapes a doubled quote', () => {
const { rows } = parseCsv('description,amount\n"He said ""hi""",5\n');
expect(rows).toEqual([['He said "hi"', '5']]);
});
it('handles CRLF and skips blank lines', () => {
const { rows } = parseCsv('a,b\r\n1,2\r\n\r\n3,4\r\n');
expect(rows).toEqual([
['1', '2'],
['3', '4'],
]);
});
});