فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
test(A03): verify atomic auditing, admin scope, rollback and financial event linkage
davoodmehraban89 committed
Oct 14, 2026 at 23:25 UTC
6bdabb5018e41479fdecb375a540eaa0a84e27ed
1 file changed
+101
tests/a03-audit-integration.test.mjs
new
+101
@@ -0,0 +1,101 @@
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 { AuditService } from '../src/core/AuditService.js';
6
+import { AccountingService } from '../src/core/AccountingService.js';
7
+import { Auth } from '../src/core/Auth.js';
8
+import { CompanyService } from '../src/core/CompanyService.js';
9
+
10
+const company = 'audit-c1';
11
+const businessDate = '1405/07/18';
12
+async function setup() {
13
+ await StorageService.clearAll();
14
+ Auth.currentUser = { id: 'audit-admin', role: 'admin', username: 'manager' };
15
+ await StorageService.put('companies', { id: company, name: 'Synthetic C1' });
16
+ await StorageService.put('companies', { id: 'audit-c2', name: 'Synthetic C2' });
17
+ CompanyService.activeCompanyId = company;
18
+ const period = await AccountingService.createPeriod({
19
+ name: '1405', startDate: '1405/01/01', endDate: '1405/12/29'
20
+ });
21
+ await AccountingService.seedDefaultChart();
22
+ const accounts = await AccountingService.listAccounts();
23
+ const cash = accounts.find(a => a.code === '101');
24
+ const sales = accounts.find(a => a.code === '401');
25
+ assert.ok(cash && sales);
26
+ return { period, cash, sales };
27
+}
28
+const journal = ({ period, cash, sales }) => ({
29
+ periodId: period.id, date: businessDate,
30
+ description: 'Sensitive customer name that must not enter audit',
31
+ lines: [
32
+ { accountId: cash.id, debit: 1000, credit: 0 },
33
+ { accountId: sales.id, debit: 0, credit: 1000 }
34
+ ]
35
+});
36
+
37
+test('A03: posting and reversal produce immutable-in-service, scoped, minimal audit records', async () => {
38
+ const state = await setup();
39
+ const posted = await AccountingService.postJournal(journal(state));
40
+ let events = await AuditService.list();
41
+ assert.equal(events.length, 1);
42
+ assert.equal(events[0].eventType, 'journal_posted');
43
+ assert.equal(events[0].actorId, 'audit-admin');
44
+ assert.equal(events[0].companyId, company);
45
+ assert.equal(events[0].sourceId, posted.id);
46
+ assert.equal(events[0].safeSummary.lineCount, 2);
47
+ assert.equal(events[0].result, 'committed');
48
+ assert.ok(!JSON.stringify(events).includes('Sensitive customer name'));
49
+
50
+ const reversed = await AccountingService.reverseJournal(posted.id, {
51
+ periodId: state.period.id, date: '1405/07/19'
52
+ });
53
+ events = await AuditService.list();
54
+ assert.equal(events.length, 2);
55
+ assert.ok(events.some(e => e.eventType === 'journal_reversed' && e.sourceId === reversed.id));
56
+ await assert.rejects(AccountingService.reverseJournal(posted.id, {
57
+ periodId: state.period.id, date: '1405/07/20'
58
+ }), /قابل برگشت/);
59
+ assert.equal((await AuditService.list()).length, 2);
60
+
61
+ CompanyService.activeCompanyId = 'audit-c2';
62
+ assert.deepEqual(await AuditService.list(), []);
63
+ Auth.currentUser = { id: 'staff', role: 'user', companyIds: ['audit-c2'] };
64
+ await assert.rejects(AuditService.list(), /مدیر/);
65
+});
66
+
67
+test('A03: duplicate audit command aborts unrelated financial writes atomically', async () => {
68
+ await setup();
69
+ const event = {
70
+ companyId: company, eventType: 'journal_posted',
71
+ sourceType: 'journal', sourceId: 'source-1',
72
+ commandKey: 'request-1', businessDate
73
+ };
74
+ await StorageService.transaction(['audit_events'], tx => AuditService.append(tx, event));
75
+ await assert.rejects(StorageService.transaction(['audit_events', 'products'], async tx => {
76
+ await tx.put('products', { id: 'never-written', companyId: company, name: 'rollback' });
77
+ await AuditService.append(tx, event);
78
+ }));
79
+ assert.equal(await StorageService.get('products', 'never-written'), undefined);
80
+ assert.equal((await AuditService.list()).length, 1);
81
+});
82
+
83
+test('A03: fiscal closing appends an event and does not permit double close', async () => {
84
+ const { period } = await setup();
85
+ await AccountingService.closePeriod(period.id);
86
+ const events = await AuditService.list();
87
+ assert.equal(events.length, 1);
88
+ assert.equal(events[0].eventType, 'period_closed');
89
+ await assert.rejects(AccountingService.closePeriod(period.id), /بسته/);
90
+ assert.equal((await AuditService.list()).length, 1);
91
+});
92
+
93
+test('A03: company backup includes audit store, without ever exporting user credentials', async () => {
94
+ const state = await setup();
95
+ await AccountingService.postJournal(journal(state));
96
+ const backup = await StorageService.exportAll();
97
+ assert.equal(backup.version, 7);
98
+ assert.equal(backup.data.audit_events.length, 1);
99
+ assert.ok(Array.isArray(backup.data.document_corrections));
100
+ assert.equal(backup.data.audit_events[0].safeSummary.lineCount, 2);
101
+});