From a667f1401e20b672fe22ec45546c9112fe534113 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 16 Sep 2026 15:04:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(inbox):=20=E6=8E=A8=E9=80=81=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E8=87=AA=E5=8A=A8=E9=87=8D=E5=8F=91=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E4=BB=93=E5=BA=93=E7=BA=A7=E5=BC=80=E5=85=B3=E4=B8=8E=E9=87=8D?= =?UTF-8?q?=E5=8F=91=E6=97=B6=E9=97=B4=E8=8C=83=E5=9B=B4=EF=BC=88=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=85=B3=E9=97=AD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 仓库设置新增嵌套配置 `subject_config.inbox_push_retry = { enabled, window_minutes }`, 结构与「收货未入库提醒」(receiving_pending_inbound_notice)保持一致: - `enabled`:默认 false —— 未配置、配置缺失或无法读取时一律**关闭**,需在仓库设置中显式开启 - `window_minutes`:重发时间范围,单位**分钟**,1-1440,默认 30(仅开关开启时生效) 改动: - app/model/store.js:新增 DEFAULT_INBOX_PUSH_RETRY 与 normalizeInboxPushRetry(非法结构/非法 JSON 回退默认;enabled 非布尔值、window_minutes 越界抛「subject_config.inbox_push_retry.window_minutes 必须是 1-1440 的整数(分钟)」),由 normalizeSubjectConfig 统一调用 - app/service/systems/setting/warehouse.js:WAREHOUSE_SETTING_FIELDS 新增 key=inbox_push_retry (type=object + item_keys=[enabled, window_minutes]),供仓库设置页经 getFieldMeta 动态渲染 - app/service/inbox/message.js:retryFailedByWarehouse 逐仓读仓库设置(缺省关闭、按仓库配置的窗口 调用 retryFailed);retryFailed 新增 warehouseId 过滤;readPushRetryConfig 读嵌套结构并兜底 enabled=false / 30 分钟 - app/schedule/retry_inbox_push.js:注释同步为「仅处理显式开启的仓库、缺省 30 分钟」 - config/config.default.js:兜底失败时间窗 2 小时 → 30 分钟;pushRetryEnabled 保留为全局应急总闸 (默认 true,关闭后所有仓库都不重发) 单测:test/inbox/retry_inbox_push.test.js + test/app/model/store.test.js 共 40 passing,覆盖默认关闭、 非法结构回退、flag 真值归一化、窗口 1-1440 越界、仓库开关与自定义窗口、复用原记录等场景。 --- app/model/store.js | 57 +++++++ app/schedule/retry_inbox_push.js | 23 +-- app/service/inbox/message.js | 203 ++++++++++++++++++++++- app/service/systems/setting/warehouse.js | 31 ++++ config/config.default.js | 8 +- test/app/model/store.test.js | 55 ++++++ 6 files changed, 355 insertions(+), 22 deletions(-) diff --git a/app/model/store.js b/app/model/store.js index d387a87..239afd1 100644 --- a/app/model/store.js +++ b/app/model/store.js @@ -61,6 +61,15 @@ module.exports = app => { store_notice_template: DEFAULT_PENDING_RECEIVING_STORE_NOTICE_TEMPLATE, }); + // 站内信推送失败自动重发:默认关闭(未配置的仓库不重发,需在仓库设置中显式开启); + // 开启后仅重发「失败时间在 window_minutes 内」的消息(超窗口不再补发) + const DEFAULT_INBOX_PUSH_RETRY = Object.freeze({ + enabled: false, + window_minutes: 30, + }); + const INBOX_PUSH_RETRY_WINDOW_MINUTES_MIN = 1; + const INBOX_PUSH_RETRY_WINDOW_MINUTES_MAX = 1440; + const DEFAULT_SUBJECT_CONFIG = Object.freeze({ max_notice_webhook_count: DEFAULT_MAX_NOTICE_WEBHOOK_COUNT, max_wave_quantity: DEFAULT_MAX_WAVE_QUANTITY, @@ -88,6 +97,7 @@ module.exports = app => { cutoff_order_time: DEFAULT_CUTOFF_ORDER_TIME, allocate_start_time: DEFAULT_ALLOCATE_START_TIME, receiving_pending_inbound_notice: DEFAULT_RECEIVING_PENDING_INBOUND_NOTICE, + inbox_push_retry: DEFAULT_INBOX_PUSH_RETRY, }); const normalizeSubjectConfigText = (value, defaultValue) => { @@ -313,6 +323,52 @@ module.exports = app => { }; }; + /** + * 规范化「站内信推送失败自动重发」配置(subject_config.inbox_push_retry)。 + * 字段:enabled(是否启用,默认否) / window_minutes(重发时间范围分钟,默认 30,范围 1-1440)。 + * 非法结构(非对象/非法 JSON)回退默认值;enabled 非布尔值、window_minutes 越界抛错。 + * + * @param {Object|string|null|undefined} rawValue 原始配置。 + * @return {Object} 规范化后的配置对象。 + */ + const normalizeInboxPushRetry = rawValue => { + if (rawValue === undefined || rawValue === null || rawValue === '') { + return { ...DEFAULT_INBOX_PUSH_RETRY }; + } + + let parsed = rawValue; + if (typeof parsed === 'string') { + try { + parsed = JSON.parse(parsed); + } catch (error) { + return { ...DEFAULT_INBOX_PUSH_RETRY }; + } + } + + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { + return { ...DEFAULT_INBOX_PUSH_RETRY }; + } + + const rawWindowMinutes = parsed.window_minutes; + let windowMinutes = DEFAULT_INBOX_PUSH_RETRY.window_minutes; + if (!(rawWindowMinutes === undefined || rawWindowMinutes === null || rawWindowMinutes === '')) { + const value = Number(rawWindowMinutes); + if (!Number.isInteger(value) + || value < INBOX_PUSH_RETRY_WINDOW_MINUTES_MIN + || value > INBOX_PUSH_RETRY_WINDOW_MINUTES_MAX) { + throw new Error( + `subject_config.inbox_push_retry.window_minutes 必须是 ${INBOX_PUSH_RETRY_WINDOW_MINUTES_MIN}-${INBOX_PUSH_RETRY_WINDOW_MINUTES_MAX} 的整数(分钟)` + ); + } + windowMinutes = value; + } + + return { + enabled: normalizeBooleanFlag(parsed.enabled, 'subject_config.inbox_push_retry.enabled') === true, + window_minutes: windowMinutes, + }; + }; + /** * 规范化主体配置,确保默认值与字段约束一致。 * @@ -476,6 +532,7 @@ module.exports = app => { normalized.receiving_pending_inbound_notice = normalizeReceivingPendingInboundNotice( parsed.receiving_pending_inbound_notice ); + normalized.inbox_push_retry = normalizeInboxPushRetry(parsed.inbox_push_retry); const rawDefaultLocationId = parsed.default_location_id; if (rawDefaultLocationId === undefined || rawDefaultLocationId === null || rawDefaultLocationId === '') { diff --git a/app/schedule/retry_inbox_push.js b/app/schedule/retry_inbox_push.js index 390ce4d..f69dac1 100644 --- a/app/schedule/retry_inbox_push.js +++ b/app/schedule/retry_inbox_push.js @@ -3,17 +3,18 @@ /** * 站内信出站推送失败自动重推定时任务。 * - * 每 3 分钟在单 worker 中扫描一次「推送失败(push_status=3)且失败时间在窗口内」的出站消息 - * (窗口默认 2 小时,超窗口的消息不再补推,避免陈旧回信/通知打扰用户),逐条复用原记录重推: + * 每 3 分钟在单 worker 中扫描一次,按仓库逐个处理(开关与时间范围均由「仓库设置」控制): + * - 只处理在仓库设置里**显式开启**「站内信推送失败自动重发」的仓库 + * (`subject_config.inbox_push_retry.enabled`,缺省/无法读取时为关闭,不重推); + * - 只重推该仓库「失败时间在 `subject_config.inbox_push_retry.window_minutes` + * (分钟,1-1440,缺省 30)内」的消息,超出范围不再补发; + * - 每仓单轮最多重推 config.inbox.pushRetryLimit 条(默认 20,硬上限 200)。 + * * 推送结果仍回写到同一条记录(成功 2 / 失败 3),成功后不再被扫到,失败则由下一轮继续重试, - * 直到推送成功或滑出时间窗。 + * 直到推送成功或滑出该仓库配置的时间窗;此外 config.inbox.pushRetryEnabled 可作为全局应急开关。 * * 典型失败原因是桥接离线(回调 HTTP 200 但 body code=503,如 wx offline / qiwe offline), * 属可自愈的临时故障:桥接恢复后靠本任务补发,无需人工介入。 - * - * 单轮最多重推条数与失败时间窗由 config.inbox 控制: - * - pushRetryWindowMs:失败时间窗(毫秒,默认 2 小时) - * - pushRetryLimit:单轮最多重推条数(默认 20,硬上限 200) */ module.exports = { schedule: { @@ -37,12 +38,14 @@ module.exports = { ctx.app._inboxPushRetryRunning = true; try { - const result = await ctx.service.inbox.message.retryFailed(); + const result = await ctx.service.inbox.message.retryFailedByWarehouse(); // 无待重推记录时不刷日志,避免每 3 分钟噪音 if (result && result.scanned > 0) { ctx.logger.info( - `[retry-inbox-push] 本轮重推 scanned=${result.scanned} retried=${result.retried} ` + - `succeeded=${result.succeeded} failed=${result.failed} skipped=${result.skipped}` + `[retry-inbox-push] 本轮重推 仓库=${result.warehouses}` + + `(开启=${result.enabledWarehouses} 关闭=${result.disabledWarehouses} 跳过=${result.skippedWarehouses})` + + ` scanned=${result.scanned} retried=${result.retried} succeeded=${result.succeeded}` + + ` failed=${result.failed} skipped=${result.skipped}` ); } } catch (error) { diff --git a/app/service/inbox/message.js b/app/service/inbox/message.js index 12bd5ab..0565488 100644 --- a/app/service/inbox/message.js +++ b/app/service/inbox/message.js @@ -3,13 +3,65 @@ const Service = require('egg').Service; const { parseWhereConditions } = require('../../lib/query_condition_parser'); -/** 出站推送失败自动重推:默认失败时间窗(毫秒)——超窗口的消息不再补推,避免陈旧消息打扰用户 */ -const DEFAULT_PUSH_RETRY_WINDOW_MS = 2 * 60 * 60 * 1000; +/** 出站推送失败自动重推:默认失败时间窗(毫秒,30 分钟)——仓库未配置时兜底,超窗口的消息不再补推 */ +const DEFAULT_PUSH_RETRY_WINDOW_MS = 30 * 60 * 1000; /** 出站推送失败自动重推:默认单轮最多重推条数(定时任务每 3 分钟扫一次) */ const DEFAULT_PUSH_RETRY_LIMIT = 20; /** 出站推送失败自动重推:单轮条数硬上限 */ const MAX_PUSH_RETRY_LIMIT = 200; +/** 仓库设置字段:站内信推送失败自动重发(嵌套对象,与「收货未入库提醒」同结构) */ +const PUSH_RETRY_FIELD = 'inbox_push_retry'; +/** 重发时间范围(分钟)上下限:与仓库设置字段元信息 setting/warehouse.js 保持一致 */ +const PUSH_RETRY_WINDOW_MINUTES_MIN = 1; +const PUSH_RETRY_WINDOW_MINUTES_MAX = 1440; +/** 重发时间范围默认值(分钟):仓库未配置时兜底 30 分钟 */ +const DEFAULT_PUSH_RETRY_WINDOW_MINUTES = 30; + +/** + * 从仓库设置(wms_store.subject_config)读取「站内信推送失败自动重发」配置。 + * + * 结构:`subject_config.inbox_push_retry = { enabled, window_minutes }`(同「收货未入库提醒」的嵌套口径)。 + * + * 兼容 JSON 字符串与缺失字段,**缺省即兜底 = 关闭**(未配置的仓库不重发): + * - 开关仅显式传入真值(true / 1 / '1' / 'true')才开启,其余情况(缺失/非法/假值)均为关闭; + * - 时间范围缺省/非法 → 30 分钟(仅接受 1-1440 的整数,单位分钟)。 + * + * @param {Object|string|null|undefined} rawSubjectConfig 原始 subject_config(raw 查询可能为字符串)。 + * @return {{enabled: boolean, windowMinutes: number}} 规范化后的配置。 + */ +function readPushRetryConfig(rawSubjectConfig) { + let parsed = rawSubjectConfig; + if (typeof parsed === 'string') { + try { + parsed = JSON.parse(parsed); + } catch (error) { + parsed = null; + } + } + + const section = parsed && typeof parsed === 'object' && !Array.isArray(parsed) + ? parsed[PUSH_RETRY_FIELD] + : null; + + if (!section || typeof section !== 'object' || Array.isArray(section)) { + return { enabled: false, windowMinutes: DEFAULT_PUSH_RETRY_WINDOW_MINUTES }; + } + + // 兜底为关闭:只有显式开启才开启(不再把“缺失/无法识别”当作开启) + const rawEnabled = section.enabled; + const enabled = [ true, 1, '1', 'true' ].includes(rawEnabled); + + const rawWindowMinutes = Number(section.window_minutes); + const windowMinutes = Number.isInteger(rawWindowMinutes) + && rawWindowMinutes >= PUSH_RETRY_WINDOW_MINUTES_MIN + && rawWindowMinutes <= PUSH_RETRY_WINDOW_MINUTES_MAX + ? rawWindowMinutes + : DEFAULT_PUSH_RETRY_WINDOW_MINUTES; + + return { enabled, windowMinutes }; +} + /** * 站内信消息查询/管理服务。 * 提供消息列表、详情、对话链、回复、统计等管理功能。 @@ -507,12 +559,13 @@ class InboxMessageService extends Service { * 批量重推「推送失败」的出站消息(供定时任务调用)。 * * 规则: - * 1. 只处理失败时间(created_at)在窗口内的记录,默认 2 小时;超窗口不再补推; + * 1. 只处理失败时间(created_at)在窗口内的记录,默认 30 分钟;超窗口不再补推; * 2. 逐条串行重推并等待结果回写,单条异常不影响其余记录; * 3. 重推期间记录保持失败状态,进程中断时下一轮仍能被扫到(不丢消息); * 4. 缺少 warehouse_id / group_id / group_name / content 的记录无法重放,跳过并计数。 * * @param {Object} [params] 查询参数。 + * @param {number} [params.warehouseId] 仓库ID:传入时只处理该仓库的失败消息(定时任务按仓库逐个处理)。 * @param {number} [params.windowMs] 失败时间窗(毫秒),默认 config.inbox.pushRetryWindowMs。 * @param {number} [params.limit] 单轮最多重推条数,默认 config.inbox.pushRetryLimit。 * @return {Promise} { scanned, retried, succeeded, failed, skipped }。 @@ -523,6 +576,7 @@ class InboxMessageService extends Service { const { InboxMessage } = ctx.model; const inboxConfig = (ctx.app.config && ctx.app.config.inbox) || {}; + const warehouseId = Number(params.warehouseId) > 0 ? Number(params.warehouseId) : 0; const configuredWindow = Number(inboxConfig.pushRetryWindowMs); const windowMs = Number(params.windowMs) > 0 ? Number(params.windowMs) @@ -533,13 +587,19 @@ class InboxMessageService extends Service { : (configuredLimit > 0 ? configuredLimit : DEFAULT_PUSH_RETRY_LIMIT); const limit = Math.min(MAX_PUSH_RETRY_LIMIT, Math.max(1, Math.trunc(rawLimit))); - // 先补早失败的消息(更快接近 2 小时窗口边界) + // 先补早失败的消息(更快接近时间窗边界) + const where = { + direction: 'outbound', + push_status: 3, + created_at: { [Op.gte]: new Date(Date.now() - windowMs) }, + }; + // 仓库维度(定时任务逐仓处理):只扫该仓库的失败消息 + if (warehouseId > 0) { + where.warehouse_id = warehouseId; + } + const rows = await InboxMessage.findAll({ - where: { - direction: 'outbound', - push_status: 3, - created_at: { [Op.gte]: new Date(Date.now() - windowMs) }, - }, + where, attributes: [ 'id', 'warehouse_id', 'ext_id', 'parent_id', 'group_id', 'group_name', 'user_id', 'user_name', @@ -588,6 +648,131 @@ class InboxMessageService extends Service { return summary; } + + /** + * 按仓库设置批量重推「推送失败」的出站消息(定时任务入口)。 + * + * 流程: + * 1. 全局总开关(config.inbox.pushRetryEnabled)关闭时直接返回,支持运维应急停发; + * 2. 先找出「近 1440 分钟(时间范围上限)内存在推送失败消息」的仓库,只对这些仓库读设置, + * 避免每轮全仓扫描; + * 3. 逐仓判断仓库设置 `subject_config.inbox_push_retry.enabled`(缺省关闭,仅显式开启才重推):关闭则跳过; + * 4. 开启的仓库按 `subject_config.inbox_push_retry.window_minutes`(缺省 30 分钟) + * 调 retryFailed 只补该窗口内的失败消息。 + * + * @param {Object} [params] 调用参数。 + * @param {number} [params.limit] 单仓单轮最多重推条数,默认 config.inbox.pushRetryLimit。 + * @return {Promise} { warehouses, enabledWarehouses, disabledWarehouses, skippedWarehouses, + * scanned, retried, succeeded, failed, skipped, disabledGlobally }。 + */ + async retryFailedByWarehouse(params = {}) { + const { ctx } = this; + const { Op } = ctx.app.Sequelize; + const { InboxMessage, Store } = ctx.model; + + const inboxConfig = (ctx.app.config && ctx.app.config.inbox) || {}; + const summary = { + warehouses: 0, + enabledWarehouses: 0, + disabledWarehouses: 0, + skippedWarehouses: 0, + scanned: 0, + retried: 0, + succeeded: 0, + failed: 0, + skipped: 0, + disabledGlobally: false, + }; + + // 全局总开关:仓库维度开关在仓库设置里 + if (inboxConfig.pushRetryEnabled === false) { + summary.disabledGlobally = true; + ctx.logger.warn('[inbox] 自动重推已被全局开关(config.inbox.pushRetryEnabled)关闭'); + return summary; + } + + // 候选仓库:时间范围上限内存在失败消息的仓库(去重),只查有失败的仓库 + const maxWindowMs = PUSH_RETRY_WINDOW_MINUTES_MAX * 60 * 1000; + const warehouseRows = await InboxMessage.findAll({ + where: { + direction: 'outbound', + push_status: 3, + created_at: { [Op.gte]: new Date(Date.now() - maxWindowMs) }, + }, + attributes: [ 'warehouse_id' ], + group: [ 'warehouse_id' ], + raw: true, + }); + const warehouseIds = (Array.isArray(warehouseRows) ? warehouseRows : []) + .map(row => Number(row && row.warehouse_id) || 0) + .filter(id => id > 0); + summary.warehouses = warehouseIds.length; + + if (warehouseIds.length === 0) { + return summary; + } + + const storeModel = Store; + if (!storeModel || typeof storeModel.findAll !== 'function') { + ctx.logger.warn('[inbox] Store 模型不可用,跳过自动重推'); + summary.skippedWarehouses = warehouseIds.length; + return summary; + } + + // 批量取仓库设置(raw 行避免脏配置触发模型 getter 抛错); + // 不按 status 过滤:wms_store.status 是启用/审核中状态(非仓库标识), + // 且仓库设置里的开关才是唯一控制项,避免审核中/停用仓库的回信被静默跳过而永久丢失 + const storeRows = await storeModel.findAll({ + where: { id: { [Op.in]: warehouseIds }, deleted_at: null }, + attributes: [ 'id', 'company_name', 'subject_config' ], + raw: true, + }); + const configMap = new Map(); + for (const storeRow of Array.isArray(storeRows) ? storeRows : []) { + const id = Number(storeRow && storeRow.id) || 0; + if (id > 0) { + configMap.set(id, storeRow); + } + } + + for (const warehouseId of warehouseIds) { + const storeRow = configMap.get(warehouseId); + if (!storeRow) { + summary.skippedWarehouses++; + ctx.logger.warn(`[inbox] 自动重推跳过仓库 ${warehouseId}(wms_store 无该仓库记录,无法读取仓库设置)`); + continue; + } + + const retryConfig = readPushRetryConfig(storeRow.subject_config); + if (!retryConfig.enabled) { + summary.disabledWarehouses++; + continue; + } + summary.enabledWarehouses++; + + const result = await this.retryFailed({ + warehouseId, + windowMs: retryConfig.windowMinutes * 60 * 1000, + limit: params.limit, + }); + + summary.scanned += result.scanned; + summary.retried += result.retried; + summary.succeeded += result.succeeded; + summary.failed += result.failed; + summary.skipped += result.skipped; + + if (result.scanned > 0) { + ctx.logger.info( + `[inbox] 仓库 ${warehouseId}(${String(storeRow.company_name || '').trim() || '-'})自动重推:` + + `窗口 ${retryConfig.windowMinutes} 分钟 scanned=${result.scanned} retried=${result.retried} ` + + `succeeded=${result.succeeded} failed=${result.failed} skipped=${result.skipped}` + ); + } + } + + return summary; + } } module.exports = InboxMessageService; diff --git a/app/service/systems/setting/warehouse.js b/app/service/systems/setting/warehouse.js index 2204f51..fee843a 100644 --- a/app/service/systems/setting/warehouse.js +++ b/app/service/systems/setting/warehouse.js @@ -69,6 +69,37 @@ const WAREHOUSE_SETTING_FIELDS = Object.freeze([ required: false, description: '消息命中任一关键词时仅记录工单不发送回信通知,默认 [](正常回信)', }), + Object.freeze({ + key: 'inbox_push_retry', + name: '站内信推送失败自动重发', + type: 'object', + default: Object.freeze({ + enabled: false, + window_minutes: 30, + }), + required: false, + description: '开启后,站内信回信/系统通知推送失败(如桥接离线返回 wx offline / qiwe offline)由定时任务每 3 分钟自动重发;仅重发失败时间在 window_minutes 内的消息,超出范围不再补发', + item_keys: Object.freeze([ + Object.freeze({ + key: 'enabled', + name: '启用自动重发', + type: 'flag', + default: false, + required: true, + description: '默认否,开启后定时任务才会自动重发推送失败的消息', + }), + Object.freeze({ + key: 'window_minutes', + name: '重发时间范围(分钟)', + type: 'integer', + default: 30, + required: false, + min: 1, + max: 1440, + description: '仅自动重发「失败时间在该范围内」的消息,单位:分钟,默认 30,范围 1-1440;仅在开关开启时生效', + }), + ]), + }), Object.freeze({ key: 'default_express_code', name: '默认快递编码', diff --git a/config/config.default.js b/config/config.default.js index dc0e289..43f0f3e 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -66,9 +66,11 @@ module.exports = appInfo => { videoRecordNotifyLimit: 10, // 视频补录结果通知:超时阈值(毫秒),超过仍无录像则按超时回信并终止,避免长期占用扫描窗口 videoRecordNotifyTimeoutMs: 60 * 60 * 1000, - // 出站推送失败自动重推:失败时间窗(毫秒),仅重推窗口内失败的消息(默认 2 小时),超窗口不再补推 - pushRetryWindowMs: 2 * 60 * 60 * 1000, - // 出站推送失败自动重推:单轮最多重推条数(定时任务每 3 分钟扫一次) + // 出站推送失败自动重推:全局总开关(默认 true;置 false 后所有仓库都不再自动重推;仓库是否启用由仓库设置的 inbox_push_retry.enabled 决定,默认关闭) + pushRetryEnabled: true, + // 出站推送失败自动重推:兜底失败时间窗(毫秒,默认 30 分钟);仓库设置项 inbox_push_retry.window_minutes 优先(缺省/非法时用本值) + pushRetryWindowMs: 30 * 60 * 1000, + // 出站推送失败自动重推:单仓单轮最多重推条数(定时任务每 3 分钟扫一次) pushRetryLimit: 20, }; diff --git a/test/app/model/store.test.js b/test/app/model/store.test.js index 8084aa4..78697d2 100644 --- a/test/app/model/store.test.js +++ b/test/app/model/store.test.js @@ -213,4 +213,59 @@ describe('test/app/model/store.test.js', () => { /subject_config\.allocate_start_time 不能等于 subject_config\.cutoff_order_time/ ); }); + + it('should default inbox_push_retry to disabled with 30-minute window (nested object)', () => { + const Store = require('../../../app/model/store')(createApp()); + + const fromNull = Store.normalizeSubjectConfig(null); + const fromEmptyObject = Store.normalizeSubjectConfig({}); + + assert.deepEqual(fromNull.inbox_push_retry, { enabled: false, window_minutes: 30 }); + assert.deepEqual(fromEmptyObject.inbox_push_retry, { enabled: false, window_minutes: 30 }); + + // 非法结构(非对象 / 非法 JSON / 数组)一律回退默认 + [ 'not-json', '[]', 123, true ].forEach(input => { + assert.deepEqual(Store.normalizeSubjectConfig({ inbox_push_retry: input }).inbox_push_retry, { + enabled: false, + window_minutes: 30, + }, `input=${input}`); + }); + }); + + it('should normalize inbox_push_retry.enabled from flag-like values', () => { + const Store = require('../../../app/model/store')(createApp()); + + [ false, 0, '0', 'false' ].forEach(input => { + const normalized = Store.normalizeSubjectConfig({ inbox_push_retry: { enabled: input } }); + assert.equal(normalized.inbox_push_retry.enabled, false, `input=${input}`); + }); + + [ true, 1, '1', 'true' ].forEach(input => { + const normalized = Store.normalizeSubjectConfig({ inbox_push_retry: { enabled: input } }); + assert.equal(normalized.inbox_push_retry.enabled, true, `input=${input}`); + }); + + // 显式传非法类型时抛错(与其它 flag 字段口径一致) + assert.throws( + () => Store.normalizeSubjectConfig({ inbox_push_retry: { enabled: 'yes' } }), + /subject_config\.inbox_push_retry\.enabled 必须是布尔值/ + ); + }); + + it('should accept 1-1440 minute inbox_push_retry.window_minutes and reject out-of-range values', () => { + const Store = require('../../../app/model/store')(createApp()); + + [ 1, 30, 120, 1440 ].forEach(input => { + const normalized = Store.normalizeSubjectConfig({ inbox_push_retry: { window_minutes: input } }); + assert.equal(normalized.inbox_push_retry.window_minutes, input); + }); + + [ 0, -1, 1441, 1.5, 'abc' ].forEach(input => { + assert.throws( + () => Store.normalizeSubjectConfig({ inbox_push_retry: { window_minutes: input } }), + /subject_config\.inbox_push_retry\.window_minutes 必须是 1-1440 的整数(分钟)/, + `input=${input}` + ); + }); + }); });