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

import {
  buildKuaishouIndustrySignSource,
  signKuaishouIndustryPayload,
  verifyKuaishouIndustrySignature,
} from './crypto.js'

const TEST_CONFIG = { signSecret: 'test-secret' } as any

test('buildKuaishouIndustrySignSource keeps nested eticket fields for destroy-code payload', () => {
  const source = buildKuaishouIndustrySignSource(
    {
      appkey: 'ks-test',
      param: {
        oid: '2618901686368642',
        reason: 'USER_APPLY_REFUND',
        etickets: [
          {
            code: null,
            goodsValue: 3800,
            id: 'KSVW9JTD4FZM4VDHKBV',
            num: 1,
          },
        ],
      },
      signMethod: 'MD5',
      timestamp: 1783497998539,
      version: '1',
    },
    TEST_CONFIG,
  )

  assert.match(
    source,
    /param=\{"etickets":\[\{"code":null,"goodsValue":3800,"id":"KSVW9JTD4FZM4VDHKBV","num":1\}\],"oid":"2618901686368642","reason":"USER_APPLY_REFUND"\}/,
  )
  assert.doesNotMatch(source, /"etickets":\[\{\}\]/)
})

test('verifyKuaishouIndustrySignature accepts nested object payload signed by the same canonical source', () => {
  const payload = {
    appkey: 'ks-test',
    param: {
      oid: '2618901686368642',
      reason: 'USER_APPLY_REFUND',
      etickets: [
        {
          code: null,
          goodsValue: 3800,
          id: 'KSVW9JTD4FZM4VDHKBV',
          num: 1,
        },
      ],
    },
    signMethod: 'MD5',
    timestamp: 1783497998539,
    version: '1',
  }

  const sign = signKuaishouIndustryPayload(payload, 'MD5', TEST_CONFIG)

  assert.equal(verifyKuaishouIndustrySignature({ ...payload, sign }, TEST_CONFIG), true)
})
