|
| 1 | +import {describe, it, expect, vi, beforeEach, afterEach} from 'vitest'; |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import os from 'os'; |
| 5 | +import {serialize, format_from_ext, print} from '../../utils/output'; |
| 6 | + |
| 7 | +describe('utils/output.serialize csv', ()=>{ |
| 8 | + it('serializes array of flat objects as RFC 4180 CSV with header row', ()=>{ |
| 9 | + const rows = [ |
| 10 | + {url: 'https://a.test/1', title: 'A', price: 1.5}, |
| 11 | + {url: 'https://a.test/2', title: 'B', price: 2.0}, |
| 12 | + ]; |
| 13 | + const out = serialize(rows, 'csv'); |
| 14 | + const lines = out.trim().split('\n'); |
| 15 | + expect(lines[0]).toBe('url,title,price'); |
| 16 | + expect(lines[1]).toBe('https://a.test/1,A,1.5'); |
| 17 | + expect(lines[2]).toBe('https://a.test/2,B,2'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('quotes and escapes embedded commas, quotes, and newlines', ()=>{ |
| 21 | + const rows = [{name: 'Smith, John', note: 'He said "hi"'}, |
| 22 | + {name: 'multi\nline', note: 'ok'}]; |
| 23 | + const out = serialize(rows, 'csv'); |
| 24 | + const lines = out.trim().split(/\n/); |
| 25 | + expect(lines[0]).toBe('name,note'); |
| 26 | + expect(lines[1]).toBe('"Smith, John","He said ""hi"""'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('unions keys across heterogeneous rows', ()=>{ |
| 30 | + const rows = [{a: 1, b: 2}, {a: 3, c: 4}]; |
| 31 | + const out = serialize(rows, 'csv'); |
| 32 | + const lines = out.trim().split('\n'); |
| 33 | + expect(lines[0]).toBe('a,b,c'); |
| 34 | + expect(lines[1]).toBe('1,2,'); |
| 35 | + expect(lines[2]).toBe('3,,4'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('wraps a single object as one CSV row', ()=>{ |
| 39 | + const out = serialize({a: 1, b: 'x'}, 'csv'); |
| 40 | + expect(out.trim()).toBe('a,b\n1,x'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('serializes nested values via JSON', ()=>{ |
| 44 | + const rows = [{id: 1, meta: {tag: 'x'}}]; |
| 45 | + const out = serialize(rows, 'csv'); |
| 46 | + const lines = out.trim().split('\n'); |
| 47 | + expect(lines[1]).toBe('1,"{""tag"":""x""}"'); |
| 48 | + }); |
| 49 | +}); |
| 50 | + |
| 51 | +describe('utils/output.serialize markdown', ()=>{ |
| 52 | + it('renders an array of objects as a Markdown table', ()=>{ |
| 53 | + const rows = [{a: 1, b: 'x'}, {a: 2, b: 'y'}]; |
| 54 | + const out = serialize(rows, 'markdown'); |
| 55 | + expect(out).toContain('| a | b |'); |
| 56 | + expect(out).toContain('| --- | --- |'); |
| 57 | + expect(out).toContain('| 1 | x |'); |
| 58 | + expect(out).toContain('| 2 | y |'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('escapes pipes and newlines inside cells', ()=>{ |
| 62 | + const rows = [{a: 'a|b', b: 'line1\nline2'}]; |
| 63 | + const out = serialize(rows, 'markdown'); |
| 64 | + expect(out).toContain('| a\\|b | line1 line2 |'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('falls back to a fenced JSON block for non-tabular data', ()=>{ |
| 68 | + const out = serialize([1, 2, 3], 'markdown'); |
| 69 | + expect(out.startsWith('```json')).toBe(true); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('utils/output.serialize html', ()=>{ |
| 74 | + it('renders an array of objects as an HTML table', ()=>{ |
| 75 | + const rows = [{a: 1, b: '<x>'}]; |
| 76 | + const out = serialize(rows, 'html'); |
| 77 | + expect(out).toContain('<thead><tr><th>a</th><th>b</th></tr></thead>'); |
| 78 | + expect(out).toContain('<td>1</td><td><x></td>'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('escapes HTML in non-tabular fallback', ()=>{ |
| 82 | + const out = serialize('<script>', 'html'); |
| 83 | + expect(out).toBe('<script>'); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe('utils/output.format_from_ext', ()=>{ |
| 88 | + it('maps known extensions', ()=>{ |
| 89 | + expect(format_from_ext('a.json')).toBe('json'); |
| 90 | + expect(format_from_ext('a.CSV')).toBe('csv'); |
| 91 | + expect(format_from_ext('a.md')).toBe('markdown'); |
| 92 | + expect(format_from_ext('a.html')).toBe('html'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('returns null for unknown extensions', ()=>{ |
| 96 | + expect(format_from_ext('a.txt')).toBeNull(); |
| 97 | + expect(format_from_ext('noext')).toBeNull(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('rejects .xlsx with a helpful message and exits 1', ()=>{ |
| 101 | + const exit = vi.spyOn(process, 'exit').mockImplementation( |
| 102 | + ((_code?: number)=>{ throw new Error('exit'); }) as never); |
| 103 | + const err = vi.spyOn(console, 'error').mockImplementation(()=>{}); |
| 104 | + expect(()=>format_from_ext('out.xlsx')).toThrow('exit'); |
| 105 | + const msg = err.mock.calls.map(c=>c.join(' ')).join(' '); |
| 106 | + expect(msg).toMatch(/XLSX output is not supported/); |
| 107 | + expect(msg).toMatch(/--pretty -o file\.json/); |
| 108 | + expect(msg).toMatch(/brightdata\.com\/cp\/scrapers/); |
| 109 | + exit.mockRestore(); |
| 110 | + err.mockRestore(); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('utils/output.print writes correct format from extension', ()=>{ |
| 115 | + const tmp_files: string[] = []; |
| 116 | + const make_tmp = (ext: string)=>{ |
| 117 | + const p = path.join(os.tmpdir(), |
| 118 | + `bdata-output-test-${Date.now()}-${Math.random()}${ext}`); |
| 119 | + tmp_files.push(p); |
| 120 | + return p; |
| 121 | + }; |
| 122 | + beforeEach(()=>{ vi.spyOn(console, 'error').mockImplementation(()=>{}); }); |
| 123 | + afterEach(()=>{ |
| 124 | + vi.restoreAllMocks(); |
| 125 | + for (const f of tmp_files) { try { fs.unlinkSync(f); } catch {} } |
| 126 | + }); |
| 127 | + |
| 128 | + it('-o file.csv writes CSV (regression: was silently writing JSON)', ()=>{ |
| 129 | + const out = make_tmp('.csv'); |
| 130 | + print([{url: 'https://x.test', title: 'T'}], {output: out}); |
| 131 | + const content = fs.readFileSync(out, 'utf8'); |
| 132 | + expect(content.split('\n')[0]).toBe('url,title'); |
| 133 | + expect(content.split('\n')[1]).toBe('https://x.test,T'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('-o file.html writes HTML (regression: was silently writing JSON)', ()=>{ |
| 137 | + const out = make_tmp('.html'); |
| 138 | + print([{a: 1}], {output: out}); |
| 139 | + const content = fs.readFileSync(out, 'utf8'); |
| 140 | + expect(content).toContain('<table>'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('-o file.md writes Markdown (regression: was silently writing JSON)', ()=>{ |
| 144 | + const out = make_tmp('.md'); |
| 145 | + print([{a: 1}], {output: out}); |
| 146 | + const content = fs.readFileSync(out, 'utf8'); |
| 147 | + expect(content).toContain('| a |'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('-o file.json writes JSON unchanged', ()=>{ |
| 151 | + const out = make_tmp('.json'); |
| 152 | + print([{a: 1}], {output: out}); |
| 153 | + const content = fs.readFileSync(out, 'utf8'); |
| 154 | + expect(JSON.parse(content)).toEqual([{a: 1}]); |
| 155 | + }); |
| 156 | +}); |
0 commit comments