* feat(arcim-migration): Briox provider with SIE-over-API import - Briox auth via account ID + application token (no app-level credentials); both tokens rotate on refresh and are persisted - New sie-fetcher pulls the general ledger as SIE through the provider API for Fortnox, Briox and Bjorn Lunden - Wizard stops on a failed SIE import and surfaces the real errors instead of proceeding to the misleading migrate-guard message - PROVIDER_SIE_ONLY_FORTNOX renamed to PROVIDER_SIE_NOT_SUPPORTED; new PROVIDER_TOKEN_INVALID for rejected provider credentials Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bookkeeping): per-line accruals (periodisering) on invoices and supplier invoices Defer revenue/costs per invoice line to 29xx/17xx interim accounts with automatic monthly dissolution (nightly cron + catch-up at registration), schedule cancellation on credit, year-end auto-detect exclusion for already-scheduled invoices, invoice-inbox service-period extraction for prefill, and an MCP tool to list schedules. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * feat(bokslut): iXBRL arsredovisning generation and Bolagsverket digital filing Generate the annual report as iXBRL from a generated taxonomy registry (K2 element lists, taxonomy:generate/check scripts + CI guard), expose it via the fiscal-period API, and add the bolagsverket extension for digital submission to eget utrymme with webhook-driven status tracking (submissions table + pg tests, lifecycle events, year-end wizard UI). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * test(mcp): raise origin-guard test timeout to 20s The dynamic import pulls in the full server module; the parse alone flirts with the 5s default under full-suite parallel load. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Add new scripts and documentation for K2 AB taxonomy generation and validation - Introduced `generate-taxonomy-registry.ts` to automate the generation of the iXBRL taxonomy concept registry from official element lists and tuple models. - Added `validate-ixbrl.mjs` for validating generated iXBRL reports against the official taxonomy package using Arelle. - Included new documentation files: - `k2-ab-arsredovisning-elementlista-2024-09-12_rev20250312_sv.xlsx` - `tuple-innehallsmodell-arsredovisning-k2-2024-09-12.xlsx` - `taxonomi-paket-2024-09-12_rev20250312.zip` * Add tests for bookkeeping accruals dissolution and supplier invoices - Implement tests for the POST /api/bookkeeping/accruals/[id]/dissolve route, covering success and error scenarios. - Add tests for the DELETE /api/supplier-invoices/[id] route, including authentication checks and validation of invoice deletion conditions. - Introduce tests for the Arcim migration provider client, ensuring token handling and error classification. - Create tests for the Bolagsverket extension, validating submission role enforcement and environment settings. - Add Zod schemas for Bolagsverket response payloads to ensure proper validation. - Implement tests for MCP server's list accrual schedules, confirming registration and scope mapping. - Add consistency tests for IXBRL document generation, ensuring duplicate facts and XML escaping are handled correctly. - Introduce typed domain errors for accrual schedules to improve error handling in the service. - Add tests for resolving consent with Briox token refresh concurrency, ensuring proper token management and error handling. * fix(tests): update payload size guard comments to reflect recent changes in tool descriptions and ceiling adjustments * fix(gitattributes): mark generated JSON files in bokslut taxonomy as linguist-generated * feat(migrations): add backfill for invoices.journal_entry_id and fallback for next_voucher_number user_id * feat(bokslut): enhance compliance and financial processing features with new submission details and security measures --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
327 lines
12 KiB
TypeScript
327 lines
12 KiB
TypeScript
import { randomUUID } from 'node:crypto'
|
|
import { describe, it, expect } from 'vitest'
|
|
import { getPool, withUserContext } from './setup'
|
|
import { seedCompany, insertAuthUser } from './fixtures'
|
|
|
|
// pg-real coverage for 20260622120000_bolagsverket_arsredovisning_submissions:
|
|
// RLS isolation on all three tables, the post-upload immutability trigger, and
|
|
// the status-transition state machine (GUIDE §5.2.2).
|
|
|
|
async function insertSubmission(
|
|
companyId: string,
|
|
userId: string,
|
|
fiscalPeriodId: string,
|
|
overrides: { status?: string; uploadedAt?: string | null; idnummer?: string | null } = {},
|
|
): Promise<string> {
|
|
const id = randomUUID()
|
|
await getPool().query(
|
|
`INSERT INTO public.arsredovisning_submissions
|
|
(id, company_id, user_id, fiscal_period_id, taxonomy_version, entry_point, status, uploaded_at, idnummer)
|
|
VALUES ($1, $2, $3, $4, '2024-09-12', 'k2-ab-risbs-2024-09-12', $5, $6, $7)`,
|
|
[
|
|
id,
|
|
companyId,
|
|
userId,
|
|
fiscalPeriodId,
|
|
overrides.status ?? 'draft',
|
|
overrides.uploadedAt ?? null,
|
|
overrides.idnummer ?? null,
|
|
],
|
|
)
|
|
return id
|
|
}
|
|
|
|
describe('arsredovisning_submissions RLS', () => {
|
|
it('members see their company rows, outsiders see nothing', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const outsider = await insertAuthUser()
|
|
await insertSubmission(companyId, userId, fiscalPeriodId)
|
|
|
|
const ownRows = await withUserContext(userId, async (client) => {
|
|
const res = await client.query(
|
|
`SELECT id FROM public.arsredovisning_submissions WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
return res.rowCount
|
|
})
|
|
expect(ownRows).toBe(1)
|
|
|
|
const outsiderRows = await withUserContext(outsider, async (client) => {
|
|
const res = await client.query(
|
|
`SELECT id FROM public.arsredovisning_submissions WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
return res.rowCount
|
|
})
|
|
expect(outsiderRows).toBe(0)
|
|
})
|
|
|
|
it('users cannot DELETE submissions (no policy — audit trail)', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const id = await insertSubmission(companyId, userId, fiscalPeriodId)
|
|
|
|
const deleted = await withUserContext(userId, async (client) => {
|
|
const res = await client.query(
|
|
`DELETE FROM public.arsredovisning_submissions WHERE id = $1`,
|
|
[id],
|
|
)
|
|
return res.rowCount
|
|
})
|
|
expect(deleted).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('arsredovisning_submissions immutability after upload', () => {
|
|
it('freezes identity/fingerprint columns once uploaded_at is set', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const id = await insertSubmission(companyId, userId, fiscalPeriodId, {
|
|
status: 'uploaded',
|
|
uploadedAt: new Date().toISOString(),
|
|
idnummer: '49679',
|
|
})
|
|
|
|
await expect(
|
|
getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET idnummer = 'tampered' WHERE id = $1`,
|
|
[id],
|
|
),
|
|
).rejects.toThrow(/kan inte ändras/)
|
|
|
|
await expect(
|
|
getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET taxonomy_version = '2021-10-31' WHERE id = $1`,
|
|
[id],
|
|
),
|
|
).rejects.toThrow(/kan inte ändras/)
|
|
})
|
|
|
|
it('still allows status-tracking updates after upload', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const id = await insertSubmission(companyId, userId, fiscalPeriodId, {
|
|
status: 'uploaded',
|
|
uploadedAt: new Date().toISOString(),
|
|
})
|
|
|
|
const res = await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions
|
|
SET status = 'inkommen', error_message = NULL
|
|
WHERE id = $1 RETURNING status`,
|
|
[id],
|
|
)
|
|
expect(res.rows[0].status).toBe('inkommen')
|
|
})
|
|
|
|
it('draft rows remain freely editable', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const id = await insertSubmission(companyId, userId, fiscalPeriodId)
|
|
const res = await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions
|
|
SET entry_point = 'k2-ab-risbs-2024-09-12', kontrollera_utfall = '[]'::jsonb
|
|
WHERE id = $1 RETURNING entry_point`,
|
|
[id],
|
|
)
|
|
expect(res.rows[0].entry_point).toBe('k2-ab-risbs-2024-09-12')
|
|
})
|
|
})
|
|
|
|
describe('arsredovisning_submissions audit survival on user deletion', () => {
|
|
it('keeps the submission row with user_id NULL when the filing user is deleted', async () => {
|
|
const { companyId, fiscalPeriodId } = await seedCompany()
|
|
// A separate filer (not the company creator) so the company itself survives.
|
|
const filer = await insertAuthUser()
|
|
const id = await insertSubmission(companyId, filer, fiscalPeriodId, {
|
|
status: 'uploaded',
|
|
uploadedAt: new Date().toISOString(),
|
|
idnummer: '70001',
|
|
})
|
|
|
|
await getPool().query(`DELETE FROM auth.users WHERE id = $1`, [filer])
|
|
|
|
const res = await getPool().query(
|
|
`SELECT user_id, status, idnummer FROM public.arsredovisning_submissions WHERE id = $1`,
|
|
[id],
|
|
)
|
|
expect(res.rowCount).toBe(1)
|
|
expect(res.rows[0].user_id).toBeNull()
|
|
expect(res.rows[0].status).toBe('uploaded')
|
|
expect(res.rows[0].idnummer).toBe('70001')
|
|
})
|
|
|
|
it('keeps avtal acceptances with user_id NULL when the accepting user is deleted', async () => {
|
|
const { companyId } = await seedCompany()
|
|
const acceptor = await insertAuthUser()
|
|
const inserted = await getPool().query(
|
|
`INSERT INTO public.bolagsverket_avtal_acceptances (company_id, user_id, avtalstext_andrad)
|
|
VALUES ($1, $2, '2017-12-06') RETURNING id`,
|
|
[companyId, acceptor],
|
|
)
|
|
|
|
await getPool().query(`DELETE FROM auth.users WHERE id = $1`, [acceptor])
|
|
|
|
const res = await getPool().query(
|
|
`SELECT user_id FROM public.bolagsverket_avtal_acceptances WHERE id = $1`,
|
|
[inserted.rows[0].id],
|
|
)
|
|
expect(res.rowCount).toBe(1)
|
|
expect(res.rows[0].user_id).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('arsredovisning_submissions status machine', () => {
|
|
it('follows the documented flow inkommen → forelagd → komplettering → registrerad', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const id = await insertSubmission(companyId, userId, fiscalPeriodId, {
|
|
status: 'uploaded',
|
|
uploadedAt: new Date().toISOString(),
|
|
})
|
|
for (const next of ['inkommen', 'forelagd', 'komplettering', 'registrerad']) {
|
|
await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET status = $2 WHERE id = $1`,
|
|
[id, next],
|
|
)
|
|
}
|
|
const final = await getPool().query(
|
|
`SELECT status, registered_at FROM public.arsredovisning_submissions WHERE id = $1`,
|
|
[id],
|
|
)
|
|
expect(final.rows[0].status).toBe('registrerad')
|
|
})
|
|
|
|
it('allows forward jumps between Bolagsverket-asserted statuses (missed webhooks)', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
// uploaded → komplettering directly (skipping inkommen + forelagd).
|
|
const skipAhead = await insertSubmission(companyId, userId, fiscalPeriodId, {
|
|
status: 'uploaded',
|
|
uploadedAt: new Date().toISOString(),
|
|
})
|
|
const res1 = await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET status = 'komplettering' WHERE id = $1 RETURNING status`,
|
|
[skipAhead],
|
|
)
|
|
expect(res1.rows[0].status).toBe('komplettering')
|
|
|
|
// inkommen → komplettering (skipping forelagd).
|
|
const inkommen = await insertSubmission(companyId, userId, fiscalPeriodId, {
|
|
status: 'uploaded',
|
|
uploadedAt: new Date().toISOString(),
|
|
})
|
|
await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET status = 'inkommen' WHERE id = $1`,
|
|
[inkommen],
|
|
)
|
|
const res2 = await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET status = 'komplettering' WHERE id = $1 RETURNING status`,
|
|
[inkommen],
|
|
)
|
|
expect(res2.rows[0].status).toBe('komplettering')
|
|
})
|
|
|
|
it('allows error capture and retry transitions (draft/kontrollerad → error → draft)', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const id = await insertSubmission(companyId, userId, fiscalPeriodId, { status: 'kontrollerad' })
|
|
const errored = await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions
|
|
SET status = 'error', error_message = 'inlamning failed'
|
|
WHERE id = $1 RETURNING status, error_message`,
|
|
[id],
|
|
)
|
|
expect(errored.rows[0].status).toBe('error')
|
|
expect(errored.rows[0].error_message).toBe('inlamning failed')
|
|
|
|
const retried = await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET status = 'draft' WHERE id = $1 RETURNING status`,
|
|
[id],
|
|
)
|
|
expect(retried.rows[0].status).toBe('draft')
|
|
})
|
|
|
|
it('rejects undocumented transitions (registrerad → draft, draft → inkommen)', async () => {
|
|
const { userId, companyId, fiscalPeriodId } = await seedCompany()
|
|
const id = await insertSubmission(companyId, userId, fiscalPeriodId, {
|
|
status: 'uploaded',
|
|
uploadedAt: new Date().toISOString(),
|
|
})
|
|
await getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET status = 'registrerad' WHERE id = $1`,
|
|
[id],
|
|
)
|
|
await expect(
|
|
getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET status = 'draft' WHERE id = $1`,
|
|
[id],
|
|
),
|
|
).rejects.toThrow(/Ogiltig statusövergång/)
|
|
|
|
const draftId = await insertSubmission(companyId, userId, fiscalPeriodId)
|
|
await expect(
|
|
getPool().query(
|
|
`UPDATE public.arsredovisning_submissions SET status = 'inkommen' WHERE id = $1`,
|
|
[draftId],
|
|
),
|
|
).rejects.toThrow(/Ogiltig statusövergång/)
|
|
})
|
|
})
|
|
|
|
describe('bolagsverket_avtal_acceptances', () => {
|
|
it('is insert-once per (company, user, avtalstext version) and RLS-scoped', async () => {
|
|
const { userId, companyId } = await seedCompany()
|
|
const outsider = await insertAuthUser()
|
|
|
|
await withUserContext(userId, async (client) => {
|
|
await client.query(
|
|
`INSERT INTO public.bolagsverket_avtal_acceptances (company_id, user_id, avtalstext_andrad)
|
|
VALUES ($1, $2, '2017-12-06')`,
|
|
[companyId, userId],
|
|
)
|
|
await expect(
|
|
client.query(
|
|
`INSERT INTO public.bolagsverket_avtal_acceptances (company_id, user_id, avtalstext_andrad)
|
|
VALUES ($1, $2, '2017-12-06')`,
|
|
[companyId, userId],
|
|
),
|
|
).rejects.toThrow(/duplicate key/)
|
|
})
|
|
|
|
// Outsider cannot accept on behalf of someone else's company.
|
|
await withUserContext(outsider, async (client) => {
|
|
await expect(
|
|
client.query(
|
|
`INSERT INTO public.bolagsverket_avtal_acceptances (company_id, user_id, avtalstext_andrad)
|
|
VALUES ($1, $2, '2017-12-06')`,
|
|
[companyId, outsider],
|
|
),
|
|
).rejects.toThrow(/row-level security/)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('bolagsverket_subscriptions', () => {
|
|
it('is unique per (company, orgnr, url, environment) and RLS-scoped', async () => {
|
|
const { userId, companyId } = await seedCompany()
|
|
const outsider = await insertAuthUser()
|
|
await getPool().query(
|
|
`INSERT INTO public.bolagsverket_subscriptions
|
|
(company_id, user_id, orgnr, url, auth_secret, environment, expires_at)
|
|
VALUES ($1, $2, '5560001111', 'https://example.test/hook', 's3cret', 'test', now() + interval '6 months')`,
|
|
[companyId, userId],
|
|
)
|
|
await expect(
|
|
getPool().query(
|
|
`INSERT INTO public.bolagsverket_subscriptions
|
|
(company_id, user_id, orgnr, url, auth_secret, environment, expires_at)
|
|
VALUES ($1, $2, '5560001111', 'https://example.test/hook', 'other', 'test', now() + interval '6 months')`,
|
|
[companyId, userId],
|
|
),
|
|
).rejects.toThrow(/duplicate key/)
|
|
|
|
const visible = await withUserContext(outsider, async (client) => {
|
|
const res = await client.query(
|
|
`SELECT id FROM public.bolagsverket_subscriptions WHERE company_id = $1`,
|
|
[companyId],
|
|
)
|
|
return res.rowCount
|
|
})
|
|
expect(visible).toBe(0)
|
|
})
|
|
})
|