From bfaf6108bdc99cd94bacecddc2e2231cc18a011a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 24 Feb 2026 03:48:58 +0000 Subject: [PATCH] test: Add CSV escaping tests for quotes and newlines Added unit tests to `field-logic/src/lib/flattener.test.ts` to verify that the CSV export logic correctly handles and escapes: - Strings containing double quotes (which should be escaped by doubling). - Strings containing newlines (which should be wrapped in quotes). - Strings with mixed special characters (commas, quotes, newlines). This ensures the generated CSVs are valid according to RFC 4180. Co-authored-by: alfieprojectsdev <11991855+alfieprojectsdev@users.noreply.github.com> --- field-logic/src/lib/flattener.test.ts | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/field-logic/src/lib/flattener.test.ts b/field-logic/src/lib/flattener.test.ts index dbf7d6c..e3e15e8 100644 --- a/field-logic/src/lib/flattener.test.ts +++ b/field-logic/src/lib/flattener.test.ts @@ -80,4 +80,41 @@ describe('SurveyFlattener', () => { // Let's expect basic quoting for commas expect(lines[1]).toContain('"Hello, World"'); }); + + it('toCSV should escape double quotes correctly', () => { + const session: UserSession = { + sessionId: 'sess3', + visitedNodes: ['start', 'q2', 'end'], + responses: { 'q2': 'He said "Hello"' } + }; + const csv = flattener.toCSV([session]); + const lines = csv.split('\n'); + // Expect: ..., "He said ""Hello""", ... + expect(lines[1]).toContain('"He said ""Hello"""'); + }); + + it('toCSV should handle newlines by wrapping in quotes', () => { + const session: UserSession = { + sessionId: 'sess4', + visitedNodes: ['start', 'q2', 'end'], + responses: { 'q2': 'Line 1\nLine 2' } + }; + const csv = flattener.toCSV([session]); + + // Since the value contains a newline, it should be wrapped in quotes + expect(csv).toContain('"Line 1\nLine 2"'); + }); + + it('toCSV should handle mixed special characters (comma, quote, newline)', () => { + const session: UserSession = { + sessionId: 'sess5', + visitedNodes: ['start', 'q2', 'end'], + responses: { 'q2': 'Line 1, "quote"\nLine 2' } + }; + const csv = flattener.toCSV([session]); + + // Expect: "Line 1, ""quote""\nLine 2" + // Note: internal quotes are doubled + expect(csv).toContain('"Line 1, ""quote""\nLine 2"'); + }); });