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

import { signKuaishouFeifeiPayload } from './http-client.js'
import { verifyKuaishouFeifeiNotifySignature } from './notify-service.js'

test('verifyKuaishouFeifeiNotifySignature 使用原始 body 校验 feifei 通知签名', () => {
  const input = {
    appKey: 'mk_app_key',
    appSecret: 'app-secret',
    timestamp: '1783411548',
    rawBody: '{"event":"order.status_changed","order":{"platform_order_no":"DT56bf8f1727a7"}}',
  }
  const sign = signKuaishouFeifeiPayload({
    appKey: input.appKey,
    appSecret: input.appSecret,
    timestamp: input.timestamp,
    body: input.rawBody,
  })
  const invalidSign = `${sign.slice(0, -1)}${sign.endsWith('0') ? '1' : '0'}`

  assert.equal(verifyKuaishouFeifeiNotifySignature({ ...input, sign }), true)
  assert.equal(verifyKuaishouFeifeiNotifySignature({ ...input, sign: invalidSign }), false)
})
