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

import { normalizeDestroyCodePayload } from './payload.js'
import { resolveDestroyCallbackToken } from './destroy-code-service.js'

test('normalizeDestroyCodePayload keeps destroy callback token when provided', () => {
  const payload = normalizeDestroyCodePayload({
    appkey: 'ks-test',
    param: {
      oid: '2618901686368642',
      reason: 'USER_APPLY_REFUND',
      token: 'destroy-token',
      etickets: [
        {
          id: 'KSVW9JTD4FZM4VDHKBV',
          num: 1,
        },
      ],
    },
    sign: 'test-sign',
    signMethod: 'MD5',
    timestamp: 1783499780936,
    version: '1',
  })

  assert.equal(payload.token, 'destroy-token')
})

test('resolveDestroyCallbackToken falls back to stored voucher token', () => {
  const token = resolveDestroyCallbackToken([{ token: '' }, { token: ' stored-token ' }])

  assert.equal(token, 'stored-token')
})
