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

test(A03): enforce immutable audit/correction history and atomic rollback on full restore

davoodmehraban89 committed Oct 15, 2026 at 21:05 UTC dea29471a2adc33134641e13f594f47b6f65ce6e
1 file changed +63
tests/a03-append-only-full-restore.test.mjs new
+63
@@ -0,0 +1,63 @@
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 +test('A03: full restore refuses to mutate prior audit events and rolls back earlier writes', async () => {
7 + await StorageService.clearAll();
8 + const audit = {
9 + id: 'audit-history-1', companyId: 'c1',
10 + commandKey: 'c1:historic', eventType: 'journal_posted',
11 + actorId: 'manager', sourceId: 'journal-1',
12 + result: 'committed', safeSummary: { lineCount: 2 }
13 + };
14 + await StorageService.put('audit_events', { ...audit });
15 + const before = await StorageService.get('audit_events', audit.id);
16 + await assert.rejects(StorageService.importAll({
17 + app: 'Factor-easyPro', version: 7,
18 + data: {
19 + products: [{ id: 'new-product', companyId: 'c1', name: 'must roll back' }],
20 + audit_events: [{ ...before, eventType: 'period_closed' }]
21 + }
22 + }), /بازنویسی سابقه/);
23 + assert.equal(await StorageService.get('products', 'new-product'), undefined);
24 + assert.deepEqual(await StorageService.get('audit_events', audit.id), before);
25 +
26 + // Identical replay with shuffled object keys is safe and idempotent.
27 + const replay = { companyId: 'c1', id: before.id, result: before.result,
28 + safeSummary: { lineCount: 2 }, commandKey: before.commandKey,
29 + eventType: before.eventType, actorId: before.actorId, sourceId: before.sourceId,
30 + createdAt: before.createdAt, updatedAt: before.updatedAt };
31 + await StorageService.importAll({
32 + app: 'Factor-easyPro', version: 7, data: { audit_events: [replay] }
33 + });
34 + assert.equal((await StorageService.getAll('audit_events')).length, 1);
35 +});
36 +
37 +test('A03: full restore preserves document corrections and rejects ID overwrite', async () => {
38 + await StorageService.clearAll();
39 + const correction = {
40 + id: 'correction-1', companyId: 'c1',
41 + idempotencyKey: 'return:invoice-1', sourceId: 'invoice-1',
42 + correctionType: 'return', resultId: 'return-1'
43 + };
44 + await StorageService.put('document_corrections', { ...correction });
45 + const before = await StorageService.get('document_corrections', correction.id);
46 + await assert.rejects(StorageService.importAll({
47 + app: 'Factor-easyPro', version: 7,
48 + data: { document_corrections: [{ ...before, resultId: 'forged' }] }
49 + }), /بازنویسی سابقه/);
50 + assert.deepEqual(await StorageService.get('document_corrections', correction.id), before);
51 +});
52 +
53 +test('A03: legitimate novel audit records may be restored without destroying existing history', async () => {
54 + await StorageService.clearAll();
55 + const newer = { id: 'new-audit', companyId: 'c1', commandKey: 'c1:novel',
56 + eventType: 'journal_posted', actorId: 'manager', sourceId: 'j1' };
57 + await StorageService.importAll({
58 + app: 'Factor-easyPro', version: 7,
59 + data: { audit_events: [newer] }
60 + });
61 + assert.equal((await StorageService.getAll('audit_events')).length, 1);
62 + assert.equal((await StorageService.get('audit_events', newer.id)).sourceId, 'j1');
63 +});