docs(production): standardiserad produktionsberedskap och gap-analys
Lägger docs/production-readiness.md och siax/production-gaps.json enligt SIAX Master Definition of Done. Underlag: git-, kod- och live-evidens 2026-11-11. Nivån är evidensbaserad, inte ett leveransgodkännande.
This commit is contained in:
@@ -1,107 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { classifyCustomer, classifySupplier } from '../classify'
|
||||
|
||||
describe('classifyCustomer', () => {
|
||||
it('classifies 12-digit personnummer as individual', () => {
|
||||
expect(classifyCustomer({
|
||||
org_number: '198001011234',
|
||||
vat_number: null,
|
||||
})).toBe('individual')
|
||||
})
|
||||
|
||||
it('classifies 10-digit Swedish org as swedish_business', () => {
|
||||
expect(classifyCustomer({
|
||||
org_number: '5560217780',
|
||||
vat_number: null,
|
||||
})).toBe('swedish_business')
|
||||
})
|
||||
|
||||
it('classifies non-SE EU VAT prefix as eu_business', () => {
|
||||
expect(classifyCustomer({
|
||||
org_number: null,
|
||||
vat_number: 'DE123456789',
|
||||
})).toBe('eu_business')
|
||||
})
|
||||
|
||||
it('classifies non-EU VAT prefix as non_eu_business', () => {
|
||||
expect(classifyCustomer({
|
||||
org_number: null,
|
||||
vat_number: 'NO12345678',
|
||||
})).toBe('non_eu_business')
|
||||
})
|
||||
|
||||
it('keeps SE VAT prefix as swedish_business', () => {
|
||||
expect(classifyCustomer({
|
||||
org_number: '5560217780',
|
||||
vat_number: 'SE556021778001',
|
||||
})).toBe('swedish_business')
|
||||
})
|
||||
|
||||
it('classifies 10-digit personnummer-month-pattern as individual', () => {
|
||||
// Third digit is 0 (month 01), classic personnummer pattern
|
||||
expect(classifyCustomer({
|
||||
org_number: '8001011234',
|
||||
vat_number: null,
|
||||
})).toBe('individual')
|
||||
})
|
||||
|
||||
it('falls back to swedish_business when no signals', () => {
|
||||
expect(classifyCustomer({
|
||||
org_number: null,
|
||||
vat_number: null,
|
||||
})).toBe('swedish_business')
|
||||
})
|
||||
|
||||
it('uses country code when VAT missing', () => {
|
||||
expect(classifyCustomer({
|
||||
org_number: null,
|
||||
vat_number: null,
|
||||
country: 'DE',
|
||||
})).toBe('eu_business')
|
||||
})
|
||||
|
||||
it('detects Norway as non-EU', () => {
|
||||
expect(classifyCustomer({
|
||||
org_number: null,
|
||||
vat_number: null,
|
||||
country: 'Norge',
|
||||
})).toBe('non_eu_business')
|
||||
})
|
||||
})
|
||||
|
||||
describe('classifySupplier', () => {
|
||||
it('never returns individual', () => {
|
||||
expect(classifySupplier({
|
||||
org_number: '198001011234',
|
||||
vat_number: null,
|
||||
})).toBe('swedish_business')
|
||||
})
|
||||
|
||||
it('classifies non-SE EU VAT prefix as eu_business', () => {
|
||||
expect(classifySupplier({
|
||||
org_number: null,
|
||||
vat_number: 'FR12345678901',
|
||||
})).toBe('eu_business')
|
||||
})
|
||||
|
||||
it('classifies post-Brexit GB VAT as non_eu_business', () => {
|
||||
expect(classifySupplier({
|
||||
org_number: null,
|
||||
vat_number: 'GB123456789',
|
||||
})).toBe('non_eu_business')
|
||||
})
|
||||
|
||||
it('classifies XI (Northern Ireland) VAT as eu_business', () => {
|
||||
expect(classifySupplier({
|
||||
org_number: null,
|
||||
vat_number: 'XI123456789',
|
||||
})).toBe('eu_business')
|
||||
})
|
||||
|
||||
it('falls back to swedish_business by default', () => {
|
||||
expect(classifySupplier({
|
||||
org_number: null,
|
||||
vat_number: null,
|
||||
})).toBe('swedish_business')
|
||||
})
|
||||
})
|
||||
@@ -1,197 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
decodeFileContent,
|
||||
decodeStringContent,
|
||||
hasEncodingIssues,
|
||||
recoverStringWithFFFD,
|
||||
recoverWordWithFFFD,
|
||||
stripBOM,
|
||||
} 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')
|
||||
})
|
||||
|
||||
it('strips a UTF-8 BOM (EF BB BF) and decodes the remainder as UTF-8', () => {
|
||||
const payload = Array.from(new TextEncoder().encode('Bokföringsdag;Belopp'))
|
||||
const text = decodeFileContent(buf([0xef, 0xbb, 0xbf, ...payload]))
|
||||
expect(text).toBe('Bokföringsdag;Belopp')
|
||||
expect(text.charCodeAt(0)).not.toBe(0xfeff)
|
||||
})
|
||||
|
||||
it('never produces a mojibake BOM prefix when a BOM-ed file needs the Windows-1252 fallback', () => {
|
||||
// 'Datum;' + 0xD6 (Ö in Windows-1252, invalid as a lone UTF-8 byte),
|
||||
// behind a UTF-8 BOM. The BOM bytes must never re-enter the fallback
|
||||
// decode, so the result starts with 'Datum', not the literal mojibake.
|
||||
const bytes = buf([0xef, 0xbb, 0xbf, 0x44, 0x61, 0x74, 0x75, 0x6d, 0x3b, 0xd6])
|
||||
const text = decodeFileContent(bytes)
|
||||
expect(text.startsWith('')).toBe(false)
|
||||
expect(text).toBe('Datum;Ö')
|
||||
})
|
||||
|
||||
it('decodes UTF-16LE content behind a FF FE BOM', () => {
|
||||
// 'Öre' in UTF-16LE: d6 00, 72 00, 65 00
|
||||
const bytes = buf([0xff, 0xfe, 0xd6, 0x00, 0x72, 0x00, 0x65, 0x00])
|
||||
expect(decodeFileContent(bytes)).toBe('Öre')
|
||||
})
|
||||
|
||||
it('decodes UTF-16BE content behind a FE FF BOM', () => {
|
||||
const bytes = buf([0xfe, 0xff, 0x00, 0xd6, 0x00, 0x72, 0x00, 0x65])
|
||||
expect(decodeFileContent(bytes)).toBe('Öre')
|
||||
})
|
||||
})
|
||||
|
||||
describe('stripBOM', () => {
|
||||
it('strips a leading U+FEFF', () => {
|
||||
expect(stripBOM('\uFEFF' + 'Datum;Belopp')).toBe('Datum;Belopp')
|
||||
})
|
||||
|
||||
it('strips a leading literal mojibake BOM ()', () => {
|
||||
expect(stripBOM('Datum;Belopp')).toBe('Datum;Belopp')
|
||||
})
|
||||
|
||||
it('is a no-op on clean content and never strips mid-string', () => {
|
||||
expect(stripBOM('Datum;Belopp')).toBe('Datum;Belopp')
|
||||
expect(stripBOM('Datum;Belopp')).toBe('Datum;Belopp')
|
||||
})
|
||||
})
|
||||
|
||||
// --- U+FFFD heuristic recovery ---
|
||||
|
||||
describe('recoverWordWithFFFD', () => {
|
||||
it('recovers uppercase Ö in common Swedish stems', () => {
|
||||
expect(recoverWordWithFFFD('F\uFFFDRENING')).toBe('FÖRENING')
|
||||
expect(recoverWordWithFFFD('F\uFFFDRETAG')).toBe('FÖRETAG')
|
||||
expect(recoverWordWithFFFD('G\uFFFDTEBORG')).toBe('GÖTEBORG')
|
||||
expect(recoverWordWithFFFD('LINK\uFFFDPING')).toBe('LINKÖPING')
|
||||
})
|
||||
|
||||
it('recovers lowercase ö in common Swedish stems', () => {
|
||||
expect(recoverWordWithFFFD('f\uFFFDrening')).toBe('förening')
|
||||
expect(recoverWordWithFFFD('malm\uFFFD')).toBe('malmö')
|
||||
expect(recoverWordWithFFFD('k\uFFFDp')).toBe('köp')
|
||||
})
|
||||
|
||||
it('recovers compound words via substring match', () => {
|
||||
expect(recoverWordWithFFFD('BOSTADSR\uFFFDTTSF\uFFFDRENING')).toBe(
|
||||
'BOSTADSRÄTTSFÖRENING'
|
||||
)
|
||||
expect(recoverWordWithFFFD('Idrottsf\uFFFDrening')).toBe('Idrottsförening')
|
||||
})
|
||||
|
||||
it('is a no-op when the input has no U+FFFD', () => {
|
||||
expect(recoverWordWithFFFD('FÖRENING')).toBe('FÖRENING')
|
||||
expect(recoverWordWithFFFD('hello')).toBe('hello')
|
||||
})
|
||||
|
||||
it('returns null for ambiguous words not in the dictionary', () => {
|
||||
// Random 4-letter word with U+FFFD; no Swedish stem hits.
|
||||
expect(recoverWordWithFFFD('Z\uFFFDXQ')).toBeNull()
|
||||
})
|
||||
|
||||
it('returns null for words with too many U+FFFDs to disambiguate', () => {
|
||||
expect(
|
||||
recoverWordWithFFFD('\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD')
|
||||
).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('recoverStringWithFFFD', () => {
|
||||
it('repairs the canonical "Levbet FÖRENING" case', () => {
|
||||
expect(recoverStringWithFFFD('Levbet F\uFFFDRENING')).toBe('Levbet FÖRENING')
|
||||
})
|
||||
|
||||
it('repairs city + business-name combos', () => {
|
||||
expect(recoverStringWithFFFD('Sjöberg AB, Malm\uFFFD')).toBe('Sjöberg AB, Malmö')
|
||||
expect(recoverStringWithFFFD('Faktura fr\uFFFDn G\uFFFDTEBORG AB')).toBe(
|
||||
'Faktura från GÖTEBORG AB'
|
||||
)
|
||||
})
|
||||
|
||||
it('preserves punctuation, whitespace, and digits', () => {
|
||||
expect(recoverStringWithFFFD('K\uFFFDp 1 234,56 SEK')).toBe('Köp 1 234,56 SEK')
|
||||
})
|
||||
|
||||
it('is a no-op on clean strings', () => {
|
||||
expect(recoverStringWithFFFD('Hello World')).toBe('Hello World')
|
||||
expect(recoverStringWithFFFD('FÖRENING')).toBe('FÖRENING')
|
||||
})
|
||||
|
||||
it('returns null when any word in the string is ambiguous', () => {
|
||||
expect(recoverStringWithFFFD('FÖRENING Z\uFFFDXQ')).toBeNull()
|
||||
})
|
||||
|
||||
it('is idempotent on recovered output', () => {
|
||||
const once = recoverStringWithFFFD('F\uFFFDRENING')
|
||||
expect(once).toBe('FÖRENING')
|
||||
expect(recoverStringWithFFFD(once!)).toBe('FÖRENING')
|
||||
})
|
||||
})
|
||||
@@ -1,77 +0,0 @@
|
||||
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'], ['Ö']])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user