|
1 | 1 | // Message program helper tests cover message command helper behavior and mocks. |
| 2 | +import { Command } from "commander"; |
2 | 3 | import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures"; |
3 | 4 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { registerMessageSendCommand } from "./register.send.js"; |
4 | 6 |
|
5 | 7 | const messageCommandMock = vi.fn(async (): Promise<unknown> => undefined); |
6 | 8 | vi.mock("../../../commands/message.js", () => ({ |
@@ -150,6 +152,66 @@ describe("runMessageAction", () => { |
150 | 152 | expect(exitMock).toHaveBeenCalledWith(0); |
151 | 153 | }); |
152 | 154 |
|
| 155 | + it.each([ |
| 156 | + { name: "sent", status: "sent" as const, exitCode: 0 }, |
| 157 | + { name: "suppressed", status: "suppressed" as const, exitCode: 1 }, |
| 158 | + { name: "failed", status: "failed" as const, exitCode: 1 }, |
| 159 | + { name: "partial_failed", status: "partial_failed" as const, exitCode: 1 }, |
| 160 | + { name: "dry-run", status: undefined, dryRun: true, exitCode: 0 }, |
| 161 | + ])( |
| 162 | + "propagates $name send outcomes through the real CLI parser", |
| 163 | + async ({ status, dryRun, exitCode }) => { |
| 164 | + const sendResult = { |
| 165 | + channel: "discord", |
| 166 | + to: "channel:123", |
| 167 | + via: "direct" as const, |
| 168 | + mediaUrl: null, |
| 169 | + ...(status ? { deliveryStatus: status } : {}), |
| 170 | + ...(status === "suppressed" |
| 171 | + ? { suppressionReason: "cancelled_by_message_sending_hook" as const } |
| 172 | + : {}), |
| 173 | + ...(status === "failed" || status === "partial_failed" |
| 174 | + ? { error: "provider rejected the message" } |
| 175 | + : {}), |
| 176 | + ...(status === "partial_failed" |
| 177 | + ? { sentBeforeError: true as const, result: { channel: "discord", messageId: "part-1" } } |
| 178 | + : {}), |
| 179 | + }; |
| 180 | + messageCommandMock.mockResolvedValueOnce({ |
| 181 | + kind: "send", |
| 182 | + channel: "discord", |
| 183 | + action: "send", |
| 184 | + to: "channel:123", |
| 185 | + handledBy: "core", |
| 186 | + payload: sendResult, |
| 187 | + sendResult, |
| 188 | + dryRun: Boolean(dryRun), |
| 189 | + }); |
| 190 | + const program = new Command(); |
| 191 | + const message = program.command("message"); |
| 192 | + registerMessageSendCommand(message, createMessageCliHelpers(message, "discord")); |
| 193 | + |
| 194 | + await expect( |
| 195 | + program.parseAsync( |
| 196 | + [ |
| 197 | + "message", |
| 198 | + "send", |
| 199 | + "--channel", |
| 200 | + "discord", |
| 201 | + "--target", |
| 202 | + "channel:123", |
| 203 | + "--message", |
| 204 | + "hi", |
| 205 | + ...(dryRun ? ["--dry-run"] : []), |
| 206 | + ], |
| 207 | + { from: "user" }, |
| 208 | + ), |
| 209 | + ).rejects.toThrow("exit"); |
| 210 | + |
| 211 | + expect(exitMock).toHaveBeenCalledWith(exitCode); |
| 212 | + }, |
| 213 | + ); |
| 214 | + |
153 | 215 | it("loads configured channel plugins when no target channel is known yet", async () => { |
154 | 216 | await runSendAction({ channel: undefined }); |
155 | 217 |
|
|
0 commit comments