Files
accounted/lib/providers/__tests__/with-provider-call.test.ts
T
Jakob WennbergandClaude Opus 5 f1d76deaba fix(providers): stop dead-ending on a resource 403, and stop dropping every migrated kreditfaktura (#2113)
* fix(providers): stop dead-ending on a resource 403, and stop dropping every migrated kreditfaktura

Two independent defects in the provider migration, both customer-visible.

A per-resource 403 was classified as a dead grant. classifyProviderError mapped
any 401 or 403 to PROVIDER_AUTH_EXPIRED, which is fatal, so a Fortnox account
without leverantorsregister permission aborted the whole migration at the
suppliers step with "Anslutningen har gatt ut. Ateranslut" even though the same
token had just succeeded on the previous step. Reconnecting can never fix that,
and steps 4 and later never ran. The provider's own reason ("Saknar behorighet
for leverantorsregister.") never reached the user. A 403 is now non-fatal once
the same token has already succeeded in the run, the migration continues, and
the provider's reason is surfaced. A 401, or a 403 on the first call, keeps the
auth-expired path.

fetchCompanyInfoDirect swallowed every error and returned null, which made the
existing PROVIDER_API_MODULE_INACTIVE remediation unreachable: a Visma customer
whose api_standard module is off got a silent 200 with an empty company card
instead of the precise Swedish explanation that was already written.

Kreditfakturor were dropped entirely. entity-mapper wrote document_type
'credit_note', but invoices_document_type_check allows only invoice, proforma
and delivery_note, and credit notes are modelled by credited_invoice_id. Every
migrated kreditfaktura was rejected and counted as skipped. One customer
imported 255 sales invoices and 0 credit notes on 2026-08-31; AR and revenue
are overstated by the credited amounts, and kreditfakturor are
rakenskapsinformation. They now import as invoice rows with reversed amounts
and status 'credited', following the in-app credit convention. They import
unlinked: no provider DTO carries a reference to the invoice being credited, so
there is nothing to match on and guessing would corrupt the AR ledger. The
wizard says so instead of burying them in skipped.

Also makes the OAuth callback non-replayable from browser history (no-store
plus history replacement), which is what the "state rejected" events were: a
replay of a callback that had already succeeded seconds earlier. No
already-connected page, so consumed-vs-unknown state stays unobservable to an
unauthenticated caller. Expected PSD2 session expiry drops from error to warn.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016ifKg6Ec67A39oxfGPU1yc

* fix(arcim): entity line needs the failed flag

The unlinked-credit-note row omitted `failed`, which the entityLines element
type requires. Caught by the zero-extensions build, not by vitest: the unit
suite does not typecheck.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016ifKg6Ec67A39oxfGPU1yc

* fix(arcim): write the missing-reference disclosure onto the credit note itself

Review finding (swedish-compliance-review-bot): ML 17 kap 22-23 § wants a
kreditfaktura to reference the invoice it credits, and BFL 5 kap 6-7 § wants a
verifikation to reference its underlag. No provider DTO carries that reference,
so the pairing cannot be resolved at import and guessing it would corrupt the
AR ledger. Reporting the count in the migration wizard is not enough: a result
screen is not rakenskapsinformation, and the gap has to be legible on the
record itself years later.

The disclosure now goes into invoices.notes and supplier_invoices.notes,
preserving whatever note the provider sent.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016ifKg6Ec67A39oxfGPU1yc

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-01 14:57:48 +02:00

191 lines
8.1 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import {
classifyProviderError,
isApiModuleInactiveError,
ProviderCallError,
type ProviderCallErrorCode,
} from '../with-provider-call'
import { FortnoxApiError } from '../fortnox/client'
import { getErrorEntry } from '@/lib/errors/structured-errors'
/**
* Locks the classification of provider-client failures into structured codes.
*
* The load-bearing case is Visma's 403 `ForbiddenRequestException - No access
* to module: api_standard` (ErrorCode 4002): the customer's plan lacks the API
* module, OAuth still succeeds, and re-authorizing loops forever. Before this
* classification it mapped to PROVIDER_AUTH_EXPIRED ("återanslut"), which sent
* a real user into exactly that loop and their report into the bug tracker.
*/
const VISMA_MODULE_BODY =
'{"ErrorCode":4002,"DeveloperErrorMessage":"ForbiddenRequestException - No access to module: api_standard","ErrorId":"x","Errors":[]}'
/** The live Fortnox answer for a supplier read the account may not make. */
const FORTNOX_SUPPLIER_BODY =
'{"ErrorInformation":{"Error":1,"Message":"Saknar beh\u00f6righet f\u00f6r leverant\u00f6rsregister.","Code":2003275}}'
/** Mirror of VismaApiError's shape: statusCode + body on a plain Error. */
function vismaError(statusCode: number, body?: string): Error {
const e = new Error(`Visma API error: ${statusCode}`) as Error & {
statusCode: number
body?: string
}
e.statusCode = statusCode
e.body = body
return e
}
describe('classifyProviderError', () => {
it('maps a Visma 403 with a "No access to module" body to PROVIDER_API_MODULE_INACTIVE, not AUTH_EXPIRED', () => {
expect(classifyProviderError(vismaError(403, VISMA_MODULE_BODY))).toBe(
'PROVIDER_API_MODULE_INACTIVE',
)
})
it('keeps a bare 403 on the first call of a run as PROVIDER_AUTH_EXPIRED', () => {
// Nothing has proven the grant yet and the body says nothing: a revoked
// grant and a closed register look identical, so "reconnect" stays.
expect(classifyProviderError(vismaError(403))).toBe('PROVIDER_AUTH_EXPIRED')
expect(classifyProviderError(vismaError(403), { grantProven: false })).toBe(
'PROVIDER_AUTH_EXPIRED',
)
})
it('reads a bare 403 as PROVIDER_RESOURCE_FORBIDDEN once the run has proven the grant', () => {
// Bokio sends an empty 403 body. The same token answered an earlier step
// in this run, so the grant is alive and one register is closed: aborting
// the run and telling the user to reconnect can never help.
expect(classifyProviderError(vismaError(403), { grantProven: true })).toBe(
'PROVIDER_RESOURCE_FORBIDDEN',
)
})
it('reads the Fortnox per-register 403 as PROVIDER_RESOURCE_FORBIDDEN even on the first call', () => {
// Fortnox answers 401 for a dead token and 403 only for a resource the
// account may not read, so its own 403 is proof enough. Classified off the
// typed error, not off the Swedish sentence in the body: the same denial
// in another locale, or reworded, must classify identically.
const err = new FortnoxApiError('Fortnox API error: 403', 403, FORTNOX_SUPPLIER_BODY)
expect(classifyProviderError(err)).toBe('PROVIDER_RESOURCE_FORBIDDEN')
const localised = new FortnoxApiError(
'Fortnox API error: 403',
403,
'{"ErrorInformation":{"Error":1,"Message":"No permission for the supplier register.","Code":2003275}}',
)
expect(classifyProviderError(localised)).toBe('PROVIDER_RESOURCE_FORBIDDEN')
// Fortnox sometimes answers with no body at all; still a per-resource 403.
expect(classifyProviderError(new FortnoxApiError('Fortnox API error: 403', 403))).toBe(
'PROVIDER_RESOURCE_FORBIDDEN',
)
})
it('does not read a Fortnox 401 as a per-resource denial', () => {
// 401 IS the dead token: the migration must keep aborting on it.
expect(classifyProviderError(new FortnoxApiError('Fortnox API error: 401', 401))).toBe(
'PROVIDER_AUTH_EXPIRED',
)
})
it('does not read another provider\'s "saknar behörighet" body as a per-resource denial', () => {
// The Swedish sentence alone proves nothing: only Fortnox is known to
// reserve 403 for the resource, and the run's own history covers the rest.
expect(classifyProviderError(vismaError(403, FORTNOX_SUPPLIER_BODY))).toBe(
'PROVIDER_AUTH_EXPIRED',
)
})
it('never downgrades a 401: a dead token is a dead token, proven grant or not', () => {
expect(classifyProviderError(vismaError(401))).toBe('PROVIDER_AUTH_EXPIRED')
expect(classifyProviderError(vismaError(401), { grantProven: true })).toBe(
'PROVIDER_AUTH_EXPIRED',
)
})
it('keeps a module/licence 403 fatal even after the grant is proven', () => {
expect(classifyProviderError(vismaError(403, VISMA_MODULE_BODY), { grantProven: true })).toBe(
'PROVIDER_API_MODULE_INACTIVE',
)
})
it('maps a Fortnox missing-license message to PROVIDER_LICENSE_MISSING', () => {
expect(classifyProviderError(new Error('token refresh failed: error_missing_license'))).toBe(
'PROVIDER_LICENSE_MISSING',
)
})
it('maps 429 and 5xx as before', () => {
expect(classifyProviderError(vismaError(429))).toBe('PROVIDER_RATE_LIMITED')
expect(classifyProviderError(vismaError(500))).toBe('PROVIDER_UPSTREAM_ERROR')
})
it('passes ProviderCallError codes through unchanged', () => {
const err = new ProviderCallError('PROVIDER_API_MODULE_INACTIVE', 'visma', 'module inactive')
expect(classifyProviderError(err)).toBe('PROVIDER_API_MODULE_INACTIVE')
})
it('re-reads a ProviderCallError 403 with the run context mapResponseError lacked', () => {
const err = new ProviderCallError('PROVIDER_AUTH_EXPIRED', 'bokio', 'Forbidden', {
status: 403,
})
expect(classifyProviderError(err)).toBe('PROVIDER_AUTH_EXPIRED')
expect(classifyProviderError(err, { grantProven: true })).toBe('PROVIDER_RESOURCE_FORBIDDEN')
const unauthorized = new ProviderCallError('PROVIDER_AUTH_EXPIRED', 'bokio', 'Unauthorized', {
status: 401,
})
expect(classifyProviderError(unauthorized, { grantProven: true })).toBe('PROVIDER_AUTH_EXPIRED')
})
it('returns null for an unclassifiable error', () => {
expect(classifyProviderError(new Error('boom'))).toBeNull()
expect(classifyProviderError('not an error')).toBeNull()
})
})
describe('isApiModuleInactiveError', () => {
it('matches the Visma module string case-insensitively', () => {
expect(isApiModuleInactiveError(VISMA_MODULE_BODY)).toBe(true)
expect(isApiModuleInactiveError('NO ACCESS TO MODULE: api_standard')).toBe(true)
})
it('does not match unrelated 403 bodies', () => {
expect(isApiModuleInactiveError('Forbidden: invalid token')).toBe(false)
})
})
describe('structured error registry wiring', () => {
it('PROVIDER_API_MODULE_INACTIVE has a 403 entry with Swedish remediation', () => {
const entry = getErrorEntry('PROVIDER_API_MODULE_INACTIVE')
expect(entry).toBeDefined()
expect(entry!.httpStatus).toBe(403)
expect(entry!.message_sv).toContain('Appar och tillägg')
expect(entry!.message_en).toBeTruthy()
})
it('every code classifyProviderError can return has a registry entry', () => {
// A code with no entry falls through entryFor() to INTERNAL_ERROR and the
// route answers 500 with "Något gick fel", which is how
// PROVIDER_RESOURCE_FORBIDDEN shipped the first time. Keyed by the union
// rather than listed in an array, so the next code added to
// ProviderCallErrorCode fails to compile until it is checked here too.
const codes: Record<ProviderCallErrorCode, true> = {
PROVIDER_AUTH_EXPIRED: true,
PROVIDER_RESOURCE_FORBIDDEN: true,
PROVIDER_LICENSE_MISSING: true,
PROVIDER_API_MODULE_INACTIVE: true,
PROVIDER_RATE_LIMITED: true,
PROVIDER_UNREACHABLE: true,
PROVIDER_UPSTREAM_ERROR: true,
}
for (const code of Object.keys(codes) as ProviderCallErrorCode[]) {
const entry = getErrorEntry(code)
expect(entry, `missing registry entry for ${code}`).toBeDefined()
expect(entry!.message_sv).toBeTruthy()
expect(entry!.message_en).toBeTruthy()
}
})
})