feat(semi-auto): 客户链接按平台严格绑定,注入后统一由前端打开指定页
This commit is contained in:
@@ -1,17 +1,93 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { readFile } from 'node:fs/promises'
|
||||
import test from 'node:test'
|
||||
|
||||
import { isCustomerFulfillmentPath } from '../src/utils/customer-fulfillment-link.ts'
|
||||
import {
|
||||
buildCustomerFulfillmentPath,
|
||||
isCustomerFulfillmentPath,
|
||||
resolveCustomerPlatformRedirect,
|
||||
} from '../src/utils/customer-fulfillment-link.ts'
|
||||
|
||||
test('客户半自动履约入口:/c 命名空间整段命中', () => {
|
||||
test('客户半自动履约入口:/c 与 /d 命名空间整段命中', () => {
|
||||
assert.equal(isCustomerFulfillmentPath('/c/263123'), true)
|
||||
assert.equal(isCustomerFulfillmentPath('/c'), true)
|
||||
assert.equal(isCustomerFulfillmentPath('/c/a/b'), true)
|
||||
assert.equal(isCustomerFulfillmentPath('/d/263123'), true)
|
||||
assert.equal(isCustomerFulfillmentPath('/d'), true)
|
||||
assert.equal(isCustomerFulfillmentPath('/d/a/b'), true)
|
||||
assert.equal(isCustomerFulfillmentPath('/d/263123?from=chat'), true)
|
||||
assert.equal(isCustomerFulfillmentPath('/d/263123#section'), true)
|
||||
})
|
||||
|
||||
test('客户半自动履约入口不误伤相似前缀和其他路径', () => {
|
||||
assert.equal(isCustomerFulfillmentPath('/claim/x'), false)
|
||||
assert.equal(isCustomerFulfillmentPath('/customer/x'), false)
|
||||
assert.equal(isCustomerFulfillmentPath('/admin/c/1'), false)
|
||||
assert.equal(isCustomerFulfillmentPath('/admin/d/1'), false)
|
||||
assert.equal(isCustomerFulfillmentPath('/dd/1'), false)
|
||||
assert.equal(isCustomerFulfillmentPath('c/1'), false)
|
||||
assert.equal(isCustomerFulfillmentPath('d/1'), false)
|
||||
})
|
||||
|
||||
test('按平台生成客户链接:虎牙 /c、微博 /d', () => {
|
||||
assert.equal(
|
||||
buildCustomerFulfillmentPath('INTSA1794902905684_360848', 'huya'),
|
||||
'/c/INTSA1794902905684_360848',
|
||||
)
|
||||
assert.equal(
|
||||
buildCustomerFulfillmentPath('INTSA1794902905684_360848', 'weibo'),
|
||||
'/d/INTSA1794902905684_360848',
|
||||
)
|
||||
assert.equal(buildCustomerFulfillmentPath(' INTWB1 ', 'WEIBO'), '/d/INTWB1')
|
||||
assert.equal(buildCustomerFulfillmentPath('a/b', 'weibo'), '/d/a%2Fb')
|
||||
assert.equal(buildCustomerFulfillmentPath('', 'weibo'), '')
|
||||
assert.equal(buildCustomerFulfillmentPath('x', 'unknown'), '/c/x')
|
||||
})
|
||||
|
||||
test('严格绑定:路径平台不匹配时返回应跳转的平台', () => {
|
||||
assert.equal(resolveCustomerPlatformRedirect('huya', [{ platform: 'weibo' }]), 'weibo')
|
||||
assert.equal(resolveCustomerPlatformRedirect('weibo', [{ platform: 'huya' }]), 'huya')
|
||||
assert.equal(resolveCustomerPlatformRedirect('huya', [{ platform: 'huya' }]), null)
|
||||
assert.equal(resolveCustomerPlatformRedirect('weibo', [{ platform: 'weibo' }]), null)
|
||||
assert.equal(
|
||||
resolveCustomerPlatformRedirect('huya', [{ platform: 'huya' }, { platform: 'weibo' }]),
|
||||
null,
|
||||
)
|
||||
assert.equal(
|
||||
resolveCustomerPlatformRedirect('weibo', [{ platform: 'huya' }, { platform: 'weibo' }]),
|
||||
null,
|
||||
)
|
||||
assert.equal(resolveCustomerPlatformRedirect('huya', []), null)
|
||||
assert.equal(resolveCustomerPlatformRedirect(undefined, [{ platform: 'weibo' }]), null)
|
||||
assert.equal(resolveCustomerPlatformRedirect('huya', [{ platform: 'unknown' }]), null)
|
||||
assert.equal(
|
||||
resolveCustomerPlatformRedirect('huya', [{ platform: 'weibo' }, { platform: 'unknown' }]),
|
||||
'weibo',
|
||||
)
|
||||
})
|
||||
|
||||
test('客户页路由与严格绑定接线', async () => {
|
||||
const app = await readFile(new URL('../src/App.tsx', import.meta.url), 'utf8')
|
||||
const page = await readFile(
|
||||
new URL('../src/pages/customer-fulfillment/CustomerFulfillmentPage.tsx', import.meta.url),
|
||||
'utf8',
|
||||
)
|
||||
|
||||
assert.match(app, /path="\/c\/:orderNo"/)
|
||||
assert.match(app, /expectedPlatform="huya"/)
|
||||
assert.match(app, /path="\/d\/:orderNo"/)
|
||||
assert.match(app, /expectedPlatform="weibo"/)
|
||||
assert.match(page, /resolveCustomerPlatformRedirect/)
|
||||
assert.match(page, /buildCustomerFulfillmentPath/)
|
||||
assert.match(page, /task\.platform === expectedPlatform/)
|
||||
})
|
||||
|
||||
test('开发 Mock 页按平台重新生成客户路径', async () => {
|
||||
const page = await readFile(
|
||||
new URL('../src/pages/admin/AdminDevMockPage.tsx', import.meta.url),
|
||||
'utf8',
|
||||
)
|
||||
assert.match(page, /buildCustomerFulfillmentPath/)
|
||||
assert.match(page, /semiAutoCustomerPath/)
|
||||
assert.match(page, /semiAutoCustomerUrl/)
|
||||
})
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
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'
|
||||
|
||||
import { POST_INJECTION_URLS, resolvePostInjectionUrl } from '../src/utils/semi-auto-injection.ts'
|
||||
|
||||
const source = readFileSync(
|
||||
join(dirname(fileURLToPath(import.meta.url)), '../src/pages/admin/AdminSemiAutoTasksPage.tsx'),
|
||||
'utf8',
|
||||
)
|
||||
|
||||
test('注入完成后打开对应平台页面', () => {
|
||||
assert.equal(resolvePostInjectionUrl('huya'), POST_INJECTION_URLS.huya)
|
||||
assert.equal(resolvePostInjectionUrl('weibo'), POST_INJECTION_URLS.weibo)
|
||||
assert.match(POST_INJECTION_URLS.huya, /^https:\/\/m-shop\.huya\.com\/points\.html\?/)
|
||||
assert.match(
|
||||
POST_INJECTION_URLS.weibo,
|
||||
/^https:\/\/vipclub\.e\.weibo\.com\/game\/homepage\?F=bowen_tjg$/,
|
||||
)
|
||||
assert.equal(resolvePostInjectionUrl('other'), '')
|
||||
assert.equal(resolvePostInjectionUrl(null), '')
|
||||
})
|
||||
|
||||
test('注入按钮预开新标签页,完成后跳转,失败/超时关闭', () => {
|
||||
assert.ok(source.includes("window.open('about:blank', '_blank')"))
|
||||
assert.ok(source.includes('function startInjection('))
|
||||
assert.ok(source.includes('onClick={() => startInjection(task)}'))
|
||||
assert.ok(source.includes('onClick={() => startInjection(detail.task)}'))
|
||||
assert.ok(source.includes('huya-exchange-result'))
|
||||
assert.ok(source.includes('resolvePostInjectionUrl(pending.platform)'))
|
||||
assert.ok(source.includes('popup.location.href = targetUrl'))
|
||||
assert.ok(source.includes('popup.focus()'))
|
||||
assert.ok(source.includes("const opened = window.open(targetUrl, '_blank')"))
|
||||
// 占位页无法导航时,先关闭残留标签再走 window.open 兜底,避免留下空白页。
|
||||
const jumpIndex = source.indexOf('popup.location.href = targetUrl')
|
||||
const fallbackIndex = source.indexOf("const opened = window.open(targetUrl, '_blank')")
|
||||
assert.ok(jumpIndex >= 0 && fallbackIndex > jumpIndex)
|
||||
assert.ok(source.slice(jumpIndex, fallbackIndex).includes('popup.close()'))
|
||||
assert.ok(source.includes('pending.popup.close()'))
|
||||
assert.ok(source.includes('INJECTION_FOLLOW_UP_TIMEOUT_MS'))
|
||||
assert.ok(source.includes('未检测到注入完成'))
|
||||
assert.ok(source.includes('上一次注入尚未完成'))
|
||||
assert.ok(source.includes("dataset.huyaExchangeHelper !== '1'"))
|
||||
assert.ok(source.includes('未检测到浏览器扩展'))
|
||||
})
|
||||
Reference in New Issue
Block a user