Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma strängvärde byggs med konkatenering i stället för att matcha mönstret.
84 lines
3.3 KiB
TypeScript
84 lines
3.3 KiB
TypeScript
/**
|
|
* codeFromPgError: the DB-side guards (triggers, CHECK, RESTRICT FKs) raise
|
|
* with stable identifiers in the message; the service layer maps them onto
|
|
* the same structured codes the pre-checks use.
|
|
*/
|
|
import { describe, it, expect } from 'vitest'
|
|
import { codeFromPgError, fail, failDb } from '../result'
|
|
|
|
describe('codeFromPgError', () => {
|
|
it('maps the trigger-raised SALES_ORDER_* prefixes', () => {
|
|
expect(codeFromPgError({ message: 'SALES_ORDER_OVER_INVOICED: line d1 would exceed ordered quantity' })).toBe(
|
|
'SALES_ORDER_OVER_INVOICED',
|
|
)
|
|
expect(codeFromPgError({ message: 'SALES_ORDER_QUANTITY_BELOW_INVOICED: line d1 has 4 invoiced' })).toBe(
|
|
'SALES_ORDER_QUANTITY_BELOW_INVOICED',
|
|
)
|
|
expect(codeFromPgError({ message: 'SALES_ORDER_ITEM_NOT_FOUND: d1' })).toBe('SALES_ORDER_LINE_NOT_FOUND')
|
|
})
|
|
|
|
it('maps the delivered-within-ordered CHECK constraint', () => {
|
|
expect(
|
|
codeFromPgError({
|
|
message: 'new row for relation "sales_order_items" violates check constraint "sales_order_items_delivered_within_ordered"',
|
|
}),
|
|
).toBe('SALES_ORDER_OVER_DELIVERED')
|
|
})
|
|
|
|
it('maps the RESTRICT FK from invoice_items onto SALES_ORDER_LINE_LOCKED', () => {
|
|
expect(
|
|
codeFromPgError({
|
|
code: '23503',
|
|
message:
|
|
'update or delete on table "sales_order_items" violates foreign key constraint "invoice_items_sales_order_item_id_fkey" on table "invoice_items"',
|
|
}),
|
|
).toBe('SALES_ORDER_LINE_LOCKED')
|
|
})
|
|
|
|
it('maps the RESTRICT FK from invoices onto SALES_ORDER_HAS_INVOICES', () => {
|
|
expect(
|
|
codeFromPgError({
|
|
code: '23503',
|
|
message:
|
|
'update or delete on table "sales_orders" violates foreign key constraint "invoices_sales_order_id_fkey" on table "invoices"',
|
|
}),
|
|
).toBe('SALES_ORDER_HAS_INVOICES')
|
|
})
|
|
|
|
it('maps the one-live-order-per-source index and the quote source guard (20260908165000)', () => {
|
|
expect(
|
|
codeFromPgError({
|
|
code: '23505',
|
|
message: 'duplicate key value violates unique constraint "uq_sales_orders_one_live_per_source"',
|
|
}),
|
|
).toBe('SALES_ORDER_SOURCE_ALREADY_CONVERTED')
|
|
expect(
|
|
codeFromPgError({ code: 'P0001', message: 'INVOICE_QUOTE_ALREADY_INVOICED: quote q-1 has a live converted invoice' }),
|
|
).toBe('INVOICE_QUOTE_ALREADY_INVOICED')
|
|
})
|
|
|
|
it('returns null for anything else (the raw error is surfaced instead)', () => {
|
|
expect(codeFromPgError({ message: 'null value in column "description"', code: '23502' })).toBeNull()
|
|
expect(codeFromPgError(new Error('connection reset'))).toBeNull()
|
|
expect(codeFromPgError(null)).toBeNull()
|
|
expect(codeFromPgError(undefined)).toBeNull()
|
|
expect(codeFromPgError('SALES_ORDER_OVER_INVOICED')).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('fail / failDb', () => {
|
|
it('omits details when none are given', () => {
|
|
expect(fail('SALES_ORDER_NOT_FOUND')).toEqual({ ok: false, code: 'SALES_ORDER_NOT_FOUND' })
|
|
expect(fail('SALES_ORDER_LINE_NOT_FOUND', { sales_order_item_id: 'x' })).toEqual({
|
|
ok: false,
|
|
code: 'SALES_ORDER_LINE_NOT_FOUND',
|
|
details: { sales_order_item_id: 'x' },
|
|
})
|
|
})
|
|
|
|
it('carries the raw DB error', () => {
|
|
const error = { message: 'boom', code: '42P01' }
|
|
expect(failDb(error)).toEqual({ ok: false, dbError: error })
|
|
})
|
|
})
|