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

test(A05): verify read-only reconciliation, privacy, company isolation and bounded output

davoodmehraban89 committed Oct 14, 2026 at 22:57 UTC 88788ef1168eb9019c06b26245336b72a26602bb
1 file changed +83
tests/financial-diagnostics.test.mjs new
+83
@@ -0,0 +1,83 @@
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 { Auth } from '../src/core/Auth.js';
6 +import { CompanyService } from '../src/core/CompanyService.js';
7 +import { inspectSettlementConsistency } from '../src/core/FinancialDiagnosticsService.js';
8 +
9 +async function setup() {
10 + await StorageService.clearAll();
11 + Auth.currentUser = { id: 'staff-c1', role: 'user', companyIds: ['c1'], username: 'staff' };
12 + await StorageService.put('companies', { id: 'c1', name: 'A' });
13 + await StorageService.put('companies', { id: 'c2', name: 'B' });
14 + CompanyService.activeCompanyId = 'c1';
15 +}
16 +
17 +test('A05 diagnostic: a consistent settlement is healthy without touching financial records', async () => {
18 + await setup();
19 + await StorageService.put('invoices', {
20 + id: 'i1', companyId: 'c1', financePosted: true,
21 + grandTotal: 100, paidAmount: 40, remaining: 60, status: 'partial'
22 + });
23 + await StorageService.put('treasury', {
24 + id: 't1', companyId: 'c1', recordType: 'transaction', amount: 40
25 + });
26 + await StorageService.put('settlements', {
27 + id: 's1', companyId: 'c1', invoiceId: 'i1', transactionId: 't1', amount: 40
28 + });
29 + await StorageService.put('invoices', {
30 + id: 'foreign', companyId: 'c2', financePosted: true,
31 + grandTotal: 1000, paidAmount: 999, remaining: 0, status: 'paid'
32 + });
33 + const before = await StorageService.get('invoices', 'i1');
34 + const diagnostics = await inspectSettlementConsistency();
35 + assert.deepEqual({
36 + companyId: diagnostics.companyId,
37 + checkedInvoices: diagnostics.checkedInvoices,
38 + checkedSettlements: diagnostics.checkedSettlements,
39 + healthy: diagnostics.healthy,
40 + issueCount: diagnostics.issueCount
41 + }, { companyId: 'c1', checkedInvoices: 1, checkedSettlements: 1, healthy: true, issueCount: 0 });
42 + assert.deepEqual(await StorageService.get('invoices', 'i1'), before);
43 +});
44 +
45 +test('A05 diagnostic: mismatched and orphan settlement records are reported but never fixed automatically', async () => {
46 + await setup();
47 + await StorageService.put('invoices', {
48 + id: 'i1', companyId: 'c1', financePosted: true,
49 + grandTotal: 100, paidAmount: 35, remaining: 25, status: 'paid'
50 + });
51 + await StorageService.put('settlements', {
52 + id: 's1', companyId: 'c1', invoiceId: 'i1', transactionId: 'missing', amount: 20
53 + });
54 + await StorageService.put('settlements', {
55 + id: 'orphan', companyId: 'c1', invoiceId: 'foreign', transactionId: 'missing', amount: 50
56 + });
57 + const result = await inspectSettlementConsistency();
58 + const codes = result.issues.map(x => x.code);
59 + assert.equal(result.healthy, false);
60 + for (const code of [
61 + 'ORPHAN_SETTLEMENT', 'MISSING_TREASURY_TRANSACTION',
62 + 'SETTLEMENT_TOTAL_MISMATCH', 'INVOICE_REMAINING_MISMATCH', 'INVOICE_STATUS_MISMATCH'
63 + ]) assert.ok(codes.includes(code), code);
64 + assert.equal((await StorageService.get('invoices', 'i1')).paidAmount, 35);
65 + assert.equal((await StorageService.getAll('settlements')).length, 2);
66 +});
67 +
68 +test('A05 diagnostic: only bounded issue identifiers and numeric facts are returned', async () => {
69 + await setup();
70 + await StorageService.put('invoices', {
71 + id: 'bad-i', companyId: 'c1', financePosted: true,
72 + grandTotal: 100, paidAmount: 10, remaining: 0, status: 'paid',
73 + customerPhone: 'PRIVATE_NUMBER_NOT_TO_LEAK'
74 + });
75 + const result = await inspectSettlementConsistency({ maxIssues: 1 });
76 + assert.equal(result.truncated, true);
77 + assert.equal(result.issues.length, 1);
78 + assert.ok(result.issueCount > 1);
79 + assert.ok(!JSON.stringify(result).includes('PRIVATE_NUMBER_NOT_TO_LEAK'));
80 + for (const maxIssues of [0, -1, 1001, 1.5]) {
81 + await assert.rejects(inspectSettlementConsistency({ maxIssues }), RangeError);
82 + }
83 +});