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"', '') 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(', ')}`) })