import assert from 'node:assert/strict'
import test from 'node:test'

import { buildDispatchStockItems } from './task-finalization.js'

test('buildDispatchStockItems 只计算兑换阶段需要补买的库存缺口', () => {
  const result = buildDispatchStockItems(
    [
      {
        cloudSkuId: 101,
        cloudSkuName: '',
        quantity: 3,
      },
    ],
    {
      skuItems: [
        {
          id: 101,
          name: '套装-暗影哥特',
          price: 88,
        },
      ],
      knapsackItems: [
        {
          id: 101,
          name: '套装-暗影哥特',
          count: 1,
        },
      ],
    },
  )

  assert.deepEqual(result, [
    {
      cloudSkuId: 101,
      cloudSkuName: '套装-暗影哥特',
      quantity: 3,
      requiredCount: 3,
      knapsackCount: 1,
      purchasedCount: 2,
    },
  ])
})

test('buildDispatchStockItems 背包足够时不需要补买', () => {
  const result = buildDispatchStockItems(
    [
      {
        cloudSkuId: 202,
        cloudSkuName: '套装-浪漫天命',
        quantity: 1,
      },
    ],
    {
      knapsackItems: [
        {
          id: 202,
          name: '套装-浪漫天命',
          count: 5,
        },
      ],
    },
  )

  assert.equal(result[0]?.knapsackCount, 5)
  assert.equal(result[0]?.purchasedCount, 0)
})
