From 8e6acded8274ccaf788fa098162013f8e2d2e97e Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 7 Mar 2026 17:03:22 +0000 Subject: [PATCH] refactor(commands): dedupe message command secret-config tests --- src/commands/message.test.ts | 205 ++++++++++++++++------------------- 1 file changed, 91 insertions(+), 114 deletions(-) diff --git a/src/commands/message.test.ts b/src/commands/message.test.ts index 658eb9fd614..4bc01909098 100644 --- a/src/commands/message.test.ts +++ b/src/commands/message.test.ts @@ -186,26 +186,94 @@ const createTelegramPollPluginRegistration = () => ({ const { messageCommand } = await import("./message.js"); +function createTelegramSecretRawConfig() { + return { + channels: { + telegram: { + token: { $secret: "vault://telegram/token" }, + }, + }, + }; +} + +function createTelegramResolvedTokenConfig(token: string) { + return { + channels: { + telegram: { + token, + }, + }, + }; +} + +function mockResolvedCommandConfig(params: { + rawConfig: Record; + resolvedConfig: Record; + diagnostics?: string[]; +}) { + testConfig = params.rawConfig; + resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({ + resolvedConfig: params.resolvedConfig, + diagnostics: params.diagnostics ?? ["resolved channels.telegram.token"], + }); +} + +async function runTelegramDirectOutboundSend(params: { + rawConfig: Record; + resolvedConfig: Record; + diagnostics?: string[]; +}) { + mockResolvedCommandConfig(params); + const sendText = vi.fn(async (_ctx: { cfg?: unknown; to?: string; text?: string }) => ({ + channel: "telegram" as const, + messageId: "msg-1", + chatId: "123456", + })); + const sendMedia = vi.fn(async (_ctx: { cfg?: unknown }) => ({ + channel: "telegram" as const, + messageId: "msg-2", + chatId: "123456", + })); + await setRegistry( + createTestRegistry([ + { + pluginId: "telegram", + source: "test", + plugin: createStubPlugin({ + id: "telegram", + label: "Telegram", + outbound: { + deliveryMode: "direct", + sendText, + sendMedia, + }, + }), + }, + ]), + ); + + const deps = makeDeps(); + await messageCommand( + { + action: "send", + channel: "telegram", + target: "123456", + message: "hi", + }, + deps, + runtime, + ); + + return { sendText }; +} + describe("messageCommand", () => { it("threads resolved SecretRef config into outbound send actions", async () => { - const rawConfig = { - channels: { - telegram: { - token: { $secret: "vault://telegram/token" }, - }, - }, - }; - const resolvedConfig = { - channels: { - telegram: { - token: "12345:resolved-token", - }, - }, - }; - testConfig = rawConfig; - resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({ + const rawConfig = createTelegramSecretRawConfig(); + const resolvedConfig = createTelegramResolvedTokenConfig("12345:resolved-token"); + mockResolvedCommandConfig({ + rawConfig: rawConfig as unknown as Record, resolvedConfig: resolvedConfig as unknown as Record, - diagnostics: ["resolved channels.telegram.token"], }); await setRegistry( createTestRegistry([ @@ -240,64 +308,12 @@ describe("messageCommand", () => { }); it("threads resolved SecretRef config into outbound adapter sends", async () => { - const rawConfig = { - channels: { - telegram: { - token: { $secret: "vault://telegram/token" }, - }, - }, - }; - const resolvedConfig = { - channels: { - telegram: { - token: "12345:resolved-token", - }, - }, - }; - testConfig = rawConfig; - resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({ + const rawConfig = createTelegramSecretRawConfig(); + const resolvedConfig = createTelegramResolvedTokenConfig("12345:resolved-token"); + const { sendText } = await runTelegramDirectOutboundSend({ + rawConfig: rawConfig as unknown as Record, resolvedConfig: resolvedConfig as unknown as Record, - diagnostics: ["resolved channels.telegram.token"], }); - const sendText = vi.fn(async (_ctx: { cfg?: unknown; to: string; text: string }) => ({ - channel: "telegram" as const, - messageId: "msg-1", - chatId: "123456", - })); - const sendMedia = vi.fn(async (_ctx: { cfg?: unknown }) => ({ - channel: "telegram" as const, - messageId: "msg-2", - chatId: "123456", - })); - await setRegistry( - createTestRegistry([ - { - pluginId: "telegram", - source: "test", - plugin: createStubPlugin({ - id: "telegram", - label: "Telegram", - outbound: { - deliveryMode: "direct", - sendText, - sendMedia, - }, - }), - }, - ]), - ); - - const deps = makeDeps(); - await messageCommand( - { - action: "send", - channel: "telegram", - target: "123456", - message: "hi", - }, - deps, - runtime, - ); expect(sendText).toHaveBeenCalledWith( expect.objectContaining({ @@ -324,50 +340,11 @@ describe("messageCommand", () => { }, }, }; - testConfig = rawConfig; - resolveCommandSecretRefsViaGateway.mockResolvedValueOnce({ + const { sendText } = await runTelegramDirectOutboundSend({ + rawConfig: rawConfig as unknown as Record, resolvedConfig: locallyResolvedConfig as unknown as Record, diagnostics: ["gateway secrets.resolve unavailable; used local resolver fallback."], }); - const sendText = vi.fn(async (_ctx: { cfg?: unknown }) => ({ - channel: "telegram" as const, - messageId: "msg-3", - chatId: "123456", - })); - const sendMedia = vi.fn(async (_ctx: { cfg?: unknown }) => ({ - channel: "telegram" as const, - messageId: "msg-4", - chatId: "123456", - })); - await setRegistry( - createTestRegistry([ - { - pluginId: "telegram", - source: "test", - plugin: createStubPlugin({ - id: "telegram", - label: "Telegram", - outbound: { - deliveryMode: "direct", - sendText, - sendMedia, - }, - }), - }, - ]), - ); - - const deps = makeDeps(); - await messageCommand( - { - action: "send", - channel: "telegram", - target: "123456", - message: "hi", - }, - deps, - runtime, - ); expect(sendText).toHaveBeenCalledWith( expect.objectContaining({