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

test(A03): prove aborted bulk clear rolls back staged contact

davoodmehraban89 committed Oct 16, 2026 at 23:32 UTC 40e5dde4ffa4173d2d3caea5590e73ea6d8564e2
1 file changed +32
tests/a03-direct-append-only-protection.test.mjs
+32
@@ -47,3 +47,35 @@ test('A03: document correction history refuses replay writes and deletion', asyn
47 /حذف مستقیم/);
48 assert.deepEqual(await StorageService.get('document_corrections', correction.id), saved);
49 });
50 +
51 +test('A03: transaction clear cannot erase either append-only history store or commit earlier writes', async () => {
52 + await StorageService.clearAll();
53 + for (const store of ['audit_events', 'document_corrections']) {
54 + const historical = {
55 + id: 'retained-' + store,
56 + companyId: 'c1',
57 + ...(store === 'audit_events'
58 + ? { commandKey: 'c1:retained-audit', eventType: 'journal_posted' }
59 + : { idempotencyKey: 'c1:retained-correction', sourceId: 'sale-1' })
60 + };
61 + await StorageService.put(store, historical);
62 + const original = structuredClone(await StorageService.get(store, historical.id));
63 + const sentinel = 'must-rollback-' + store;
64 + await assert.rejects(
65 + StorageService.transaction(['contacts', store], async tx => {
66 + await tx.putRaw('contacts', { id: sentinel, companyId: 'c1', name: 'Aborted write' });
67 + await tx.clear(store);
68 + }),
69 + /پاک‌سازی مستقیم/
70 + );
71 + assert.equal(await StorageService.get('contacts', sentinel), undefined,
72 + 'A rejected history clear must roll back the earlier contact write');
73 + assert.deepEqual(await StorageService.get(store, historical.id), original,
74 + 'Append-only history must survive the blocked bulk clear');
75 + assert.equal((await StorageService.getAll(store)).length, 1);
76 + }
77 + // Clearing a regular non-immutable table remains an authorized transaction.
78 + await StorageService.put('contacts', { id: 'temporary', companyId: 'c1', name: 'Disposable' });
79 + await StorageService.transaction('contacts', async tx => tx.clear('contacts'));
80 + assert.equal((await StorageService.getAll('contacts')).length, 0);
81 +});