Files
order_site/apps/frontend/tests/customer-fulfillment-bind-confirmed.test.ts
T
yml 7d7a996f7d 调整(微博绑定): 确认后同样引导截图联系客服,换绑冷却按平台天数并留证
- 客户页抽出统一的「绑定已确认」渲染:标题 + 「请截图本页,把订单号和这张截图发给客服」+
  可复制订单号;虎牙与微博共用同一段文案,删除微博原来的「客服正在安排 / 请耐心等待」。
- 微博换绑冷却:天数只取后端 `rebindCooldownDays`(来自平台 `rebind_remain_days`);
  冷却中且已有绑定角色时禁用「微信绑定 / QQ 绑定」并提示剩余天数,
  「刷新角色状态」不受影响。
- 微博绑定失败原因改为常驻展示(平台原因 + 任务 lastError 去重),
  冷却拒绝不再只弹一个瞬时 toast。
- 后端补留证:微博换绑被平台冷却拒绝时写 `last_error` 并追加
  `weibo_binding_rejected` 事件(channel + 原因),便于客服排障。
- 权威文档补微博第 ⑥ 条:冷却天数只认平台、拒绝需留证、确认后直接引导截图联系客服。
- 测试:新增微博冷却与共用渲染断言(前端 491 全绿),后端补微博留证回归断言(1552 全绿)。
2026-12-02 11:23:33 +08:00

