Files
accounted/lib/tax/__tests__/deadline-generator.test.ts
T
Jakob WennbergandClaude Fable 5 da4d5a39ae feat(deadlines): gate AGI on employer registration + stop completing AGI at XML generation (#1062)
* feat(deadlines): gate F-skatt reminders on debited preliminary tax, add durable dismissal

The f_skatt deadline was gated on the F-skatt approval flag (DB default
true), giving nearly every company 12 monthly payment reminders for a tax
Skatteverket may not have debited at all (64% of all system deadline rows,
one lifetime completion). Approval carries no recurring obligation; the
monthly duty is payment of debiterad preliminarskatt and exists only while
the debited amount is > 0 (SFL 62 kap. 4-5 par., 55 kap. 2 par.).

- Gate the f_skatt deadline on preliminary_tax_monthly > 0 (field already
  collected at onboarding, previously unread) and retitle it as a payment.
- Storforetag keep the 12th in August (January-only 17th, 62 kap. 3 par.).
- Declare the prod-only preliminary_tax_monthly column in a migration so
  installs built purely from migrations stop failing tax-settings saves.
- Add deadlines.dismissed_at: DELETE on a system deadline now soft-dismisses
  it durably (hard deletes were resurrected by the nightly backfill within
  24h); generator, backfill, and every read surface respect it.
- Prune upcoming f_skatt rows for companies with no debited amount.

Closes part of #1028.

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

* feat(deadlines): gate AGI on employer registration, stop completing AGI deadline at XML generation

The arbetsgivardeklaration deadline was gated on pays_salaries, which is
wrong in both directions: a registered employer must file AGI every month
including nil months (SFL 26 kap. 3 par.), and companies actively running
payroll with the flag off got no AGI reminders at all (each missed monthly
filing risks a forseningsavgift).

- New company_settings.employer_registered (nullable, no default) gates
  AGI and the storforetag skatteinbetalning row; pays_salaries remains a
  fallback for rows saved before the flag existed and keeps its UI meaning.
- Migration backfills employer_registered=true from pays_salaries=true and
  from actual payroll activity (salary_runs).
- New employer_seasonal flag: sasongsregistrerade file only for payment
  months plus a December nil declaration, so only the December-period row
  is generated.
- Settings UI: registration + seasonal checkboxes (sv/en strings).
- AGI XML generation no longer auto-completes the deadline as submitted:
  SFL 26 kap. deems the obligation satisfied only when the declaration has
  come in to Skatteverket. The Skatteverket extension's kvittens reconcile
  remains the confirming path; manual filers tick the deadline themselves.

Part of #1028.

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

* fix(deadlines): include dismissed_at in DeadlineForm payload

The Deadline type gained the required dismissed_at field; the form's
submit payload literal must carry it for the Omit<Deadline, ...> shape.

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

* fix(deadlines): make system-deadline dismissal atomic

Constrain the dismiss update to source='system' and verify a row was
actually updated: a concurrent regeneration can delete the row between
lookup and update, and the route must not report a phantom success.

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

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-17 16:04:05 +02:00

307 lines
9.9 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import type { SupabaseClient } from '@supabase/supabase-js'
import {
findSettingsMissingUpcomingDeadlines,
generateTaxDeadlinesForUser,
getExpectedUpcomingDeadlineKeys,
shouldRegenerateTaxDeadlines,
} from '../deadline-generator'
import type { CompanySettingsForDeadlines } from '../deadline-config'
const SETTINGS: CompanySettingsForDeadlines = {
entity_type: 'aktiebolag',
moms_period: 'monthly',
f_skatt: true,
preliminary_tax_monthly: 5000,
vat_registered: true,
pays_salaries: true,
employer_registered: null,
employer_seasonal: false,
fiscal_year_start_month: 1,
vat_taxable_base_over_40m: false,
vat_has_eu_trade: false,
vat_filing_method: 'electronic',
periodisk_sammanstallning_enabled: false,
periodisk_sammanstallning_period: 'monthly',
periodisk_sammanstallning_filing_method: 'electronic',
}
// Future year so generated dates are never skipped as "in the past".
const FUTURE_YEAR = new Date().getFullYear() + 1
/**
* Recording mock: captures the order of insert/delete operations and the
* insert payload, so the tests can assert the insert-first/delete-after
* ordering that prevents regeneration failures from wiping deadlines.
*/
function makeRecordingSupabase(opts: {
insertError?: { code: string; message: string }
completedRows?: Array<{ tax_deadline_type: string; tax_period: string }>
} = {}) {
const calls: string[] = []
let insertPayload: Array<Record<string, unknown>> | null = null
const from = vi.fn(() => {
const chain: Record<string, ReturnType<typeof vi.fn>> = {}
let isDelete = false
const self = () => chain
chain.insert = vi.fn((rows: Array<Record<string, unknown>>) => {
calls.push('insert')
insertPayload = rows
return {
select: vi.fn(async () =>
opts.insertError
? { data: null, error: opts.insertError }
: { data: rows.map((_, i) => ({ id: `new-${i}` })), error: null }
),
}
})
chain.delete = vi.fn(() => {
calls.push('delete')
isDelete = true
return chain
})
chain.eq = vi.fn(self)
chain.or = vi.fn(self)
chain.is = vi.fn(self)
chain.gte = vi.fn(self)
chain.lte = vi.fn(self)
chain.not = vi.fn((...args: unknown[]) => {
calls.push(`not(${String(args[2]).slice(0, 20)}…)`)
return chain
})
chain.select = vi.fn(() => {
if (isDelete) {
return Promise.resolve({ data: [{ id: 'old-1' }, { id: 'old-2' }], error: null })
}
return chain
})
chain.then = vi.fn((resolve: (value: unknown) => unknown) => Promise.resolve({
data: opts.completedRows ?? [],
error: null,
}).then(resolve))
return chain
})
return {
supabase: { from } as unknown as SupabaseClient,
calls,
getInsertPayload: () => insertPayload,
}
}
beforeEach(() => {
vi.clearAllMocks()
})
describe('generateTaxDeadlinesForUser', () => {
it('inserts replacement rows before deleting the old set', async () => {
const { supabase, calls } = makeRecordingSupabase()
const result = await generateTaxDeadlinesForUser(supabase, 'company-1', SETTINGS, [FUTURE_YEAR])
expect(calls[0]).toBe('insert')
expect(calls[1]).toBe('delete')
expect(result.created).toBeGreaterThan(0)
expect(result.deleted).toBe(2)
})
it('excludes the newly inserted rows from the delete', async () => {
const { supabase, calls } = makeRecordingSupabase()
await generateTaxDeadlinesForUser(supabase, 'company-1', SETTINGS, [FUTURE_YEAR])
expect(calls.some((c) => c.startsWith('not('))).toBe(true)
})
it('builds rows owned by company_id, without a user_id field', async () => {
const { supabase, getInsertPayload } = makeRecordingSupabase()
await generateTaxDeadlinesForUser(supabase, 'company-1', SETTINGS, [FUTURE_YEAR])
const rows = getInsertPayload()
expect(rows).not.toBeNull()
for (const row of rows!) {
expect(row.company_id).toBe('company-1')
expect('user_id' in row).toBe(false)
expect(row.source).toBe('system')
expect(row.is_auto_generated).toBe(true)
}
})
it('does not delete existing deadlines when the insert fails', async () => {
const { supabase, calls } = makeRecordingSupabase({
insertError: { code: '23502', message: 'null value in column "user_id"' },
})
await expect(
generateTaxDeadlinesForUser(supabase, 'company-1', SETTINGS, [FUTURE_YEAR])
).rejects.toMatchObject({ code: '23502' })
expect(calls).toContain('insert')
expect(calls).not.toContain('delete')
})
it('does not replace a completed future obligation with a new pending row', async () => {
const completedPeriod = `${FUTURE_YEAR}-01`
const { supabase, getInsertPayload } = makeRecordingSupabase({
completedRows: [{ tax_deadline_type: 'f_skatt', tax_period: completedPeriod }],
})
await generateTaxDeadlinesForUser(supabase, 'company-1', SETTINGS, [FUTURE_YEAR])
expect(getInsertPayload()).not.toContainEqual(expect.objectContaining({
tax_deadline_type: 'f_skatt',
tax_period: completedPeriod,
}))
})
})
describe('findSettingsMissingUpcomingDeadlines', () => {
const fromDate = new Date(2030, 0, 1)
const years = [2030]
function rowsFor(
companyId: string,
keys: Set<string>,
isCompleted = false,
dismissedAt: string | null = null,
) {
return Array.from(keys, (key, index) => {
const [taxDeadlineType, taxPeriod, dueDate] = key.split(':')
return {
id: `${companyId}-${index}`,
company_id: companyId,
tax_deadline_type: taxDeadlineType,
tax_period: taxPeriod,
due_date: dueDate,
is_completed: isCompleted,
dismissed_at: dismissedAt,
}
})
}
it('returns only companies missing at least one expected obligation', () => {
const settings = [
{ company_id: 'company-1', ...SETTINGS },
{ company_id: 'company-2', ...SETTINGS },
]
const completeRows = rowsFor(
'company-1',
getExpectedUpcomingDeadlineKeys(SETTINGS, years, fromDate),
)
expect(findSettingsMissingUpcomingDeadlines(
settings,
completeRows,
years,
fromDate,
)).toEqual([settings[1]])
})
it('repairs a company that has F-tax deadlines but is missing VAT deadlines', () => {
const settings = [{ company_id: 'company-1', ...SETTINGS }]
const expectedKeys = getExpectedUpcomingDeadlineKeys(SETTINGS, years, fromDate)
const fTaxRows = rowsFor(
'company-1',
new Set(Array.from(expectedKeys).filter((key) => key.startsWith('f_skatt:'))),
)
expect(findSettingsMissingUpcomingDeadlines(
settings,
fTaxRows,
years,
fromDate,
)).toEqual(settings)
})
it('repairs a company whose rows carry dates from a superseded schedule', () => {
const settings = [{ company_id: 'company-1', ...SETTINGS }]
const staleRows = rowsFor(
'company-1',
getExpectedUpcomingDeadlineKeys(SETTINGS, years, fromDate),
).map((row) => ({ ...row, due_date: '2030-12-31' }))
expect(findSettingsMissingUpcomingDeadlines(
settings,
staleRows,
years,
fromDate,
)).toEqual(settings)
})
it('treats a completed obligation as satisfied even with a superseded due date', () => {
// A filed (is_completed) row keeps its old statutory date. The generator
// never replaces completed rows, so flagging it by date would make the
// repair loop re-run for this company every day without converging.
const settings = [{ company_id: 'company-1', ...SETTINGS }]
const completedStaleRows = rowsFor(
'company-1',
getExpectedUpcomingDeadlineKeys(SETTINGS, years, fromDate),
true,
).map((row) => ({ ...row, due_date: '2029-01-15' }))
expect(findSettingsMissingUpcomingDeadlines(
settings,
completedStaleRows,
years,
fromDate,
)).toEqual([])
})
it('treats a dismissed obligation as satisfied even with a superseded due date', () => {
// A dismissed row is an explicit opt-out. The generator never replaces
// dismissed rows, so flagging one by date would resurrect the obligation
// the user opted out of on every cron run.
const settings = [{ company_id: 'company-1', ...SETTINGS }]
const dismissedStaleRows = rowsFor(
'company-1',
getExpectedUpcomingDeadlineKeys(SETTINGS, years, fromDate),
false,
'2029-06-01T00:00:00Z',
).map((row) => ({ ...row, due_date: '2029-01-15' }))
expect(findSettingsMissingUpcomingDeadlines(
settings,
dismissedStaleRows,
years,
fromDate,
)).toEqual([])
})
it('still repairs missing pending obligations when other obligations are completed', () => {
const settings = [{ company_id: 'company-1', ...SETTINGS }]
const expectedKeys = getExpectedUpcomingDeadlineKeys(SETTINGS, years, fromDate)
// Only the F-tax obligations exist (completed); everything else is missing.
const completedFTaxRows = rowsFor(
'company-1',
new Set(Array.from(expectedKeys).filter((key) => key.startsWith('f_skatt:'))),
true,
)
expect(findSettingsMissingUpcomingDeadlines(
settings,
completedFTaxRows,
years,
fromDate,
)).toEqual(settings)
})
})
describe('shouldRegenerateTaxDeadlines', () => {
it('regenerates when a tax-relevant field changed', () => {
expect(shouldRegenerateTaxDeadlines(true, 42)).toBe(true)
})
it('regenerates when the company has no system deadlines yet, even with no field change', () => {
// The reported bug: settings were filled at onboarding, so a later save with
// no tax-field change never generated deadlines and the page stayed empty.
expect(shouldRegenerateTaxDeadlines(false, 0)).toBe(true)
})
it('does not regenerate when nothing changed and deadlines already exist', () => {
// Avoid clobbering existing status/progress on unrelated settings saves.
expect(shouldRegenerateTaxDeadlines(false, 12)).toBe(false)
})
})