فینورا پرو - سامانه جامع حسابداری و صدور فاکتور

test: add Phase A financial isolation and journal integrity regressions

davoodmehraban89 committed Oct 14, 2026 at 10:51 UTC 3c2cc1dc459d3cda23602d291c26c0bcdf5c9d68
1 file changed +65
tests/phase-a-ledger-invariants.test.mjs new
+65
@@ -0,0 +1,65 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { journalSourceKey, validateJournalLines, reversingLines } from '../src/core/AccountingCore.mjs';
4 +
5 +// Phase A regression gate: pure domain checks, no customer data or DB migrations.
6 +const accounts = [
7 + { id: 'cash-c1', companyId: 'c1', code: '101', level: 'general', type: 'asset' },
8 + { id: 'sales-c1', companyId: 'c1', code: '401', level: 'general', type: 'revenue' },
9 + { id: 'cash-c2', companyId: 'c2', code: '101', level: 'general', type: 'asset' }
10 +];
11 +const lines = (amount) => [
12 + { accountId: 'cash-c1', debit: amount, credit: 0 },
13 + { accountId: 'sales-c1', debit: 0, credit: amount }
14 +];
15 +
16 +test('A/P0: source identity isolates company, type, source and revision', () => {
17 + const baseline = journalSourceKey('c1', 'invoice', '42');
18 + for (const other of [
19 + journalSourceKey('c2', 'invoice', '42'),
20 + journalSourceKey('c1', 'purchase', '42'),
21 + journalSourceKey('c1', 'invoice', '43'),
22 + journalSourceKey('c1', 'invoice', '42', 2)
23 + ]) assert.notEqual(baseline, other);
24 + assert.equal(baseline, journalSourceKey('c1', 'invoice', '42', 1));
25 + for (const revision of [0, -1, 1.5, Number.NaN]) {
26 + assert.throws(() => journalSourceKey('c1', 'invoice', '42', revision));
27 + }
28 + assert.throws(() => journalSourceKey('c1', 'invoice', ' '));
29 +});
30 +
31 +test('A/P0: ledger rejects cross-company posting even with identical account codes', () => {
32 + const foreign = { companyId: 'c1', lines: [
33 + { accountId: 'cash-c2', debit: 100, credit: 0 },
34 + { accountId: 'sales-c1', debit: 0, credit: 100 }
35 + ] };
36 + assert.throws(() => validateJournalLines(foreign, accounts), /شرکت دیگر|غیرمجاز/);
37 +});
38 +
39 +test('A/P0: ledger rejects unsafe amounts, zero lines, double-sided lines and overflow', () => {
40 + const invalid = [
41 + [{ accountId: 'cash-c1', debit: 100, credit: 100 }, lines(100)[1]],
42 + [{ accountId: 'cash-c1', debit: 0, credit: 0 }, lines(100)[1]],
43 + [{ accountId: 'cash-c1', debit: -100, credit: 0 }, lines(100)[1]],
44 + [{ accountId: 'cash-c1', debit: 0.5, credit: 0 }, lines(100)[1]],
45 + [{ accountId: 'cash-c1', debit: Number.MAX_SAFE_INTEGER + 1, credit: 0 }, lines(100)[1]],
46 + [
47 + { accountId: 'cash-c1', debit: Number.MAX_SAFE_INTEGER, credit: 0 },
48 + { accountId: 'cash-c1', debit: 1, credit: 0 },
49 + { accountId: 'sales-c1', debit: 0, credit: Number.MAX_SAFE_INTEGER }
50 + ]
51 + ];
52 + for (const entryLines of invalid) {
53 + assert.throws(() => validateJournalLines({ companyId: 'c1', lines: entryLines }, accounts));
54 + }
55 + assert.deepEqual(validateJournalLines({ companyId: 'c1', lines: lines(100) }, accounts), { debit: 100, credit: 100 });
56 +});
57 +
58 +test('A/P0: reversing lines preserves originals and balances exactly', () => {
59 + const original = lines(735);
60 + const snapshot = structuredClone(original);
61 + const reversed = reversingLines(original);
62 + assert.deepEqual(original, snapshot, 'posted source lines must remain immutable');
63 + assert.deepEqual(validateJournalLines({ companyId: 'c1', lines: reversed }, accounts), { debit: 735, credit: 735 });
64 + assert.deepEqual(reversingLines(reversed), original.map(line => ({ ...line, description: '' })));
65 +});