|
1 | 1 | // Covers core message-action send fallback, TTS application, and durable send |
2 | 2 | // policy after plugin preparation is absent. |
3 | 3 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { |
| 5 | + GATEWAY_CLIENT_MODES, |
| 6 | + GATEWAY_CLIENT_NAMES, |
| 7 | +} from "../../../packages/gateway-protocol/src/client-info.js"; |
4 | 8 | import { getReplyPayloadMetadata } from "../../auto-reply/reply-payload.js"; |
5 | 9 | import type { OpenClawConfig } from "../../config/config.js"; |
6 | 10 | import { setActivePluginRegistry } from "../../plugins/runtime.js"; |
7 | 11 | import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js"; |
| 12 | +import { OutboundDeliveryError } from "./deliver-types.js"; |
8 | 13 | import { runMessageAction } from "./message-action-runner.js"; |
9 | 14 |
|
10 | 15 | const ttsMocks = vi.hoisted(() => ({ |
@@ -77,6 +82,104 @@ describe("runMessageAction core send routing", () => { |
77 | 82 | .mockReset() |
78 | 83 | .mockImplementation(async (params: { payload: unknown }) => params.payload); |
79 | 84 | }); |
| 85 | + |
| 86 | + it.each([ |
| 87 | + { |
| 88 | + label: "failed", |
| 89 | + error: new Error("provider rejected the message"), |
| 90 | + expectedStatus: "failed" as const, |
| 91 | + expectedMessageId: undefined, |
| 92 | + }, |
| 93 | + { |
| 94 | + label: "partial_failed", |
| 95 | + error: new OutboundDeliveryError("second send failed", { |
| 96 | + cause: new Error("provider rejected the attachment"), |
| 97 | + results: [{ channel: "testchat", messageId: "first-send-1" }], |
| 98 | + stage: "platform_send", |
| 99 | + }), |
| 100 | + expectedStatus: "partial_failed" as const, |
| 101 | + expectedMessageId: "first-send-1", |
| 102 | + }, |
| 103 | + ])( |
| 104 | + "returns structured $label core delivery outcomes to CLI callers", |
| 105 | + async ({ error, expectedStatus, expectedMessageId }) => { |
| 106 | + const sendText = vi.fn().mockRejectedValue(error); |
| 107 | + setActivePluginRegistry( |
| 108 | + createTestRegistry([ |
| 109 | + { |
| 110 | + pluginId: "testchat", |
| 111 | + source: "test", |
| 112 | + plugin: createOutboundTestPlugin({ |
| 113 | + id: "testchat", |
| 114 | + outbound: { deliveryMode: "direct", sendText }, |
| 115 | + }), |
| 116 | + }, |
| 117 | + ]), |
| 118 | + ); |
| 119 | + |
| 120 | + const result = await runMessageAction({ |
| 121 | + cfg: { |
| 122 | + channels: { testchat: { enabled: true } }, |
| 123 | + } as OpenClawConfig, |
| 124 | + action: "send", |
| 125 | + params: { |
| 126 | + channel: "testchat", |
| 127 | + target: "channel:abc", |
| 128 | + message: "hello", |
| 129 | + }, |
| 130 | + gateway: { |
| 131 | + clientName: GATEWAY_CLIENT_NAMES.CLI, |
| 132 | + mode: GATEWAY_CLIENT_MODES.CLI, |
| 133 | + }, |
| 134 | + conversationReadOrigin: "direct-operator", |
| 135 | + dryRun: false, |
| 136 | + }); |
| 137 | + |
| 138 | + expect(result).toMatchObject({ |
| 139 | + kind: "send", |
| 140 | + handledBy: "core", |
| 141 | + sendResult: { |
| 142 | + deliveryStatus: expectedStatus, |
| 143 | + error: expect.any(String), |
| 144 | + ...(expectedMessageId ? { result: { messageId: expectedMessageId } } : {}), |
| 145 | + }, |
| 146 | + }); |
| 147 | + expect(sendText).toHaveBeenCalledOnce(); |
| 148 | + }, |
| 149 | + ); |
| 150 | + |
| 151 | + it("keeps throwing core delivery failures for non-CLI callers", async () => { |
| 152 | + const sendText = vi.fn().mockRejectedValue(new Error("provider rejected the message")); |
| 153 | + setActivePluginRegistry( |
| 154 | + createTestRegistry([ |
| 155 | + { |
| 156 | + pluginId: "testchat", |
| 157 | + source: "test", |
| 158 | + plugin: createOutboundTestPlugin({ |
| 159 | + id: "testchat", |
| 160 | + outbound: { deliveryMode: "direct", sendText }, |
| 161 | + }), |
| 162 | + }, |
| 163 | + ]), |
| 164 | + ); |
| 165 | + |
| 166 | + await expect( |
| 167 | + runMessageAction({ |
| 168 | + cfg: { |
| 169 | + channels: { testchat: { enabled: true } }, |
| 170 | + } as OpenClawConfig, |
| 171 | + action: "send", |
| 172 | + params: { |
| 173 | + channel: "testchat", |
| 174 | + target: "channel:abc", |
| 175 | + message: "hello", |
| 176 | + }, |
| 177 | + dryRun: false, |
| 178 | + }), |
| 179 | + ).rejects.toThrow("provider rejected the message"); |
| 180 | + expect(sendText).toHaveBeenCalledOnce(); |
| 181 | + }); |
| 182 | + |
80 | 183 | it("does not misclassify send as poll when zero-valued poll params are present", async () => { |
81 | 184 | const sendMedia = vi.fn().mockResolvedValue({ |
82 | 185 | channel: "testchat", |
|
0 commit comments