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

test(A01): validate real company-scoped preview with IDB read-only isolation

davoodmehraban89 committed Oct 14, 2026 at 22:51 UTC 3398a39980b70a27a3fa690356a57da450b6e916
1 file changed +70
tests/return-settlement-service.test.mjs new
+70
@@ -0,0 +1,70 @@
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 { CompanyService } from '../src/core/CompanyService.js';
6 +import { Auth } from '../src/core/Auth.js';
7 +import { RetailPostingService } from '../src/core/RetailPostingService.js';
8 +
9 +async function reset() {
10 + await StorageService.clearAll();
11 + Auth.currentUser = { id: 'cashier', role: 'user', username: 'cashier', companyIds: ['c1'] };
12 + await StorageService.put('companies', { id: 'c1', name: 'One' });
13 + await StorageService.put('companies', { id: 'c2', name: 'Two' });
14 + CompanyService.activeCompanyId = 'c1';
15 +}
16 +
17 +test('A01: read-only return preview uses actual original invoice, prior returns and payments', async () => {
18 + await reset();
19 + await StorageService.put('invoices', {
20 + id: 'sale-1', companyId: 'c1', kind: 'sale', financePosted: true,
21 + grandTotal: 1_000_000, paidAmount: 400_000
22 + });
23 + await StorageService.put('invoices', {
24 + id: 'return-1', companyId: 'c1', kind: 'sale_return', financePosted: true,
25 + originalInvoiceId: 'sale-1', grandTotal: 200_000
26 + });
27 + const before = await StorageService.getAll('invoices');
28 + const p = await RetailPostingService.previewSaleReturn('sale-1', 600_000);
29 + assert.equal(p.receivableBefore, 400_000);
30 + assert.equal(p.allocatedToReceivable, 400_000);
31 + assert.equal(p.newCustomerCredit, 200_000);
32 + assert.equal(p.receivableAfter, 0);
33 + assert.equal(p.autoRefundAllowed, false);
34 + assert.deepEqual(await StorageService.getAll('invoices'), before);
35 + assert.equal((await StorageService.getAll('treasury')).length, 0);
36 + assert.equal((await StorageService.getAll('journal_entries')).length, 0);
37 +});
38 +
39 +test('A01: read-only preview rejects cross-company origin and nonfinal invoices', async () => {
40 + await reset();
41 + await StorageService.put('invoices', {
42 + id: 'foreign', companyId: 'c2', kind: 'sale',
43 + financePosted: true, grandTotal: 100, paidAmount: 0
44 + });
45 + await assert.rejects(RetailPostingService.previewSaleReturn('foreign', 50), /شرکت فعال/);
46 + await StorageService.put('invoices', {
47 + id: 'draft', companyId: 'c1', kind: 'sale',
48 + isPreInvoice: true, grandTotal: 100, paidAmount: 0
49 + });
50 + await assert.rejects(RetailPostingService.previewSaleReturn('draft', 50), /فروش قطعی/);
51 + await StorageService.put('invoices', {
52 + id: 'purchased', companyId: 'c1', kind: 'purchase',
53 + financePosted: true, grandTotal: 100, paidAmount: 0
54 + });
55 + await assert.rejects(RetailPostingService.previewSaleReturn('purchased', 50), /فروش قطعی/);
56 +});
57 +
58 +test('A01: read-only preview rejects return totals above source', async () => {
59 + await reset();
60 + await StorageService.put('invoices', {
61 + id: 'sale', companyId: 'c1', kind: 'sale',
62 + financePosted: true, grandTotal: 500, paidAmount: 100
63 + });
64 + await StorageService.put('invoices', {
65 + id: 'prior-return', companyId: 'c1', kind: 'sale_return',
66 + financePosted: true, originalInvoiceId: 'sale', grandTotal: 400
67 + });
68 + await assert.rejects(RetailPostingService.previewSaleReturn('sale', 101), /بیشتر/);
69 + assert.equal((await StorageService.getAll('invoices')).length, 2);
70 +});