2026-10-02 21:36:10 +08:00
|
|
|
import assert from 'node:assert/strict'
|
|
|
|
|
import test from 'node:test'
|
|
|
|
|
import { readFileSync } from 'node:fs'
|
|
|
|
|
import ts from 'typescript'
|
|
|
|
|
import * as vue from 'vue'
|
|
|
|
|
import * as pinia from 'pinia'
|
2026-10-15 19:39:35 +08:00
|
|
|
import { requireOrderProgress } from '../utils/orderProgress.ts'
|
2026-10-02 21:36:10 +08:00
|
|
|
|
|
|
|
|
function compile(path, dependencies) {
|
|
|
|
|
const source = readFileSync(new URL(path, import.meta.url), 'utf8')
|
|
|
|
|
const code = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
|
|
|
|
|
const exports = {}
|
|
|
|
|
new Function('require', 'exports', code)(id => { assert.ok(id in dependencies, id); return dependencies[id] }, exports)
|
|
|
|
|
return exports
|
|
|
|
|
}
|
|
|
|
|
function harness(t) {
|
|
|
|
|
const context = pinia.createPinia(); pinia.setActivePinia(context)
|
|
|
|
|
t.after(() => pinia.disposePinia(context))
|
|
|
|
|
const calls = []
|
|
|
|
|
const behavior = { failWrite: false, failRead: false, version: 9, status: 'DRAFT' }
|
|
|
|
|
const api = compile('../api/firstLeg.ts', { '@/api/http': { apiRequest: async config => {
|
|
|
|
|
calls.push(structuredClone(config))
|
|
|
|
|
if (config.method === 'GET') {
|
|
|
|
|
if (behavior.failRead) throw new Error('读取失败')
|
2026-10-03 00:16:10 +08:00
|
|
|
return { order: { id: 'one', orderNo: 'FL-ONE', version: behavior.version, status: behavior.status, receivingWarehouseId: 'warehouse', receivingWarehouseAddress: behavior.receivingWarehouseAddress, destinationCountryCode: 'US' }, cargoes: [] }
|
2026-10-02 21:36:10 +08:00
|
|
|
}
|
2026-10-02 23:08:58 +08:00
|
|
|
if (behavior.failWrite || (behavior.failSubmit && config.url.endsWith('/submit'))) throw new Error('响应丢失')
|
|
|
|
|
if (config.url.endsWith('/submit') && behavior.failSubmitRead) behavior.failRead = true
|
|
|
|
|
if (config.method === 'POST' && config.url === '/merchant/v1/orders/first-leg') return 'one'
|
2026-10-02 21:36:10 +08:00
|
|
|
} } })
|
|
|
|
|
const module = compile('./firstLegOrders.ts', {
|
|
|
|
|
vue, pinia,
|
|
|
|
|
'@/modules/first-leg/api/firstLeg': api,
|
2026-10-02 22:26:42 +08:00
|
|
|
'@/modules/first-leg/api/unifiedOrders': {
|
2026-10-15 19:39:35 +08:00
|
|
|
getMerchantUnifiedOrder: async () => ({ progress: { orderId: 'one', stage: 'none', partial: false }, summary: { orderId: 'one', mainStatus: null }, order: { order: { id: 'one', status: behavior.status, version: behavior.version, receivingWarehouseId: 'warehouse', receivingWarehouseAddress: behavior.receivingWarehouseAddress } }, timeline: [] }),
|
2026-10-02 22:26:42 +08:00
|
|
|
},
|
2026-10-02 21:36:10 +08:00
|
|
|
'@/modules/first-leg/utils/firstLegOrder': { canClientEditFirstLegOrder: status => status === 'DRAFT', getFirstLegStatusLabel: value => value },
|
|
|
|
|
'@/modules/first-leg/utils/warehousePagination': {},
|
2026-10-15 19:39:35 +08:00
|
|
|
'@/modules/first-leg/utils/orderProgress': { requireOrderProgress },
|
2026-10-02 21:36:10 +08:00
|
|
|
})
|
|
|
|
|
const store = module.useFirstLegOrdersStore()
|
|
|
|
|
store.warehouses = [{ id: 'warehouse', name: '国内仓', warehouseType: 'DOMESTIC' }]
|
|
|
|
|
const draft = { receivingWarehouseId: 'warehouse', destinationType: 'PRIVATE', destinationCountryCode: 'US', items: [], customerRemark: '原草稿' }
|
|
|
|
|
return { store, api, calls, behavior, draft }
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 00:16:10 +08:00
|
|
|
function displayedWarehouseAddress(order) {
|
|
|
|
|
const view = readFileSync(new URL('../views/FirstLegOrdersView.vue', import.meta.url), 'utf8')
|
|
|
|
|
const expressions = [...view.matchAll(/\{\{ (detailOrder\.receivingWarehouseSnapshot\.(?:province|city|address)) \}\}/g)]
|
|
|
|
|
assert.equal(expressions.length, 3)
|
|
|
|
|
return expressions.map(([, expression]) => new Function('detailOrder', `return ${expression}`)(order)).join('')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test('complete address is identical for cold unified, warm unified, raw detail and remark-only save', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
const fullAddress = 'CN 浙江省 义乌市 仓库路 8 号 322000'
|
|
|
|
|
h.behavior.receivingWarehouseAddress = fullAddress
|
|
|
|
|
h.store.warehouses = []
|
|
|
|
|
const cold = await h.store.loadDetail('one')
|
|
|
|
|
assert.equal(displayedWarehouseAddress(cold), fullAddress)
|
|
|
|
|
h.store.warehouses = [{ id: 'warehouse', name: '国内仓', warehouseType: 'DOMESTIC', province: '浙江省', city: '义乌市', address: '主档已更新的地址' }]
|
|
|
|
|
h.store.orders = []
|
|
|
|
|
const warm = await h.store.loadDetail('one')
|
|
|
|
|
const raw = await h.store.loadDraftDetail('one')
|
|
|
|
|
for (const detail of [warm, raw]) {
|
|
|
|
|
assert.equal(displayedWarehouseAddress(detail), fullAddress)
|
|
|
|
|
assert.equal(detail.receivingWarehouseSnapshot.province, '')
|
|
|
|
|
assert.equal(detail.receivingWarehouseSnapshot.city, '')
|
|
|
|
|
}
|
|
|
|
|
const session = h.store.beginDraftEdit(raw)
|
|
|
|
|
h.draft.customerRemark = '仅修改备注'
|
|
|
|
|
const saved = await h.store.saveDraft(h.draft, '客户', 'one', session)
|
|
|
|
|
assert.equal(displayedWarehouseAddress(saved), fullAddress)
|
|
|
|
|
const write = h.calls.find(call => call.method === 'PUT')
|
|
|
|
|
assert.equal(write.data.draft.customerRemark, '仅修改备注')
|
|
|
|
|
assert.equal(Object.hasOwn(write.data.draft, 'receivingWarehouseAddress'), false)
|
|
|
|
|
assert.equal(h.behavior.receivingWarehouseAddress, fullAddress)
|
|
|
|
|
assert.equal(h.store.warehouses[0].province, '浙江省')
|
|
|
|
|
assert.equal(h.store.warehouses[0].address, '主档已更新的地址')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('structured master address stays structured when no order address snapshot exists', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
h.store.warehouses = [{ id: 'warehouse', name: '国内仓', province: '浙江省', city: '义乌市', address: '仓库路 8 号', warehouseType: 'DOMESTIC' }]
|
|
|
|
|
const detail = await h.store.loadDetail('one')
|
|
|
|
|
assert.equal(detail.receivingWarehouseSnapshot.province, '浙江省')
|
|
|
|
|
assert.equal(detail.receivingWarehouseSnapshot.city, '义乌市')
|
|
|
|
|
assert.equal(detail.receivingWarehouseSnapshot.address, '仓库路 8 号')
|
|
|
|
|
assert.equal(displayedWarehouseAddress(detail), '浙江省义乌市仓库路 8 号')
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-02 22:26:42 +08:00
|
|
|
test('raw and unified cancelled detail remain CANCELLED even when unified mainStatus is null', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
h.behavior.status = 'CANCELLED'
|
|
|
|
|
const raw = await h.store.loadDraftDetail('one')
|
|
|
|
|
assert.equal(raw.status, 'CANCELLED')
|
|
|
|
|
const unified = await h.store.loadDetail('one')
|
|
|
|
|
assert.equal(unified.status, 'CANCELLED')
|
|
|
|
|
h.behavior.status = 'APPROVED'
|
|
|
|
|
const unknown = await h.store.loadDetail('one')
|
|
|
|
|
assert.notEqual(unknown.status, 'CANCELLED', 'unknown summary must not be reclassified as cancellation')
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-02 23:08:58 +08:00
|
|
|
test('fresh direct draft detail preserves raw DRAFT with null unified summary and opens an edit session', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
assert.equal(h.store.orders.length, 0)
|
|
|
|
|
const direct = await h.store.loadDetail('one')
|
|
|
|
|
assert.equal(direct.status, 'DRAFT')
|
|
|
|
|
assert.equal(h.store.beginDraftEdit(direct).expectedVersion, 9)
|
|
|
|
|
h.store.orders = []
|
|
|
|
|
const refreshed = await h.store.loadDetail('one')
|
|
|
|
|
assert.equal(refreshed.status, 'DRAFT')
|
|
|
|
|
h.store.orders = []
|
|
|
|
|
h.behavior.status = 'APPROVED'
|
|
|
|
|
const unknown = await h.store.loadDetail('one')
|
|
|
|
|
assert.notEqual(unknown.status, 'DRAFT')
|
|
|
|
|
assert.notEqual(unknown.status, 'CANCELLED')
|
|
|
|
|
assert.throws(() => h.store.beginDraftEdit(unknown), /重新打开/)
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-02 22:26:42 +08:00
|
|
|
test('cancelled detail summary has no pending overseas synchronization entries', () => {
|
|
|
|
|
const source = readFileSync(new URL('../views/FirstLegOrdersView.vue', import.meta.url), 'utf8')
|
|
|
|
|
const script = source.match(/<script setup lang="ts">([\s\S]*?)<\/script>/)[1]
|
|
|
|
|
const ast = ts.createSourceFile('view.ts', script, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
|
|
|
|
|
const declaration = ast.statements.flatMap(node => ts.isVariableStatement(node) ? [...node.declarationList.declarations] : []).find(node => node.name.getText(ast) === 'progressSummaryItems')
|
|
|
|
|
const code = ts.transpileModule('const progressSummaryItems = ' + declaration.initializer.getText(ast), { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText
|
|
|
|
|
const summary = new Function('computed', 'detailOrder', code + '; return progressSummaryItems.value')(vue.computed, { value: { status: 'CANCELLED' } })
|
|
|
|
|
assert.deepEqual(summary, [])
|
|
|
|
|
})
|
|
|
|
|
|
2026-10-02 21:36:10 +08:00
|
|
|
test('edit freezes detail version rather than cached list version and replays same key/body after uncertain result', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
const original = await h.store.loadDraftDetail('one')
|
|
|
|
|
const session = h.store.beginDraftEdit(original)
|
2026-10-03 14:57:54 +08:00
|
|
|
h.store.orders = [{ ...original, version: 0 }]
|
2026-10-02 21:36:10 +08:00
|
|
|
h.behavior.failWrite = true
|
|
|
|
|
await assert.rejects(h.store.saveDraft(h.draft, '测试企业', 'one', session), /响应丢失/)
|
|
|
|
|
const first = h.calls.find(call => call.method === 'PUT')
|
|
|
|
|
assert.equal(first.data.expectedVersion, 9)
|
|
|
|
|
assert.equal(first.data.draft.customerRemark, '原草稿')
|
|
|
|
|
assert.ok(first.headers['Idempotency-Key'])
|
|
|
|
|
assert.deepEqual(h.calls.map(call => call.method), ['GET', 'PUT'], 'no pre-save version refresh')
|
|
|
|
|
h.behavior.failWrite = false; h.behavior.version = 10
|
|
|
|
|
const result = await h.store.saveDraft(h.draft, '测试企业', 'one', session)
|
|
|
|
|
const writes = h.calls.filter(call => call.method === 'PUT')
|
|
|
|
|
assert.deepEqual(writes[1], first)
|
|
|
|
|
assert.deepEqual(h.calls.map(call => call.method), ['GET', 'PUT', 'GET', 'PUT', 'GET'])
|
|
|
|
|
assert.equal(result.version, 10)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('changed retry payload or missing edit session fails closed without issuing a new write', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
await assert.rejects(h.store.saveDraft(h.draft, '客户', 'one'), /重新打开/)
|
|
|
|
|
assert.equal(h.calls.length, 0)
|
|
|
|
|
const session = h.store.beginDraftEdit(await h.store.loadDraftDetail('one'))
|
|
|
|
|
h.behavior.failWrite = true
|
|
|
|
|
await assert.rejects(h.store.saveDraft(h.draft, '客户', 'one', session))
|
|
|
|
|
h.draft.customerRemark = '不同内容'
|
|
|
|
|
await assert.rejects(h.store.saveDraft(h.draft, '客户', 'one', session), /重试原内容/)
|
|
|
|
|
assert.equal(h.calls.filter(call => call.method === 'PUT').length, 1)
|
|
|
|
|
assert.equal(session.expectedVersion, 9)
|
|
|
|
|
assert.ok(Object.isFrozen(session))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('read failure after successful edit preserves the same command for retry', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
const session = h.store.beginDraftEdit(await h.store.loadDraftDetail('one'))
|
|
|
|
|
h.behavior.failRead = true
|
|
|
|
|
await assert.rejects(h.store.saveDraft(h.draft, '客户', 'one', session), /读取失败/)
|
|
|
|
|
h.behavior.failRead = false
|
|
|
|
|
await h.store.saveDraft(h.draft, '客户', 'one', session)
|
|
|
|
|
const writes = h.calls.filter(call => call.method === 'PUT')
|
|
|
|
|
assert.deepEqual(writes[0], writes[1])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('cancel forwards version and reason without rejecting submitted orders locally; retry keeps key and body', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
h.behavior.status = 'SUBMITTED'
|
|
|
|
|
h.store.orders = [{ id: 'one', status: 'PENDING_REVIEW', version: 0 }]
|
|
|
|
|
h.behavior.failWrite = true
|
|
|
|
|
await assert.rejects(h.store.cancelOrder('one', 12, '客户要求取消'), /响应丢失/)
|
|
|
|
|
const first = h.calls[0]
|
|
|
|
|
assert.equal(first.method, 'POST')
|
|
|
|
|
assert.equal(first.url, '/merchant/v1/orders/first-leg/one/cancel')
|
|
|
|
|
assert.deepEqual(first.data, { expectedVersion: 12, reason: '客户要求取消' })
|
|
|
|
|
await assert.rejects(h.store.cancelOrder('one', 13, '其他原因'), /原版本和原因/)
|
|
|
|
|
assert.equal(h.calls.length, 1)
|
|
|
|
|
h.behavior.failWrite = false
|
|
|
|
|
await h.store.cancelOrder('one', 12, '客户要求取消')
|
|
|
|
|
assert.deepEqual(h.calls[2], first)
|
|
|
|
|
assert.equal(h.calls[1].method, 'GET')
|
|
|
|
|
assert.equal(h.store.orders.length, 0)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('cancel requires a detail version and a nonempty reason', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
await assert.rejects(h.store.cancelOrder('one', undefined, '理由'))
|
|
|
|
|
await assert.rejects(h.store.cancelOrder('one', 1, ' '))
|
|
|
|
|
assert.equal(h.calls.length, 0)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('cancellation decision reads raw server status and version including SUBMITTED', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
for (const status of ['DRAFT', 'SUBMITTED', 'APPROVED', 'ACCEPTED', 'REJECTED']) {
|
|
|
|
|
h.behavior.status = status
|
|
|
|
|
const target = await h.store.prepareCancellation('one')
|
|
|
|
|
assert.equal(target.expectedVersion, 9)
|
|
|
|
|
assert.equal(target.businessNo, 'FL-ONE')
|
|
|
|
|
}
|
|
|
|
|
for (const status of ['PROCESSING', 'COMPLETED', 'CANCELLED']) {
|
|
|
|
|
h.behavior.status = status
|
|
|
|
|
await assert.rejects(h.store.prepareCancellation('one'), /不允许取消/)
|
|
|
|
|
}
|
|
|
|
|
assert.ok(h.calls.every(call => call.method === 'GET'))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
function cancellationViewHarness() {
|
|
|
|
|
const source = readFileSync(new URL('../views/FirstLegOrdersView.vue', import.meta.url), 'utf8').match(/<script setup lang="ts">([\s\S]*?)<\/script>/)[1]
|
|
|
|
|
const ast = ts.createSourceFile('view.ts', source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
|
|
|
|
|
const functions = ast.statements.filter(node => ts.isFunctionDeclaration(node) && ['canOfferCancellation', 'cancelOrder'].includes(node.name?.text)).map(node => node.getText(ast)).join('\n')
|
|
|
|
|
const code = ts.transpileModule(functions, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText
|
|
|
|
|
const calls = [], messages = [], prompts = []
|
|
|
|
|
const canCancelOrder = vue.ref(true), cancellingOrderId = vue.ref(''), pendingCancellation = vue.shallowRef()
|
|
|
|
|
class ApiError extends Error { constructor() { super('订单版本已变化'); this.code = 'A0409'; this.status = 409 } }
|
|
|
|
|
const behavior = { error: null }
|
|
|
|
|
const store = {
|
|
|
|
|
prepareCancellation: async id => { calls.push(['prepare', id]); return { id, expectedVersion: 12, businessNo: 'FL-ONE', statusLabel: '审核中' } },
|
|
|
|
|
cancelOrder: async (...args) => { calls.push(['cancel', ...args]); if (behavior.error) throw behavior.error },
|
|
|
|
|
load: async query => calls.push(['load', query]),
|
|
|
|
|
}
|
|
|
|
|
const ElMessageBox = {
|
|
|
|
|
prompt: async (message, title, options) => { prompts.push({ message, title, options }); return { value: '客户申请取消' } },
|
|
|
|
|
confirm: async (...args) => calls.push(['confirm', ...args]),
|
|
|
|
|
}
|
|
|
|
|
const ElMessage = { error: message => messages.push(message), warning: message => messages.push(message), success: message => messages.push(message) }
|
|
|
|
|
const globals = { canCancelOrder, cancellingOrderId, pendingCancellation, store, ElMessageBox, ElMessage, ApiError,
|
2026-10-03 14:57:54 +08:00
|
|
|
loadOrderPage: async () => calls.push(['load']),
|
2026-10-02 21:36:10 +08:00
|
|
|
isDetailPage: vue.ref(true), router: { push: async route => calls.push(['route', route]) } }
|
|
|
|
|
const handler = new Function(...Object.keys(globals), code + '\nreturn cancelOrder')(...Object.values(globals))
|
|
|
|
|
return { handler, calls, messages, prompts, canCancelOrder, pendingCancellation, behavior, ApiError }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test('cancel UI uses mandatory-reason centered prompt and refreshes after successful submitted-order cancellation', async () => {
|
|
|
|
|
const h = cancellationViewHarness()
|
|
|
|
|
await h.handler({ id: 'one', status: 'PENDING_REVIEW' })
|
|
|
|
|
assert.match(h.prompts[0].message, /FL-ONE.*审核中/)
|
|
|
|
|
const validate = h.prompts[0].options.inputValidator
|
|
|
|
|
assert.notEqual(validate(' '), true)
|
|
|
|
|
assert.notEqual(validate('x'.repeat(501)), true)
|
|
|
|
|
assert.equal(validate('合理原因'), true)
|
|
|
|
|
assert.deepEqual(h.calls.map(call => call[0]), ['prepare', 'cancel', 'load', 'route'])
|
|
|
|
|
assert.deepEqual(h.calls[1], ['cancel', 'one', 12, '客户申请取消'])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('cancel UI keeps an uncertain command for explicit retry, but conflict requires a fresh decision', async () => {
|
|
|
|
|
const h = cancellationViewHarness()
|
|
|
|
|
const order = { id: 'one', status: 'DRAFT' }
|
|
|
|
|
h.behavior.error = new Error('网络无响应')
|
|
|
|
|
await h.handler(order)
|
|
|
|
|
assert.equal(h.pendingCancellation.value.expectedVersion, 12)
|
|
|
|
|
h.behavior.error = null
|
|
|
|
|
await h.handler(order)
|
|
|
|
|
assert.equal(h.prompts.length, 1)
|
|
|
|
|
assert.equal(h.calls.filter(call => call[0] === 'prepare').length, 1)
|
|
|
|
|
assert.equal(h.calls.filter(call => call[0] === 'confirm').length, 1)
|
|
|
|
|
assert.deepEqual(h.calls.filter(call => call[0] === 'cancel')[0], h.calls.filter(call => call[0] === 'cancel')[1])
|
|
|
|
|
h.behavior.error = new h.ApiError()
|
|
|
|
|
await h.handler(order)
|
|
|
|
|
assert.equal(h.pendingCancellation.value, undefined)
|
|
|
|
|
assert.match(h.messages.at(-1), /刷新订单后重新确认/)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('cancel UI blocks unavailable permission and irreversible visible stages before reads or writes', async () => {
|
|
|
|
|
const h = cancellationViewHarness()
|
|
|
|
|
h.canCancelOrder.value = false
|
|
|
|
|
await h.handler({ id: 'one', status: 'DRAFT' })
|
|
|
|
|
h.canCancelOrder.value = true
|
|
|
|
|
await h.handler({ id: 'one', status: 'LOADED' })
|
|
|
|
|
assert.equal(h.calls.length, 0)
|
|
|
|
|
assert.equal(h.prompts.length, 0)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('both editor entry points capture detail sessions and both save paths pass them', () => {
|
|
|
|
|
const view = readFileSync(new URL('../views/FirstLegOrdersView.vue', import.meta.url), 'utf8')
|
|
|
|
|
assert.equal((view.match(/editingSession\.value = store\.beginDraftEdit\(detailedOrder\)/g) ?? []).length, 2)
|
2026-10-02 23:08:58 +08:00
|
|
|
assert.equal((view.match(/store\.saveDraft(?:ForSubmission)?\(cloneDraft\(form\), tenantStore\.company\.companyName, editingId\.value, editingSession\.value\)/g) ?? []).length, 2)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('submit reads raw detail version rather than list zero and replays original key and version after timeout', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
h.store.orders = [{ id: 'one', status: 'DRAFT', version: 0 }]
|
|
|
|
|
h.behavior.failSubmit = true
|
|
|
|
|
await assert.rejects(h.store.submitSaved('one', 'ACTIVE'), /响应丢失/)
|
|
|
|
|
const first = h.calls[1]
|
|
|
|
|
assert.equal(first.url, '/merchant/v1/orders/first-leg/one/submit')
|
|
|
|
|
assert.deepEqual(first.data, { expectedVersion: 9 })
|
|
|
|
|
assert.ok(first.headers['Idempotency-Key'])
|
|
|
|
|
h.behavior.version = 10; h.behavior.status = 'SUBMITTED'; h.behavior.failSubmit = false
|
|
|
|
|
await h.store.submitSaved('one', 'ACTIVE')
|
|
|
|
|
assert.deepEqual(h.calls[3], first)
|
|
|
|
|
assert.deepEqual(h.calls.map(call => call.method), ['GET', 'POST', 'GET', 'POST', 'GET'])
|
|
|
|
|
assert.equal(h.store.hasPendingSubmission('one'), false)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('submit refuses missing version or non-draft first decision and preserves command after failed readback', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
for (const version of [undefined, -1, 1.5]) {
|
|
|
|
|
h.behavior.version = version
|
|
|
|
|
await assert.rejects(h.store.submitSaved('one'), /版本缺失/)
|
|
|
|
|
}
|
|
|
|
|
h.behavior.version = 0; h.behavior.status = 'SUBMITTED'
|
|
|
|
|
await assert.rejects(h.store.submitSaved('one'), /不是草稿/)
|
|
|
|
|
assert.ok(h.calls.every(call => call.method === 'GET'))
|
|
|
|
|
h.behavior.status = 'DRAFT'; h.behavior.failSubmitRead = true
|
|
|
|
|
await assert.rejects(h.store.submitSaved('one'), /读取失败/)
|
|
|
|
|
assert.equal(h.store.hasPendingSubmission('one'), true)
|
|
|
|
|
h.behavior.failRead = false; h.behavior.failSubmitRead = false
|
|
|
|
|
await h.store.submitSaved('one')
|
|
|
|
|
const posts = h.calls.filter(call => call.method === 'POST')
|
|
|
|
|
assert.deepEqual(posts[0], posts[1])
|
|
|
|
|
assert.deepEqual(posts[0].data, { expectedVersion: 0 }, 'raw detail zero is valid')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('save then submit retries without another save and changed form requires an explicit new decision', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
const session = h.store.beginDraftEdit(await h.store.loadDraftDetail('one'))
|
|
|
|
|
await h.store.saveDraftForSubmission(h.draft, '客户', 'one', session)
|
|
|
|
|
h.behavior.failSubmit = true
|
|
|
|
|
await assert.rejects(h.store.submitSaved('one'))
|
|
|
|
|
const first = h.calls.find(call => call.url.endsWith('/submit'))
|
|
|
|
|
await h.store.saveDraftForSubmission(h.draft, '客户', 'one', session)
|
|
|
|
|
assert.equal(h.calls.filter(call => call.method === 'PUT').length, 1)
|
|
|
|
|
h.draft.customerRemark = '新决定'
|
|
|
|
|
await assert.rejects(h.store.saveDraftForSubmission(h.draft, '客户', 'one', session), /重新编辑/)
|
|
|
|
|
h.behavior.status = 'SUBMITTED'
|
|
|
|
|
await assert.rejects(h.store.beginNewSubmissionEdit('one'), /不能重新编辑/)
|
|
|
|
|
assert.equal(h.store.hasPendingSubmission('one'), true)
|
|
|
|
|
h.behavior.status = 'DRAFT'; h.behavior.version = 12
|
|
|
|
|
const newSession = await h.store.beginNewSubmissionEdit('one')
|
|
|
|
|
assert.equal(newSession.expectedVersion, 12)
|
|
|
|
|
await h.store.saveDraftForSubmission(h.draft, '客户', 'one', newSession)
|
|
|
|
|
h.behavior.version = 13; h.behavior.failSubmit = false
|
|
|
|
|
await h.store.submitSaved('one')
|
|
|
|
|
const posts = h.calls.filter(call => call.url.endsWith('/submit'))
|
|
|
|
|
assert.notEqual(posts[1].headers['Idempotency-Key'], first.headers['Idempotency-Key'])
|
|
|
|
|
assert.deepEqual(posts[1].data, { expectedVersion: 13 })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('failed initial submit read does not resave the prepared draft on retry', async t => {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
const session = h.store.beginDraftEdit(await h.store.loadDraftDetail('one'))
|
|
|
|
|
await h.store.saveDraftForSubmission(h.draft, '客户', 'one', session)
|
|
|
|
|
h.behavior.failRead = true
|
|
|
|
|
await assert.rejects(h.store.submitSaved('one'))
|
|
|
|
|
await h.store.saveDraftForSubmission(h.draft, '客户', 'one', session)
|
|
|
|
|
assert.equal(h.calls.filter(call => call.method === 'PUT').length, 1)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
async function submissionViewHarness(t) {
|
|
|
|
|
const h = harness(t)
|
|
|
|
|
const source = readFileSync(new URL('../views/FirstLegOrdersView.vue', import.meta.url), 'utf8').match(/<script setup lang="ts">([\s\S]*?)<\/script>/)[1]
|
|
|
|
|
const ast = ts.createSourceFile('view.ts', source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
|
|
|
|
|
const functions = ast.statements.filter(node => ts.isFunctionDeclaration(node) && ['submitOrder', 'confirmSubmissionEdit', 'submitSelectedDraftOrders'].includes(node.name?.text)).map(node => node.getText(ast)).join('\n')
|
|
|
|
|
const code = ts.transpileModule(functions, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText
|
|
|
|
|
const messages = [], confirmations = []
|
|
|
|
|
const globals = {
|
|
|
|
|
store: h.store, saving: vue.ref(false), form: h.draft, cloneDraft: draft => structuredClone(draft),
|
|
|
|
|
editingId: vue.ref('one'), editingSession: vue.shallowRef(h.store.beginDraftEdit(await h.store.loadDraftDetail('one'))),
|
|
|
|
|
companySubmissionBlocked: vue.ref(false), companyAuditLoaded: vue.ref(true), tenantStore: { company: { companyName: '客户', auditStatus: 'ACTIVE' } },
|
|
|
|
|
validateEditorForm: async () => true, markEditorClean: () => {}, editorOpen: vue.ref(true), isCreatePage: vue.ref(false), loadPageData: async () => {},
|
|
|
|
|
batchSubmitting: vue.ref(false), selectedDraftOrders: vue.ref([{ id: 'one', version: 0 }]), pendingBatchSubmissionIds: vue.ref([]),
|
|
|
|
|
searchOrders: async () => { globals.selectedDraftOrders.value = [] }, appliedFilters: vue.ref({}),
|
|
|
|
|
ElMessageBox: { confirm: async message => { confirmations.push(message); if (h.behavior.cancelConfirm) throw 'cancel' } },
|
|
|
|
|
ElMessage: { error: message => messages.push(message), warning: message => messages.push(message), success: message => messages.push(message) },
|
|
|
|
|
}
|
|
|
|
|
const handlers = new Function(...Object.keys(globals), code + '\nreturn {submitOrder, submitSelectedDraftOrders}')(...Object.values(globals))
|
|
|
|
|
return { ...h, ...handlers, globals, messages, confirmations }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test('real editor submit handler retries original submission without PUT and requires confirmation for changed form', async t => {
|
|
|
|
|
const h = await submissionViewHarness(t)
|
|
|
|
|
h.behavior.failSubmit = true
|
|
|
|
|
await h.submitOrder()
|
|
|
|
|
await h.submitOrder()
|
|
|
|
|
assert.equal(h.calls.filter(call => call.method === 'PUT').length, 1)
|
|
|
|
|
const posts = h.calls.filter(call => call.url.endsWith('/submit'))
|
|
|
|
|
assert.deepEqual(posts[0], posts[1])
|
|
|
|
|
h.draft.customerRemark = '修改后的决定'
|
|
|
|
|
h.behavior.cancelConfirm = true
|
|
|
|
|
await h.submitOrder()
|
|
|
|
|
assert.equal(h.confirmations.length, 1)
|
|
|
|
|
assert.equal(h.calls.filter(call => call.method === 'PUT').length, 1)
|
|
|
|
|
h.behavior.cancelConfirm = false; h.behavior.failSubmit = false; h.behavior.version = 12
|
|
|
|
|
await h.submitOrder()
|
|
|
|
|
assert.equal(h.confirmations.length, 2)
|
|
|
|
|
assert.equal(h.calls.filter(call => call.method === 'PUT').length, 2)
|
|
|
|
|
const last = h.calls.filter(call => call.url.endsWith('/submit')).at(-1)
|
|
|
|
|
assert.deepEqual(last.data, { expectedVersion: 12 })
|
|
|
|
|
assert.notEqual(last.headers['Idempotency-Key'], posts[0].headers['Idempotency-Key'])
|
|
|
|
|
assert.equal(h.globals.saving.value, false)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('real batch handler retains uncertain IDs across list refresh and retries original commands even if now submitted', async t => {
|
|
|
|
|
const h = await submissionViewHarness(t)
|
|
|
|
|
h.behavior.failSubmit = true
|
|
|
|
|
await h.submitSelectedDraftOrders()
|
|
|
|
|
assert.deepEqual(h.globals.pendingBatchSubmissionIds.value, ['one'])
|
|
|
|
|
assert.deepEqual(h.globals.selectedDraftOrders.value, [])
|
|
|
|
|
h.behavior.failSubmit = false; h.behavior.status = 'SUBMITTED'; h.behavior.version = 10
|
|
|
|
|
await h.submitSelectedDraftOrders()
|
|
|
|
|
const posts = h.calls.filter(call => call.url.endsWith('/submit'))
|
|
|
|
|
assert.equal(posts.length, 2)
|
|
|
|
|
assert.deepEqual(posts[1], posts[0])
|
|
|
|
|
assert.deepEqual(posts[0].data, { expectedVersion: 9 })
|
|
|
|
|
assert.deepEqual(h.globals.pendingBatchSubmissionIds.value, [])
|
|
|
|
|
assert.equal(h.globals.batchSubmitting.value, false)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('real new-order handler creates once and retries the saved order submission with the original version', async t => {
|
|
|
|
|
const h = await submissionViewHarness(t)
|
|
|
|
|
h.globals.editingId.value = undefined; h.globals.editingSession.value = undefined
|
|
|
|
|
h.behavior.version = 0; h.behavior.failSubmit = true
|
|
|
|
|
await h.submitOrder()
|
|
|
|
|
assert.equal(h.globals.editingId.value, 'one')
|
|
|
|
|
h.behavior.version = 1; h.behavior.status = 'SUBMITTED'; h.behavior.failSubmit = false
|
|
|
|
|
await h.submitOrder()
|
|
|
|
|
assert.equal(h.calls.filter(call => call.method === 'POST' && call.url === '/merchant/v1/orders/first-leg').length, 1)
|
|
|
|
|
assert.equal(h.calls.filter(call => call.method === 'PUT').length, 0)
|
|
|
|
|
const posts = h.calls.filter(call => call.url.endsWith('/submit'))
|
|
|
|
|
assert.deepEqual(posts[0], posts[1])
|
|
|
|
|
assert.deepEqual(posts[0].data, { expectedVersion: 0 })
|
2026-10-02 21:36:10 +08:00
|
|
|
})
|