fix: complete first version issue remediation

This commit is contained in:
hectorzhao
2026-07-11 10:14:37 +08:00
parent 709ac97764
commit 208a6c23f8
73 changed files with 1549 additions and 245 deletions
+25 -3
View File
@@ -183,6 +183,8 @@ describe('ChannelsService', () => {
srcId: '10690000',
desiredConnections: 2,
windowSize: 32,
rateLimitPerSecond: 750,
config: { extensionDigits: 4 },
});
await service.createGroup({ code: 'G-MOBILE', name: '移动组', carrier: 'mobile', retryEnabled: true, retryTimeLimitMinutes: 750 });
await service.createRouteRule({ tenantId: 'tenant-1', applicationId: 'app-1', groupId: 'group-1', carrier: 'mobile' });
@@ -191,10 +193,10 @@ describe('ChannelsService', () => {
data: expect.objectContaining({
protocol: 'CMPP',
cmppVersion: '2.0',
rateLimitPerSecond: 100,
rateLimitPerSecond: 750,
sendRegion: '全国',
status: 'active',
config: expect.objectContaining({ desiredConnections: 2, windowSize: 32 }),
config: expect.objectContaining({ desiredConnections: 2, windowSize: 32, extensionDigits: 4 }),
}),
});
expect(prisma.cmppConnectionState.create).toHaveBeenCalledWith({
@@ -263,6 +265,23 @@ describe('ChannelsService', () => {
})).rejects.toThrow('cmppVersion must be 2.0 or 3.0');
});
it('rejects invalid channel rate limits and extension digit counts', async () => {
const prisma = createPrismaMock();
const service = new ChannelsService(prisma as never);
const channel = {
code: 'CMPP-CONFIG',
name: '配置校验通道',
gatewayHost: '127.0.0.1',
gatewayPort: 17890,
account: 'sp',
passwordCipher: 'secret',
srcId: '10690000',
};
await expect(service.createChannel({ ...channel, rateLimitPerSecond: 2001 })).rejects.toThrow('rateLimitPerSecond must be between 1 and 2000');
await expect(service.createChannel({ ...channel, config: { extensionDigits: 3 } })).rejects.toThrow('extensionDigits must be one of 0, 2, 4, or 6');
});
it('updates CMPP channel configuration without requiring password changes', async () => {
const prisma = createPrismaMock();
const service = new ChannelsService(prisma as never);
@@ -277,6 +296,8 @@ describe('ChannelsService', () => {
srcId: '10690001',
desiredConnections: 3,
windowSize: 64,
rateLimitPerSecond: 320,
config: { extensionDigits: 2 },
unitPrice: 4,
})).resolves.toEqual(expect.objectContaining({
id: 'channel-1',
@@ -292,7 +313,8 @@ describe('ChannelsService', () => {
gatewayPort: 27890,
carrier: 'all',
passwordCipher: undefined,
config: expect.objectContaining({ desiredConnections: 3, windowSize: 64 }),
rateLimitPerSecond: 320,
config: expect.objectContaining({ desiredConnections: 3, windowSize: 64, extensionDigits: 2 }),
}),
});
expect(prisma.operationLog.create).toHaveBeenCalledWith({
+41 -7
View File
@@ -215,7 +215,8 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
throw new BadRequestException('gatewayPort must be an integer between 1 and 65535');
}
const cmppVersion = normalizeCmppVersion(data.cmppVersion);
const config = normalizeChannelRuntimeConfig(data.config, data.desiredConnections, data.windowSize);
const config = normalizeChannelRuntimeConfig(undefined, data.config, data.desiredConnections, data.windowSize);
const rateLimitPerSecond = normalizeChannelRateLimit(data.rateLimitPerSecond);
const channel = await this.prisma.smsChannel.create({
data: {
code: data.code,
@@ -230,7 +231,7 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
passwordCipher: data.passwordCipher,
srcId: data.srcId,
cmppVersion,
rateLimitPerSecond: data.rateLimitPerSecond ?? 100,
rateLimitPerSecond,
unitPrice: data.unitPrice ?? 0,
status: data.status ?? 'active',
config: config as Prisma.InputJsonValue,
@@ -253,8 +254,11 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
}
const cmppVersion = data.cmppVersion === undefined ? undefined : normalizeCmppVersion(data.cmppVersion);
const config = data.config !== undefined || data.desiredConnections !== undefined || data.windowSize !== undefined
? normalizeChannelRuntimeConfig(channel.config, data.desiredConnections, data.windowSize)
? normalizeChannelRuntimeConfig(channel.config, data.config, data.desiredConnections, data.windowSize)
: undefined;
const rateLimitPerSecond = data.rateLimitPerSecond === undefined
? undefined
: normalizeChannelRateLimit(data.rateLimitPerSecond);
const updated = await this.prisma.smsChannel.update({
where: { id: channelId },
data: {
@@ -270,7 +274,7 @@ export class ChannelsService implements OnModuleInit, OnModuleDestroy {
passwordCipher: data.passwordCipher,
srcId: data.srcId,
cmppVersion,
rateLimitPerSecond: data.rateLimitPerSecond,
rateLimitPerSecond,
unitPrice: data.unitPrice,
status: data.status,
config: config as Prisma.InputJsonValue | undefined,
@@ -1287,6 +1291,7 @@ function buildChannelTestSubmitCommand({
cmpp: {
serviceId: getStringConfigValue(channel.config, 'serviceId', 'SMS'),
srcId,
extensionDigits: normalizeExtensionDigits(getConfigValue(channel.config, 'extensionDigits')),
registeredDelivery: 1,
msgFmt: 8,
},
@@ -1380,15 +1385,44 @@ function getDesiredConnections(config?: Prisma.JsonValue | null) {
return 1;
}
function normalizeChannelRuntimeConfig(config?: Prisma.JsonValue | Record<string, unknown> | null, desiredConnections?: number, windowSize?: number) {
const base = config && typeof config === 'object' && !Array.isArray(config)
? { ...(config as Record<string, unknown>) }
function normalizeChannelRuntimeConfig(
existingConfig?: Prisma.JsonValue | Record<string, unknown> | null,
incomingConfig?: Record<string, unknown> | null,
desiredConnections?: number,
windowSize?: number,
) {
const existing = existingConfig && typeof existingConfig === 'object' && !Array.isArray(existingConfig)
? existingConfig as Record<string, unknown>
: {};
const incoming = incomingConfig && typeof incomingConfig === 'object' && !Array.isArray(incomingConfig)
? incomingConfig
: {};
const base = { ...existing, ...incoming };
base.desiredConnections = getPositiveRuntimeInteger(desiredConnections ?? base.desiredConnections, 1, 'desiredConnections');
base.windowSize = getPositiveRuntimeInteger(windowSize ?? base.windowSize, 16, 'windowSize');
base.extensionDigits = normalizeExtensionDigits(base.extensionDigits);
return base;
}
function normalizeChannelRateLimit(value: unknown) {
const normalized = getPositiveRuntimeInteger(value, 100, 'rateLimitPerSecond');
if (normalized > 2000) {
throw new BadRequestException('rateLimitPerSecond must be between 1 and 2000');
}
return normalized;
}
function normalizeExtensionDigits(value: unknown) {
if (value === undefined || value === null || value === '') {
return 0;
}
const normalized = Number(value);
if (![0, 2, 4, 6].includes(normalized)) {
throw new BadRequestException('extensionDigits must be one of 0, 2, 4, or 6');
}
return normalized;
}
function getPositiveRuntimeInteger(value: unknown, fallback: number, fieldName: string) {
if (value === undefined || value === null || value === '') {
return fallback;