Files
accounted/lib/invoices/__tests__/late-payment-interest.test.ts
T
MattssonandClaude Opus 4.7 32d9978f1b Fix/chrome pdf preview csp (#572)
* feat: add option to exclude year-end closing entries in SIE export and related reports

* delete docs

* fix: allow Chrome's PDF viewer in verifikat document preview

The /api/documents/:id/inline route shipped with
`object-src 'none'` in its CSP, which blocked Chrome's built-in PDF
viewer (it renders inline PDFs via an internal <embed>). Users on
Chrome saw "Det här innehållet har blockerats" when expanding a PDF
attachment in the bookkeeping view; Firefox (PDF.js) and Edge (own
viewer) were unaffected, and JPGs worked because <img> isn't subject
to object-src.

Drops the CSP for this route to the minimum needed for embeddability:
`frame-ancestors 'self'`. X-Content-Type-Options: nosniff plus the
fixed Content-Type from the handler already block MIME confusion;
X-Frame-Options: SAMEORIGIN + frame-ancestors still block clickjacking.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(auth): add webmail deep link to email confirmation screens

Mirrors Stripe's signup UX: after asking the user to verify their email,
detect their webmail provider from the domain and show a button that
opens the inbox in a new tab. Gmail gets a from:<sender> search
pre-populated; Outlook/Yahoo/iCloud/Proton open the inbox directly.
Unknown / custom domains fall back to the existing copy.

Sender address is configurable via NEXT_PUBLIC_BRANDING_AUTH_EMAIL_FROM
(default noreply@gnubok.se) so white-label installs can match their
Supabase Auth SMTP config.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(auth): unblock first-time password set for BankID users with MFA

Supabase rejects updateUser({password}) and mfa.unenroll with "AAL2 session
is required" whenever a TOTP factor is enrolled. BankID magic-link logins
produce AAL1, and middleware skips MFA enforcement for bankid_linked users,
so they had no path to AAL2 — leaving them unable to set a backup password
or disable MFA without going through the email-recovery escape hatch.

- /api/account/password: branch on app_metadata.has_password. First-time set
  writes via service.auth.admin.updateUserById (no existing credential to
  protect, AAL2 guard does not apply). Change-password keeps the user-session
  updateUser so AAL2 still fires for credential rotation.
- /mfa/verify: accept a safeReturnTo query param and route there after
  successful verify, so step-up flows can land back where they came from.
- SecuritySettings: detect the AAL2 error from both change-password and
  mfa.unenroll and redirect through /mfa/verify?returnTo=/settings/account
  instead of toasting a dead-end error.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Add tests and rounding utility for öre precision in bokslut calculations

- Implemented `roundOre` function for rounding SEK amounts to two decimal places, ensuring consistent monetary calculations.
- Introduced `ORE_TOLERANCE` constant for comparing rounded amounts, facilitating invariant checks in financial entries.
- Created comprehensive tests for `roundOre`, covering typical cases, edge cases, and idempotency.
- Added year-end invariants tests to verify database-level guarantees for closing entries, ensuring they balance to the öre and reject discrepancies.
- Developed end-to-end tests for the dispositions chain, validating the correctness of calculations across various scenarios.

* fix: update PDF rendering to remove Swish QR code generation and set default to disable Swish visibility

* fix: enhance security by rejecting data URIs in safeReturnTo function tests

* fix: improve rounding logic in roundOre function and add customer_type migration

* fix: add customer_type column to customers and enforce CHECK constraint

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 22:29:41 +02:00

154 lines
4.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, it, expect } from 'vitest'
import {
calculateLatePaymentInterest,
getAnnualInterestRate,
getReferensrantaAt,
} from '../late-payment-interest'
describe('getReferensrantaAt', () => {
it('returns 0 for early 2022', () => {
expect(getReferensrantaAt('2022-01-15')).toBe(0)
})
it('returns 0.005 for late 2022', () => {
expect(getReferensrantaAt('2022-12-31')).toBe(0.005)
})
it('returns the boundary value exactly on switch date', () => {
expect(getReferensrantaAt('2024-07-01')).toBe(0.0425)
})
it('returns the most recent rate for dates in 2026', () => {
expect(getReferensrantaAt('2026-05-15')).toBe(0.025)
})
it('falls back to earliest entry for pre-2022 dates', () => {
expect(getReferensrantaAt('2021-01-01')).toBe(0)
})
})
describe('getAnnualInterestRate', () => {
it('adds 8 percentage points to referensränta by default', () => {
// 2026 referensränta = 0.025 → 0.025 + 0.08 = 0.105
const rate = getAnnualInterestRate('2026-05-01')
expect(rate).toBeCloseTo(0.105, 4)
})
it('uses the override rate when supplied', () => {
const rate = getAnnualInterestRate('2026-05-01', 0.115)
expect(rate).toBe(0.115)
})
it('respects override of 0 (interest-free reminder)', () => {
const rate = getAnnualInterestRate('2026-05-01', 0)
expect(rate).toBe(0)
})
it('ignores undefined override but respects 0', () => {
expect(getAnnualInterestRate('2026-05-01', undefined)).toBeCloseTo(0.105, 4)
expect(getAnnualInterestRate('2026-05-01', null)).toBeCloseTo(0.105, 4)
})
})
describe('calculateLatePaymentInterest', () => {
it('returns 0 when invoice is not yet overdue', () => {
const result = calculateLatePaymentInterest({
overdueAmount: 10_000,
dueDate: '2026-06-01',
asOfDate: '2026-05-15',
})
expect(result.amount).toBe(0)
expect(result.days).toBe(0)
expect(result.fromDate).toBe('2026-06-01')
})
it('returns 0 when asOfDate equals dueDate', () => {
const result = calculateLatePaymentInterest({
overdueAmount: 10_000,
dueDate: '2026-05-15',
asOfDate: '2026-05-15',
})
expect(result.amount).toBe(0)
expect(result.days).toBe(0)
})
it('computes Räntelagen §6 default for 30 days on 10 000 kr (2026 rate 10.5%)', () => {
// 2026-01-01 referensränta = 0.025 → annual rate = 0.105
// interest = 10 000 × 0.105 × 30 / 365 ≈ 86.30
const result = calculateLatePaymentInterest({
overdueAmount: 10_000,
dueDate: '2026-04-15',
asOfDate: '2026-05-15',
})
expect(result.days).toBe(30)
expect(result.rate).toBeCloseTo(0.105, 4)
expect(result.amount).toBeCloseTo(86.3, 1)
})
it('computes with explicit override 5% on 10 000 kr for 30 days ≈ 41.10', () => {
// interest = 10 000 × 0.05 × 30 / 365 ≈ 41.0959
const result = calculateLatePaymentInterest({
overdueAmount: 10_000,
dueDate: '2026-04-15',
asOfDate: '2026-05-15',
overrideRate: 0.05,
})
expect(result.amount).toBeCloseTo(41.1, 1)
expect(result.rate).toBe(0.05)
})
it('computes with explicit override 11.5% on 10 000 kr for 30 days ≈ 94.52', () => {
// interest = 10 000 × 0.115 × 30 / 365 ≈ 94.5205
const result = calculateLatePaymentInterest({
overdueAmount: 10_000,
dueDate: '2026-04-15',
asOfDate: '2026-05-15',
overrideRate: 0.115,
})
expect(result.amount).toBeCloseTo(94.52, 1)
expect(result.rate).toBe(0.115)
})
it('uses the rate at the dueDate even when asOfDate is in a later rate period', () => {
// Due in late 2025 (rate at 2025-12-15: referensränta 0.0325 → annual 0.1125),
// checked in 2026 (rate 0.025 + 0.08 = 0.105). We should use the dueDate rate.
const result = calculateLatePaymentInterest({
overdueAmount: 10_000,
dueDate: '2025-12-15',
asOfDate: '2026-02-15',
})
expect(result.rate).toBeCloseTo(0.1125, 4)
})
it('throws on a negative overdue amount', () => {
expect(() =>
calculateLatePaymentInterest({
overdueAmount: -500,
dueDate: '2026-04-15',
asOfDate: '2026-05-15',
}),
).toThrow()
})
it('returns 0 amount when overdueAmount is 0 even if days > 0', () => {
const result = calculateLatePaymentInterest({
overdueAmount: 0,
dueDate: '2026-04-15',
asOfDate: '2026-05-15',
})
expect(result.amount).toBe(0)
expect(result.days).toBe(0)
})
it('rounds to 2 decimals (no toFixed drift)', () => {
// 12 345 × 0.115 × 17 / 365 = 66.1150... → 66.12
const result = calculateLatePaymentInterest({
overdueAmount: 12_345,
dueDate: '2026-04-15',
asOfDate: '2026-05-02',
overrideRate: 0.115,
})
expect(result.amount).toBe(66.12)
})
})