chore: add legacy SMS provider cleanup helper

This commit is contained in:
Codex
2026-07-04 00:43:59 +08:00
parent ca482f7feb
commit 0efdb0660c
5 changed files with 203 additions and 1 deletions

View File

@@ -0,0 +1,68 @@
import assert from 'node:assert/strict';
import {
buildConfig,
disableLegacySmsProviders,
findLegacySmsProviderRows,
} from './disable-legacy-sms-providers.js';
const tenantId = '00000000-0000-0000-0000-000000000001';
const rows = [
{ id: 'legacy-aliyun', tenantId, provider: 'aliyun', status: 'active', displayName: '旧阿里云短信' },
{ id: 'legacy-tencent', tenantId, provider: 'tencent-sms', status: 'testing', displayName: '旧腾讯云短信' },
{ id: 'disabled-aliyun', tenantId, provider: 'aliyun-sms', status: 'disabled', displayName: '已停用阿里云短信' },
{ id: 'pnvs', tenantId, provider: 'aliyun-pnvs', status: 'active', displayName: '阿里云短信认证' },
];
function queryFixture() {
return async (sql, params) => {
assert.equal(params[0], tenantId);
if (/select[\s\S]*from public\.tenant_auth_providers/i.test(sql)) {
return {
rows: rows.filter(row =>
params[1].includes(String(row.provider).toLowerCase()) &&
['active', 'testing'].includes(row.status),
),
};
}
throw new Error(`Unexpected SQL: ${sql}`);
};
}
const cfg = buildConfig(
{ DATABASE_URL: 'postgresql://example', PNVS_TENANT_ID: tenantId },
[],
);
assert.equal(cfg.apply, false);
assert.equal(cfg.tenantId, tenantId);
const applyCfg = buildConfig(
{ DATABASE_URL: 'postgresql://example', PNVS_TENANT_ID: tenantId },
['--apply'],
);
assert.equal(applyCfg.apply, true);
const found = await findLegacySmsProviderRows(queryFixture(), tenantId);
assert.deepEqual(found.map(row => row.id), ['legacy-aliyun', 'legacy-tencent']);
const dryRun = await disableLegacySmsProviders(
{ databaseUrl: 'postgresql://example', tenantId, apply: false },
{ query: queryFixture() },
);
assert.equal(dryRun.dryRun, true);
assert.equal(dryRun.changed, 0);
assert.deepEqual(dryRun.rows.map(row => row.id), ['legacy-aliyun', 'legacy-tencent']);
const applyRun = await disableLegacySmsProviders(
{ databaseUrl: 'postgresql://example', tenantId, apply: true },
{ query: queryFixture() },
);
assert.equal(applyRun.dryRun, false);
assert.equal(applyRun.changed, 2);
assert.deepEqual(applyRun.rows.map(row => row.id), ['legacy-aliyun', 'legacy-tencent']);
assert.throws(
() => buildConfig({ PNVS_TENANT_ID: tenantId }, []),
/Missing required env: DATABASE_URL/,
);
console.log('[PASS] disable legacy SMS providers script');