Files
accounted/lib/reports/__tests__/opening-balances.test.ts
T
Jakob WennbergandClaude Opus 4.6 b387a77bfd chore: remove Sentry, consolidate migrations, add test coverage (#244)
* chore: remove Sentry, consolidate migrations, add test coverage

Remove @sentry/nextjs and all Sentry integration code — error tracking
now handled by Recapt. Consolidate 22 incremental migrations into a
single schema sync migration. Add 6 new test suites (auth, invoice
matching, VAT rules, opening balances) and extend report tests with
edge cases. Update Docker image name to gnubok, sync crontabs and
extension presets, fix CSP missing space, simplify journal entry
missing-document dialog.

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

* fix: remove viewer bank import migration never applied to production

20260413150000_viewer_bank_import_permissions.sql (PR #234) was merged
to main but never applied to the production database. It references
current_active_company_id() which does not exist in production either.
This breaks fresh installs and Supabase preview branches because the
migration runs before the consolidated schema sync.

Remove it so the migration chain matches production. The viewer bank
import RLS policies should be re-added in a future migration alongside
the helper functions they depend on.

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

* fix: correct delete policies for tables without company_id column

Seven tables in the generic delete-policy loop don't have a direct
company_id column, causing fresh installs to fail with "column
company_id does not exist". Fix by moving them out of the loop:

- invoice_items, journal_entry_lines, receipt_line_items,
  supplier_invoice_items → join through parent table
- extension_toggles, notification_settings, push_subscriptions →
  user-scoped (auth.uid() = user_id)

All policies match their existing production definitions.

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

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 10:52:00 +02:00

133 lines
4.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
vi.mock('@/lib/supabase/fetch-all', () => ({
fetchAllRows: vi.fn(),
}))
import { getOpeningBalances } from '../opening-balances'
import { fetchAllRows } from '@/lib/supabase/fetch-all'
const mockFetchAllRows = vi.mocked(fetchAllRows)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const supabase = {} as any
beforeEach(() => {
vi.clearAllMocks()
})
describe('getOpeningBalances', () => {
it('returns empty map and null obEntryId when period is null', async () => {
const { balances, obEntryId } = await getOpeningBalances(supabase, 'company-1', null)
expect(balances.size).toBe(0)
expect(obEntryId).toBeNull()
})
describe('with opening_balance_entry_id (OB entry path)', () => {
const period = {
period_start: '2025-01-01',
opening_balance_entry_id: 'ob-entry-123',
}
it('returns balances from the OB entry lines', async () => {
mockFetchAllRows.mockResolvedValue([
{ account_number: '1930', debit_amount: 50000, credit_amount: 0 },
{ account_number: '2440', debit_amount: 0, credit_amount: 10000 },
])
const { balances, obEntryId } = await getOpeningBalances(supabase, 'company-1', period)
expect(balances.get('1930')).toEqual({ debit: 50000, credit: 0 })
expect(balances.get('2440')).toEqual({ debit: 0, credit: 10000 })
expect(obEntryId).toBe('ob-entry-123')
})
it('aggregates multiple lines for the same account', async () => {
mockFetchAllRows.mockResolvedValue([
{ account_number: '1930', debit_amount: 30000, credit_amount: 0 },
{ account_number: '1930', debit_amount: 20000, credit_amount: 0 },
])
const { balances } = await getOpeningBalances(supabase, 'company-1', period)
expect(balances.get('1930')).toEqual({ debit: 50000, credit: 0 })
})
it('returns the obEntryId string', async () => {
mockFetchAllRows.mockResolvedValue([])
const { obEntryId } = await getOpeningBalances(supabase, 'company-1', period)
expect(obEntryId).toBe('ob-entry-123')
})
})
describe('without opening_balance_entry_id (fallback path)', () => {
const period = {
period_start: '2025-01-01',
opening_balance_entry_id: null,
}
it('computes balances from all prior entries', async () => {
mockFetchAllRows.mockResolvedValue([
{ account_number: '1930', debit_amount: 100000, credit_amount: 5000 },
{ account_number: '3001', debit_amount: 0, credit_amount: 80000 },
])
const { balances, obEntryId } = await getOpeningBalances(supabase, 'company-1', period)
expect(balances.get('1930')).toEqual({ debit: 100000, credit: 5000 })
expect(balances.get('3001')).toEqual({ debit: 0, credit: 80000 })
expect(obEntryId).toBeNull()
})
it('aggregates multiple lines per account', async () => {
mockFetchAllRows.mockResolvedValue([
{ account_number: '1510', debit_amount: 5000, credit_amount: 0 },
{ account_number: '1510', debit_amount: 3000, credit_amount: 1000 },
])
const { balances } = await getOpeningBalances(supabase, 'company-1', period)
expect(balances.get('1510')).toEqual({ debit: 8000, credit: 1000 })
})
it('returns null obEntryId', async () => {
mockFetchAllRows.mockResolvedValue([])
const { obEntryId } = await getOpeningBalances(supabase, 'company-1', period)
expect(obEntryId).toBeNull()
})
})
it('coerces null/undefined debit/credit to 0', async () => {
const period = {
period_start: '2025-01-01',
opening_balance_entry_id: 'ob-entry-1',
}
mockFetchAllRows.mockResolvedValue([
{ account_number: '1930', debit_amount: null, credit_amount: undefined },
])
const { balances } = await getOpeningBalances(supabase, 'company-1', period)
expect(balances.get('1930')).toEqual({ debit: 0, credit: 0 })
})
it('returns empty map when no lines found', async () => {
const period = {
period_start: '2025-01-01',
opening_balance_entry_id: null,
}
mockFetchAllRows.mockResolvedValue([])
const { balances } = await getOpeningBalances(supabase, 'company-1', period)
expect(balances.size).toBe(0)
})
})