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

import {
  preReviewExpenseClaim,
  submitExpenseClaim
} from '../src/services/reimbursements.js'

test('reimbursement service sends pre-review request identity and submit handshake payload', async () => {
  const requests = []
  const originalFetch = globalThis.fetch
  globalThis.fetch = async (url, options = {}) => {
    requests.push({ url: String(url), options })
    return {
      ok: true,
      status: 200,
      async json() {
        return { ok: true }
      }
    }
  }

  try {
    await preReviewExpenseClaim('claim / 001', 'pre-review:test-request-001')
    await submitExpenseClaim('claim / 001', {
      pre_review_id: 'review-ready-001',
      input_fingerprint: 'sha256:ready-input'
    })
  } finally {
    globalThis.fetch = originalFetch
  }

  assert.equal(requests.length, 2)
  assert.equal(
    requests[0].url,
    '/api/v1/reimbursements/claims/claim%20%2F%20001/pre-review'
  )
  assert.equal(requests[0].options.method, 'POST')
  assert.equal(requests[0].options.headers['X-Request-ID'], 'pre-review:test-request-001')
  assert.equal(requests[0].options.body, '{}')

  assert.equal(
    requests[1].url,
    '/api/v1/reimbursements/claims/claim%20%2F%20001/submit'
  )
  assert.equal(requests[1].options.method, 'POST')
  assert.deepEqual(JSON.parse(requests[1].options.body), {
    pre_review_id: 'review-ready-001',
    input_fingerprint: 'sha256:ready-input'
  })
})
