Files
order_site/apps/frontend/tests/semi-auto-injection.test.ts
T

48 lines
2.3 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'
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('未检测到浏览器扩展'))
})