From 245451148029cf440829a85deabaa4e69ef6dc46 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Mon, 13 Jul 2026 14:02:10 +0800 Subject: [PATCH] refactor(sms): consolidate webhook test hooks (#106072) --- extensions/sms/src/webhook.test.ts | 8 +++----- extensions/sms/src/webhook.ts | 10 ++++++++-- scripts/deadcode-exports.baseline.mjs | 3 +-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/extensions/sms/src/webhook.test.ts b/extensions/sms/src/webhook.test.ts index 6591f347958e..5e15859b26ef 100644 --- a/extensions/sms/src/webhook.test.ts +++ b/extensions/sms/src/webhook.test.ts @@ -5,11 +5,9 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; import type { SmsChannelRuntime } from "./inbound.js"; import { computeTwilioSignature, parseTwilioFormBody } from "./twilio.js"; import type { ResolvedSmsAccount } from "./types.js"; -import { - createSmsWebhookHandler, - createSmsWebhookReplayGuard, - resetSmsWebhookReplayGuardsForTest, -} from "./webhook.js"; +import { createSmsWebhookHandler, testing } from "./webhook.js"; + +const { createSmsWebhookReplayGuard, resetSmsWebhookReplayGuardsForTest } = testing; const dispatchSmsInboundEvent = vi.hoisted(() => vi.fn(async () => undefined)); diff --git a/extensions/sms/src/webhook.ts b/extensions/sms/src/webhook.ts index 2cfd94e0b7b4..f62f215ee795 100644 --- a/extensions/sms/src/webhook.ts +++ b/extensions/sms/src/webhook.ts @@ -46,7 +46,7 @@ type SmsWebhookReplayGuard = { const replayGuardsByAccount = new Map(); -export function createSmsWebhookReplayGuard( +function createSmsWebhookReplayGuard( options: { ttlMs?: number; maxKeys?: number; @@ -102,7 +102,7 @@ function resolveSmsWebhookReplayGuard(account: ResolvedSmsAccount): SmsWebhookRe return created; } -export function resetSmsWebhookReplayGuardsForTest(): void { +function resetSmsWebhookReplayGuardsForTest(): void { replayGuardsByAccount.clear(); invalidRequestRateLimiter.clear(); callbackDispatchRateLimiter.clear(); @@ -154,6 +154,12 @@ function rejectInvalidRequestRateLimit(params: { return true; } +/** Test-only hooks for webhook state that is otherwise private. */ +export const testing = { + createSmsWebhookReplayGuard, + resetSmsWebhookReplayGuardsForTest, +}; + // Each account route owns its guard so one saturated account cannot block sibling accounts. export function createSmsWebhookHandler( params: SmsWebhookHandlerParams, diff --git a/scripts/deadcode-exports.baseline.mjs b/scripts/deadcode-exports.baseline.mjs index 7617cc21e1af..87f89d80b4f8 100644 --- a/scripts/deadcode-exports.baseline.mjs +++ b/scripts/deadcode-exports.baseline.mjs @@ -914,8 +914,7 @@ export const KNIP_UNUSED_EXPORT_BASELINE = [ "extensions/sms/src/twilio.ts: computeTwilioSignature", "extensions/sms/src/twilio.ts: parseTwilioFormBody", "extensions/sms/src/twilio.ts: TwilioSmsApiError", - "extensions/sms/src/webhook.ts: createSmsWebhookReplayGuard", - "extensions/sms/src/webhook.ts: resetSmsWebhookReplayGuardsForTest", + "extensions/sms/src/webhook.ts: testing", "extensions/synology-chat/src/channel.ts: createSynologyChatPlugin", "extensions/synology-chat/src/channel.ts: synologyChatMessageAdapter", "extensions/synology-chat/src/client.ts: fetchChatUsers (synologyClient)",