فینورا پرو - سامانه جامع حسابداری و صدور فاکتور
test(edge): reject near-zero hidden precision and malformed money type coercions
davoodmehraban89 committed
Oct 31, 2026 at 20:25 UTC
179e6b9e76bd21c48e4ef3ffee934730e238a1c8
3 files changed
+17
-2
src/core/RetailFinanceCore.mjs
+3
-2
@@ -9,6 +9,7 @@ export const INVOICE_DIRECTIONS = Object.freeze({
9
sale:'out',non_formal:'out',purchase:'in',sale_return:'in',purchase_return:'out'
10
});
11
export function amount(value,label='مبلغ') {
12
+ if(typeof value!=='number' && typeof value!=='string') throw Error(label+' نامعتبر است');
13
if(typeof value==='string' && !/^\d+$/.test(value.trim())) throw Error(label+' باید عدد صحیح باشد');
14
const n=Number(value);
15
if(!Number.isSafeInteger(n)||n<0) throw Error(label+' نامعتبر است');
@@ -22,7 +23,7 @@ export function positiveAmount(v,label) {
23
export function qtyUnits(q) {
24
const n=Number(q), scaled=n*QTY_SCALE;
25
if(!Number.isFinite(n)||n<=0||!Number.isSafeInteger(Math.round(scaled))||
25
- Math.abs(scaled-Math.round(scaled))>1e-7) throw Error('مقدار کالا نامعتبر است');
26
+ Math.round(scaled)/QTY_SCALE!==n) throw Error('مقدار کالا نامعتبر است');
27
return Math.round(scaled);
28
}
29
// Round a quantity multiplied by a whole-rial unit price ONCE per line.
@@ -46,7 +47,7 @@ export function percentOfRials(rials, rate, label='درصد') {
47
throw Error(label+' باید حداکثر دو رقم اعشار داشته باشد');
48
const n=Number(rate), scaled=n*100;
49
if(!Number.isFinite(n)||n<0||n>100||
49
- Math.abs(scaled-Math.round(scaled))>1e-7)
50
+ Math.abs(scaled-Math.round(scaled))>1e-10)
51
throw Error(label+' باید بین صفر و صد با حداکثر دو رقم اعشار باشد');
52
return Number((BigInt(base)*BigInt(Math.round(scaled))+5000n)/10000n);
53
}
tests/financial-percent-boundary.test.mjs
+7
@@ -33,3 +33,10 @@ test('posting verifies typed row discount, total discount and enabled VAT',()=>{
33
const d=fixture();d.vatEnabled=false;
34
assert.throws(()=>assertInvoiceTotals(d),/مالیات فاکتور/);
35
});
36
+
37
+
38
+test('overprecise near-boundary percentages cannot silently round to 100 percent',()=>{
39
+ for(const value of [99.9999999999,50.00000000001,0.000000001])
40
+ assert.throws(()=>percentOfRials(100,value),/درصد/);
41
+ assert.equal(percentOfRials(10000,10.29),1029);
42
+});
tests/fractional-invoice-rial-rounding.test.mjs
+7
@@ -35,3 +35,10 @@ test('invoice UI preview and persistence use identical gross line function', ()
35
assert.ok(!controller.includes('const lineSubtotal = qty * price'));
36
assert.ok(!view.includes('const lineSubtotal = qty * price'));
37
});
38
+
39
+
40
+test('fractional stock rejects near-millisecond-precision numeric edits',()=>{
41
+ for(const quantity of [1.234000000001,0.001000000001,2.500000001])
42
+ assert.throws(()=>qtyUnits(quantity),/مقدار کالا نامعتبر/);
43
+ assert.equal(qtyUnits(2.501),2501);
44
+});