
























@@ -93,6 +93,28 @@ function expectNoAccountFieldInPassedOptions() {
9393expect(passedOpts).not.toHaveProperty("account");
9494}
959596+function requireRecord(value: unknown, label: string): Record<string, unknown> {
97+if (typeof value !== "object" || value === null || Array.isArray(value)) {
98+throw new Error(`expected ${label} to be an object`);
99+}
100+return value as Record<string, unknown>;
101+}
102+103+function expectMessageCommandOptions(expected: Record<string, unknown>, callIndex = 0): void {
104+const call = (messageCommandMock.mock.calls as unknown[][])[callIndex];
105+if (!call) {
106+throw new Error(`expected messageCommand call ${callIndex}`);
107+}
108+const options = requireRecord(call[0], `messageCommand options ${callIndex}`);
109+for (const [key, expectedValue] of Object.entries(expected)) {
110+expect(options[key], `messageCommand options.${key}`).toEqual(expectedValue);
111+}
112+expect(call[1], "messageCommand runtime").not.toBeNull();
113+expect(call[1], "messageCommand runtime").not.toBeUndefined();
114+expect(call[2], "messageCommand deps").not.toBeNull();
115+expect(call[2], "messageCommand deps").not.toBeUndefined();
116+}
117+96118describe("runMessageAction", () => {
97119beforeEach(() => {
98120vi.clearAllMocks();
@@ -141,16 +163,12 @@ describe("runMessageAction", () => {
141163await runSendAction({ target: "channel:12345" });
142164143165expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled();
144-expect(messageCommandMock).toHaveBeenCalledWith(
145-expect.objectContaining({
146-action: "send",
147-channel: "discord",
148-target: "channel:12345",
149-message: "hi",
150-}),
151-expect.anything(),
152-expect.anything(),
153-);
166+expectMessageCommandOptions({
167+action: "send",
168+channel: "discord",
169+target: "channel:12345",
170+message: "hi",
171+});
154172expect(exitMock).toHaveBeenCalledWith(0);
155173});
156174@@ -168,15 +186,11 @@ describe("runMessageAction", () => {
168186scope: "configured-channels",
169187onlyChannelIds: ["telegram"],
170188});
171-expect(messageCommandMock).toHaveBeenCalledWith(
172-expect.objectContaining({
173-action: "broadcast",
174-targets: ["telegram:1", "telegram:2"],
175-message: "hi",
176-}),
177-expect.anything(),
178-expect.anything(),
179-);
189+expectMessageCommandOptions({
190+action: "broadcast",
191+targets: ["telegram:1", "telegram:2"],
192+message: "hi",
193+});
180194});
181195182196it("keeps unknown actions on the local preload path", async () => {
@@ -194,13 +208,7 @@ describe("runMessageAction", () => {
194208scope: "configured-channels",
195209onlyChannelIds: ["discord"],
196210});
197-expect(messageCommandMock).toHaveBeenCalledWith(
198-expect.objectContaining({
199-action: "custom-action",
200-}),
201-expect.anything(),
202-expect.anything(),
203-);
211+expectMessageCommandOptions({ action: "custom-action" });
204212});
205213206214it("preloads when the scoped channel plugin is not cheaply available", async () => {
@@ -218,15 +226,11 @@ describe("runMessageAction", () => {
218226await runSendAction({ channel: undefined, target: "telegram:12345" });
219227220228expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled();
221-expect(messageCommandMock).toHaveBeenCalledWith(
222-expect.objectContaining({
223-action: "send",
224-target: "telegram:12345",
225-message: "hi",
226-}),
227-expect.anything(),
228-expect.anything(),
229-);
229+expectMessageCommandOptions({
230+action: "send",
231+target: "telegram:12345",
232+message: "hi",
233+});
230234expect(exitMock).toHaveBeenCalledWith(0);
231235});
232236@@ -242,21 +246,17 @@ describe("runMessageAction", () => {
242246});
243247244248expect(ensurePluginRegistryLoaded).not.toHaveBeenCalled();
245-expect(messageCommandMock).toHaveBeenCalledWith(
246-expect.objectContaining({
247-action: "send",
248-channel: "telegram",
249-accountId: "default",
250-target: "@ops",
251-message: "hi",
252-media: "./diagram.png",
253-presentation: '{"blocks":[{"type":"buttons","buttons":[{"label":"OK","value":"ok"}]}]}',
254-delivery: '{"pin":true}',
255-forceDocument: true,
256-}),
257-expect.anything(),
258-expect.anything(),
259-);
249+expectMessageCommandOptions({
250+action: "send",
251+channel: "telegram",
252+accountId: "default",
253+target: "@ops",
254+message: "hi",
255+media: "./diagram.png",
256+presentation: '{"blocks":[{"type":"buttons","buttons":[{"label":"OK","value":"ok"}]}]}',
257+delivery: '{"pin":true}',
258+forceDocument: true,
259+});
260260expectNoAccountFieldInPassedOptions();
261261expect(exitMock).toHaveBeenCalledWith(0);
262262});
@@ -419,17 +419,13 @@ describe("runMessageAction", () => {
419419}),
420420).rejects.toThrow("exit");
421421422-expect(messageCommandMock).toHaveBeenCalledWith(
423-expect.objectContaining({
424-action: "poll",
425-channel: "discord",
426-target: "456",
427-accountId: "acct-1",
428-message: "hi",
429-}),
430-expect.anything(),
431-expect.anything(),
432-);
422+expectMessageCommandOptions({
423+action: "poll",
424+channel: "discord",
425+target: "456",
426+accountId: "acct-1",
427+message: "hi",
428+});
433429// account key should be stripped in favor of accountId
434430expectNoAccountFieldInPassedOptions();
435431});
@@ -446,16 +442,12 @@ describe("runMessageAction", () => {
446442}),
447443).rejects.toThrow("exit");
448444449-expect(messageCommandMock).toHaveBeenCalledWith(
450-expect.objectContaining({
451-action: "send",
452-channel: "discord",
453-target: "789",
454-accountId: undefined,
455-}),
456-expect.anything(),
457-expect.anything(),
458-);
445+expectMessageCommandOptions({
446+action: "send",
447+channel: "discord",
448+target: "789",
449+accountId: undefined,
450+});
459451expectNoAccountFieldInPassedOptions();
460452});
461453});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。