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

test(A04): reject full-snapshot overwrites of posted invoices, journals, cash and stock

davoodmehraban89 committed Oct 16, 2026 at 17:28 UTC ab3da211eb297e01e33c829b0b6fddff86a2376e
1 file changed +84
tests/a04-full-restore-finalized-records.test.mjs new
+84
@@ -0,0 +1,84 @@
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 +
6 +const backup = data => ({ app: 'Factor-easyPro', version: 7, data });
7 +const C = 'historic-company';
8 +
9 +test('A04: full backup cannot mutate posted invoices and must roll back earlier master changes', async () => {
10 + await StorageService.clearAll();
11 + await StorageService.put('invoices', {
12 + id: 'posted-sale', companyId: C, financePosted: true,
13 + kind: 'sale', grandTotal: 1000, paidAmount: 200, remaining: 800
14 + });
15 + const original = await StorageService.get('invoices', 'posted-sale');
16 + await assert.rejects(StorageService.importAll(backup({
17 + contacts: [{ id: 'rollback-contact', companyId: C, name: 'Must not remain' }],
18 + invoices: [{ ...original, grandTotal: 100 }]
19 + })), /بازنویسی سند قطعی/);
20 + assert.equal(await StorageService.get('contacts', 'rollback-contact'), undefined);
21 + assert.deepEqual(await StorageService.get('invoices', 'posted-sale'), original);
22 +
23 + await StorageService.importAll(backup({ invoices: [{ ...original }] }));
24 + assert.deepEqual(await StorageService.get('invoices', 'posted-sale'), original);
25 +});
26 +
27 +test('A04: full restore rejects mutation of original posted ledger lines', async () => {
28 + await StorageService.clearAll();
29 + await StorageService.put('journal_entries', {
30 + id: 'historic-journal', companyId: C, status: 'posted',
31 + date: '1405/01/01', periodId: 'old-period'
32 + });
33 + await StorageService.put('journal_lines', {
34 + id: 'historic-line', companyId: C, journalId: 'historic-journal',
35 + accountId: 'account-id', debit: 400, credit: 0
36 + });
37 + const original = await StorageService.get('journal_lines', 'historic-line');
38 + await assert.rejects(StorageService.importAll(backup({
39 + journal_lines: [{ ...original, debit: 99 }]
40 + })), /بازنویسی سند قطعی/);
41 + assert.deepEqual(await StorageService.get('journal_lines', 'historic-line'), original);
42 +});
43 +
44 +test('A04: full restore protects historical treasury, stock and settlement records', async () => {
45 + await StorageService.clearAll();
46 + const cases = [
47 + ['stock_movements', { id: 'stock-1', companyId: C, type: 'out', cost: 100 }, { cost: 999 }],
48 + ['settlements', { id: 'settlement-1', companyId: C, amount: 100 }, { amount: 5 }],
49 + ['treasury', { id: 'cash-1', companyId: C, recordType: 'transaction', amount: 100 }, { amount: 5 }],
50 + ['cheques', { id: 'cheque-1', companyId: C, financePosted: true, status: 'pending' }, { status: 'cleared' }],
51 + ['expenses', { id: 'expense-1', companyId: C, financePosted: true, amount: 100 }, { amount: 1 }]
52 + ];
53 + for (const [store, initial, changed] of cases) {
54 + await StorageService.put(store, { ...initial });
55 + const old = await StorageService.get(store, initial.id);
56 + await assert.rejects(StorageService.importAll(backup({
57 + [store]: [{ ...old, ...changed }]
58 + })), /بازنویسی سند قطعی/);
59 + assert.deepEqual(await StorageService.get(store, initial.id), old);
60 + }
61 +});
62 +
63 +test('A04: original finalized actor cannot be switched to another company by global restore', async () => {
64 + await StorageService.clearAll();
65 + await StorageService.put('invoices', {
66 + id: 'company-owned-sale', companyId: 'original-c1',
67 + financePosted: true, grandTotal: 110
68 + });
69 + const old = await StorageService.get('invoices', 'company-owned-sale');
70 + await assert.rejects(StorageService.importAll(backup({
71 + invoices: [{ ...old, companyId: 'other-c2' }]
72 + })), /بازنویسی سند قطعی/);
73 + assert.deepEqual(await StorageService.get('invoices', old.id), old);
74 +});
75 +
76 +test('A04: legitimate master data changes remain permitted for explicit recovery', async () => {
77 + await StorageService.clearAll();
78 + await StorageService.put('contacts', { id: 'cust', companyId: C, name: 'Before' });
79 + const before = await StorageService.get('contacts', 'cust');
80 + await StorageService.importAll(backup({
81 + contacts: [{ ...before, name: 'After' }]
82 + }));
83 + assert.equal((await StorageService.get('contacts', 'cust')).name, 'After');
84 +});