fix(customs): expose clearance query failure with scoped retry

This commit is contained in:
2026-10-03 02:35:28 +08:00
parent 81a2e6473d
commit b1f538b3ac
2 changed files with 108 additions and 8 deletions
@@ -7,7 +7,7 @@ import * as vue from 'vue'
function deferred() { let resolve, reject; const promise = new Promise((yes, no) => { resolve = yes; reject = no }); return { promise, resolve, reject } }
function harness(t) {
const source = readFileSync(new URL('./CustomsDocumentsDialog.vue', import.meta.url), 'utf8').match(/<script setup lang="ts">([\s\S]*?)<\/script>/)[1]
const code = ts.transpileModule(source + '\nexport { loadDetail, detail, clearanceResults, loading, errorMessage, selectedInboundIds }', { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
const code = ts.transpileModule(source + '\nexport { loadDetail, detail, clearanceResults, loading, errorMessage, selectedInboundIds, clearanceError, clearanceLoading, retryClearanceResults, close }', { compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 } }).outputText
const props = vue.reactive({ modelValue: false, orderId: 'A', canEdit: true }), requests = [], clearances = [], updates = [], view = {}
const unmounts = []
const dependencies = { vue: { ...vue, onBeforeUnmount: fn => unmounts.push(fn) }, 'element-plus': { ElMessage: {} }, '@/modules/first-leg/api/customs': {
@@ -69,3 +69,62 @@ test('unmount invalidates pending detail requests', async t => {
h.requests[0].resolve(detail('A')); await tick()
assert.equal(h.clearances.length, 0); assert.equal(h.updates.length, 0); assert.equal(h.view.detail.value, null)
})
test('result failure retains documents and exposes error; retry queries only results and deduplicates clicks', async t => {
const h = harness(t); h.props.modelValue = true; await tick()
h.requests[0].resolve({ ...detail('A'), documents: [{ documentId: 'document-A' }] }); await tick()
h.clearances[0].reject(new Error('结果服务暂不可用')); await tick()
assert.equal(h.view.errorMessage.value, '')
assert.equal(h.view.clearanceError.value, '结果服务暂不可用')
assert.equal(h.view.detail.value.documents[0].documentId, 'document-A')
assert.equal(h.view.loading.value, false)
const retry = h.view.retryClearanceResults()
await h.view.retryClearanceResults()
assert.equal(h.requests.length, 1); assert.equal(h.clearances.length, 2)
assert.equal(h.view.clearanceLoading.value, true)
h.clearances[1].resolve([{ id: 'result-A' }]); await retry
assert.equal(h.view.clearanceError.value, '')
assert.deepEqual(h.view.clearanceResults.value, [{ id: 'result-A' }])
assert.equal(h.view.clearanceLoading.value, false)
})
test('normal open requests detail and results exactly once and distinguishes successful empty results', async t => {
const h = harness(t); h.props.modelValue = true; await tick()
h.requests[0].resolve(detail('A')); await tick(); h.clearances[0].resolve([]); await tick()
assert.equal(h.requests.length, 1); assert.equal(h.clearances.length, 1)
assert.equal(h.view.clearanceError.value, '')
assert.equal(h.view.clearanceLoading.value, false)
})
for (const transition of ['switch', 'close', 'unmount']) {
for (const outcome of ['resolve', 'reject']) {
test(`${transition} ignores stale result retry ${outcome}`, async t => {
const h = harness(t); h.props.modelValue = true; await tick()
h.requests[0].resolve(detail('A')); await tick(); h.clearances[0].reject(new Error('first failure')); await tick()
const retry = h.view.retryClearanceResults()
if (transition === 'switch') { h.props.orderId = 'B'; await tick() }
else if (transition === 'close') { h.view.close(); h.props.modelValue = false; await tick() }
else h.unmount()
if (outcome === 'resolve') h.clearances[1].resolve([{ id: 'old-A' }])
else h.clearances[1].reject(new Error('old retry failure'))
await retry
assert.equal(h.view.clearanceError.value, '')
assert.deepEqual(h.view.clearanceResults.value, [])
if (transition === 'switch') {
h.requests[1].resolve(detail('B')); await tick(); h.clearances[2].resolve([{ id: 'B' }]); await tick()
assert.deepEqual(h.view.clearanceResults.value, [{ id: 'B' }])
} else {
await h.view.retryClearanceResults()
assert.equal(h.clearances.length, 2)
}
})
}
}
test('result error and retry reuse existing section error styling before the empty state', () => {
const source = readFileSync(new URL('./CustomsDocumentsDialog.vue', import.meta.url), 'utf8')
assert.match(source, /v-if="clearanceError" class="customs-error" role="alert"/)
assert.match(source, /@click="retryClearanceResults"/)
assert.match(source, /v-else-if="clearanceLoading"/)
assert.match(source, /v-else-if="!clearanceResults.length"/)
})