selectOrderCatalogClassification.test.mjs 3.33 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  SELECT_ORDER_GOODS_FLAG,
  isPositionBucketCatalogItem,
  classifySelectOrderCatalog,
  applySelectOrderCatalogItem,
  createEmptyCatalogClassification,
  flattenCatalogClassification
} from '../services/selectOrderCatalogClassification.js'

describe('selectOrderCatalogClassification', () => {
  it('isPositionBucketCatalogItem is true only for single goods', () => {
    assert.equal(
      isPositionBucketCatalogItem({ goodsFlag: SELECT_ORDER_GOODS_FLAG.SINGLE }),
      true
    )
    assert.equal(
      isPositionBucketCatalogItem({
        goodsFlag: SELECT_ORDER_GOODS_FLAG.MULTI_SPEC
      }),
      false
    )
    assert.equal(
      isPositionBucketCatalogItem({ goodsFlag: SELECT_ORDER_GOODS_FLAG.PACKAGE }),
      false
    )
  })

  it('classifySelectOrderCatalog buckets single goods by position', () => {
    const result = classifySelectOrderCatalog([
      {
        goodsFlag: 1,
        goods: {
          merchantGoodsId: 'a',
          merchantGoodsPosition: 1,
          sellPrice: 100
        }
      }
    ])
    assert.equal(result.frontGearArr.length, 1)
    assert.equal(result.otherProduct.length, 0)
    assert.equal(result.packageList.length, 0)
  })

  it('classifySelectOrderCatalog puts single goods without position in otherProduct', () => {
    const result = classifySelectOrderCatalog([
      {
        goodsFlag: 1,
        goods: { merchantGoodsId: 'b', sellPrice: 50 }
      }
    ])
    assert.equal(result.otherProduct.length, 1)
    assert.equal(result.frontGearArr.length, 0)
  })

  it('classifySelectOrderCatalog keeps multi-spec in otherProduct only even with SKU position', () => {
    const result = classifySelectOrderCatalog([
      {
        goodsFlag: 2,
        multipleSpecificationsGoods: {
          specificationList: [
            {
              specificationValueList: [{ show: true, specificationValue: 'S' }]
            }
          ],
          goods: { merchantGoodsPosition: 1, sellPrice: 80 }
        }
      }
    ])
    assert.equal(result.otherProduct.length, 1)
    assert.equal(result.frontGearArr.length, 0)
    assert.equal(result.packageList.length, 0)
  })

  it('classifySelectOrderCatalog puts package only in packageList', () => {
    const result = classifySelectOrderCatalog([
      {
        goodsFlag: 3,
        packageGoods: {
          packageName: 'pkg',
          goodsList: [{ merchantGoodsId: 'c', sellPrice: 10 }]
        }
      }
    ])
    assert.equal(result.packageList.length, 1)
    assert.equal(result.otherProduct.length, 0)
    assert.equal(result.frontGearArr.length, 0)
  })

  it('applySelectOrderCatalogItem syncs subOrderList position for single goods', () => {
    const subOrderList = [
      {
        merchantGoodsId: 'x',
        upgradeGoodsId: 'gid',
        upgradeGoodsPosition: 2
      }
    ]
    const accum = createEmptyCatalogClassification()
    applySelectOrderCatalogItem(
      accum,
      {
        goodsFlag: 1,
        goods: {
          merchantGoodsId: 'x',
          upgradeGoodsId: 'gid',
          merchantGoodsPosition: 1,
          sellPrice: 1
        }
      },
      { subOrderList }
    )
    assert.equal(subOrderList[0].upgradeGoodsPosition, 1)
    assert.equal(subOrderList[0].active, true)
    assert.equal(flattenCatalogClassification(accum).frontGearArr.length, 1)
  })
})