forked from lunanting/cangku
111 lines
3.2 KiB
JavaScript
111 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
_test: {
|
|
filterSkuRowsForSelectionContext,
|
|
createPrefsPatch,
|
|
getSelectionSkuCodeSet,
|
|
normalizeCalculatorType,
|
|
readPrefsProfile,
|
|
sanitizeHistoryProduct,
|
|
writePrefsProfile,
|
|
},
|
|
} = require('../src/api/pricingCalculator');
|
|
|
|
const skuRows = [
|
|
{ skuCode: 'SKU-A' },
|
|
{ skuCode: 'SKU-B' },
|
|
{ skuCode: 'SKU-C' },
|
|
];
|
|
|
|
test('selection pricing only returns explicitly selected SKU rows', () => {
|
|
const selectionContext = {
|
|
skuSceneImages: { selectedSkuCodes: ['sku-a', 'SKU-C'] },
|
|
rows: [{ skuCode: 'SKU-A' }, { skuCode: 'SKU-B' }, { skuCode: 'SKU-C' }],
|
|
};
|
|
|
|
assert.deepEqual(
|
|
filterSkuRowsForSelectionContext(skuRows, selectionContext),
|
|
[{ skuCode: 'SKU-A' }, { skuCode: 'SKU-C' }]
|
|
);
|
|
});
|
|
|
|
test('selection pricing falls back to stored pricing rows for legacy tasks', () => {
|
|
const selectionContext = {
|
|
skuSceneImages: {},
|
|
rows: [{ skuCode: 'SKU-B' }],
|
|
};
|
|
|
|
assert.deepEqual([...getSelectionSkuCodeSet(selectionContext)], ['SKU-B']);
|
|
assert.deepEqual(
|
|
filterSkuRowsForSelectionContext(skuRows, selectionContext),
|
|
[{ skuCode: 'SKU-B' }]
|
|
);
|
|
});
|
|
|
|
test('standalone pricing continues to return all SPU SKU rows', () => {
|
|
assert.deepEqual(filterSkuRowsForSelectionContext(skuRows, null), skuRows);
|
|
});
|
|
|
|
test('pricing preferences keep legacy company values and isolate 1688 defaults', () => {
|
|
const legacy = JSON.stringify({
|
|
coupon: 30,
|
|
activityDiscount: 0.7,
|
|
installFee: 5,
|
|
targetMargin: 0.15,
|
|
});
|
|
const withDropship = writePrefsProfile(legacy, 'alibaba1688', {
|
|
coupon: 20,
|
|
activityDiscount: 0.6,
|
|
installFee: 0,
|
|
targetMargin: 0.12,
|
|
});
|
|
|
|
assert.deepEqual(readPrefsProfile(withDropship, 'company'), {
|
|
coupon: 30,
|
|
activityDiscount: 0.7,
|
|
installFee: 5,
|
|
targetMargin: 0.15,
|
|
});
|
|
assert.deepEqual(readPrefsProfile(withDropship, 'alibaba1688'), {
|
|
coupon: 20,
|
|
activityDiscount: 0.6,
|
|
installFee: 0,
|
|
targetMargin: 0.12,
|
|
});
|
|
|
|
const updatedCompany = writePrefsProfile(withDropship, 'company', { targetMargin: 0.18 });
|
|
assert.equal(readPrefsProfile(updatedCompany, 'company').targetMargin, 0.18);
|
|
assert.equal(readPrefsProfile(updatedCompany, 'alibaba1688').targetMargin, 0.12);
|
|
assert.deepEqual(createPrefsPatch('company', { targetMargin: 0.15 }), { targetMargin: 0.15 });
|
|
assert.deepEqual(createPrefsPatch('alibaba1688', { targetMargin: 0.12 }), {
|
|
_profiles: { alibaba1688: { targetMargin: 0.12 } },
|
|
});
|
|
});
|
|
|
|
test('1688 history product keeps bounded identity and specification dimensions', () => {
|
|
assert.equal(normalizeCalculatorType('alibaba1688'), 'alibaba1688');
|
|
assert.equal(normalizeCalculatorType('unknown'), 'company');
|
|
assert.deepEqual(sanitizeHistoryProduct({
|
|
productId: ' 1073252781818 ',
|
|
title: ' 测试商品 ',
|
|
skuDimensions: [
|
|
{ name: '颜色', values: ['原木色', '胡桃色'] },
|
|
{ name: '尺寸', values: ['60cm'] },
|
|
],
|
|
}), {
|
|
productId: '1073252781818',
|
|
title: '测试商品',
|
|
mainImage: '',
|
|
sourceUrl: '',
|
|
storeName: '',
|
|
category: '',
|
|
skuDimensions: [
|
|
{ name: '颜色', values: ['原木色', '胡桃色'] },
|
|
{ name: '尺寸', values: ['60cm'] },
|
|
],
|
|
});
|
|
});
|