import assert from 'node:assert/strict'; import { test } from 'node:test'; import { buildOzonProductImportPayload, OzonProductImportPayloadError, } from '../src/ozon-product-import.ts'; const defaultPackageSpec = { length_mm: '240', width_mm: '120', height_mm: '60', shipping_weight_g: '850', status: 'confirmed', }; function source(overrides = {}) { return { default_package_spec: defaultPackageSpec, category: { description_category_id: '17028922', type_id: '91234' }, title: '测试商品', description: '商品描述', barcode: '1234567890123', common_attributes: [ { id: 85, complex_id: 0, values: [{ value: '品牌' }] }, ], variants: [ { variant_id: 'sku-a', offer_id: 'offer-a', price: '199.90', currency_code: 'RUB', package_spec: { length_mm: null, width_mm: null, height_mm: null, shipping_weight_g: null, status: null, }, images: ['https://cdn.example.test/a.jpg'], attributes: [ { id: 8229, complex_id: 0, values: [{ value: '红色' }] }, ], }, ], ...overrides, }; } test('按 SKU 将 SPU 默认包装规格展开为 Ozon items[]', () => { const payload = buildOzonProductImportPayload(source()); assert.deepEqual(payload, { items: [ { offer_id: 'offer-a', name: '测试商品', description_category_id: 17028922, type_id: 91234, price: 199.9, currency_code: 'RUB', description: '商品描述', barcode: '1234567890123', images: ['https://cdn.example.test/a.jpg'], attributes: [ { id: 85, complex_id: 0, values: [{ value: '品牌' }] }, { id: 8229, complex_id: 0, values: [{ value: '红色' }] }, ], depth: 240, width: 120, height: 60, dimension_unit: 'mm', weight: 850, weight_unit: 'g', }, ], }); }); test('SKU 包装覆盖值优先于 SPU 默认值', () => { const input = source(); input.variants[0].package_spec = { length_mm: '300', width_mm: '160', height_mm: '70', shipping_weight_g: '1000', status: 'confirmed', }; const payload = buildOzonProductImportPayload(input); assert.deepEqual( [ payload.items[0].depth, payload.items[0].width, payload.items[0].height, payload.items[0].weight, ], [300, 160, 70, 1000], ); }); test('重量与尺寸可分别覆盖,未覆盖字段继续继承 SPU', () => { const input = source(); input.variants[0].package_spec.shipping_weight_g = '920'; const item = buildOzonProductImportPayload(input).items[0]; assert.deepEqual( [item.depth, item.width, item.height, item.weight], [240, 120, 60, 920], ); }); test('缺少有效包装规格时拒绝生成对应 SKU 的导入项', () => { const input = source({ default_package_spec: { ...defaultPackageSpec, shipping_weight_g: null, }, }); assert.throws( () => buildOzonProductImportPayload(input), (error) => error instanceof OzonProductImportPayloadError && error.variantId === 'sku-a' && error.field === 'package_spec.shipping_weight_g', ); });