marketingFlag.test.mjs
3.94 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
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
const {
buildMarketingFlagMap,
buildActivityIdMap,
resolveMarketingFlag,
applyMarketingFlagToFlatGoods,
normalizeMarketingFlagOnItem,
normalizeActivityIdOnItem,
isMarketingActive,
MARKETING_FLAG_ACTIVE
} = await import('../utils/marketingFlag.js')
describe('marketingFlag', () => {
it('buildMarketingFlagMap indexes active goods and resolves inactive goods to zero', () => {
const map = buildMarketingFlagMap([
{ goodsFlag: 1, marketingFlag: 1, goods: { merchantGoodsId: 100 } },
{ goodsFlag: 1, marketingFlag: 0, goods: { merchantGoodsId: 200 } }
])
assert.equal(map['100'], MARKETING_FLAG_ACTIVE)
assert.equal(map['200'], undefined)
assert.equal(resolveMarketingFlag(map, { merchantGoodsId: 200 }), 0)
})
it('resolveMarketingFlag returns active only when mapped', () => {
const map = { 100: MARKETING_FLAG_ACTIVE }
assert.equal(resolveMarketingFlag(map, { merchantGoodsId: 100 }), 1)
assert.equal(resolveMarketingFlag(map, { merchantGoodsId: 101 }), 0)
})
it('applyMarketingFlagToFlatGoods writes marketingFlag on items', () => {
const list = applyMarketingFlagToFlatGoods(
[{ id: 100, upgradeGoodsName: 'A' }],
{ 100: MARKETING_FLAG_ACTIVE }
)
assert.equal(list[0].marketingFlag, 1)
})
})
describe('normalizeMarketingFlagOnItem', () => {
it('lifts nested goods marketingFlag to item root', () => {
const item = normalizeMarketingFlagOnItem({
goodsFlag: 1,
goods: { marketingFlag: 1, merchantGoodsId: 1 }
})
assert.equal(item.marketingFlag, 1)
assert.equal(isMarketingActive(item.marketingFlag), true)
})
it('lifts nested goods activityId to item root', () => {
const item = normalizeActivityIdOnItem({
goodsFlag: 1,
goods: { activityId: 'ACT-1', merchantGoodsId: 1 }
})
assert.equal(item.activityId, 'ACT-1')
assert.equal(item.goods.activityId, 'ACT-1')
})
it('lifts nested packageGoods marketingFlag to item root', () => {
const item = normalizeMarketingFlagOnItem({
goodsFlag: 3,
packageGoods: { marketingFlag: '1', packageCode: 'P1' }
})
assert.equal(item.marketingFlag, 1)
assert.equal(isMarketingActive(item.marketingFlag), true)
})
it('lifts package marketingFlag when only child goodsList item is active', () => {
const item = normalizeMarketingFlagOnItem({
goodsFlag: 3,
goods: null,
packageGoods: {
packageCode: 'MPG2026051610543826317',
packageName: '套餐名称2',
goodsList: [{ marketingFlag: 1, upgradeGoodsName: '测试太阳膜多规格' }]
}
})
assert.equal(item.marketingFlag, 1)
})
it('lifts package marketingFlag from order packageGoodsOrderList children', () => {
const item = normalizeMarketingFlagOnItem({
packageName: '套餐名称2',
packageGoodsOrderList: [{ marketingFlag: 1, upgradeGoodsName: '子商品' }]
})
assert.equal(item.marketingFlag, 1)
})
it('buildMarketingFlagMap maps package when child goodsList is active', () => {
const map = buildMarketingFlagMap([
{
goodsFlag: 3,
packageGoods: {
packageCode: 'PKG1',
goodsList: [{ marketingFlag: 1 }]
}
}
])
assert.equal(map['pkg:PKG1'], MARKETING_FLAG_ACTIVE)
})
it('buildActivityIdMap maps activityId from same list source', () => {
const activityIdMap = buildActivityIdMap([
{
goodsFlag: 1,
marketingFlag: 1,
goods: { merchantGoodsId: 100, activityId: 'ACT-100' }
},
{
goodsFlag: 3,
packageGoods: {
packageCode: 'PKG2',
activityId: 'ACT-PKG',
goodsList: [{ marketingFlag: 1, merchantGoodsId: 9, activityId: 'ACT-9' }]
}
}
])
assert.equal(activityIdMap['100'], 'ACT-100')
assert.equal(activityIdMap['pkg:PKG2'], 'ACT-PKG')
assert.equal(activityIdMap['9'], 'ACT-9')
})
})