import assert from 'node:assert/strict'

import { executeStewardAction } from '../src/services/steward.js'

async function testExecuteStewardActionUsesActionEndpoint() {
  let capturedUrl = ''
  let capturedOptions = null

  global.fetch = async (url, options) => {
    capturedUrl = String(url)
    capturedOptions = options
    return {
      ok: true,
      async json() {
        return {
          action_type: 'save_application_draft',
          status: 'succeeded',
          message: '申请草稿已保存。',
          result_payload: {
            draft_payload: {
              claim_id: 'claim-action-draft',
              claim_no: 'A12345678',
              status: 'draft'
            }
          }
        }
      }
    }
  }

  const payload = await executeStewardAction({
    action_type: 'save_application_draft',
    message: '保存草稿',
    task: {
      task_id: 'task-app-1',
      task_type: 'expense_application'
    }
  })

  assert.equal(capturedUrl, '/api/v1/steward/actions/execute')
  assert.equal(capturedOptions.method, 'POST')
  assert.equal(JSON.parse(capturedOptions.body).action_type, 'save_application_draft')
  assert.equal(payload.status, 'succeeded')
}

async function run() {
  await testExecuteStewardActionUsesActionEndpoint()
  console.log('steward actions service tests passed')
}

run().catch((error) => {
  console.error(error)
  process.exit(1)
})
