quotationSubmit.workflow.test.mjs 4.97 KB
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { describe, it } from 'node:test'
import { fileURLToPath } from 'node:url'
import {
  classifyQuotationResponse,
  submitStandardQuotation
} from '../pages/carQuotation/services/standardQuotationSubmit.workflow.js'
import { submitSimpleQuotation } from '../pages/carQuotation/services/simpleQuotationSubmit.workflow.js'

const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')

it('quotation pages do not use an async Promise executor', () => {
  for (const relativePath of [
    'packageEquity/pages/carQuotation/createOrderCarQuotation/createOrderCarQuotation.js',
    'packageEquity/pages/carQuotation/createOrderCarQuotationSimple/createOrderCarQuotationSimple.js'
  ]) {
    assert.doesNotMatch(
      readFileSync(path.join(projectRoot, relativePath), 'utf8'),
      /new\s+Promise\s*\(\s*async\b/,
      relativePath
    )
  }
})

it('quotation pages consume workflow status before navigating', () => {
  const standard = readFileSync(
    path.join(
      projectRoot,
      'packageEquity/pages/carQuotation/createOrderCarQuotation/createOrderCarQuotation.js'
    ),
    'utf8'
  )
  const simple = readFileSync(
    path.join(
      projectRoot,
      'packageEquity/pages/carQuotation/createOrderCarQuotationSimple/createOrderCarQuotationSimple.js'
    ),
    'utf8'
  )
  assert.match(standard, /outcome\.status\s*!==\s*['"]success['"]/)
  assert.match(standard, /return\s+submitStandardQuotation\s*\(/)
  assert.match(simple, /submitOutcome\.status\s*!==\s*['"]success['"]/)
  assert.match(simple, /status:\s*submitOutcome\.status/)
})

describe('standardQuotationSubmit.workflow', () => {
  it('keeps add payload identity and classifies a string zero as success', async () => {
    const payload = { owner: { owner: '张三' }, check: true }
    let received
    const result = await submitStandardQuotation(
      { payload, isEdit: false },
      {
        quotationAdd: async value => {
          received = value
          return { code: '0', data: 101 }
        },
        updateQuotation: async () => assert.fail('must not update')
      }
    )

    assert.equal(received, payload)
    assert.deepEqual(result, {
      status: 'success',
      response: { code: '0', data: 101 }
    })
  })

  it('selects update only for edit and propagates API rejection', async () => {
    const rejection = new Error('network unavailable')
    await assert.rejects(
      submitStandardQuotation(
        { payload: { id: 9 }, isEdit: true },
        {
          quotationAdd: async () => assert.fail('must not add'),
          updateQuotation: async () => {
            throw rejection
          }
        }
      ),
      rejection
    )
  })

  it('does not leave a rejected request pending', async () => {
    const settled = await Promise.race([
      submitStandardQuotation(
        { payload: {}, isEdit: false },
        {
          quotationAdd: async () => {
            throw new Error('boom')
          },
          updateQuotation: async () => ({ code: 0 })
        }
      ).then(
        () => 'resolved',
        () => 'rejected'
      ),
      new Promise(resolve => setTimeout(() => resolve('timeout'), 50))
    ])

    assert.equal(settled, 'rejected')
  })

  it('classifies non-zero and missing responses as business errors', () => {
    assert.deepEqual(classifyQuotationResponse({ code: 2, message: '失败' }), {
      ok: false,
      code: 2,
      message: '失败',
      data: undefined
    })
    assert.deepEqual(classifyQuotationResponse(undefined), {
      ok: false,
      code: undefined,
      message: '报价失败',
      data: undefined
    })
  })
})

describe('simpleQuotationSubmit.workflow', () => {
  for (const [action, expectedDependency] of [
    ['add', 'quotationAdd'],
    ['update', 'updateQuotation'],
    ['correct', 'errorOrderById']
  ]) {
    it(`selects ${expectedDependency} for ${action} and preserves payload identity`, async () => {
      const payload = { id: 7, marker: action }
      const calls = []
      const dependencies = Object.fromEntries(
        ['quotationAdd', 'updateQuotation', 'errorOrderById'].map(name => [
          name,
          async value => {
            calls.push([name, value])
            return { code: 0, data: 7 }
          }
        ])
      )

      const result = await submitSimpleQuotation(
        { action, payload },
        dependencies
      )

      assert.deepEqual(calls, [[expectedDependency, payload]])
      assert.equal(result.status, 'success')
    })
  }

  it('rejects an unsupported action before invoking an API', async () => {
    await assert.rejects(
      submitSimpleQuotation(
        { action: 'unknown', payload: {} },
        {
          quotationAdd: async () => assert.fail('must not add'),
          updateQuotation: async () => assert.fail('must not update'),
          errorOrderById: async () => assert.fail('must not correct')
        }
      ),
      /Unsupported simple quotation action/
    )
  })
})