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

test(A03): prove audit and correction records reject normal mutation APIs

davoodmehraban89 committed Oct 16, 2026 at 22:16 UTC 58040733b457124a019eb2373f888f16c657b121
1 file changed +49
tests/a03-direct-append-only-protection.test.mjs new
+49
@@ -0,0 +1,49 @@
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: public and transaction writes cannot replace or delete persisted audit events', async () => {
7 + await StorageService.clearAll();
8 + const event = {
9 + id: 'history-native-1', companyId: 'c1', commandKey: 'c1:posted-1',
10 + eventType: 'journal_posted', actorId: 'manager', sourceId: 'j1'
11 + };
12 + await StorageService.put('audit_events', event);
13 + const original = structuredClone(await StorageService.get('audit_events', event.id));
14 + await assert.rejects(StorageService.put('audit_events',
15 + { ...original, eventType: 'period_closed' }));
16 + await assert.rejects(StorageService.delete('audit_events', event.id),
17 + /حذف مستقیم/);
18 + await assert.rejects(StorageService.transaction('audit_events', async tx => {
19 + await tx.put('audit_events', { ...original, eventType: 'period_closed' });
20 + }));
21 + await assert.rejects(StorageService.transaction('audit_events', async tx => {
22 + await tx.putRaw('audit_events', { ...original, sourceId: 'forged' });
23 + }));
24 + await assert.rejects(StorageService.transaction('audit_events', async tx => {
25 + await tx.delete('audit_events', event.id);
26 + }), /حذف مستقیم/);
27 + assert.deepEqual(await StorageService.get('audit_events', event.id), original);
28 + assert.equal((await StorageService.getAll('audit_events')).length, 1);
29 + await StorageService.transaction('audit_events', async tx => {
30 + await tx.putRaw('audit_events', {
31 + id: 'history-native-2', companyId: 'c1',
32 + commandKey: 'c1:posted-2', eventType: 'journal_posted'
33 + });
34 + });
35 + assert.equal((await StorageService.getAll('audit_events')).length, 2);
36 +});
37 +
38 +test('A03: document correction history refuses replay writes and deletion', async () => {
39 + await StorageService.clearAll();
40 + const correction = { id: 'fix-1', companyId: 'c1',
41 + idempotencyKey: 'c1:return-1', sourceId: 'invoice-1' };
42 + await StorageService.put('document_corrections', correction);
43 + const saved = structuredClone(await StorageService.get('document_corrections', correction.id));
44 + await assert.rejects(StorageService.put('document_corrections',
45 + { ...saved, sourceId: 'tampered-invoice' }));
46 + await assert.rejects(StorageService.delete('document_corrections', correction.id),
47 + /حذف مستقیم/);
48 + assert.deepEqual(await StorageService.get('document_corrections', correction.id), saved);
49 +});