fix: improve tester timeouts and telegram proxy config

This commit is contained in:
ciat-777
2026-03-14 16:18:43 +08:00
parent 201a806e51
commit 7bd48a3c6f
12 changed files with 369 additions and 6 deletions
@@ -49,6 +49,10 @@ describe('settings and auth events', () => {
config.logCleanupProgramLogsEnabled = false;
config.logCleanupRetentionDays = 30;
config.routingFallbackUnitCost = 1;
(config as any).telegramEnabled = false;
(config as any).telegramApiBaseUrl = 'https://api.telegram.org';
(config as any).telegramBotToken = '';
(config as any).telegramChatId = '';
});
afterAll(async () => {
@@ -170,6 +174,49 @@ describe('settings and auth events', () => {
expect(body.message).toContain('Telegram Chat ID');
});
it('persists and returns telegram api base url from runtime settings', async () => {
const updateResponse = await app.inject({
method: 'PUT',
url: '/api/settings/runtime',
payload: {
telegramApiBaseUrl: 'https://tg-proxy.example.com/custom/',
},
});
expect(updateResponse.statusCode).toBe(200);
const updated = updateResponse.json() as { telegramApiBaseUrl?: string };
expect(updated.telegramApiBaseUrl).toBe('https://tg-proxy.example.com/custom');
expect((config as any).telegramApiBaseUrl).toBe('https://tg-proxy.example.com/custom');
const saved = await db.select().from(schema.settings).where(eq(schema.settings.key, 'telegram_api_base_url')).get();
expect(saved?.value).toBe(JSON.stringify('https://tg-proxy.example.com/custom'));
const getResponse = await app.inject({
method: 'GET',
url: '/api/settings/runtime',
});
expect(getResponse.statusCode).toBe(200);
const runtime = getResponse.json() as { telegramApiBaseUrl?: string };
expect(runtime.telegramApiBaseUrl).toBe('https://tg-proxy.example.com/custom');
});
it('rejects invalid telegram api base url when telegram is enabled', async () => {
const response = await app.inject({
method: 'PUT',
url: '/api/settings/runtime',
payload: {
telegramEnabled: true,
telegramBotToken: '123456:telegram-token',
telegramChatId: '-1001234567890',
telegramApiBaseUrl: 'not-a-url',
},
});
expect(response.statusCode).toBe(400);
const body = response.json() as { message?: string };
expect(body.message).toContain('Telegram API Base URL');
});
it('persists and returns routing fallback unit cost from runtime settings', async () => {
const updateResponse = await app.inject({
method: 'PUT',
+30
View File
@@ -40,6 +40,7 @@ interface RuntimeSettingsBody {
serverChanEnabled?: boolean;
serverChanKey?: string;
telegramEnabled?: boolean;
telegramApiBaseUrl?: string;
telegramBotToken?: string;
telegramChatId?: string;
smtpEnabled?: boolean;
@@ -137,6 +138,10 @@ function isValidHttpUrl(raw: string): boolean {
}
}
function normalizeTelegramApiBaseUrl(raw: string): string {
return String(raw || '').trim().replace(/\/+$/, '');
}
function applyImportedSettingToRuntime(key: string, value: unknown) {
switch (key) {
case 'checkin_cron': {
@@ -223,6 +228,11 @@ function applyImportedSettingToRuntime(key: string, value: unknown) {
config.telegramEnabled = !!value;
return;
}
case 'telegram_api_base_url': {
if (typeof value !== 'string') return;
config.telegramApiBaseUrl = normalizeTelegramApiBaseUrl(value) || 'https://api.telegram.org';
return;
}
case 'telegram_bot_token': {
if (typeof value !== 'string') return;
config.telegramBotToken = value.trim();
@@ -322,6 +332,7 @@ function getRuntimeSettingsResponse(currentAdminIp = '') {
serverChanEnabled: config.serverChanEnabled,
serverChanKeyMasked: maskSecret(config.serverChanKey),
telegramEnabled: config.telegramEnabled,
telegramApiBaseUrl: config.telegramApiBaseUrl,
telegramBotTokenMasked: maskSecret(config.telegramBotToken),
telegramChatId: config.telegramChatId,
smtpEnabled: config.smtpEnabled,
@@ -452,11 +463,15 @@ export async function settingsRoutes(app: FastifyInstance) {
}
const telegramTouched = body.telegramEnabled !== undefined
|| body.telegramApiBaseUrl !== undefined
|| body.telegramBotToken !== undefined
|| body.telegramChatId !== undefined;
const nextTelegramEnabled = body.telegramEnabled !== undefined
? !!body.telegramEnabled
: config.telegramEnabled;
const nextTelegramApiBaseUrl = body.telegramApiBaseUrl !== undefined
? normalizeTelegramApiBaseUrl(body.telegramApiBaseUrl)
: config.telegramApiBaseUrl;
const nextTelegramBotToken = body.telegramBotToken !== undefined
? String(body.telegramBotToken || '').trim()
: config.telegramBotToken;
@@ -473,6 +488,11 @@ export async function settingsRoutes(app: FastifyInstance) {
if (!nextTelegramChatId) {
return reply.code(400).send({ success: false, message: 'Telegram Chat ID 不能为空(启用 Telegram 时)' });
}
if (nextTelegramApiBaseUrl && !isValidHttpUrl(nextTelegramApiBaseUrl)) {
return reply.code(400).send({ success: false, message: 'Telegram API Base URL 无效,请填写 http/https 地址' });
}
} else if (body.telegramApiBaseUrl !== undefined && nextTelegramApiBaseUrl && !isValidHttpUrl(nextTelegramApiBaseUrl)) {
return reply.code(400).send({ success: false, message: 'Telegram API Base URL 无效,请填写 http/https 地址' });
}
if (body.checkinCron !== undefined) {
@@ -639,6 +659,16 @@ export async function settingsRoutes(app: FastifyInstance) {
upsertSetting('telegram_enabled', config.telegramEnabled);
}
if (body.telegramApiBaseUrl !== undefined) {
const normalizedTelegramApiBaseUrl = normalizeTelegramApiBaseUrl(body.telegramApiBaseUrl);
const nextTelegramApiBaseUrl = normalizedTelegramApiBaseUrl || 'https://api.telegram.org';
if (nextTelegramApiBaseUrl !== config.telegramApiBaseUrl) {
changedLabels.push('Telegram API Base URL');
}
config.telegramApiBaseUrl = nextTelegramApiBaseUrl;
upsertSetting('telegram_api_base_url', config.telegramApiBaseUrl);
}
if (body.telegramBotToken !== undefined) {
if (String(body.telegramBotToken || '').trim() !== config.telegramBotToken) {
changedLabels.push('Telegram Bot Token');