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', ) /** 取样一段源码,找不到时直接失败,避免测试静默通过。 */ 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) } const weiboBindingCard = () => pickSlice('{showWeiboBindingStage ? (', '{showBindingStage ? (') test('换绑冷却天数只来自后端 rebindCooldownDays,不用其它字段推算', () => { // 页面内冷却天数只有一个来源。 assert.ok( source.includes('const rebindCooldownDays = Number(bindingInfo?.rebindCooldownDays || 0)'), ) assert.equal(source.split('rebindCooldownDays =').length - 1, 1) // 不得从平台规则字段或原始天数推算「从现在起还剩几天」。 assert.ok(!source.includes('rebindRemainDays')) assert.ok(!source.includes('rebindCooldownDays + ')) assert.ok(!source.includes('rebindCooldownDays - ')) assert.ok(!/(platformChangeBindDay|platformChangeBindTime)[^\n]*rebindCooldownDays/.test(source)) assert.ok(!/rebindCooldownDays[^\n]*(platformChangeBindDay|platformChangeBindTime)/.test(source)) }) test('冷却天数 > 0 且已有绑定角色时微信/QQ 按钮 disabled,刷新角色状态仍可用', () => { const cooldown = pickSlice('const weiboBoundRoleName', 'const isRebindBlocked') // 判断条件 = 后端天数 > 0 且已经有绑定角色,天数本身直接来自 rebindCooldownDays。 assert.ok(cooldown.includes('bindingInfo?.bindGameRole || selectedTask?.bindGameRole')) assert.ok( cooldown.includes( 'const weiboRebindCooldownBlocked = rebindCooldownDays > 0 && Boolean(weiboBoundRoleName)', ), ) // 文案:剩余约 N 天 + 冷却结束后才能换绑,N 只来自 rebindCooldownDays。 assert.ok( cooldown.includes( '当前角色仍在换绑冷却期,剩余约 ${rebindCooldownDays} 天,冷却结束后才能换绑', ), ) const card = weiboBindingCard() // 微信绑定 / QQ 绑定两个按钮都受冷却门禁约束。 assert.equal( card.split('disabled={bindingSessionExpired || weiboRebindCooldownBlocked}').length - 1, 2, ) const wxStart = card.indexOf('微信绑定') const qqStart = card.indexOf('QQ 绑定') assert.ok(wxStart > 0 && qqStart > wxStart) // 冷却提示必须在两个绑定按钮之前(按钮上方)。 const cooldownNotice = card.indexOf('weiboRebindCooldownText ? (') assert.ok(cooldownNotice > 0) assert.ok(cooldownNotice < card.indexOf("handleStartWeiboBinding('wx')")) // 「刷新角色状态」始终可用:只受登录会话影响,不受冷却影响。 const refreshStart = card.lastIndexOf('刷新角色状态') assert.ok(refreshStart > 0) const refreshButton = card.slice(card.lastIndexOf(' void handlePollBindingNow()}')) }) test('微博绑定失败原因同时展示平台原因与任务 lastError,且同一句不重复', () => { // 任务 lastError 与平台原因统一由去重收集器完成。 const collected = pickSlice( 'const bindingFailureReasons = collectBindingFailureReasons([', 'const weiboBindingFailureReasons', ) assert.ok(collected.includes('bindingInfo?.bindConfirmMessage')) assert.ok(collected.includes('bindingDecision?.message')) assert.ok(collected.includes('selectedTask?.lastError')) // 微信/QQ 绑定抛错(如冷却拒绝)写进 bindingLoadError,和平台原因共用同一个展示位。 const weiboReasons = pickSlice('const weiboBindingFailureReasons', 'const bindingRequiresSupport') assert.ok(weiboReasons.includes('...bindingFailureReasons')) assert.ok(weiboReasons.includes('bindingLoadError')) const card = weiboBindingCard() assert.ok(card.includes('weiboBindingFailureReasons.length')) assert.ok(card.includes('weiboBindingFailureReasons.map((reason) => (')) // 不再用单条瞬时提示代替失败原因展示。 assert.ok(!card.includes('message={bindingLoadError}')) assert.ok(!card.includes('description={bindingInfo?.message}')) // 去重逻辑只有一份(页面与微博区共用同一函数)。 assert.equal(source.split('const seen = new Set()').length - 1, 1) assert.ok(source.includes('function collectBindingFailureReasons(')) })