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

import { extractAgisoTradePayload, resolveAgisoTradePlatformOrderId } from './agiso-trade-parsing.js'

test('extractAgisoTradePayload preserves large integer order ids from raw json', () => {
  const payload = extractAgisoTradePayload({
    json: '{"biz_order_id":4502259793206016214,"item_id":978102431355,"order_status":4}',
  })

  assert.equal(payload.biz_order_id, '4502259793206016214')
  assert.equal(typeof payload.biz_order_id, 'string')
})

test('resolveAgisoTradePlatformOrderId keeps exact webhook order id', () => {
  const payload = extractAgisoTradePayload({
    json: '{"biz_order_id":4502259793206016214,"item_id":978102431355,"order_status":4}',
  })

  assert.equal(resolveAgisoTradePlatformOrderId(payload), '4502259793206016214')
})

test('resolveAgisoTradePlatformOrderId falls back to tid when biz_order_id is absent', () => {
  assert.equal(
    resolveAgisoTradePlatformOrderId({
      tid: '4502251008019011224',
      orders: [{ oid: '4502251008019011888' }],
    }),
    '4502251008019011224',
  )
})
