764348e99c
* feat(dimensions): PR10 advanced — custom dimensions, hierarchy, account rules, commit enforcement The final rung of the dimensions ladder (dev_docs/dimensions_implementation_plan.md §7 row 10): - custom dimensions: POST /api/dimensions creates registry dims (next free SIE number >= 20 when omitted; explicit numbers allowed — SIE import already mints reserved ones); register gets a 'Ny dimension' dialog with a quiet Avancerat disclosure for the #UNDERDIM parent; GET now carries parent_sie_dim_no (the column + SIE round-trip existed since PR1/PR5 — this exposes it) - account_dimension_rules (migration 20260703120000): one rule per (account, dimension) — required / default / fixed, per-rule is_active, company-scoped RLS, composite FK to the registry, value-presence CHECK - enforcement, opt-in BY CONSTRUCTION (zero rules = engine byte-identical; deliberately NO settings toggle — a rule that exists but is ignored is worse than either extreme): default/fixed apply onto line bags at draft creation (fixed overwrites, default fills); required asserts at commitEntry with a Swedish MANDATORY_DIMENSION_MISSING naming every account + dimension; the bulk-book route runs the same policy before its RPC; storno/correction paths never pass through commitEntry so history always reverses regardless of policy; rule fetches fail open incl. thrown exceptions - chart of accounts: per-account Dimensionsregler section in EditAccountDialog (Krävs/Förval/Låst, value picker, pause switch), gated on the existing dimensions toggle, quiet when empty - pickers: LineDimensionFields is registry-driven (one combobox per active dimension, cached fetch, hardcoded 1/6 fallback) — every existing mount lights up custom dims with zero changes - agent briefing: per-dimension required_on_accounts/default_on_accounts so agents self-correct instead of bouncing off the policy error - rules CRUD API with existence/active/company validation and qualified DTO ids; firm_id FK deferred until the firms table lands (per plan) 39 new tests (pure-fn rules, engine enforcement, both new API surfaces, pg-real RLS/CHECK/cascade suite); full suite 6,791 green; migration replayed on a fresh container. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: renumber migration to 20260703200000 — version collision with prod The concurrent session shipped pending_operations_add_link_document_to_voucher as 20260703120000 today; the Supabase preview branch (cloned from prod) rejected the duplicate version key. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: review round — auto-pick retry on collision, fail-open warnings, query schema - POST /api/dimensions retries once past a concurrent number claim when the number was auto-picked (explicit choices still 409) - every fail-open skip of the dimension-rules policy now logs a structured warning (engine draft/commit paths + bulk-book) — deliberate fail-open, but observable - GET /api/dimensions/rules validates its query through ListDimensionRulesQuerySchema instead of an inline regex Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
256 lines
8.6 KiB
TypeScript
256 lines
8.6 KiB
TypeScript
/**
|
|
* Pure-function tests for the account dimension rule layer (dimensions PR10).
|
|
*
|
|
* applyDimensionRules: 'default' fills only absent bag keys, 'fixed' always
|
|
* overwrites (including alias-sourced values, with the deprecated aliases
|
|
* cleared on changed lines), and the zero-effect paths preserve array/line
|
|
* identity so the common rule-less booking allocates nothing.
|
|
*
|
|
* assertMandatoryDimensions: throws MandatoryDimensionMissingError with one
|
|
* violation per (account, dimension) regardless of line count, treats
|
|
* alias-sourced values as satisfying (normalize folds them into the bag),
|
|
* and no-ops when no 'required' rule exists.
|
|
*/
|
|
import { describe, it, expect } from 'vitest'
|
|
import {
|
|
applyDimensionRules,
|
|
assertMandatoryDimensions,
|
|
type AccountDimensionRule,
|
|
} from '../dimension-rules'
|
|
import {
|
|
MANDATORY_DIMENSION_MISSING,
|
|
MandatoryDimensionMissingError,
|
|
} from '../dimension-errors'
|
|
|
|
interface TestLine {
|
|
account_number: string
|
|
dimensions?: Record<string, string> | null
|
|
cost_center?: string | null
|
|
project?: string | null
|
|
}
|
|
|
|
function makeRule(overrides: Partial<AccountDimensionRule> = {}): AccountDimensionRule {
|
|
return {
|
|
account_number: '4010',
|
|
rule_type: 'default',
|
|
sie_dim_no: '6',
|
|
dimension_name: 'Projekt',
|
|
value_code: 'P001',
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
describe('applyDimensionRules', () => {
|
|
it('default fills only absent keys — caller-set keys win', () => {
|
|
const lines: TestLine[] = [
|
|
{ account_number: '4010', dimensions: { '6': 'CALLER' } },
|
|
]
|
|
const rules = [
|
|
makeRule({ rule_type: 'default', sie_dim_no: '6', value_code: 'PDEF' }),
|
|
makeRule({
|
|
rule_type: 'default',
|
|
sie_dim_no: '1',
|
|
dimension_name: 'Kostnadsställe',
|
|
value_code: 'KS01',
|
|
}),
|
|
]
|
|
|
|
const out = applyDimensionRules(lines, rules)
|
|
|
|
// Absent key '1' filled; present key '6' untouched.
|
|
expect(out[0].dimensions).toEqual({ '6': 'CALLER', '1': 'KS01' })
|
|
// The input line object was not mutated.
|
|
expect(lines[0].dimensions).toEqual({ '6': 'CALLER' })
|
|
})
|
|
|
|
it('default does not override an alias-sourced value (line identity preserved)', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010', cost_center: 'KS-ALIAS' }]
|
|
const rules = [
|
|
makeRule({
|
|
rule_type: 'default',
|
|
sie_dim_no: '1',
|
|
dimension_name: 'Kostnadsställe',
|
|
value_code: 'KS99',
|
|
}),
|
|
]
|
|
|
|
// normalize folds cost_center into key '1', so the default has nothing to
|
|
// fill — nothing applies and the SAME array comes back.
|
|
expect(applyDimensionRules(lines, rules)).toBe(lines)
|
|
expect(lines[0].cost_center).toBe('KS-ALIAS')
|
|
})
|
|
|
|
it('fixed overwrites an alias-sourced value and clears the aliases', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010', cost_center: 'OLD' }]
|
|
const rules = [
|
|
makeRule({
|
|
rule_type: 'fixed',
|
|
sie_dim_no: '1',
|
|
dimension_name: 'Kostnadsställe',
|
|
value_code: 'KS99',
|
|
}),
|
|
]
|
|
|
|
const out = applyDimensionRules(lines, rules)
|
|
|
|
expect(out[0].dimensions).toEqual({ '1': 'KS99' })
|
|
// Aliases nulled so downstream normalization cannot resurrect 'OLD'.
|
|
expect(out[0].cost_center).toBeNull()
|
|
expect(out[0].project).toBeNull()
|
|
})
|
|
|
|
it('fixed overwrites a caller-supplied bag value', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010', dimensions: { '6': 'CALLER' } }]
|
|
const rules = [makeRule({ rule_type: 'fixed', sie_dim_no: '6', value_code: 'PLOCK' })]
|
|
|
|
const out = applyDimensionRules(lines, rules)
|
|
|
|
expect(out[0].dimensions).toEqual({ '6': 'PLOCK' })
|
|
})
|
|
|
|
it('a fixed rule already satisfied is a no-op — same array identity', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010', dimensions: { '6': 'P001' } }]
|
|
const rules = [makeRule({ rule_type: 'fixed', sie_dim_no: '6', value_code: 'P001' })]
|
|
|
|
expect(applyDimensionRules(lines, rules)).toBe(lines)
|
|
})
|
|
|
|
it('untouched lines keep identity while changed lines are copied', () => {
|
|
const lines: TestLine[] = [
|
|
{ account_number: '4010' },
|
|
{ account_number: '1930', dimensions: { '1': 'KS01' } },
|
|
]
|
|
const rules = [makeRule({ rule_type: 'fixed', sie_dim_no: '6', value_code: 'P001' })]
|
|
|
|
const out = applyDimensionRules(lines, rules)
|
|
|
|
expect(out).not.toBe(lines)
|
|
expect(out[0]).not.toBe(lines[0])
|
|
expect(out[0].dimensions).toEqual({ '6': 'P001' })
|
|
// The 1930 line has no rule — the exact same object rides through.
|
|
expect(out[1]).toBe(lines[1])
|
|
})
|
|
|
|
it('returns the same array for zero rules and for required-only rules', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010' }]
|
|
|
|
expect(applyDimensionRules(lines, [])).toBe(lines)
|
|
// 'required' rules carry no value — they never apply at draft time.
|
|
expect(
|
|
applyDimensionRules(lines, [
|
|
makeRule({ rule_type: 'required', value_code: null }),
|
|
]),
|
|
).toBe(lines)
|
|
})
|
|
|
|
it('rules for other accounts do not leak onto unrelated lines', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010' }]
|
|
const rules = [
|
|
makeRule({ account_number: '5010', rule_type: 'fixed', value_code: 'P001' }),
|
|
makeRule({ account_number: '5010', rule_type: 'default', value_code: 'P002' }),
|
|
]
|
|
|
|
expect(applyDimensionRules(lines, rules)).toBe(lines)
|
|
expect(lines[0].dimensions).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('assertMandatoryDimensions', () => {
|
|
const requiredProjekt = makeRule({ rule_type: 'required', value_code: null })
|
|
|
|
it('throws with one deduped violation across multiple missing lines', () => {
|
|
const lines: TestLine[] = [
|
|
{ account_number: '4010', dimensions: {} },
|
|
{ account_number: '4010' },
|
|
{ account_number: '1930' },
|
|
]
|
|
|
|
let caught: unknown
|
|
try {
|
|
assertMandatoryDimensions(lines, [requiredProjekt])
|
|
} catch (err) {
|
|
caught = err
|
|
}
|
|
|
|
expect(caught).toBeInstanceOf(MandatoryDimensionMissingError)
|
|
const error = caught as MandatoryDimensionMissingError
|
|
expect(error.code).toBe(MANDATORY_DIMENSION_MISSING)
|
|
// Two 4010 lines miss the same rule → ONE violation, not two.
|
|
expect(error.violations).toEqual([
|
|
{ account_number: '4010', sie_dim_no: '6', dimension_name: 'Projekt' },
|
|
])
|
|
})
|
|
|
|
it('reports one violation per (account, dimension) pair', () => {
|
|
const lines: TestLine[] = [
|
|
{ account_number: '4010' },
|
|
{ account_number: '5010' },
|
|
]
|
|
const rules = [
|
|
requiredProjekt,
|
|
makeRule({
|
|
account_number: '5010',
|
|
rule_type: 'required',
|
|
sie_dim_no: '1',
|
|
dimension_name: 'Kostnadsställe',
|
|
value_code: null,
|
|
}),
|
|
]
|
|
|
|
let caught: unknown
|
|
try {
|
|
assertMandatoryDimensions(lines, rules)
|
|
} catch (err) {
|
|
caught = err
|
|
}
|
|
|
|
const error = caught as MandatoryDimensionMissingError
|
|
expect(error.violations).toEqual([
|
|
{ account_number: '4010', sie_dim_no: '6', dimension_name: 'Projekt' },
|
|
{ account_number: '5010', sie_dim_no: '1', dimension_name: 'Kostnadsställe' },
|
|
])
|
|
})
|
|
|
|
it('uses the Swedish message format naming account and dimension', () => {
|
|
expect(() =>
|
|
assertMandatoryDimensions([{ account_number: '4010' }], [requiredProjekt]),
|
|
).toThrow('Konto 4010 kräver Projekt — välj ett värde innan bokföring.')
|
|
})
|
|
|
|
it('is satisfied via the deprecated cost_center alias through normalize', () => {
|
|
const requiredKostnadsstalle = makeRule({
|
|
rule_type: 'required',
|
|
sie_dim_no: '1',
|
|
dimension_name: 'Kostnadsställe',
|
|
value_code: null,
|
|
})
|
|
const lines: TestLine[] = [{ account_number: '4010', cost_center: 'KS01' }]
|
|
|
|
expect(() => assertMandatoryDimensions(lines, [requiredKostnadsstalle])).not.toThrow()
|
|
})
|
|
|
|
it('is satisfied by a bag value on the required key', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010', dimensions: { '6': 'P001' } }]
|
|
|
|
expect(() => assertMandatoryDimensions(lines, [requiredProjekt])).not.toThrow()
|
|
})
|
|
|
|
it('never throws when no required rule exists (default/fixed only)', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010' }]
|
|
const rules = [
|
|
makeRule({ rule_type: 'default' }),
|
|
makeRule({ rule_type: 'fixed', sie_dim_no: '1', value_code: 'KS01' }),
|
|
]
|
|
|
|
expect(() => assertMandatoryDimensions(lines, rules)).not.toThrow()
|
|
expect(() => assertMandatoryDimensions(lines, [])).not.toThrow()
|
|
})
|
|
|
|
it('required rules on other accounts do not fire', () => {
|
|
const lines: TestLine[] = [{ account_number: '4010' }]
|
|
const rules = [makeRule({ account_number: '5010', rule_type: 'required', value_code: null })]
|
|
|
|
expect(() => assertMandatoryDimensions(lines, rules)).not.toThrow()
|
|
})
|
|
})
|