سامانه صدور فاکتور و حسابداری
test(stage3): assert legacy posted metadata gaps fail closed without mutation or identifiers
davoodmehraban89 committed
Oct 23, 2026 at 11:11 UTC
83bfd02ff889fb839d4241864215ec5b8fc49633
1 file changed
+61
scripts/stage3-legacy-source-audit.test.mjs
new
+61
@@ -0,0 +1,61 @@
1
+#!/usr/bin/env node
2
+import assert from 'node:assert/strict';
3
+import {auditSourceRecords} from './stage3-legacy-source-audit.mjs';
4
+const clone=x=>JSON.parse(JSON.stringify(x));
5
+const sale={kind:'formal',companyId:'SYNTHETIC_ONLY',accountingVersion:1,
6
+ currencyCode:'IRR',paymentMethod:'cash',
7
+ items:[{qty:2,price:100,lineTotal:200}],
8
+ subtotal:200,discountType:'fixed',discountInput:0,discount:0,
9
+ vatMode:'with_vat',vatType:'percent',vatRate:10,vat:20,grandTotal:220,
10
+ accountingStatus:'draft',name:'Secret-Client-Name'};
11
+const purchase={...clone(sale),kind:undefined,paymentMethod:'credit',
12
+ subtotal:200,discountType:'fixed',discountInput:10,discount:10,
13
+ vat:19,grandTotal:209};
14
+const wrap=(collection,data,id)=>({collection,id,data});
15
+const first=clone(sale);first.accountingStatus='posted';
16
+delete first.vatMode;delete first.currencyCode;delete first.accountingVersion;
17
+const zeroVat=clone(sale);zeroVat.kind='non_formal';
18
+zeroVat.vat=0;zeroVat.grandTotal=200;zeroVat.vatMode='none';
19
+delete zeroVat.vatMode;
20
+const foreign=clone(sale);foreign.currencyCode='UNSUPPORTED_NON_RIAL';
21
+const fractional=clone(sale);fractional.items[0].qty=1.5;
22
+const tampered=clone(sale);tampered.grandTotal=221;
23
+const proforma=clone(sale);proforma.kind='pre_invoice';
24
+const rows=[
25
+ wrap('invoices',clone(sale),'SAFE_SALE'),
26
+ wrap('purchases',clone(purchase),'SAFE_PURCHASE'),
27
+ wrap('invoices',first,'LEGACY_POSTED'),
28
+ wrap('invoices',zeroVat,'LEGACY_ZERO_VAT'),
29
+ wrap('invoices',foreign,'NON_RIAL'),
30
+ wrap('invoices',fractional,'FRACTIONAL'),
31
+ wrap('invoices',tampered,'TAMPERED'),
32
+ wrap('invoices',proforma,'OPERATIONAL')
33
+];
34
+const before=JSON.stringify(rows);
35
+const result=auditSourceRecords(rows);
36
+assert.deepEqual(
37
+ {total:result.total,eligible:result.eligible,
38
+ reconciliationRequired:result.reconciliationRequired,
39
+ unsupported:result.unsupported,contractMismatch:result.contractMismatch,
40
+ outOfScope:result.outOfScope,postedWithGaps:result.postedWithGaps},
41
+ {total:8,eligible:2,reconciliationRequired:2,unsupported:2,
42
+ contractMismatch:1,outOfScope:1,postedWithGaps:1}
43
+);
44
+assert.equal(result.reasonCounts.CURRENCY_NOT_APPROVED,1);
45
+assert.equal(result.reasonCounts.ITEM_QUANTITY_OR_AMOUNT_UNSUPPORTED,1);
46
+assert.equal(result.reasonCounts.SERVER_V1_AMOUNT_MISMATCH,1);
47
+assert.equal(result.reasonCounts.NON_POSTING_INVOICE_KIND,1);
48
+assert.equal(Object.values(result.reasonCounts).reduce((a,b)=>a+b,0),6);
49
+assert.equal(JSON.stringify(rows),before,'Classifier must not change historical sources');
50
+assert.equal(JSON.stringify(auditSourceRecords(rows)),JSON.stringify(result),'Deterministic report required');
51
+assert.ok(!JSON.stringify(result).includes('Secret-Client-Name'),'Never leak names');
52
+assert.ok(!JSON.stringify(result).includes('LEGACY_POSTED'),'Never leak IDs');
53
+assert.throws(()=>auditSourceRecords({}),/FINORA_AUDIT_SOURCE_ARRAY_REQUIRED/);
54
+assert.throws(()=>auditSourceRecords([{collection:'unknown',data:{}}]),/FINORA_AUDIT_INVALID_SOURCE_SHAPE/);
55
+// A zero-VAT historic document with no vatMode is NOT automatically fixed.
56
+assert.equal(auditSourceRecords([wrap('invoices',zeroVat,'ZERO')]).reconciliationRequired,1);
57
+console.log('FINORA_STAGE3_LEGACY_CLASSIFIER_PASS '+JSON.stringify({
58
+ synthetic:8,eligible:2,reconciliation:2,unsupported:2,
59
+ mismatch:1,outOfScope:1,postedWithGaps:1,
60
+ originalRowsMutated:false,financialDataOutput:false,
61
+ productionConnection:false}));