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

const service = fs.readFileSync('web/src/services/reimbursements.js', 'utf8')
const approvalFlow = fs.readFileSync(
  'web/src/views/scripts/useTravelRequestDetailApprovalFlow.js',
  'utf8'
)
const paymentFlow = fs.readFileSync(
  'web/src/views/scripts/travelRequestDetailPaymentFlow.js',
  'utf8'
)

test('approval actions keep one request id across an uncertain retry', () => {
  assert.match(service, /createExpenseClaimActionRequestId/)
  assert.match(approvalFlow, /const returnRequestId = ref\(''\)/)
  assert.match(approvalFlow, /const approveRequestId = ref\(''\)/)
  assert.match(approvalFlow, /request_id: returnRequestId\.value/)
  assert.match(approvalFlow, /request_id: approveRequestId\.value/)
  assert.doesNotMatch(
    approvalFlow.match(/async function confirmApproveRequest\(\)[\s\S]*?\n  }/)?.[0] || '',
    /createExpenseClaimActionRequestId/
  )
})

test('approve return and pay send optimistic status and stage preconditions', () => {
  assert.match(approvalFlow, /returnExpectedStatus\.value = String\(request\.value\.status/)
  assert.match(approvalFlow, /expected_status: returnExpectedStatus\.value/)
  assert.match(approvalFlow, /expected_approval_stage: approveExpectedStage\.value/)
  assert.match(paymentFlow, /const payRequestId = ref\(''\)/)
  assert.match(paymentFlow, /payExpenseClaim\(request\.value\.claimId, \{/)
  assert.match(paymentFlow, /request_id: payRequestId\.value/)
  assert.match(paymentFlow, /payExpectedStatus\.value = String\(request\.value\.status/)
  assert.match(paymentFlow, /expected_status: payExpectedStatus\.value/)
  assert.match(paymentFlow, /expected_approval_stage: payExpectedStage\.value/)
})
