81e9dd224e
* feat(import): add customer and supplier parsing functionality - Implemented customer file parsing in `lib/import/customers/parser.ts` with support for Excel and CSV formats. - Created types for detected customer columns and parsed customer rows in `lib/import/customers/types.ts`. - Added tests for customer classification logic in `lib/import/shared/__tests__/classify.test.ts`. - Developed classification functions for customers and suppliers in `lib/import/shared/classify.ts`. - Introduced shared column utility functions in `lib/import/shared/column-utils.ts`. - Implemented supplier file parsing in `lib/import/suppliers/parser.ts` with validation for various fields. - Created types for detected supplier columns and parsed supplier rows in `lib/import/suppliers/types.ts`. - Added tests for supplier column detection and parsing in `lib/import/suppliers/__tests__/column-detector.test.ts` and `lib/import/suppliers/__tests__/parser.test.ts`. * fix(labels): update 'Svenskt företag' to 'Svenskt företag eller organisation' for clarity * feat(import): refactor encoding handling for Swedish files and add tests for character preservation * feat(recapt): implement clearRecaptIdentity function and integrate into logout flow * feat(bookkeeping): implement copy functionality and next voucher sequence retrieval * feat(import): enhance customer and supplier import functionality with normalization and event handling
78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import * as XLSX from 'xlsx'
|
|
import { readWorkbookFromBuffer } from '../workbook-reader'
|
|
|
|
function bufFromBytes(bytes: number[]): ArrayBuffer {
|
|
return new Uint8Array(bytes).buffer
|
|
}
|
|
|
|
function rowsOf(workbook: XLSX.WorkBook): string[][] {
|
|
const sheet = workbook.Sheets[workbook.SheetNames[0]]
|
|
return XLSX.utils.sheet_to_json(sheet, { header: 1, defval: '', raw: false }) as string[][]
|
|
}
|
|
|
|
describe('readWorkbookFromBuffer', () => {
|
|
it('decodes UTF-8 CSV with Swedish characters correctly', () => {
|
|
const csv = new TextEncoder().encode('Namn,Ort\nAcme,GÖTEBORG\nBeta,KÄRRA\n').buffer
|
|
const wb = readWorkbookFromBuffer(csv, 'lev.csv')
|
|
expect(rowsOf(wb)).toEqual([
|
|
['Namn', 'Ort'],
|
|
['Acme', 'GÖTEBORG'],
|
|
['Beta', 'KÄRRA'],
|
|
])
|
|
})
|
|
|
|
it('decodes UTF-8 CSV with BOM', () => {
|
|
const bom = [0xef, 0xbb, 0xbf]
|
|
const body = Array.from(new TextEncoder().encode('Namn,Ort\nAcme,GÖTEBORG\n'))
|
|
const wb = readWorkbookFromBuffer(bufFromBytes([...bom, ...body]), 'lev.csv')
|
|
expect(rowsOf(wb)).toEqual([
|
|
['Namn', 'Ort'],
|
|
['Acme', 'GÖTEBORG'],
|
|
])
|
|
})
|
|
|
|
it('decodes Windows-1252 CSV with Swedish characters', () => {
|
|
// "Namn,Ort\nAcme,GÖTEBORG\nBeta,KÄRRA\n" in Windows-1252:
|
|
// Ö = 0xD6, Ä = 0xC4
|
|
const bytes = [
|
|
0x4e, 0x61, 0x6d, 0x6e, 0x2c, 0x4f, 0x72, 0x74, 0x0a,
|
|
0x41, 0x63, 0x6d, 0x65, 0x2c, 0x47, 0xd6, 0x54, 0x45, 0x42, 0x4f, 0x52, 0x47, 0x0a,
|
|
0x42, 0x65, 0x74, 0x61, 0x2c, 0x4b, 0xc4, 0x52, 0x52, 0x41, 0x0a,
|
|
]
|
|
const wb = readWorkbookFromBuffer(bufFromBytes(bytes), 'lev.csv')
|
|
expect(rowsOf(wb)).toEqual([
|
|
['Namn', 'Ort'],
|
|
['Acme', 'GÖTEBORG'],
|
|
['Beta', 'KÄRRA'],
|
|
])
|
|
})
|
|
|
|
it('reads xlsx files via the binary path', () => {
|
|
const ws = XLSX.utils.aoa_to_sheet([
|
|
['Namn', 'Ort'],
|
|
['Acme', 'GÖTEBORG'],
|
|
])
|
|
const wb = XLSX.utils.book_new()
|
|
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
|
|
const buffer = XLSX.write(wb, { type: 'array', bookType: 'xlsx' }) as ArrayBuffer
|
|
|
|
const result = readWorkbookFromBuffer(buffer, 'data.xlsx')
|
|
expect(rowsOf(result)).toEqual([
|
|
['Namn', 'Ort'],
|
|
['Acme', 'GÖTEBORG'],
|
|
])
|
|
})
|
|
|
|
it('treats non-csv extensions as binary spreadsheets', () => {
|
|
// .xls and .ods both go through the binary path; xlsx handles encoding internally
|
|
const ws = XLSX.utils.aoa_to_sheet([['A'], ['Ö']])
|
|
const wb = XLSX.utils.book_new()
|
|
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
|
|
const buffer = XLSX.write(wb, { type: 'array', bookType: 'xlsx' }) as ArrayBuffer
|
|
|
|
const result = readWorkbookFromBuffer(buffer, 'data.xls')
|
|
expect(rowsOf(result)).toEqual([['A'], ['Ö']])
|
|
})
|
|
})
|