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

Test reconciliation calculations, company isolation and non-mutating service reporting

davoodmehraban89 committed Oct 13, 2026 at 13:22 UTC 53156c7affe7d65fb3339d20289bb733054aef8a
1 file changed +103
tests/reconciliation.test.mjs new
+103
@@ -0,0 +1,103 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import 'fake-indexeddb/auto';
4 +import { reconcileFinancialData } from '../src/core/ReconciliationCore.mjs';
5 +import { StorageService as db } from '../src/core/StorageService.js';
6 +import { AccountingService as accounts } from '../src/core/AccountingService.js';
7 +import { RetailPostingService as retail } from '../src/core/RetailPostingService.js';
8 +import { Auth } from '../src/core/Auth.js';
9 +import { CompanyService } from '../src/core/CompanyService.js';
10 +
11 +const date='1405/07/20', company='reconciliation-c1';
12 +const fixture=()=>({
13 + integrated:true,
14 + accounts:[{id:'a103',code:'103'}, {id:'a201',code:'201'}],
15 + journals:[{id:'j1',status:'posted',sourceType:'invoice',sourceId:'i1',debit:100,credit:100}],
16 + journalLines:[{id:'l1',journalId:'j1',accountId:'a103',debit:100,credit:0},
17 + {id:'l2',journalId:'j1',accountId:'a201',debit:0,credit:100}],
18 + invoices:[{id:'i1',number:'120',journalId:'j1',financePosted:true,grandTotal:100,
19 + paidAmount:0,remaining:100,isPreInvoice:false}],
20 + settlements:[],transactions:[],
21 + inventoryBalances:[{id:'b1',warehouseId:'w1',productId:'p1',quantityUnits:1000,value:100}],
22 + stockMovements:[{id:'m1',warehouseId:'w1',productId:'p1',type:'in',qty:1,cost:100}]
23 +});
24 +
25 +test('balanced purchase, inventory, invoice and journal produce zero reconciliation issues',()=>{
26 + const report=reconcileFinancialData(fixture());
27 + assert.equal(report.counts.issues,0);
28 + assert.equal(report.totals.inventoryLedger,100);
29 + assert.equal(report.totals.inventoryBook,100);
30 + assert.equal(report.totals.stockUnits,1000);
31 + assert.equal(report.counts.invoices,1);
32 +});
33 +
34 +test('mismatched settlement and missing transaction are detected without writes',()=>{
35 + const f=fixture();
36 + f.invoices[0].paidAmount=50;f.invoices[0].remaining=50;
37 + f.settlements.push({id:'s1',invoiceId:'i1',transactionId:'missing',amount:40});
38 + const before=JSON.stringify(f);
39 + const report=reconcileFinancialData(f);
40 + assert.deepEqual(report.issues.map(x=>x.code).sort(),
41 + ['INVOICE_SETTLEMENT_MISMATCH','MISSING_SETTLEMENT_TRANSACTION'].sort());
42 + assert.equal(JSON.stringify(f),before,'reconciliation is strictly read-only');
43 +});
44 +
45 +test('orphan postings, unbalanced journals and inventory discrepancies are reported',()=>{
46 + const f=fixture();
47 + f.journalLines[1].credit=90;
48 + f.stockMovements[0].qty=2;
49 + f.inventoryBalances[0].value=120;
50 + f.journalLines.push({id:'orphan',journalId:'missing',accountId:'a103',debit:1,credit:0});
51 + const report=reconcileFinancialData(f);
52 + for(const code of ['ORPHAN_JOURNAL_LINE','JOURNAL_UNBALANCED','JOURNAL_TOTAL_MISMATCH',
53 + 'STOCK_QUANTITY_MISMATCH','INVENTORY_LEDGER_MISMATCH']) {
54 + assert.ok(report.issues.some(x=>x.code===code),code);
55 + }
56 +});
57 +
58 +test('legacy company report never compares stock to a non-integrated ledger',()=>{
59 + const report=reconcileFinancialData({...fixture(),integrated:false});
60 + assert.equal(report.integrated,false);
61 + assert.equal(report.totals.inventoryBook,0);
62 + assert.ok(!report.issues.some(x=>x.code==='INVENTORY_LEDGER_MISMATCH'));
63 +});
64 +
65 +async function setup(){
66 + await db.clearAll();
67 + Auth.currentUser={id:'recon-admin',role:'admin',username:'admin'};
68 + CompanyService.activeCompanyId=company;
69 + await db.put('companies',{id:company,name:'Company A'});
70 + await db.put('companies',{id:'foreign-c',name:'Company B'});
71 + await accounts.createPeriod({name:'۱۴۰۵',startDate:'1405/01/01',endDate:'1405/12/29'});
72 + await accounts.seedDefaultChart();
73 + await db.put('products',{id:'product-a',companyId:company,name:'Product',trackInventory:true});
74 + await db.put('contacts',{id:'contact-a',companyId:company,name:'Customer'});
75 + await retail.activate();
76 +}
77 +
78 +test('real integrated purchase-sale-settlement reconciles without errors and rejects non-admin',async()=>{
79 + await setup();
80 + const inv=(id,kind,qty,price,paidAmount=0)=>({
81 + id,number:id,kind,companyId:company,date,contactId:'contact-a',
82 + items:[{productId:'product-a',productName:'Product',qty,price,lineTotal:qty*price,
83 + discount:0}],subtotal:qty*price,totalDiscount:0,vatAmount:0,grandTotal:qty*price,
84 + paidAmount,remaining:qty*price-paidAmount,paymentMethod:'cash',isPreInvoice:false
85 + });
86 + await retail.issueInvoice(inv('buy','purchase',5,100));
87 + const sold=await retail.issueInvoice(inv('sale','sale',2,200,100));
88 + assert.equal(sold.remaining,300);
89 + const before=await db.getAll('journal_entries');
90 + const result=await accounts.reconciliationReport();
91 + assert.equal(result.integrated,true);
92 + assert.equal(result.counts.invoices,2);
93 + assert.equal(result.counts.issues,0,JSON.stringify(result.issues));
94 + assert.equal(result.totals.inventoryLedger,300);
95 + assert.equal(result.totals.inventoryBook,300);
96 + assert.equal((await db.getAll('journal_entries')).length,before.length);
97 + await db.put('journal_entries',{id:'foreign-broken',companyId:'foreign-c',status:'posted',
98 + debit:100,credit:0,date});
99 + assert.equal((await accounts.reconciliationReport()).counts.issues,0,
100 + 'foreign company errors must not leak into the active company');
101 + Auth.currentUser={id:'guest',role:'user'};
102 + await assert.rejects(accounts.reconciliationReport(),/مدیر/);
103 +});