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

import { isKuaishouFeifeiSuccessResponse, signKuaishouFeifeiPayload } from './http-client.js'

test('signKuaishouFeifeiPayload signs app key, timestamp and raw body with HMAC SHA256', () => {
  const input = {
    appKey: 'app-key-1',
    appSecret: 'secret-1',
    timestamp: '1783394218',
    body: '{"platform_order_no":"DT-1","product_code":"10000001","platform_buy_num":1}',
  }
  const expected = crypto
    .createHmac('sha256', input.appSecret)
    .update(`${input.appKey}${input.timestamp}${input.body}`, 'utf8')
    .digest('hex')
    .toLowerCase()

  assert.equal(signKuaishouFeifeiPayload(input), expected)
})

test('isKuaishouFeifeiSuccessResponse accepts documented and actual success codes', () => {
  assert.equal(isKuaishouFeifeiSuccessResponse({ code: 0, message: 'success' }), true)
  assert.equal(isKuaishouFeifeiSuccessResponse({ code: 10000, message: 'success' }), true)
  assert.equal(isKuaishouFeifeiSuccessResponse({ code: 10000, message: '下单成功' }), true)
  assert.equal(isKuaishouFeifeiSuccessResponse({ code: 10000, message: '查询成功' }), true)
  assert.equal(isKuaishouFeifeiSuccessResponse({ code: 10000, message: '商品不存在' }), false)
  assert.equal(isKuaishouFeifeiSuccessResponse({ code: 1, message: 'success' }), false)
})
