Files
one-pipe-system/scripts/tests/wechatAuthorization.test.mjs
luo 7f3ff10fda
All checks were successful
构建并部署前端到测试环境 / deploy (push) Successful in 55s
fix:some
2026-10-20 09:21:23 +08:00

109 lines
3.6 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import ts from 'typescript'
import * as vue from 'vue'
import { compileScript, parse } from '@vue/compiler-sfc'
const require = createRequire(import.meta.url)
const elementPlusRequire = createRequire(require.resolve('element-plus'))
const Schema = elementPlusRequire('async-validator').default
const { descriptor } = parse(
readFileSync(
'src/views/settings/payment-merchant-pools/components/WechatAuthorizationManagement.vue',
'utf8'
)
)
const compiled = ts.transpileModule(
compileScript(descriptor, { id: 'wechat-authorization-test' }).content,
{ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS } }
).outputText
function harness() {
const calls = []
const imports = {
vue: { ...vue, onMounted: () => {}, onUnmounted: () => {} },
'element-plus': { ElMessage: { success: () => {} } },
'@/api/modules': {
PaymentMerchantPoolsService: {
updateWechatAuthorization: async (payload) => {
calls.push({ ...payload })
return { code: 0, data: payload }
}
}
},
'@/composables/useAuth': { useAuth: () => ({ hasAuth: () => true }) },
'@/config/constants/paymentMerchantPools': {
PAYMENT_MERCHANT_POOL_PERMISSIONS: { wechatAuthEdit: 'wechat_auth_edit' }
}
}
const module = { exports: {} }
new Function('require', 'module', 'exports', 'computed', compiled)(
(name) => {
assert.ok(imports[name], `Unexpected runtime dependency: ${name}`)
return imports[name]
},
module,
module.exports,
vue.computed
)
const state = module.exports.default.setup({}, { expose: () => {} })
state.formRef.value = {
validate: () => new Schema(state.formRules).validate(state.form)
}
return { state, calls }
}
test('miniapp AppID starts empty without default or placeholder text', () => {
const { state } = harness()
assert.equal(state.form.miniapp_app_id, '')
const input = descriptor.template.content.match(
/<ElInput\b[^>]*v-model="form\.miniapp_app_id"[^>]*\/>/
)?.[0]
assert.ok(input)
assert.doesNotMatch(input, /\b(?:placeholder|(?:default-)?value)\s*=/)
})
for (const enabled of [false, true]) {
for (const miniappAppId of ['', ' ', null, undefined]) {
test(`saves ${enabled ? 'enabled' : 'disabled'} authorization without optional AppID ${JSON.stringify(miniappAppId)}`, async () => {
const { state, calls } = harness()
Object.assign(state.form, {
enabled,
miniapp_app_id: miniappAppId,
oa_app_id: 'wx-official-account',
oa_oauth_redirect_url: 'https://example.com/oauth'
})
await state.handleSubmit()
assert.deepEqual(calls, [
{
enabled,
oa_app_id: 'wx-official-account',
oa_oauth_redirect_url: 'https://example.com/oauth'
}
])
})
}
}
test('submits a provided miniapp AppID after trimming and clears sensitive inputs after saving', async () => {
const { state, calls } = harness()
state.form.miniapp_app_id = ' wx-miniapp '
state.sensitive.miniapp_app_secret = 'new-miniapp-secret'
await state.handleSubmit()
assert.equal(calls[0].miniapp_app_id, 'wx-miniapp')
assert.equal(calls[0].miniapp_app_secret, 'new-miniapp-secret')
assert.equal(state.sensitive.miniapp_app_secret, '')
})
test('still rejects a miniapp AppID longer than 64 characters', async () => {
const { state, calls } = harness()
state.form.miniapp_app_id = 'x'.repeat(65)
await state.handleSubmit()
assert.deepEqual(calls, [])
})