Files
accounted/lib/salary/__tests__/personnummer.test.ts
T
Mattsson 04dbb31d7e Salary module (#245)
* feat: implement salary module with personnummer encryption, salary entries, tax tables, and AGI tracking

- Added personnummer encryption and decryption functions for secure storage.
- Created salary entries handling for journal entries including gross salary, tax withholding, and employer contributions.
- Implemented tax table lookup functionality for calculating tax amounts based on monthly income.
- Developed SQL migration for salary module including tables for payroll configuration, tax rates, employees, salary runs, salary run employees, salary line items, and AGI declarations.
- Established row-level security policies for all new tables to ensure company-scoped access.

* feat: add salary calculation modules for 2026

- Implemented engångsskatt calculation for one-time payments with tax brackets.
- Added löneväxling functionality for salary sacrifice to pension, including employer savings and warnings.
- Created pain.001 generator for salary batch payments in compliance with Swedish banking standards.
- Developed PDF template for payslips, including detailed breakdowns and employer costs.
- Generated seed data for Swedish tax tables for 2026, including SQL insert statements.
- Implemented traktamente calculations for per diem and mileage allowances, adhering to Skatteverket regulations.
- Added seed script for populating tax tables in the database.

* feat: Update meal reduction percentages in traktamente calculation

fix: Remove obsolete seed script for 2026 tax tables

feat: Extend SalaryRunStatus type to include 'corrected' status

feat: Implement KU10 XML generation endpoint for annual employee income statements

feat: Add endpoint for creating corrections to booked salary runs

feat: Implement endpoint for sending payslip PDFs to employees

feat: Create KU10 XML generator for annual reporting

feat: Add salary transaction matcher for auto-linking bank transactions to salary entries

chore: Add database migration for salary correction support

* feat: replace select elements with custom Select component for employment and salary types

* feat: enhance salary calculations with pension entry and avgifter category support
2026-04-15 11:17:39 +02:00

112 lines
3.1 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
validatePersonnummer,
extractLast4,
extractBirthDate,
calculateAge,
calculateAgeAtYearStart,
maskPersonnummer,
formatPersonnummer,
encryptPersonnummer,
decryptPersonnummer,
} from '../personnummer'
describe('validatePersonnummer', () => {
it('accepts valid 12-digit personnummer', () => {
// Valid test personnummer (checksum matches)
const result = validatePersonnummer('199001019802')
expect(result.valid).toBe(true)
})
it('rejects non-12-digit input', () => {
const result = validatePersonnummer('9001019802')
expect(result.valid).toBe(false)
expect(result.error).toContain('12 siffror')
})
it('rejects invalid month', () => {
const result = validatePersonnummer('199013019802')
expect(result.valid).toBe(false)
expect(result.error).toContain('månad')
})
it('rejects invalid day', () => {
const result = validatePersonnummer('199001329802')
expect(result.valid).toBe(false)
expect(result.error).toContain('dag')
})
it('strips non-digits before validation', () => {
const result = validatePersonnummer('19900101-9802')
expect(result.valid).toBe(true)
})
})
describe('extractLast4', () => {
it('extracts last 4 digits', () => {
expect(extractLast4('199001019802')).toBe('9802')
})
it('handles dash-formatted input', () => {
expect(extractLast4('19900101-9802')).toBe('9802')
})
})
describe('extractBirthDate', () => {
it('extracts birth date from 12-digit personnummer', () => {
const result = extractBirthDate('199001019802')
expect(result.year).toBe(1990)
expect(result.month).toBe(1)
expect(result.day).toBe(1)
})
})
describe('calculateAge', () => {
it('calculates age at a given date', () => {
expect(calculateAge('199001019802', '2026-04-14')).toBe(36)
})
it('returns age minus one before birthday', () => {
expect(calculateAge('199006159802', '2026-06-14')).toBe(35)
expect(calculateAge('199006159802', '2026-06-15')).toBe(36)
})
})
describe('calculateAgeAtYearStart', () => {
it('calculates age at January 1 of given year', () => {
expect(calculateAgeAtYearStart('199001019802', 2026)).toBe(36)
expect(calculateAgeAtYearStart('199012319802', 2026)).toBe(35)
})
})
describe('maskPersonnummer', () => {
it('masks with XXXXXXXX-XXXX format', () => {
expect(maskPersonnummer('9802')).toBe('XXXXXXXX-9802')
})
})
describe('formatPersonnummer', () => {
it('formats with dash', () => {
expect(formatPersonnummer('199001019802')).toBe('19900101-9802')
})
})
describe('encryption roundtrip', () => {
it('encrypts and decrypts correctly', () => {
const pnr = '199001019802'
const encrypted = encryptPersonnummer(pnr)
expect(encrypted).not.toBe(pnr)
expect(encrypted.length).toBeGreaterThan(pnr.length)
const decrypted = decryptPersonnummer(encrypted)
expect(decrypted).toBe(pnr)
})
it('produces different ciphertexts for same input (random IV)', () => {
const pnr = '199001019802'
const a = encryptPersonnummer(pnr)
const b = encryptPersonnummer(pnr)
expect(a).not.toBe(b)
})
})