diff --git a/packages/backend/src/connectors/engines/download-follow.util.spec.ts b/packages/backend/src/connectors/engines/download-follow.util.spec.ts index c18ac0d53..e231aff2b 100644 --- a/packages/backend/src/connectors/engines/download-follow.util.spec.ts +++ b/packages/backend/src/connectors/engines/download-follow.util.spec.ts @@ -78,6 +78,26 @@ describe('followDownload', () => { ).rejects.toThrow(/Columns not in this file: nope/); }); + it('filters a tab-separated report that quotes every field', async () => { + // The FBA inventory ledger wraps each field in double quotes. + const quoted = + '"Date"\t"FNSKU"\t"Title"\t"Quantity"\n' + + '"12/01/2026"\t"X001"\t"Mug ""XL"""\t"4"\n' + + '"12/01/2026"\t"X002"\t"Cup"\t"2"\n'; + const out = await followDownload( + { url: LINK }, + config, + { filterColumn: 'FNSKU', filterValue: 'X001' }, + fakeFetch(Buffer.from(quoted)) as any, + ); + expect(out).toMatchObject({ + format: 'tsv', + columns: ['Date', 'FNSKU', 'Title', 'Quantity'], + matchedRows: 1, + rows: [{ Date: '12/01/2026', FNSKU: 'X001', Title: 'Mug "XL"', Quantity: '4' }], + }); + }); + it('caps the page size', async () => { const out = await followDownload({ url: LINK }, config, { limit: 100000 }, fakeFetch(Buffer.from(TSV)) as any); expect(out.returned).toBe(3); @@ -126,6 +146,11 @@ describe('download helpers', () => { ]); }); + it('strips the quotes around tab-separated fields only when a field is wrapped', () => { + expect(parseDelimited('"a"\tb\t"say ""hi"""\t"\t""\n', '\t')).toEqual([['a', 'b', 'say "hi"', '"', '']]); + expect(parseDelimited('12" pipe\tx\n', '\t')).toEqual([['12" pipe', 'x']]); + }); + it('validates the stored block', () => { expect(validateDownloadConfig(config)).toBeNull(); expect(validateDownloadConfig({ urlField: 'url' })).toMatch(/allowedHosts/); diff --git a/packages/backend/src/connectors/engines/download-follow.util.ts b/packages/backend/src/connectors/engines/download-follow.util.ts index 69d01d720..cfc354af4 100644 --- a/packages/backend/src/connectors/engines/download-follow.util.ts +++ b/packages/backend/src/connectors/engines/download-follow.util.ts @@ -114,15 +114,27 @@ export function decodeText(buf: Buffer, contentType: string | undefined): string } /** - * Split delimited text into records. Tabs: Amazon flat files, no quoting. - * Commas: RFC 4180 quoting ("" inside quotes, line breaks in quoted fields). + * A tab-separated field wrapped in double quotes loses them, and "" inside + * becomes ". Some Amazon reports (FBA inventory ledger) quote every field. + */ +function unquoteTabField(field: string): string { + if (field.length >= 2 && field.startsWith('"') && field.endsWith('"')) { + return field.slice(1, -1).replace(/""/g, '"'); + } + return field; +} + +/** + * Split delimited text into records. Tabs: Amazon flat files, one record per + * line; a field may be wrapped in double quotes. Commas: RFC 4180 quoting + * ("" inside quotes, line breaks in quoted fields). */ export function parseDelimited(text: string, delimiter: '\t' | ','): string[][] { const out: string[][] = []; if (delimiter === '\t') { for (const line of text.split(/\r?\n/)) { if (line.length === 0) continue; - out.push(line.split('\t')); + out.push(line.split('\t').map(unquoteTabField)); } return out; }