This commit is contained in:
@@ -118,26 +118,29 @@ async function run() {
|
||||
const originalNonce = requests[requests.length - 1].header['X-Nonce']
|
||||
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||
|
||||
// 4510 不清会话、不跳登录页,只弹一次弹窗,并挂起 30 秒兜底定时器
|
||||
// 4510 不清会话、不跳登录页,只弹一次弹窗,并挂起 3 分钟兜底定时器(30 秒内不会因超时结算)
|
||||
assert.strictEqual(clearedSessions, 2)
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index'])
|
||||
assert.strictEqual(modals.length, 1)
|
||||
assert.strictEqual(modals[0].confirmText, '查看并同意')
|
||||
assert.strictEqual(modals[0].cancelText, '稍后')
|
||||
assert.strictEqual(modals[0].content, agreementMessage)
|
||||
assert.strictEqual(pendingTimers(30000).length, 1)
|
||||
assert.strictEqual(pendingTimers(180000).length, 1)
|
||||
// 超时已放宽到 3 分钟,不再存在 30 秒定时器;切后台(onHide)不会额外结算,
|
||||
// 因此 30 秒内无论用户是否切后台,挂起请求都保持 pending。
|
||||
assert.strictEqual(pendingTimers(30000).length, 0)
|
||||
|
||||
// 查看并同意:进入协议页,挂起期间定时器仍在跑
|
||||
modals[0].success({ confirm: true, cancel: false })
|
||||
assert.deepStrictEqual(navigateToPages, ['/pages/legal/index?mode=accept'])
|
||||
assert.strictEqual(clearedSessions, 2)
|
||||
assert.strictEqual(pendingTimers(30000).length, 1)
|
||||
assert.strictEqual(pendingTimers(180000).length, 1)
|
||||
|
||||
// 同意后自动重放首个被拦请求一次:取走时清定时器、重新生成 nonce,原调用方无感
|
||||
const requestsBeforeRetry = requests.length
|
||||
request.retryPendingAfterAgreement()
|
||||
assert.strictEqual(requests.length, requestsBeforeRetry + 1)
|
||||
assert.strictEqual(pendingTimers(30000).length, 0)
|
||||
assert.strictEqual(pendingTimers(180000).length, 0)
|
||||
const retriedRequest = requests[requests.length - 1]
|
||||
assert.strictEqual(retriedRequest.url.includes('/manager/orders'), true)
|
||||
assert.deepStrictEqual(retriedRequest.data, { page: 1 })
|
||||
@@ -160,14 +163,14 @@ async function run() {
|
||||
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||
assert.strictEqual(modals.length, 2)
|
||||
const oldErrorPromise = oldRequest.promise.catch(error => error)
|
||||
assert.strictEqual(pendingTimers(30000).length, 1)
|
||||
assert.strictEqual(pendingTimers(180000).length, 1)
|
||||
|
||||
const newRequest = startRequest({ url: '/manager/customers', method: 'POST', data: { page: 3 } })
|
||||
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||
assert.strictEqual(modals.length, 2)
|
||||
const oldError = await oldErrorPromise
|
||||
assertAgreementPendingError(oldError)
|
||||
assert.strictEqual(pendingTimers(30000).length, 1)
|
||||
assert.strictEqual(pendingTimers(180000).length, 1)
|
||||
|
||||
modals[1].success({ confirm: true, cancel: false })
|
||||
assert.strictEqual(navigateToPages.length, 2)
|
||||
@@ -188,26 +191,28 @@ async function run() {
|
||||
assert.strictEqual(modals.length, 3)
|
||||
const backErrorPromise = backRequest.promise.catch(error => error)
|
||||
modals[2].success({ confirm: true, cancel: false })
|
||||
assert.strictEqual(pendingTimers(30000).length, 1)
|
||||
assert.strictEqual(pendingTimers(180000).length, 1)
|
||||
request.discardPendingAgreement()
|
||||
const backError = await backErrorPromise
|
||||
assertAgreementPendingError(backError)
|
||||
assert.strictEqual(pendingTimers(30000).length, 0)
|
||||
assert.strictEqual(pendingTimers(180000).length, 0)
|
||||
const requestsBeforeBackRetry = requests.length
|
||||
request.retryPendingAfterAgreement()
|
||||
assert.strictEqual(requests.length, requestsBeforeBackRetry)
|
||||
|
||||
// 30 秒超时:自动 reject,之后用户再同意也不会重放
|
||||
// 3 分钟超时兜底:只有真的超时才会 reject;超时后用户再同意只记录同意,不再自动重放
|
||||
auth.setSession({ accessToken: 'timeout-token' }, true)
|
||||
const timedRequest = startRequest({ url: '/manager/orders', method: 'POST', data: { page: 5 } })
|
||||
respondLatest(200, { code: 4510, message: agreementMessage })
|
||||
assert.strictEqual(modals.length, 4)
|
||||
const timedErrorPromise = timedRequest.promise.catch(error => error)
|
||||
assert.strictEqual(pendingTimers(30000).length, 1)
|
||||
runTimer(30000)
|
||||
assert.strictEqual(pendingTimers(180000).length, 1)
|
||||
// 3 分钟以内不结算:模拟用户在协议页停留/切后台 30 秒,仍有 2 分 30 秒余量。
|
||||
assert.strictEqual(pendingTimers(30000).length, 0)
|
||||
runTimer(180000)
|
||||
const timedError = await timedErrorPromise
|
||||
assertAgreementPendingError(timedError)
|
||||
assert.strictEqual(pendingTimers(30000).length, 0)
|
||||
assert.strictEqual(pendingTimers(180000).length, 0)
|
||||
modals[3].success({ confirm: true, cancel: false })
|
||||
const requestsBeforeTimeoutConfirm = requests.length
|
||||
request.retryPendingAfterAgreement()
|
||||
@@ -223,7 +228,7 @@ async function run() {
|
||||
const laterError = await laterPromise
|
||||
assertAgreementPendingError(laterError)
|
||||
assert.strictEqual(toasts[toasts.length - 1].title, '请先同意最新协议后再操作')
|
||||
assert.strictEqual(pendingTimers(30000).length, 0)
|
||||
assert.strictEqual(pendingTimers(180000).length, 0)
|
||||
assert.strictEqual(clearedSessions, 2)
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index'])
|
||||
|
||||
@@ -234,6 +239,18 @@ async function run() {
|
||||
assert.strictEqual(pendingTimers(700).length, 1)
|
||||
runTimer(700)
|
||||
assert.deepStrictEqual(navigations, ['/pages/login/index', '/pages/login/index', '/pages/login/index'])
|
||||
|
||||
// 文案映射:内部错误 message 保持技术语义,展示层由公共提示工具统一映射为用户友好文案
|
||||
const mappedError = new Error('x')
|
||||
mappedError.code = AGREEMENT_NOT_ACCEPTED_CODE
|
||||
assert.strictEqual(mappedError.code, 'AGREEMENT_NOT_ACCEPTED')
|
||||
assert.strictEqual(request.resolveUserMessage(mappedError, '兜底文案'), '协议未确认,请稍后重试')
|
||||
assert.strictEqual(request.resolveUserMessage(new Error('业务失败'), '兜底文案'), '业务失败')
|
||||
assert.strictEqual(request.resolveUserMessage(null, '兜底文案'), '兜底文案')
|
||||
const { showErrorToast } = require('./toast')
|
||||
showErrorToast(mappedError, '兜底文案')
|
||||
assert.strictEqual(toasts[toasts.length - 1].title, '协议未确认,请稍后重试')
|
||||
assert.strictEqual(toasts[toasts.length - 1].icon, 'none')
|
||||
}
|
||||
|
||||
run().catch(error => {
|
||||
|
||||
Reference in New Issue
Block a user