goodsPriceEdit.helpers.test.mjs
5.67 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
applyBoutiqueDiscount,
compareBoutiqueMoney,
computeBoutiqueLineAmounts,
divideBoutiqueMoneyByQuantity,
isMarketingGoodsPriceLocked,
isBoutiqueErrorOrderBalanced,
isBoutiqueTotalAtLeastOneCent,
multiplyBoutiqueMoney,
normalizeBoutiqueMoney,
sumBoutiqueAssociatedFeeCents
} from '../services/goodsPriceEdit.helpers.js'
describe('goodsPriceEdit.helpers', () => {
it('compares money in integer cents without float coercion', () => {
assert.deepEqual(compareBoutiqueMoney('0.29', 0.29000000000000004), {
ok: true,
value: 0
})
assert.equal(compareBoutiqueMoney('invalid', 1).ok, false)
})
it('divides authoritative totals with explicit half-up cent rounding', () => {
assert.deepEqual(divideBoutiqueMoneyByQuantity('9999.95', 5), {
ok: true,
value: 1999.99,
cents: 199999
})
assert.equal(divideBoutiqueMoneyByQuantity('1', 3).value, 0.33)
assert.equal(divideBoutiqueMoneyByQuantity('0.02', 3).value, 0.01)
assert.equal(divideBoutiqueMoneyByQuantity('1', 0).ok, false)
})
it('applies ratio, fixed and gift discounts in integer cents', () => {
assert.deepEqual(applyBoutiqueDiscount('0.29', 1, '8.5'), {
ok: true,
finalPrice: 0.25,
discountAmount: 0.04,
finalCents: 25,
discountCents: 4
})
assert.equal(applyBoutiqueDiscount('10', 2, '0.01').finalPrice, 9.99)
assert.equal(applyBoutiqueDiscount('10', 4).finalPrice, 0)
assert.equal(applyBoutiqueDiscount('10', 1, '10.001').ok, false)
})
it('locks price when marketingFlag and activityId are both present', () => {
assert.equal(
isMarketingGoodsPriceLocked({
marketingFlag: 1,
activityId: 9,
goods: {}
}),
true
)
})
it('does not lock when marketingFlag is inactive', () => {
assert.equal(
isMarketingGoodsPriceLocked({
marketingFlag: 0,
activityId: 9
}),
false
)
})
it('does not lock when activityId is missing', () => {
assert.equal(
isMarketingGoodsPriceLocked({
marketingFlag: 1,
goods: { marketingFlag: 1 }
}),
false
)
})
it('reads marketing meta from goods body', () => {
assert.equal(
isMarketingGoodsPriceLocked({
goods: { marketingFlag: 1, activityId: 3 }
}),
true
)
})
it('normalizes exact yuan values and API numeric float tails to cents', () => {
assert.deepEqual(normalizeBoutiqueMoney('0.29'), {
ok: true,
value: 0.29,
cents: 29
})
assert.deepEqual(normalizeBoutiqueMoney(1999.9900000000002), {
ok: true,
value: 1999.99,
cents: 199999
})
})
it('rejects missing, negative, non-finite and over-precision user money', () => {
for (const value of ['', null, undefined, '-1', '0.001', Infinity]) {
assert.equal(normalizeBoutiqueMoney(value).ok, false, String(value))
}
})
it('multiplies in integer cents and rejects zero quantity', () => {
assert.deepEqual(multiplyBoutiqueMoney('1999.99', 5), {
ok: true,
value: 9999.95,
cents: 999995
})
assert.equal(multiplyBoutiqueMoney('1.00', 0).ok, false)
assert.deepEqual(multiplyBoutiqueMoney('1.00', undefined), {
ok: true,
value: 1,
cents: 100
})
})
it('uses an authoritative line total before unit-price multiplication', () => {
assert.deepEqual(
computeBoutiqueLineAmounts({
unitPrice: '1999.99',
quantity: 5,
discountType: 3,
authoritativeTotalPrice: '9999.96'
}),
{
ok: true,
unitPrice: 1999.99,
finalPrice: 9999.96,
singleProductFinalPrice: 1999.99,
discountAmount: 0
}
)
})
it('gift line remains zero regardless of source price', () => {
const result = computeBoutiqueLineAmounts({
unitPrice: '88.88',
quantity: 2,
discountType: 4
})
assert.equal(result.ok, true)
assert.equal(result.finalPrice, 0)
assert.equal(result.singleProductFinalPrice, 0)
})
it('balances error-order yuan totals against an associated fee in cents', () => {
assert.deepEqual(
isBoutiqueErrorOrderBalanced({
originalPrice: '0.10',
associatedFeeCents: 20,
currentTotal: '0.30'
}),
{ ok: true, value: true }
)
assert.equal(
isBoutiqueErrorOrderBalanced({
originalPrice: '0.10',
associatedFeeCents: 20,
currentTotal: '0.31'
}).value,
false
)
})
it('sums associated fee cents exactly across safe integers and digit strings', () => {
const input = [1, '2', '0003', 9007199254740980]
const snapshot = structuredClone(input)
assert.deepEqual(sumBoutiqueAssociatedFeeCents(input), {
ok: true,
value: 9007199254740986
})
assert.deepEqual(input, snapshot)
assert.deepEqual(sumBoutiqueAssociatedFeeCents([]), { ok: true, value: 0 })
})
it('rejects invalid, negative, fractional and overflowing associated fees', () => {
for (const values of [
[''],
[null],
[undefined],
['1.5'],
[-1],
['-1'],
[Number.MAX_SAFE_INTEGER, 1],
['9007199254740992']
]) {
assert.equal(sumBoutiqueAssociatedFeeCents(values).ok, false, String(values))
}
assert.equal(sumBoutiqueAssociatedFeeCents('1').ok, false)
})
it('validates the one-cent minimum without Number coercion', () => {
assert.deepEqual(isBoutiqueTotalAtLeastOneCent('0.01'), {
ok: true,
value: true
})
assert.equal(isBoutiqueTotalAtLeastOneCent(0).value, false)
assert.equal(isBoutiqueTotalAtLeastOneCent('invalid').ok, false)
})
})