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

test(A04): protect posted invoices, ledger lines, stock and treasury against backup replace

davoodmehraban89 committed Oct 16, 2026 at 17:25 UTC 690555ee0b3e451ccbdda186dee4391e30506e8f
1 file changed +94
tests/a04-immutable-posted-restore.test.mjs new
+94
@@ -0,0 +1,94 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import 'fake-indexeddb/auto';
4 +import { StorageService } from '../src/core/StorageService.js';
5 +import { Auth } from '../src/core/Auth.js';
6 +import { restoreCompanyBackup } from '../src/core/CompanyBackupService.js';
7 +
8 +const companyId = 'restore-immutable-c1';
9 +const userId = 'restore-immutable-admin';
10 +async function reset() {
11 + await StorageService.clearAll();
12 + Auth.currentUser = { id: userId, username: 'manager', role: 'admin' };
13 + await StorageService.put('companies', { id: companyId, name: 'Company' });
14 +}
15 +const opts = { companyId, userId, mode: 'replace' };
16 +
17 +test('A04: replace cannot rewrite an already posted invoice or overwrite unrelated records', async () => {
18 + await reset();
19 + const original = {
20 + id: 'sale-1', companyId, kind: 'sale', number: '1',
21 + financePosted: true, grandTotal: 1000, paidAmount: 0, remaining: 1000
22 + };
23 + await StorageService.put('invoices', { ...original });
24 + const persisted = await StorageService.get('invoices', original.id);
25 +
26 + await assert.rejects(restoreCompanyBackup({
27 + contacts: [{ id: 'extra-person', companyId, name: 'Must rollback' }],
28 + invoices: [{ ...persisted, grandTotal: 1 }]
29 + }, opts), /بازنویسی سند مالی قطعی/);
30 + assert.deepEqual(await StorageService.get('invoices', original.id), persisted);
31 + assert.equal(await StorageService.get('contacts', 'extra-person'), undefined);
32 + assert.equal((await StorageService.getAll('audit_events')).length, 0);
33 +
34 + const result = await restoreCompanyBackup({ invoices: [{ ...persisted }] }, opts);
35 + assert.equal(result.skipped, 1);
36 + assert.deepEqual(await StorageService.get('invoices', original.id), persisted);
37 + assert.equal((await StorageService.getAll('audit_events')).length, 1,
38 + 'Successful restore is independently audited');
39 +});
40 +
41 +test('A04: replace cannot rewrite a line attached to a posted journal', async () => {
42 + await reset();
43 + await StorageService.put('journal_entries', {
44 + id: 'jn-posted', companyId, status: 'posted', periodId: 'period-1'
45 + });
46 + await StorageService.put('journal_lines', {
47 + id: 'jn-line-1', companyId, journalId: 'jn-posted',
48 + accountId: 'chart-1', debit: 100, credit: 0
49 + });
50 + const original = await StorageService.get('journal_lines', 'jn-line-1');
51 + await assert.rejects(restoreCompanyBackup({
52 + journal_lines: [{ ...original, debit: 999 }]
53 + }, opts), /بازنویسی سند مالی قطعی/);
54 + assert.deepEqual(await StorageService.get('journal_lines', 'jn-line-1'), original);
55 + assert.equal((await StorageService.getAll('audit_events')).length, 0);
56 +});
57 +
58 +test('A04: finalized movements, settlements and treasury transactions cannot be modified via backup', async () => {
59 + await reset();
60 + const scenarios = [
61 + ['stock_movements', { id: 'move-1', companyId, type: 'out', qty: 1 }, { qty: 100 }],
62 + ['settlements', { id: 'settled-1', companyId, invoiceId: 'sale', amount: 5 }, { amount: 999 }],
63 + ['treasury', { id: 'treasury-1', companyId, recordType: 'transaction',
64 + type: 'receipt', amount: 5 }, { amount: 999 }],
65 + ['cheques', { id: 'cheque-1', companyId, financePosted: true,
66 + amount: 5, status: 'pending' }, { status: 'cleared' }]
67 + ];
68 + for (const [store, original, changes] of scenarios) {
69 + await StorageService.put(store, { ...original });
70 + const persisted = await StorageService.get(store, original.id);
71 + await assert.rejects(restoreCompanyBackup({
72 + [store]: [{ ...persisted, ...changes }]
73 + }, opts), /بازنویسی سند مالی قطعی/);
74 + assert.deepEqual(await StorageService.get(store, original.id), persisted);
75 + }
76 + assert.equal((await StorageService.getAll('audit_events')).length, 0);
77 +});
78 +
79 +test('A04: editable draft journals remain replaceable for authorized recovery', async () => {
80 + await reset();
81 + await StorageService.put('journal_entries', {
82 + id: 'draft-1', companyId, status: 'draft',
83 + periodId: 'period-1', description: 'Old draft'
84 + });
85 + // Journal validation requires a complete chart if a journal is included in
86 + // the incoming backup, so we test the permitted edit boundary via a master.
87 + const existing = { id: 'person-1', companyId, name: 'Old' };
88 + await StorageService.put('contacts', { ...existing });
89 + const result = await restoreCompanyBackup({
90 + contacts: [{ ...(await StorageService.get('contacts', existing.id)), name: 'New' }]
91 + }, opts);
92 + assert.equal(result.updated, 1);
93 + assert.equal((await StorageService.get('contacts', existing.id)).name, 'New');
94 +});