Files
accounted/lib/import/customers/__tests__/parser.test.ts
T
Mattsson 81e9dd224e Add/csv import options (#420)
* 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
2026-05-08 15:42:06 +02:00

135 lines
4.4 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import * as XLSX from 'xlsx'
import { parseCustomersFile } from '../parser'
function buildXlsx(rows: (string | number)[][]): ArrayBuffer {
const ws = XLSX.utils.aoa_to_sheet(rows)
const wb = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(wb, ws, 'Kunder')
// Returning a Node Buffer; ArrayBuffer view is interchangeable with XLSX.read
const out = XLSX.write(wb, { type: 'array', bookType: 'xlsx' }) as ArrayBuffer
return out
}
describe('parseCustomersFile', () => {
it('parses a basic Swedish customer register', () => {
const buffer = buildXlsx([
['Namn', 'Orgnr', 'E-post', 'Telefon', 'Adress', 'Postnr', 'Ort'],
['Acme AB', '5560217780', 'kontakt@acme.se', '0701234567', 'Storgatan 1', '11122', 'Stockholm'],
['Beta AB', '5562345678', 'info@beta.se', '0709876543', 'Vasagatan 2', '11329', 'Stockholm'],
])
const result = parseCustomersFile(buffer, 'kunder.xlsx')
expect(result.total_rows).toBe(2)
expect(result.rows[0].name).toBe('Acme AB')
expect(result.rows[0].org_number).toBe('5560217780')
expect(result.rows[0].email).toBe('kontakt@acme.se')
expect(result.rows[0].postal_code).toBe('11122')
expect(result.rows[0].city).toBe('Stockholm')
expect(result.rows[0].is_valid).toBe(true)
})
it('auto-classifies customer_type by org_number length', () => {
const buffer = buildXlsx([
['Namn', 'Orgnr'],
['Acme AB', '5560217780'], // 10-digit company
['Sven Svensson', '198001011234'], // 12-digit personnummer
])
const result = parseCustomersFile(buffer, 'mixed.xlsx')
expect(result.rows[0].customer_type).toBe('swedish_business')
expect(result.rows[1].customer_type).toBe('individual')
})
it('classifies eu_business from VAT number prefix', () => {
const buffer = buildXlsx([
['Namn', 'VAT'],
['Müller GmbH', 'DE123456789'],
])
const result = parseCustomersFile(buffer, 'eu.xlsx')
expect(result.rows[0].customer_type).toBe('eu_business')
expect(result.rows[0].vat_number).toBe('DE123456789')
})
it('skips rows with empty name', () => {
const buffer = buildXlsx([
['Namn', 'E-post'],
['Acme AB', 'a@a.se'],
['', 'b@b.se'],
['Beta AB', 'c@c.se'],
])
const result = parseCustomersFile(buffer, 'sparse.xlsx')
expect(result.total_rows).toBe(2)
expect(result.rows.map((r) => r.name)).toEqual(['Acme AB', 'Beta AB'])
})
it('flags invalid email format', () => {
const buffer = buildXlsx([
['Namn', 'E-post'],
['Acme AB', 'not-an-email'],
])
const result = parseCustomersFile(buffer, 'bad-email.xlsx')
expect(result.rows[0].is_valid).toBe(false)
expect(result.rows[0].validation_errors).toContain('Ogiltig e-postadress')
})
it('parses payment terms with day suffix', () => {
const buffer = buildXlsx([
['Namn', 'Betalningsvillkor'],
['Acme AB', '45 dagar'],
['Beta AB', ''],
])
const result = parseCustomersFile(buffer, 'terms.xlsx')
expect(result.rows[0].default_payment_terms).toBe(45)
expect(result.rows[1].default_payment_terms).toBe(30) // default fallback
})
it('returns warning when zero rows match', () => {
const buffer = buildXlsx([
['Namn', 'Orgnr'],
])
const result = parseCustomersFile(buffer, 'empty.xlsx')
expect(result.total_rows).toBe(0)
expect(result.warnings.length).toBeGreaterThan(0)
})
it('honors explicit customer_type column', () => {
const buffer = buildXlsx([
['Namn', 'Kundtyp'],
['Acme AB', 'aktiebolag'],
['Sven', 'privatperson'],
])
const result = parseCustomersFile(buffer, 'types.xlsx')
expect(result.rows[0].customer_type).toBe('swedish_business')
expect(result.rows[1].customer_type).toBe('individual')
})
it('preserves row_index pointing to spreadsheet row', () => {
const buffer = buildXlsx([
['Namn'],
['Acme AB'],
['Beta AB'],
])
const result = parseCustomersFile(buffer, 'rows.xlsx')
expect(result.rows[0].row_index).toBe(2) // header is row 1
expect(result.rows[1].row_index).toBe(3)
})
it('preserves Swedish characters when reading a UTF-8 CSV', () => {
const csv = new TextEncoder().encode(
'Namn,Ort\nAcme AB,GÖTEBORG\nBeta AB,HISINGS KÄRRA\n',
).buffer
const result = parseCustomersFile(csv, 'kunder.csv')
expect(result.rows[0].city).toBe('GÖTEBORG')
expect(result.rows[1].city).toBe('HISINGS KÄRRA')
})
})