import { describe, it, expect } from 'vitest'; import { csvCell, csvLine, csvComment, csvFileName, EPI_CSV_COLUMNS } from '@/lib/explorer-csv'; describe('csvCell', () => { it('leaves plain values untouched and renders null/undefined as empty', () => { expect(csvCell('lung')).toBe('lung'); expect(csvCell(12.5)).toBe('12.5'); expect(csvCell(null)).toBe(''); expect(csvCell(undefined)).toBe(''); }); it('quotes commas, quotes, CR and LF and doubles inner quotes', () => { expect(csvCell('USCS "Pancreas" (ICD-10 C25; ICD-O-3 C250-C259)')).toBe('"USCS ""Pancreas"" (ICD-10 C25; ICD-O-3 C250-C259)"'); expect(csvCell('a,b')).toBe('"a,b"'); expect(csvCell('line1\nline2')).toBe('"line1\nline2"'); expect(csvCell('x\r\ny')).toBe('"x\r\ny"'); }); it('serializes dates as ISO-8601', () => { expect(csvCell(new Date('2026-09-08T12:22:48.904Z'))).toBe('2026-09-08T12:22:48.904Z'); }); }); describe('csvLine / csvComment', () => { it('joins cells with commas', () => { expect(csvLine(['a', 1, null, 'b,c'])).toBe('a,1,,"b,c"'); }); it('keeps comments on one line', () => { expect(csvComment('Source: X\nlicense: Y')).toBe('# Source: X license: Y'); }); }); describe('columns and file name', () => { it('exports provenance id and source URL columns', () => { expect(EPI_CSV_COLUMNS).toContain('provenance_id'); expect(EPI_CSV_COLUMNS).toContain('source_url'); expect(EPI_CSV_COLUMNS).toContain('standard_population'); expect(new Set(EPI_CSV_COLUMNS).size).toBe(EPI_CSV_COLUMNS.length); }); it('builds a safe file name', () => { expect(csvFileName(['mortality_count', 'united-states', 'all', 'all', 1999, 2024, '2026-09-11'])).toBe('cancerindex-epidemiology-mortality-count-united-states-all-all-1999-2024-2026-09-11.csv'); expect(csvFileName([null, '', undefined])).toBe('cancerindex-epidemiology-export.csv'); expect(csvFileName(['../etc/passwd'])).toBe('cancerindex-epidemiology-etc-passwd.csv'); }); });