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

import { normalizeCloudtentaclesOverrideRuleConfig } from './cloudtentacles-override-rule-service.js'

test('normalizeCloudtentaclesOverrideRuleConfig merges duplicated delivery items', () => {
  const config = normalizeCloudtentaclesOverrideRuleConfig({
    enabled: true,
    rules: [
      {
        productName: '套装1',
        deliveryItems: [
          {
            cloudSkuId: 28,
            cloudSkuName: '商品A',
            quantity: 1,
          },
          {
            cloudSkuId: 28,
            cloudSkuName: '商品A',
            quantity: 2,
          },
          {
            cloudSkuName: '商品B',
            quantity: 20,
          },
        ],
      },
    ],
  })

  assert.equal(config.enabled, true)
  assert.equal(config.rules.length, 1)
  assert.deepEqual(config.rules[0]?.deliveryItems, [
    {
      cloudSkuId: 28,
      cloudSkuName: '商品A',
      quantity: 3,
    },
    {
      cloudSkuId: 0,
      cloudSkuName: '商品B',
      quantity: 20,
    },
  ])
})

test('normalizeCloudtentaclesOverrideRuleConfig drops invalid rules', () => {
  const config = normalizeCloudtentaclesOverrideRuleConfig({
    rules: [
      {
        productName: '',
        deliveryItems: [
          {
            cloudSkuId: 28,
            quantity: 1,
          },
        ],
      },
      {
        productName: '有效商品',
        deliveryItems: [],
      },
    ],
  })

  assert.deepEqual(config.rules, [])
})
