goodsChooseEcho.helpers.test.mjs
4.6 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
mergeSubOrderListForEcho,
findCatalogSingleGoodsItem,
resolveSubOrderUnitPrice,
applySubOrderDiscountEcho,
buildSingleGoodsChooseEntry,
resolveSubOrderMarketingEcho,
isMultiSpecSubOrderLine
} from '../services/goodsChooseEcho.helpers.js'
describe('goodsChooseEcho.helpers', () => {
it('IT-WO-ECHO-001: merge filters package rows from subOrder', () => {
const merged = mergeSubOrderListForEcho(
[{ id: 1 }, { packageName: 'pkg' }],
[{ id: 2 }]
)
assert.equal(merged.length, 2)
assert.equal(merged[0].id, 1)
assert.equal(merged[1].id, 2)
})
it('IT-WO-ECHO-002: findCatalogSingleGoodsItem', () => {
const cat = findCatalogSingleGoodsItem(
[{ goodsFlag: 1, goods: { merchantGoodsId: 9 } }],
9
)
assert.equal(cat.goods.merchantGoodsId, 9)
})
it('IT-WO-ECHO-003: resolveSubOrderUnitPrice resetOrder uses sellPrice', () => {
assert.equal(
resolveSubOrderUnitPrice({ sellPrice: 88, number: 2 }, true),
88
)
})
it('IT-WO-ECHO-004: resolveSubOrderUnitPrice from total', () => {
assert.equal(
resolveSubOrderUnitPrice({ totalPrice: 200, number: 2 }, false),
100
)
})
it('IT-WO-ECHO-005: applySubOrderDiscountEcho ratio', () => {
const g = { sellPrice: 100, discountType: 1, discountValue: 8 }
applySubOrderDiscountEcho(g)
assert.equal(g.finalPrice, 80)
})
it('IT-WO-ECHO-006: applySubOrderDiscountEcho gift', () => {
const g = { sellPrice: 50, discountType: 4 }
applySubOrderDiscountEcho(g)
assert.equal(g.finalPrice, 0)
})
it('IT-WO-ECHO-007: resolveSubOrderMarketingEcho', () => {
const meta = resolveSubOrderMarketingEcho(
{ marketingFlag: 1, activityId: 3 },
{ marketingFlag: 0, activityId: 9 }
)
assert.equal(meta.activityId, 3)
assert.equal(meta.isMarketingGoods, true)
})
it('IT-WO-ECHO-008: buildSingleGoodsChooseEntry', () => {
const line = {
merchantGoodsId: 1,
number: 2,
specificationData: 'S'
}
const entry = buildSingleGoodsChooseEntry(line, {
marketingFlag: 1,
activityId: 5
})
assert.equal(entry.goodsFlag, 1)
assert.equal(entry.count, 2)
assert.equal(entry.activityId, 5)
})
it('IT-WO-ECHO-009: skips multi-spec lines', () => {
assert.equal(
buildSingleGoodsChooseEntry({
specificationData: 'A;~;B'
}),
null
)
assert.equal(isMultiSpecSubOrderLine({ specificationData: 'A;~;B' }), true)
})
it('IT-WO-ECHO-010: normalizes echoed unit-price float tails to cents', () => {
assert.equal(
resolveSubOrderUnitPrice(
{ price: 1999.9900000000002, totalPrice: 9999.95, number: 5 },
false
),
1999.99
)
assert.equal(
resolveSubOrderUnitPrice(
{ sellPrice: 1999.9900000000002, totalPrice: 9999.95, number: 5 },
true
),
1999.99
)
})
it('IT-WO-ECHO-011: normalizes total divided by quantity in both echo modes', () => {
assert.equal(
resolveSubOrderUnitPrice({ totalPrice: 9999.95, number: 5 }, false),
1999.99
)
assert.equal(
resolveSubOrderUnitPrice({ totalPrice: 9999.95, number: 5 }, true),
1999.99
)
})
it('IT-WO-ECHO-012: keeps explicit zero as the preferred echoed unit price', () => {
assert.equal(
resolveSubOrderUnitPrice({ price: 0, totalPrice: 100, number: 2 }, false),
0
)
assert.equal(
resolveSubOrderUnitPrice(
{ sellPrice: 0, totalPrice: 100, number: 2 },
true
),
0
)
})
it('IT-WO-ECHO-013: rejects empty and non-finite echoed prices', () => {
assert.equal(resolveSubOrderUnitPrice({ price: '' }, false), undefined)
assert.equal(resolveSubOrderUnitPrice({ sellPrice: null }, true), undefined)
assert.equal(resolveSubOrderUnitPrice({ price: 'invalid' }, false), undefined)
assert.equal(resolveSubOrderUnitPrice({ sellPrice: Infinity }, true), undefined)
})
it('IT-WO-ECHO-014: rejects explicit zero quantity and over-precision totals', () => {
assert.equal(
resolveSubOrderUnitPrice({ totalPrice: 100, number: 0 }, false),
undefined
)
assert.equal(
resolveSubOrderUnitPrice({ totalPrice: '1.001', number: 1 }, false),
undefined
)
})
it('IT-WO-ECHO-015: ratio discount rounds upward to a cent exactly', () => {
const goods = {
sellPrice: '0.29',
discountType: 1,
discountValue: '8.5'
}
applySubOrderDiscountEcho(goods)
assert.equal(goods.finalPrice, 0.25)
assert.equal(goods.discountAmount, 0.04)
})
})