quotationMoney.helpers.test.mjs
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
divideByHundred,
multipliedByHundred,
divideByHundredObj,
multipliedByHundredObj,
DEFAULT_NEED_CHANGE_LIST,
yuanToCentInteger,
centToYuanText,
compareCentAmounts,
validateQuotationOrderTotal
} from '../pages/carQuotation/services/quotationMoney.helpers.js'
describe('quotationMoney.helpers', () => {
it('IT-EQ-MNY-001: divideByHundred 10000 → 100', () => {
assert.equal(divideByHundred(10000), 100)
})
it('IT-EQ-MNY-002: multipliedByHundred 100 → 10000', () => {
assert.equal(multipliedByHundred(100), 10000)
})
it('IT-EQ-MNY-003: NaN returns 0', () => {
assert.equal(divideByHundred('abc'), 0)
assert.equal(multipliedByHundred('abc'), 0)
})
it('IT-EQ-MNY-004: divideByHundredObj only needChangeList keys', () => {
const out = divideByHundredObj(
{ price: 10000, name: 'x' },
DEFAULT_NEED_CHANGE_LIST
)
assert.equal(out.price, 100)
assert.equal(out.name, 'x')
})
it('IT-EQ-MNY-005: multipliedByHundredObj', () => {
const out = multipliedByHundredObj({ base: 1.5 }, DEFAULT_NEED_CHANGE_LIST)
assert.equal(out.base, 150)
})
it('IT-EQ-MNY-006: exact decimal yuan converts to integer cents without float tails', () => {
assert.deepEqual(yuanToCentInteger('0.29'), { ok: true, value: 29 })
assert.deepEqual(yuanToCentInteger('1000000.01'), {
ok: true,
value: 100000001
})
assert.deepEqual(yuanToCentInteger(0), { ok: true, value: 0 })
})
it('IT-EQ-MNY-007: invalid, negative and over-precision yuan are rejected explicitly', () => {
for (const value of ['', null, undefined, 'abc', '-1', '0.001']) {
assert.equal(yuanToCentInteger(value).ok, false, String(value))
}
})
it('IT-EQ-MNY-008: cents render as an exact two-decimal display string', () => {
assert.deepEqual(centToYuanText(1), { ok: true, value: '0.01' })
assert.deepEqual(centToYuanText('10000'), {
ok: true,
value: '100.00'
})
assert.equal(centToYuanText('1.2').ok, false)
assert.equal(centToYuanText(-1).ok, false)
})
it('IT-EQ-MNY-009: integer-cent comparison never converts through floating point', () => {
assert.deepEqual(compareCentAmounts('9007199254740990', '9007199254740989'), {
ok: true,
value: 1
})
assert.equal(compareCentAmounts('', 0).ok, false)
})
it('IT-EQ-MNY-010: validates normal and error-order totals in integer cents', () => {
assert.deepEqual(
validateQuotationOrderTotal({ resetOrder: false, total: '0.01' }),
{ ok: true }
)
assert.deepEqual(
validateQuotationOrderTotal({ resetOrder: false, total: '0' }),
{ ok: false, reason: 'below-minimum' }
)
assert.deepEqual(
validateQuotationOrderTotal({
resetOrder: true,
originalPrice: '0.10',
associatedFeeCents: 20,
total: '0.30'
}),
{ ok: true }
)
assert.equal(
validateQuotationOrderTotal({
resetOrder: true,
originalPrice: '0.10',
associatedFeeCents: 19,
total: '0.30'
}).reason,
'unbalanced'
)
})
it('IT-EQ-MNY-011: rejects malformed totals and accepts API float tails', () => {
assert.deepEqual(yuanToCentInteger(1999.9900000000002), {
ok: true,
value: 199999
})
for (const total of ['', 'invalid', '-1', '0.001', Infinity]) {
assert.equal(
validateQuotationOrderTotal({ resetOrder: false, total }).reason,
'invalid'
)
}
})
})