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

test(A02): validate balanced opening, discrepancies, duplicates, periods and no writes

davoodmehraban89 committed Oct 14, 2026 at 23:30 UTC af98bf8ce431eee50b085b69008ae756f22c2ce7
1 file changed +70
tests/a02-opening-balance-dry-run.test.mjs new
+70
@@ -0,0 +1,70 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { previewOpeningBalances } from '../src/core/OpeningBalanceDryRun.mjs';
4 +
5 +const period = { companyId: 'c1', startDate: '1405/01/01', endDate: '1405/12/29',
6 + status: 'open', closed: false };
7 +const fixture = () => ({
8 + companyId: 'c1', openingDate: '1405/01/01', period,
9 + lines: [
10 + { accountId: 'a-rec', role: 'receivable', debit: 300, credit: 0 },
11 + { accountId: 'a-pay', role: 'payable', debit: 0, credit: 200 },
12 + { accountId: 'a-stock', role: 'inventory', debit: 500, credit: 0 },
13 + { accountId: 'a-cash', role: 'cash', debit: 100, credit: 0 },
14 + { accountId: 'a-equity', role: 'equity', debit: 0, credit: 700 }
15 + ],
16 + contacts: [{ contactId: 'customer', receivable: 300, payable: 200 }],
17 + stock: [{ warehouseId: 'warehouse', productId: 'sku', quantityUnits: 1000, value: 500 }],
18 + cash: [{ accountId: 'cash-1', balance: 100 }],
19 + priorPostedJournals: 0
20 +});
21 +
22 +test('A02: balanced opening dataset is ready for explicit review without changing anything', () => {
23 + const input = fixture();
24 + const old = structuredClone(input);
25 + const result = previewOpeningBalances(input);
26 + assert.equal(result.readiness, 'ready-for-review');
27 + assert.deepEqual(result.issues, []);
28 + assert.equal(result.journal.debit, 900);
29 + assert.equal(result.journal.credit, 900);
30 + assert.deepEqual(result.differences, { receivable: 0, payable: 0, inventory: 0, cash: 0 });
31 + assert.equal(result.writePerformed, false);
32 + assert.deepEqual(input, old);
33 + assert.equal(Object.isFrozen(result), true);
34 +});
35 +
36 +test('A02: mismatched inventory blocks activation even if journal is balanced', () => {
37 + const input = fixture();
38 + input.stock[0].value = 499;
39 + const result = previewOpeningBalances(input);
40 + assert.equal(result.readiness, 'blocked');
41 + assert.ok(result.issues.includes('OPENING_INVENTORY_MISMATCH'));
42 + assert.equal(result.differences.inventory, -1);
43 + assert.deepEqual(input.stock[0], { warehouseId: 'warehouse', productId: 'sku',
44 + quantityUnits: 1000, value: 499 });
45 +});
46 +
47 +test('A02: unbalanced journal and existing posted transactions must block write', () => {
48 + const input = fixture();
49 + input.lines[4].credit = 699;
50 + input.priorPostedJournals = 1;
51 + const report = previewOpeningBalances(input);
52 + assert.deepEqual(report.issues, ['EXISTING_POSTED_JOURNALS', 'UNBALANCED_OPENING_JOURNAL']);
53 +});
54 +
55 +test('A02: reject cross-company, closed period, date error, duplicate and integer overflow', () => {
56 + const invalid = [
57 + { companyId: 'c2' }, { period: { ...period, status: 'closed' } },
58 + { openingDate: '1405/13/31' }, { openingDate: '1406/01/01' },
59 + { priorPostedJournals: -1 },
60 + { lines: [...fixture().lines, fixture().lines[0]] },
61 + { stock: [...fixture().stock, fixture().stock[0]] },
62 + { contacts: [...fixture().contacts, fixture().contacts[0]] },
63 + { cash: [{ accountId: 'cash-1', balance: Infinity }] },
64 + { stock: [{ ...fixture().stock[0], quantityUnits: -1 }] },
65 + { lines: fixture().lines.map(x => ({ ...x, debit: Number.MAX_SAFE_INTEGER })) }
66 + ];
67 + for (const patch of invalid) {
68 + assert.throws(() => previewOpeningBalances({ ...fixture(), ...patch }));
69 + }
70 +});