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

test(A05): enforce balanced posted journal and closed stocktake before period close

davoodmehraban89 committed Oct 14, 2026 at 23:44 UTC a9c7e4d7fcca09f387e8b9ce21dc373b16d6a568
1 file changed +63
tests/a05-fiscal-close-gates.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 +import { AccountingService } from '../src/core/AccountingService.js';
6 +import { AuditService } from '../src/core/AuditService.js';
7 +import { CompanyService } from '../src/core/CompanyService.js';
8 +import { Auth } from '../src/core/Auth.js';
9 +
10 +async function setup() {
11 + await StorageService.clearAll();
12 + Auth.currentUser = { id: 'manager-a05', role: 'admin', username: 'manager' };
13 + await StorageService.put('companies', { id: 'a05', name: 'Test A05' });
14 + CompanyService.activeCompanyId = 'a05';
15 + const period = await AccountingService.createPeriod({
16 + name: '1405', startDate: '1405/01/01', endDate: '1405/12/29'
17 + });
18 + await AccountingService.seedDefaultChart();
19 + const accounts = await AccountingService.listAccounts();
20 + return { period, debit: accounts.find(x => x.code === '102'),
21 + credit: accounts.find(x => x.code === '401') };
22 +}
23 +
24 +test('A05: a historically unbalanced posted journal must block fiscal close', async () => {
25 + const { period, debit, credit } = await setup();
26 + await StorageService.put('journal_entries', {
27 + id: 'corrupt-posted', companyId: 'a05', periodId: period.id,
28 + date: '1405/08/20', status: 'posted'
29 + });
30 + await StorageService.put('journal_lines', {
31 + id: 'line-1', companyId: 'a05', journalId: 'corrupt-posted',
32 + accountId: debit.id, debit: 100, credit: 0
33 + });
34 + await StorageService.put('journal_lines', {
35 + id: 'line-2', companyId: 'a05', journalId: 'corrupt-posted',
36 + accountId: credit.id, debit: 0, credit: 90
37 + });
38 + await assert.rejects(AccountingService.closePeriod(period.id), /تراز|سطرها/);
39 + assert.equal((await StorageService.get('fiscal_periods', period.id)).closed, false);
40 + assert.ok(!(await AuditService.list()).some(e => e.eventType === 'period_closed'));
41 +
42 + await StorageService.put('journal_lines', {
43 + id: 'line-2', companyId: 'a05', journalId: 'corrupt-posted',
44 + accountId: credit.id, debit: 0, credit: 100
45 + });
46 + await AccountingService.closePeriod(period.id);
47 + assert.equal((await StorageService.get('fiscal_periods', period.id)).closed, true);
48 + assert.equal((await AuditService.list()).filter(e => e.eventType === 'period_closed').length, 1);
49 +});
50 +
51 +test('A05: an open stocktake blocks period close without changing the period', async () => {
52 + const { period } = await setup();
53 + await StorageService.put('stocktakes', {
54 + id: 'stocktake-draft', companyId: 'a05', warehouseId: 'wh',
55 + status: 'draft'
56 + });
57 + await assert.rejects(AccountingService.closePeriod(period.id), /انبارگردانی باز/);
58 + assert.equal((await StorageService.get('fiscal_periods', period.id)).closed, false);
59 + assert.ok(!(await AuditService.list()).some(e => e.eventType === 'period_closed'));
60 + await StorageService.delete('stocktakes', 'stocktake-draft');
61 + await AccountingService.closePeriod(period.id);
62 + await assert.rejects(AccountingService.closePeriod(period.id), /بسته/);
63 +});