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