Files
accounted/lib/reconciliation/__tests__/service.test.ts
T
0a8544e0cb feat(reconciliation): account-keyed engine: one bridge for bank and skattekonto (#1813)
* feat(reconciliation): skattekonto bridge engine, sync-time twin proposals, account-keyed facade

The engine half of the reconciliation page (design: Avstämningsmotorn).

- lib/reconciliation/skattekonto-reconciliation.ts: getSkattekontoReconciliationStatus
  anchors at the saldo snapshot and returns the bridge (saldo hos Skatteverket,
  händelser som saknas, 1630-rader utan händelse, ignorerade, ingående skillnad,
  bokfört), the item buckets the page shows (proposed, unmatched external,
  unmatched ledger, matched, ignored, upcoming), opening_difference,
  unexplained_difference (0,00 by construction when data is consistent),
  dead-link handling (a link to a reversed/draft entry counts as unlinked and is
  flagged), awaiting_external for ledger lines within 5 days of the snapshot,
  staleness, and a window that scopes item lists without hiding older rows.
  Core reads skattekonto_transactions and the extension's snapshot row directly;
  no @/extensions import.
- lib/reconciliation/gl-balance.ts: one ledger-balance helper with the
  trial-balance predicate status IN (posted, reversed). The drift check summed
  posted only, which misstated 1630 for any company with a storno on the account;
  skattekonto-drift.ts now delegates to the helper.
- Proposals at sync: migration 20260823120000 adds suggested_journal_entry_id /
  suggested_at (ON DELETE SET NULL, partial index on open rows); the sync calls
  refreshSkattekontoProposals after the upsert. findMatchSuggestionsBulk now
  assigns one-to-one across rows (AGI period first, then nearest date) and falls
  back to an entry whose 1630 lines net to the amount (split lines); a proposal
  is never a link.
- lib/reconciliation/service.ts + schemas.ts: the account-keyed facade
  (bank:<cash_account_id> | skattekonto | manual:NNNN) with listReconciliationAccounts
  (enabled cash accounts folded per IBAN, skattekonto when configured) and
  getAccountStatus dispatching to the bank engine or the new one; shared Zod
  shapes for the v1 registry, MCP schemas and the UI (PR 2).

Tests: identity on a mixed fixture, storno pair, stale snapshot, awaiting window,
window scoping, failed ledger read, live-linked entries never proposed; matcher
one-to-one and split-line cases; proposal refresh writes/clears; service
dedupe and dispatch. No UI in this PR.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(reconciliation): roundOre instead of inline öre rounding (guard ratchet)

The antipattern ratchet counts Math.round(x*100)/100; the new engine used it in
five places. Switch to roundOre from @/lib/money and ratchet the baseline down
by the three occurrences this removes net of the matcher rewrite.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-24 13:55:08 +02:00

241 lines
8.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { createQueuedMockSupabase } from '@/tests/helpers'
const skattekontoStatusMock = vi.fn()
const bankStatusMock = vi.fn()
vi.mock('../skattekonto-reconciliation', () => ({
getSkattekontoReconciliationStatus: (...args: unknown[]) => skattekontoStatusMock(...args),
}))
vi.mock('../bank-reconciliation', () => ({
getReconciliationStatus: (...args: unknown[]) => bankStatusMock(...args),
}))
import { bankAccountKey, parseAccountKey } from '../schemas'
import { getAccountStatus, listReconciliationAccounts } from '../service'
const COMPANY = 'company-1'
const ID_A = '11111111-1111-4111-8111-111111111111'
const ID_B = '22222222-2222-4222-8222-222222222222'
const ID_C = '33333333-3333-4333-8333-333333333333'
function cashAccount(id: string, overrides: Record<string, unknown> = {}) {
return {
id,
name: `Konto ${id.slice(0, 2)}`,
ledger_account: '1930',
currency: 'SEK',
iban: null,
enabled: true,
is_primary: false,
source: 'enable_banking',
bank_connection_id: 'conn-1',
updated_at: '2026-08-01T00:00:00Z',
...overrides,
}
}
function bankStatus(overrides: Record<string, unknown> = {}) {
return {
currency: 'SEK',
bank_transaction_total: 100,
ignored_transaction_total: 0,
ignored_transaction_count: 0,
gl_1930_balance: 100,
gl_1930_period_movement: 100,
gl_1930_opening_balance: 0,
gl_1930_correction_adjustment: 0,
difference: 0,
is_reconciled: true,
matched_count: 3,
unmatched_transaction_count: 0,
unmatched_transaction_total: 0,
unmatched_gl_line_count: 0,
unmatched_gl_line_total: 0,
unexplained_difference: 0,
unconvertible_gl_line_count: 0,
not_reconcilable_reason: null,
...overrides,
}
}
describe('account keys', () => {
it('parses the three kinds and rejects anything else', () => {
expect(parseAccountKey(bankAccountKey(ID_A))).toEqual({ kind: 'bank', cashAccountId: ID_A })
expect(parseAccountKey('skattekonto')).toEqual({ kind: 'skattekonto' })
expect(parseAccountKey('manual:1910')).toEqual({ kind: 'manual', accountNumber: '1910' })
expect(parseAccountKey('bank:not-a-uuid')).toBeNull()
expect(parseAccountKey('1930')).toBeNull()
expect(parseAccountKey('')).toBeNull()
})
})
describe('listReconciliationAccounts', () => {
beforeEach(() => {
vi.clearAllMocks()
skattekontoStatusMock.mockReset()
bankStatusMock.mockReset()
})
it('lists enabled cash accounts, folds reconnect duplicates by IBAN, and appends the skattekonto when configured', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({
data: [
cashAccount(ID_A, { is_primary: true, iban: 'SE1', updated_at: '2026-08-10T00:00:00Z' }),
cashAccount(ID_B, { iban: 'SE1', updated_at: '2026-06-01T00:00:00Z' }),
cashAccount(ID_C, { ledger_account: '1931', iban: 'SE2' }),
],
})
// latestBankSyncAt per account (withStatus=false skips bankStatus): three maybeSingle reads
enqueue({ data: { created_at: '2026-08-19T06:00:00Z' } })
enqueue({ data: { created_at: '2026-06-01T06:00:00Z' } })
enqueue({ data: null })
skattekontoStatusMock.mockResolvedValue({
account_key: 'skattekonto',
kind: 'skattekonto',
account_number: '1630',
currency: 'SEK',
as_of: '2026-08-20T04:00:00.000Z',
stale: false,
is_reconciled: false,
unexplained_difference: 0,
counts: { proposed: 2, unmatched_external: 3, unmatched_ledger: 1, matched: 41, ignored: 0 },
skattekonto: { fetched_at: '2026-08-20T04:00:00.000Z' },
})
const accounts = await listReconciliationAccounts(supabase as never, COMPANY, {
today: '2026-08-20',
withStatus: false,
})
expect(accounts.map((a) => a.account_key)).toEqual([
bankAccountKey(ID_A),
bankAccountKey(ID_B),
bankAccountKey(ID_C),
'skattekonto',
])
const byKey = Object.fromEntries(accounts.map((a) => [a.account_key, a]))
// The older duplicate is marked, never dropped.
expect(byKey[bankAccountKey(ID_B)].superseded_by).toBe(bankAccountKey(ID_A))
expect(byKey[bankAccountKey(ID_A)].superseded_by).toBeNull()
expect(byKey[bankAccountKey(ID_C)].superseded_by).toBeNull()
// Sync age drives staleness (7 days).
expect(byKey[bankAccountKey(ID_A)].source).toMatchObject({ type: 'psd2', stale: false })
expect(byKey[bankAccountKey(ID_B)].source.stale).toBe(true)
expect(byKey[bankAccountKey(ID_C)].source).toMatchObject({ synced_at: null, stale: true })
expect(byKey[bankAccountKey(ID_A)].status).toBeNull()
// Skattekonto row carries the logo and the open counts.
expect(byKey.skattekonto).toMatchObject({
logo_url: '/logos/skatteverket_color.svg',
source: { type: 'skatteverket_api', stale: false },
status: { state: 'open', open_counts: { proposed: 2, unmatched_external: 3, unmatched_ledger: 1 } },
})
})
it('omits the skattekonto when the company has neither snapshot nor rows', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [cashAccount(ID_A, { is_primary: true })] })
enqueue({ data: null })
skattekontoStatusMock.mockResolvedValue(null)
const accounts = await listReconciliationAccounts(supabase as never, COMPANY, {
today: '2026-08-20',
withStatus: false,
})
expect(accounts.map((a) => a.kind)).toEqual(['bank'])
})
it('computes the bank status through the existing engine with the account scope and maps it to the common shape', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: [cashAccount(ID_A, { is_primary: true, currency: 'SEK' })] })
bankStatusMock.mockResolvedValue(bankStatus({ unmatched_transaction_count: 2, unmatched_transaction_total: -1046, is_reconciled: false }))
enqueue({ data: { created_at: '2026-08-20T06:00:00Z' } }) // latestBankSyncAt inside bankStatus
enqueue({ data: { created_at: '2026-08-20T06:00:00Z' } }) // latestBankSyncAt for the account row
skattekontoStatusMock.mockResolvedValue(null)
const accounts = await listReconciliationAccounts(supabase as never, COMPANY, {
today: '2026-08-20',
windowFrom: '2026-01-01',
windowTo: '2026-08-20',
})
expect(bankStatusMock).toHaveBeenCalledWith(
supabase,
COMPANY,
'2026-01-01',
'2026-08-20',
'1930',
'SEK',
ID_A,
true,
)
expect(accounts[0].status).toMatchObject({
state: 'open',
open_counts: { proposed: 0, unmatched_external: 2, unmatched_ledger: 0 },
unexplained_difference: 0,
})
})
})
describe('getAccountStatus', () => {
beforeEach(() => {
vi.clearAllMocks()
skattekontoStatusMock.mockReset()
bankStatusMock.mockReset()
})
it('returns null for an invalid key and for an unknown cash account', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
expect(await getAccountStatus(supabase as never, COMPANY, 'nope')).toBeNull()
enqueue({ data: null })
expect(await getAccountStatus(supabase as never, COMPANY, bankAccountKey(ID_A))).toBeNull()
})
it('dispatches skattekonto to its engine with the window', async () => {
const { supabase } = createQueuedMockSupabase()
skattekontoStatusMock.mockResolvedValue({ account_key: 'skattekonto' })
const s = await getAccountStatus(supabase as never, COMPANY, 'skattekonto', {
today: '2026-08-20',
windowFrom: '2026-07-01',
windowTo: '2026-07-31',
})
expect(s).toEqual({ account_key: 'skattekonto' })
expect(skattekontoStatusMock).toHaveBeenCalledWith(supabase, COMPANY, {
today: '2026-08-20',
windowFrom: '2026-07-01',
windowTo: '2026-07-31',
})
})
it('builds the bank bridge in the #1737 shape', async () => {
const { supabase, enqueue } = createQueuedMockSupabase()
enqueue({ data: cashAccount(ID_A, { is_primary: true }) })
bankStatusMock.mockResolvedValue(
bankStatus({
bank_transaction_total: 122288,
gl_1930_period_movement: 122334,
difference: -46,
unmatched_transaction_total: -1046,
unmatched_transaction_count: 1,
unmatched_gl_line_total: -1000,
unmatched_gl_line_count: 1,
unexplained_difference: 0,
is_reconciled: false,
}),
)
enqueue({ data: { created_at: '2026-08-20T06:00:00Z' } })
const s = await getAccountStatus(supabase as never, COMPANY, bankAccountKey(ID_A), { today: '2026-08-20' })
if (!s) throw new Error('expected status')
expect(s.kind).toBe('bank')
expect(s.bridge.map((b) => [b.key, b.amount])).toEqual([
['bank_transactions', 122288],
['unmatched_external', 1046],
['unmatched_ledger', -1000],
['ledger_balance', 122334],
])
expect(s.unexplained_difference).toBe(0)
expect(s.bank).toMatchObject({ bank_transaction_total: 122288 })
})
})