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

import {
  matchAffiliateDashSku,
  matchAffiliateDashSkuPassthrough,
  normalizeAffiliateDashMappingKey,
} from './product-resolution-service.js'

test('matchAffiliateDashSku 原样精确命中', () => {
  const mapping = { 幸运币90个: 'lucky_coin_x90' }
  assert.equal(matchAffiliateDashSku(mapping, '幸运币90个'), 'lucky_coin_x90')
})

test('matchAffiliateDashSku 全角数字/空格归一命中', () => {
  const mapping = { 幸运币90个: 'lucky_coin_x90' }
  assert.equal(matchAffiliateDashSku(mapping, '幸运币９０个'), 'lucky_coin_x90')
  assert.equal(matchAffiliateDashSku(mapping, ' 幸运币 90 个 '), 'lucky_coin_x90')
})

test('matchAffiliateDashSku 大小写归一命中', () => {
  const mapping = { 星际漫游服装礼包: 'pack_star_roam_outfit' }
  assert.equal(matchAffiliateDashSku(mapping, '星际漫游服装礼包'), 'pack_star_roam_outfit')
})

test('matchAffiliateDashSku 未命中返回空串', () => {
  const mapping = { 幸运币90个: 'lucky_coin_x90' }
  assert.equal(matchAffiliateDashSku(mapping, '神秘新商品'), '')
  assert.equal(matchAffiliateDashSku(mapping, ''), '')
})

test('normalizeAffiliateDashMappingKey NFKC + 去空白 + 小写', () => {
  assert.equal(normalizeAffiliateDashMappingKey(' 幸运币９０个　'), '幸运币90个')
  assert.equal(normalizeAffiliateDashMappingKey('M416-仓鼠灰灰'), 'm416-仓鼠灰灰')
})

test('matchAffiliateDashSkuPassthrough sku 一致时命中', () => {
  const skus = new Set(['suit_alan_walker', 'lucky_coin_x90'])
  assert.equal(matchAffiliateDashSkuPassthrough('suit_alan_walker', skus), true)
  assert.equal(matchAffiliateDashSkuPassthrough('lucky_coin_x90', skus), true)
})

test('matchAffiliateDashSkuPassthrough 不在商品列表时未命中', () => {
  const skus = new Set(['suit_alan_walker'])
  assert.equal(matchAffiliateDashSkuPassthrough('not_a_real_sku', skus), false)
  assert.equal(matchAffiliateDashSkuPassthrough('', skus), false)
  assert.equal(matchAffiliateDashSkuPassthrough('suit_alan_walker', null), false)
  assert.equal(matchAffiliateDashSkuPassthrough('suit_alan_walker', undefined), false)
})

test('matchAffiliateDashSkuPassthrough 空集合未命中', () => {
  assert.equal(matchAffiliateDashSkuPassthrough('suit_alan_walker', new Set()), false)
})
