test(admin): constrain payment visible method sources

This commit is contained in:
IanShaw027
2026-04-21 00:03:27 +08:00
parent 0fa47f18ed
commit 4ebdfcd13a
4 changed files with 631 additions and 13 deletions

View File

@@ -0,0 +1,63 @@
import { describe, expect, it } from 'vitest'
import {
getPaymentVisibleMethodSourceOptions,
normalizePaymentVisibleMethodSource,
} from '@/api/admin/settings'
describe('admin settings payment visible method helpers', () => {
it('normalizes aliases into canonical source keys per visible method', () => {
expect(normalizePaymentVisibleMethodSource('alipay', 'official')).toBe('official_alipay')
expect(normalizePaymentVisibleMethodSource('alipay', 'alipay_direct')).toBe('official_alipay')
expect(normalizePaymentVisibleMethodSource('alipay', 'easypay')).toBe('easypay_alipay')
expect(normalizePaymentVisibleMethodSource('wxpay', 'official')).toBe('official_wxpay')
expect(normalizePaymentVisibleMethodSource('wxpay', 'wechat')).toBe('official_wxpay')
expect(normalizePaymentVisibleMethodSource('wxpay', 'easypay')).toBe('easypay_wxpay')
})
it('rejects unknown or cross-method source values', () => {
expect(normalizePaymentVisibleMethodSource('alipay', 'official_wxpay')).toBe('')
expect(normalizePaymentVisibleMethodSource('wxpay', 'official_alipay')).toBe('')
expect(normalizePaymentVisibleMethodSource('alipay', 'unknown')).toBe('')
expect(normalizePaymentVisibleMethodSource('wxpay', null)).toBe('')
})
it('exposes method-scoped source options instead of arbitrary strings', () => {
expect(getPaymentVisibleMethodSourceOptions('alipay')).toEqual([
{
value: '',
labelZh: '自动路由',
labelEn: 'Automatic routing',
},
{
value: 'official_alipay',
labelZh: '支付宝官方',
labelEn: 'Official Alipay',
},
{
value: 'easypay_alipay',
labelZh: '易支付支付宝',
labelEn: 'EasyPay Alipay',
},
])
expect(getPaymentVisibleMethodSourceOptions('wxpay')).toEqual([
{
value: '',
labelZh: '自动路由',
labelEn: 'Automatic routing',
},
{
value: 'official_wxpay',
labelZh: '微信官方',
labelEn: 'Official WeChat Pay',
},
{
value: 'easypay_wxpay',
labelZh: '易支付微信',
labelEn: 'EasyPay WeChat Pay',
},
])
})
})

View File

@@ -22,10 +22,60 @@ export interface AuthSourceDefaultsValue {
}
export type AuthSourceDefaultsState = Record<AuthSourceType, AuthSourceDefaultsValue>
export type PaymentVisibleMethod = 'alipay' | 'wxpay'
export type PaymentVisibleMethodSource =
| ''
| 'official_alipay'
| 'easypay_alipay'
| 'official_wxpay'
| 'easypay_wxpay'
export interface PaymentVisibleMethodSourceOption {
value: PaymentVisibleMethodSource
labelZh: string
labelEn: string
}
const AUTH_SOURCE_TYPES: AuthSourceType[] = ['email', 'linuxdo', 'oidc', 'wechat']
const AUTH_SOURCE_DEFAULT_BALANCE = 0
const AUTH_SOURCE_DEFAULT_CONCURRENCY = 5
const PAYMENT_VISIBLE_METHOD_SOURCE_OPTIONS: Record<
PaymentVisibleMethod,
PaymentVisibleMethodSourceOption[]
> = {
alipay: [
{ value: '', labelZh: '自动路由', labelEn: 'Automatic routing' },
{ value: 'official_alipay', labelZh: '支付宝官方', labelEn: 'Official Alipay' },
{ value: 'easypay_alipay', labelZh: '易支付支付宝', labelEn: 'EasyPay Alipay' },
],
wxpay: [
{ value: '', labelZh: '自动路由', labelEn: 'Automatic routing' },
{ value: 'official_wxpay', labelZh: '微信官方', labelEn: 'Official WeChat Pay' },
{ value: 'easypay_wxpay', labelZh: '易支付微信', labelEn: 'EasyPay WeChat Pay' },
],
}
const PAYMENT_VISIBLE_METHOD_SOURCE_ALIASES: Record<
PaymentVisibleMethod,
Record<string, PaymentVisibleMethodSource>
> = {
alipay: {
official_alipay: 'official_alipay',
alipay: 'official_alipay',
alipay_direct: 'official_alipay',
official: 'official_alipay',
easypay_alipay: 'easypay_alipay',
easypay: 'easypay_alipay',
},
wxpay: {
official_wxpay: 'official_wxpay',
wxpay: 'official_wxpay',
wxpay_direct: 'official_wxpay',
wechat: 'official_wxpay',
official: 'official_wxpay',
easypay_wxpay: 'easypay_wxpay',
easypay: 'easypay_wxpay',
},
}
export function normalizeDefaultSubscriptionSettings(
subscriptions: DefaultSubscriptionSetting[] | null | undefined
@@ -86,6 +136,24 @@ export function appendAuthSourceDefaultsToUpdateRequest(
return payload
}
export function getPaymentVisibleMethodSourceOptions(
method: PaymentVisibleMethod
): PaymentVisibleMethodSourceOption[] {
return PAYMENT_VISIBLE_METHOD_SOURCE_OPTIONS[method]
}
export function normalizePaymentVisibleMethodSource(
method: PaymentVisibleMethod,
source: unknown
): PaymentVisibleMethodSource {
if (typeof source !== 'string') return ''
const normalized = source.trim().toLowerCase()
if (!normalized) return ''
return PAYMENT_VISIBLE_METHOD_SOURCE_ALIASES[method][normalized] ?? ''
}
/**
* System settings interface
*/