From 3c59cc4e677c865161b26cbaa145ccffc6490f6f Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 10 May 2026 14:23:18 +0100 Subject: [PATCH] test: clear cli message helper broad matchers --- src/cli/program/message/helpers.test.ts | 134 +++++++++++------------- 1 file changed, 63 insertions(+), 71 deletions(-) diff --git a/src/cli/program/message/helpers.test.ts b/src/cli/program/message/helpers.test.ts index 78bc833804b..97abd764ae6 100644 --- a/src/cli/program/message/helpers.test.ts +++ b/src/cli/program/message/helpers.test.ts @@ -93,6 +93,28 @@ function expectNoAccountFieldInPassedOptions() { expect(passedOpts).not.toHaveProperty("account"); } +function requireRecord(value: unknown, label: string): Record { + if (typeof value !== "object" || value === null || Array.isArray(value)) { + throw new Error(`expected ${label} to be an object`); + } + return value as Record; +} + +function expectMessageCommandOptions(expected: Record, callIndex = 0): void { + const call = (messageCommandMock.mock.calls as unknown[][])[callIndex]; + if (!call) { + throw new Error(`expected messageCommand call ${callIndex}`); + } + const options = requireRecord(call[0], `messageCommand options ${callIndex}`); + for (const [key, expectedValue] of Object.entries(expected)) { + expect(options[key], `messageCommand options.${key}`).toEqual(expectedValue); + } + expect(call[1], "messageCommand runtime").not.toBeNull(); + expect(call[1], "messageCommand runtime").not.toBeUndefined(); + expect(call[2], "messageCommand deps").not.toBeNull(); + expect(call[2], "messageCommand deps").not.toBeUndefined(); +} + describe("runMessageAction", () => { beforeEach(() => { vi.clearAllMocks(); @@ -141,16 +163,12 @@ describe("runMessageAction", () => { await runSendAction({ target: "channel:12345" }); expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled(); - expect(messageCommandMock).toHaveBeenCalledWith( - expect.objectContaining({ - action: "send", - channel: "discord", - target: "channel:12345", - message: "hi", - }), - expect.anything(), - expect.anything(), - ); + expectMessageCommandOptions({ + action: "send", + channel: "discord", + target: "channel:12345", + message: "hi", + }); expect(exitMock).toHaveBeenCalledWith(0); }); @@ -168,15 +186,11 @@ describe("runMessageAction", () => { scope: "configured-channels", onlyChannelIds: ["telegram"], }); - expect(messageCommandMock).toHaveBeenCalledWith( - expect.objectContaining({ - action: "broadcast", - targets: ["telegram:1", "telegram:2"], - message: "hi", - }), - expect.anything(), - expect.anything(), - ); + expectMessageCommandOptions({ + action: "broadcast", + targets: ["telegram:1", "telegram:2"], + message: "hi", + }); }); it("keeps unknown actions on the local preload path", async () => { @@ -194,13 +208,7 @@ describe("runMessageAction", () => { scope: "configured-channels", onlyChannelIds: ["discord"], }); - expect(messageCommandMock).toHaveBeenCalledWith( - expect.objectContaining({ - action: "custom-action", - }), - expect.anything(), - expect.anything(), - ); + expectMessageCommandOptions({ action: "custom-action" }); }); it("preloads when the scoped channel plugin is not cheaply available", async () => { @@ -218,15 +226,11 @@ describe("runMessageAction", () => { await runSendAction({ channel: undefined, target: "telegram:12345" }); expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled(); - expect(messageCommandMock).toHaveBeenCalledWith( - expect.objectContaining({ - action: "send", - target: "telegram:12345", - message: "hi", - }), - expect.anything(), - expect.anything(), - ); + expectMessageCommandOptions({ + action: "send", + target: "telegram:12345", + message: "hi", + }); expect(exitMock).toHaveBeenCalledWith(0); }); @@ -242,21 +246,17 @@ describe("runMessageAction", () => { }); expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled(); - expect(messageCommandMock).toHaveBeenCalledWith( - expect.objectContaining({ - action: "send", - channel: "telegram", - accountId: "default", - target: "@ops", - message: "hi", - media: "./diagram.png", - presentation: '{"blocks":[{"type":"buttons","buttons":[{"label":"OK","value":"ok"}]}]}', - delivery: '{"pin":true}', - forceDocument: true, - }), - expect.anything(), - expect.anything(), - ); + expectMessageCommandOptions({ + action: "send", + channel: "telegram", + accountId: "default", + target: "@ops", + message: "hi", + media: "./diagram.png", + presentation: '{"blocks":[{"type":"buttons","buttons":[{"label":"OK","value":"ok"}]}]}', + delivery: '{"pin":true}', + forceDocument: true, + }); expectNoAccountFieldInPassedOptions(); expect(exitMock).toHaveBeenCalledWith(0); }); @@ -419,17 +419,13 @@ describe("runMessageAction", () => { }), ).rejects.toThrow("exit"); - expect(messageCommandMock).toHaveBeenCalledWith( - expect.objectContaining({ - action: "poll", - channel: "discord", - target: "456", - accountId: "acct-1", - message: "hi", - }), - expect.anything(), - expect.anything(), - ); + expectMessageCommandOptions({ + action: "poll", + channel: "discord", + target: "456", + accountId: "acct-1", + message: "hi", + }); // account key should be stripped in favor of accountId expectNoAccountFieldInPassedOptions(); }); @@ -446,16 +442,12 @@ describe("runMessageAction", () => { }), ).rejects.toThrow("exit"); - expect(messageCommandMock).toHaveBeenCalledWith( - expect.objectContaining({ - action: "send", - channel: "discord", - target: "789", - accountId: undefined, - }), - expect.anything(), - expect.anything(), - ); + expectMessageCommandOptions({ + action: "send", + channel: "discord", + target: "789", + accountId: undefined, + }); expectNoAccountFieldInPassedOptions(); }); });