187 lines
8.7 KiB
TypeScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import test from 'node:test'
const root = dirname(fileURLToPath(import.meta.url))
const source = readFileSync(
join(root, '../src/pages/customer-fulfillment/CustomerFulfillmentPage.tsx'),
'utf8',
)
const customerTypes = readFileSync(join(root, '../src/types/customer-fulfillment.ts'), 'utf8')
/** 取样一段源码,找不到时直接失败,避免测试静默通过。 */
function pickSlice(startMarker: string, endMarker: string) {
const start = source.indexOf(startMarker)
assert.ok(start >= 0, `源码缺少 ${startMarker}`)
const end = source.indexOf(endMarker, start)
assert.ok(end > start, `源码缺少 ${endMarker}`)
return source.slice(start, end)
}
test('确认成功区块引导客户截图并发给客服,同时给出可复制订单号', () => {
// 文案与订单号复制只写一份:两个平台共用同一个渲染函数。
const shared = pickSlice('const renderBindingConfirmedNotice = (options: {', 'const displayTitle')
assert.ok(shared.includes('绑定已确认'))
assert.ok(shared.includes('请截图本页'))
assert.ok(shared.includes('发给客服'))
assert.ok(shared.includes('订单号'))
// 订单号直接取页面已有的 orderNo / task.orderNo,不另建字段。
assert.ok(shared.includes('{orderNo || selectedTask?.orderNo || '))
assert.ok(shared.includes('copyOrderNoForSupport('))
assert.ok(shared.includes('{options.refreshLabel}'))
// 页面里「请截图本页」只出现一次,不能再复制出第二套文案。
assert.equal(source.split('请截图本页').length - 1, 1)
const huyaBlock = pickSlice(
"bindingDecision?.flowState === 'confirmed' && !profileEditing",
'{isWeibo &&',
)
assert.ok(huyaBlock.includes('renderBindingConfirmedNotice({'))
assert.ok(huyaBlock.includes("refreshLabel: '刷新订单状态'"))
assert.ok(!huyaBlock.includes('请截图本页'))
// 微博 confirmed 与其他平台完全同一个渲染,只换刷新按钮文案。
assert.equal(source.split('renderBindingConfirmedNotice({').length - 1, 2)
assert.ok(source.includes("refreshLabel: '刷新状态'"))
assert.ok(source.includes("selectedTask.bindingStatus === 'confirmed'"))
// 旧文案已被替换。
assert.ok(!source.includes('平台角色绑定已确认,客服正在处理订单'))
assert.ok(!source.includes('客服正在安排'))
assert.ok(!source.includes('请耐心等待'))
})
test('确认按钮不吞掉重新扫码与修改资料两个退回入口', () => {
const panel = pickSlice('className="customer-binding-role-actions"', '</div>')
const confirmIndex = panel.indexOf('确认绑定这个角色到虎牙')
assert.ok(confirmIndex > 0)
assert.ok(panel.indexOf('重新扫码换绑') > confirmIndex)
assert.ok(panel.indexOf('上一步修改资料') > confirmIndex)
assert.ok(panel.includes('onClick={() => void openProfileEditor()}'))
assert.ok(panel.includes('onClick={() => void handleRefreshBinding(true)}'))
// 不再用 canConfirmDecision ? confirm : 其它动作 的三元结构。
assert.ok(!panel.includes(') : ('))
})
test('页面任何位置都不渲染 40 位 role token', () => {
const confirmStart = source.indexOf('async function handleConfirmBinding()')
const confirmEnd = source.indexOf('async function openProfileEditor()')
assert.ok(confirmStart > 0 && confirmEnd > confirmStart)
const confirmFn = source.slice(confirmStart, confirmEnd)
// token 只用于「确认绑定」请求体,不参与展示。
assert.ok(confirmFn.includes('expectedRoleToken'))
const outside = source.slice(0, confirmStart) + source.slice(confirmEnd)
assert.ok(!outside.includes('bindGameRoleToken'))
assert.ok(!source.includes('>{bindingInfo?.bindGameRoleToken'))
assert.ok(!source.includes('>{selectedTask?.bindGameRoleToken'))
})
test('冷却天数只按后端返回值展示,为 0 时不出现「剩余约」', () => {
const needle = '剩余约'
let index = source.indexOf(needle)
assert.ok(index > 0, '冷却期文案应当存在')
let occurrences = 0
while (index > 0) {
occurrences += 1
const guard = source.slice(Math.max(0, index - 200), index)
assert.ok(
guard.includes('rebindCooldownDays > 0'),
'「剩余约」必须由 rebindCooldownDays > 0 守卫',
)
index = source.indexOf(needle, index + needle.length)
}
assert.ok(occurrences >= 1)
// 虎牙口径的冷却文案必须存在(客户页绑定阶段使用)。
assert.ok(source.includes('当前仍处于虎牙换绑冷却期,剩余约 ${rebindCooldownDays} 天'))
// 冷却天数不得由 platformChangeBindDay 推算:窗口天数只用于「每 N 天可换绑 1 次」说明。
const dayMarkers = source.split('platformChangeBindDay').length - 1
assert.ok(dayMarkers <= 3)
assert.ok(!source.includes('rebindCooldownDays + '))
})
test('首次绑定缺码时给出生成二维码主按钮,二维码待扫态可退回改资料', () => {
const initialBlock = pickSlice(
"bindingDecision?.flowState === 'initial_bind_required'",
'{bindingQrVisible && !bindingSessionExpired',
)
assert.ok(initialBlock.includes('生成绑定二维码'))
assert.ok(initialBlock.includes('handleRefreshBinding(true)'))
assert.ok(initialBlock.includes('bindingDecision.message'))
const qrStart = source.indexOf('customer-binding-qr-card customer-huya-qr-card')
const qrBlock = source.slice(qrStart, source.indexOf('绑定二维码已过期', qrStart))
assert.ok(qrBlock.includes('上一步修改资料'))
assert.ok(qrBlock.includes('openProfileEditor()'))
})
test('换绑冷却态提供只读刷新,并明确冷却后的下一步', () => {
const blocked = pickSlice('isRebindBlocked ? (', ') : showRebindFailure ? (')
assert.ok(blocked.includes('冷却结束后再回来点「重新扫码换绑」'))
assert.ok(blocked.includes('平台未返回下次可换绑时间,请联系客服核实'))
assert.ok(!blocked.includes('剩余约'))
// 冷却态没有换绑动作,只能用只读刷新按钮重新查状态。
assert.ok(
source.includes('const canRefreshBindingState = decisionCanRefreshBinding || isRebindBlocked'),
)
assert.ok(source.includes('刷新绑定状态'))
})
test('失败提示展示平台原因与任务异常且去重', () => {
assert.ok(source.includes('bindingInfo?.bindConfirmMessage'))
assert.ok(source.includes('selectedTask?.bindConfirmMessage'))
assert.ok(source.includes('bindingDecision?.message'))
assert.ok(source.includes('selectedTask?.lastError'))
assert.ok(source.includes('seen.has(text)'))
assert.ok(source.includes('bindingFailureReasons'))
// 任务 DTO 新增字段已进类型。
assert.ok(customerTypes.includes('bindConfirmState'))
assert.ok(customerTypes.includes('bindConfirmMessage'))
assert.ok(customerTypes.includes('lastError'))
})
test('first_bind_role_mismatch 走可自愈动作,不归到人工核实', () => {
// 首次绑定扫错角色仍由后端产出该状态,页面必须渲染它(含重新扫码 + 修改资料),
// 且不能把它当成「两侧信息不完整,只能联系客服」。
assert.ok(source.includes("'first_bind_role_mismatch'"))
assert.ok(source.includes('bindingRequiresSupport'))
assert.ok(!source.includes("['manual_review', 'first_bind_role_mismatch']"))
})
test('后端可能产出的每个 flowState 都在客户页有落点,不存在只提示无动作的死状态', () => {
const pageSource = readFileSync(
join(root, '../src/pages/customer-fulfillment/CustomerFulfillmentPage.tsx'),
'utf8',
)
const policySource = readFileSync(
join(root, '../../../apps/backend/src/services/semi-auto/huya-binding-policy.ts'),
'utf8',
)
const enumBlock = policySource.slice(
policySource.indexOf('export type HuyaBindingFlowState ='),
policySource.indexOf('export type HuyaBindingAction ='),
)
const flowStates = [...enumBlock.matchAll(/'([a-z_]+)'/g)].map((match) => match[1])
assert.ok(flowStates.length >= 15, '未解析到 flowState 枚举')
// 这些状态由其它区块承载:二维码卡片、已确认区块、过期提示、首查、初绑。
const handledElsewhere = new Set([
'binding_query_required',
'initial_bind_required',
'first_bind_qr_pending',
'rebind_qr_pending',
'confirmed',
'expired',
])
const decisionStageBlock = pageSource.slice(
pageSource.indexOf('const showBindingDecisionStage = Boolean('),
pageSource.indexOf('const bindingComparison ='),
)
const missing = flowStates.filter(
(state) => !handledElsewhere.has(state) && !decisionStageBlock.includes(`'${state}'`),
)
assert.deepEqual(missing, [], `以下状态在客户页没有任何可操作落点:${missing.join(', ')}`)
})