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

test(A04): enforce legitimate finance-posted invoice, cheque and treasury journal references

davoodmehraban89 committed Oct 16, 2026 at 17:31 UTC d9804e9b5a491d0c1f8a2120e57cf1dc57a14bb2
1 file changed +108
tests/a04-company-restore-journal-references.test.mjs new
+108
@@ -0,0 +1,108 @@
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 C = 'references-c1';
9 +const opts = { companyId: C, userId: 'manager', mode: 'merge' };
10 +
11 +async function reset() {
12 + await StorageService.clearAll();
13 + Auth.currentUser = { id: 'manager', username: 'manager', role: 'admin' };
14 + await StorageService.put('companies', { id: C, name: 'Example company' });
15 + await StorageService.put('companies', { id: 'references-c2', name: 'Foreign company' });
16 +}
17 +
18 +test('A04: a posted invoice without a journal is rejected before any import writes', async () => {
19 + await reset();
20 + await assert.rejects(restoreCompanyBackup({
21 + contacts: [{ id: 'should-roll-back', companyId: C, name: 'Must not persist' }],
22 + invoices: [{ id: 'fake-posted', companyId: C, financePosted: true,
23 + kind: 'sale', journalId: 'no-journal', grandTotal: 1000 }]
24 + }, opts), /ارجاع دفتر کل/);
25 + assert.equal(await StorageService.get('contacts', 'should-roll-back'), undefined);
26 + assert.equal(await StorageService.get('invoices', 'fake-posted'), undefined);
27 + assert.equal((await StorageService.getAll('audit_events')).length, 0);
28 +});
29 +
30 +test('A04: a posted invoice cannot cite another company or another source journal', async () => {
31 + await reset();
32 + await StorageService.put('journal_entries', {
33 + id: 'foreign-journal', companyId: 'references-c2',
34 + status: 'posted', sourceType: 'invoice', sourceId: 'fake-posted'
35 + });
36 + await StorageService.put('journal_entries', {
37 + id: 'wrong-source', companyId: C,
38 + status: 'posted', sourceType: 'invoice', sourceId: 'unrelated-invoice'
39 + });
40 + const row = { id: 'fake-posted', companyId: C, financePosted: true,
41 + kind: 'sale', journalId: 'foreign-journal', grandTotal: 100 };
42 + await assert.rejects(restoreCompanyBackup({ invoices: [row] }, opts),
43 + /ارجاع دفتر کل/);
44 + await assert.rejects(restoreCompanyBackup({
45 + invoices: [{ ...row, journalId: 'wrong-source' }]
46 + }, opts), /ارجاع دفتر کل/);
47 + assert.equal(await StorageService.get('invoices', 'fake-posted'), undefined);
48 +});
49 +
50 +test('A04: a complete posted invoice referencing the correct journal may be recovered', async () => {
51 + await reset();
52 + await StorageService.put('journal_entries', {
53 + id: 'verified-source', companyId: C, status: 'posted',
54 + sourceType: 'invoice', sourceId: 'valid-invoice'
55 + });
56 + const restored = await restoreCompanyBackup({
57 + invoices: [{ id: 'valid-invoice', companyId: C,
58 + financePosted: true, journalId: 'verified-source',
59 + kind: 'sale', grandTotal: 1000 }]
60 + }, opts);
61 + assert.equal(restored.added, 1);
62 + assert.equal((await StorageService.get('invoices', 'valid-invoice')).journalId,
63 + 'verified-source');
64 + assert.equal((await StorageService.getAll('audit_events')).length, 1);
65 +});
66 +
67 +test('A04: posted treasury transactions cannot cite a ledger event from another command', async () => {
68 + await reset();
69 + await StorageService.put('journal_entries', {
70 + id: 'posted-treasury', companyId: C, status: 'posted',
71 + sourceType: 'treasury', sourceId: 'other-payment'
72 + });
73 + const item = { id: 'payment-1', companyId: C, recordType: 'transaction',
74 + financePosted: true, type: 'receipt', journalId: 'posted-treasury', amount: 100 };
75 + await assert.rejects(restoreCompanyBackup({ treasury: [item] }, opts),
76 + /به عملیات دیگری/);
77 + assert.equal(await StorageService.get('treasury', 'payment-1'), undefined);
78 +
79 + await StorageService.put('journal_entries', {
80 + id: 'right-treasury', companyId: C, status: 'posted',
81 + sourceType: 'treasury', sourceId: 'payment-1'
82 + });
83 + const result = await restoreCompanyBackup({
84 + treasury: [{ ...item, journalId: 'right-treasury' }]
85 + }, opts);
86 + assert.equal(result.added, 1);
87 + assert.equal((await StorageService.get('treasury', 'payment-1')).journalId, 'right-treasury');
88 +});
89 +
90 +test('A04: cheque status restore cannot reference a foreign or missing status journal', async () => {
91 + await reset();
92 + await StorageService.put('journal_entries', {
93 + id: 'cheque-created', companyId: C, status: 'posted',
94 + sourceType: 'cheque', sourceId: 'ch-1'
95 + });
96 + const row = { id: 'ch-1', companyId: C, financePosted: true,
97 + journalId: 'cheque-created', status: 'cleared', statusVersion: 2 };
98 + await assert.rejects(restoreCompanyBackup({ cheques: [row] }, opts),
99 + /شناسه سند تغییر وضعیت/);
100 + await StorageService.put('journal_entries', {
101 + id: 'status-from-other', companyId: 'references-c2',
102 + status: 'posted', sourceType: 'cheque_status', sourceId: 'ch-1'
103 + });
104 + await assert.rejects(restoreCompanyBackup({
105 + cheques: [{ ...row, statusJournalId: 'status-from-other' }]
106 + }, opts), /ارجاع سند تغییر وضعیت/);
107 + assert.equal(await StorageService.get('cheques', 'ch-1'), undefined);
108 +});