Files
accounted/lib/transactions/__tests__/quick-review-defaults.test.ts
T
Jakob Wennberg 198d3092c7 fix: counterparty template pick crashes the page (#1291)
Picking a suggestion under "Tidigare motparter" in Bokför transaktion replaced
the page with "Något gick fel". handleOpenTemplateReview built the review state
from `{ id, name_sv } as BookingTemplate`, so `template.debit_account` was
undefined, reached QuickReviewDialog's required `defaultAccount: string`, and
threw on `accountOverride.startsWith('2')` during the first render.

Typed the dialog's template prop as a narrow ReviewTemplate whose optional
fields are actually optional, so the cast disappears and the compiler owns this
class of bug. Also carries the counterparty's learned accounts and VAT (the
preview showed the category fallback, not what the server books) and decides
"is this a counterparty booking" from the template id rather than the presence
of a line_pattern (single-line templates got an account/VAT editor the
categorize route discards).

Five more page-crashes of the same shape, adversarially verified:

- suppliers/[id] and supplier-invoices/[id] passed the error envelope OBJECT as
  a toast description. The Toaster is a sibling of {children} in the ROOT
  layout, so that throw escapes both segment error boundaries onto global-error.
- components/reports/views wrote the same object into a useState<string | null>
  at 13 sites and rendered it bare.
- components/ui/toaster.tsx now coerces non-renderable values as a choke point.
- skattekonto read data.informationstext.length off Skatteverket's raw JSON,
  where the field is not required.
- TicWorkspace read profile.statuses.length off a persisted jsonb blob. 17 of 17
  prod rows predate the TIC v2 upgrade (#584) and lack the key, so that
  workspace was in the error boundary for every company that had opened it.

Plus hardening: formatCurrency coerces a null currency to SEK (prod has 0 NULL
across 28 416 transactions, so defense not a live bug) and cleanSignatory
returns [] for a missing description.

Verified by rendering the real dialog against a throwaway /sandbox route: the
pre-fix prop shape reproduces the exact error boundary, the fixed one renders
D: 6570 Bankavgifter / K: 1930 Företagskonto and the matching verifikat.

No migrations.
2026-07-29 19:20:25 +02:00

73 lines
3.1 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { resolveQuickReviewDefaults, type ReviewTemplate } from '../quick-review-defaults'
import { getDefaultAccountForCategory } from '@/lib/bookkeeping/category-mapping'
/**
* Regression cover for the "Tidigare motparter" crash: picking a learned
* counterparty template in the Bokför-transaktion modal replaced the whole
* page with the "Något gick fel" error boundary.
*
* The synthetic counterparty template carried only { id, name_sv }, so
* `template.debit_account` was undefined, the dialog's `defaultAccount` prop
* (declared `string`) received undefined, and the first render threw on
* `accountOverride.startsWith('2')`.
*/
describe('resolveQuickReviewDefaults', () => {
const counterparty: ReviewTemplate = {
id: 'counterparty:11111111-1111-1111-1111-111111111111',
name_sv: 'Fee',
debit_account: '6570',
credit_account: '1930',
vat_treatment: null,
}
it('never returns undefined for the account, whatever the template omits', () => {
const bare: ReviewTemplate = { id: 'counterparty:abc', name_sv: 'Fee' }
const { account, vat } = resolveQuickReviewDefaults(bare, undefined, 'expense_other')
expect(account).toBe(getDefaultAccountForCategory('expense_other'))
expect(typeof account).toBe('string')
expect(vat).toBe('none')
})
it('returns an empty account rather than undefined when there is nothing at all', () => {
expect(resolveQuickReviewDefaults(null, undefined, null)).toEqual({ account: '', vat: 'none' })
expect(resolveQuickReviewDefaults({ id: 'counterparty:abc', name_sv: 'Fee' }, undefined, null))
.toEqual({ account: '', vat: 'none' })
})
it('seeds from the counterparty template accounts, not the category fallback', () => {
const { account, vat } = resolveQuickReviewDefaults(counterparty, undefined, 'expense_other')
expect(account).toBe('6570')
expect(vat).toBe('none')
})
it('carries the learned VAT treatment of a counterparty template', () => {
const { vat } = resolveQuickReviewDefaults(
{ ...counterparty, debit_account: '5420', vat_treatment: 'standard_25' },
undefined,
'expense_other',
)
expect(vat).toBe('standard_25')
})
it('ignores the template and uses the category when a catalog templateId is present', () => {
const catalog: ReviewTemplate = {
id: 'bank_fees',
name_sv: 'Bankavgifter',
debit_account: '6570',
credit_account: '1930',
vat_treatment: null,
}
const { account } = resolveQuickReviewDefaults(catalog, 'bank_fees', 'expense_other')
// Catalog templates are validated server-side by id; the form's account
// field is not the source of truth for them.
expect(account).toBe(getDefaultAccountForCategory('expense_other'))
})
it('falls back to the category defaults when no template is involved', () => {
const { account, vat } = resolveQuickReviewDefaults(null, undefined, 'expense_other')
expect(account).toBe(getDefaultAccountForCategory('expense_other'))
expect(vat === 'none' || typeof vat === 'string').toBe(true)
})
})