Files
accounted/lib/import/shared/__tests__/encoding.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

80 lines
2.9 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
decodeFileContent,
decodeStringContent,
hasEncodingIssues,
} from '../encoding'
describe('decodeStringContent', () => {
it('recovers UTF-8-as-Latin-1 mojibake for lowercase Swedish chars', () => {
expect(decodeStringContent('Malmö')).toBe('Malmö')
expect(decodeStringContent('Ã¥re')).toBe('Åre'.toLowerCase())
expect(decodeStringContent('Linköping')).toBe('Linköping')
})
it('recovers UTF-8-as-Latin-1 mojibake for uppercase Swedish chars', () => {
// The middle char is U+0096 (control), invisible in most renderings → "GÃTEBORG"
expect(decodeStringContent('GÃ\u0096TEBORG')).toBe('GÖTEBORG')
expect(decodeStringContent('HISINGS KÃ\u0084RRA')).toBe('HISINGS KÄRRA')
expect(decodeStringContent('Ã\u0085NGE')).toBe('ÅNGE')
})
it('is a no-op on already-correct Swedish strings', () => {
expect(decodeStringContent('GÖTEBORG')).toBe('GÖTEBORG')
expect(decodeStringContent('Malmö')).toBe('Malmö')
expect(decodeStringContent('STOCKHOLM')).toBe('STOCKHOLM')
expect(decodeStringContent('')).toBe('')
})
it('is idempotent (running twice equals running once)', () => {
const once = decodeStringContent('Malmö')
const twice = decodeStringContent(once)
expect(twice).toBe(once)
expect(twice).toBe('Malmö')
})
it('preserves non-Swedish strings unchanged', () => {
expect(decodeStringContent('Café')).toBe('Café')
expect(decodeStringContent('München')).toBe('München')
expect(decodeStringContent('123 Main St')).toBe('123 Main St')
})
})
describe('hasEncodingIssues', () => {
it('detects U+FFFD replacement characters', () => {
expect(hasEncodingIssues('Foo\uFFFDbar')).toBe(true)
})
it('detects all six Swedish mojibake patterns', () => {
expect(hasEncodingIssues('Malmö')).toBe(true) // ö
expect(hasEncodingIssues('Ã¥re')).toBe(true) // å
expect(hasEncodingIssues('älg')).toBe(true) // ä
expect(hasEncodingIssues('GÃ\u0096TEBORG')).toBe(true) // Ö
expect(hasEncodingIssues('Ã\u0085NGE')).toBe(true) // Å
expect(hasEncodingIssues('Ã\u0084RRA')).toBe(true) // Ä
})
it('returns false for clean strings', () => {
expect(hasEncodingIssues('Stockholm')).toBe(false)
expect(hasEncodingIssues('Malmö')).toBe(false)
expect(hasEncodingIssues('Café')).toBe(false)
})
})
describe('decodeFileContent', () => {
function buf(bytes: number[]): ArrayBuffer {
return new Uint8Array(bytes).buffer
}
it('decodes UTF-8 bytes correctly', () => {
const utf8 = new TextEncoder().encode('GÖTEBORG').buffer
expect(decodeFileContent(utf8)).toBe('GÖTEBORG')
})
it('falls back to Windows-1252 when UTF-8 decode is invalid', () => {
// 0xD6 = Ö in Windows-1252; lone 0xD6 is not valid UTF-8 start byte
const cp1252 = buf([0x47, 0xd6, 0x54, 0x45, 0x42, 0x4f, 0x52, 0x47])
expect(decodeFileContent(cp1252)).toBe('GÖTEBORG')
})
})