|
1 | | -// Whatsapp tests cover resolve outbound target plugin behavior. |
2 | | -import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | | -import * as normalize from "./normalize-target.js"; |
4 | | - |
5 | | -vi.mock("./normalize-target.js"); |
6 | | -vi.mock("openclaw/plugin-sdk/channel-feedback", () => ({ |
7 | | - missingTargetError: (platform: string, format: string) => new Error(`${platform}: ${format}`), |
8 | | -})); |
9 | | - |
10 | | -let resolveWhatsAppOutboundTarget: typeof import("./resolve-outbound-target.js").resolveWhatsAppOutboundTarget; |
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { resolveWhatsAppOutboundTarget } from "./resolve-outbound-target.js"; |
11 | 3 |
|
12 | 4 | type ResolveParams = Parameters<typeof resolveWhatsAppOutboundTarget>[0]; |
| 5 | + |
13 | 6 | const PRIMARY_TARGET = "+11234567890"; |
14 | 7 | const SECONDARY_TARGET = "+19876543210"; |
15 | 8 |
|
16 | | -function expectResolutionError(params: ResolveParams) { |
17 | | - const result = resolveWhatsAppOutboundTarget(params); |
18 | | - expect(result.ok).toBe(false); |
19 | | - if (result.ok) { |
20 | | - throw new Error("expected resolution to fail"); |
21 | | - } |
22 | | - expect(result.error.message).toContain("WhatsApp"); |
23 | | -} |
24 | | - |
25 | | -function expectResolutionErrorMessage(params: ResolveParams, expectedMessage: string) { |
26 | | - const result = resolveWhatsAppOutboundTarget(params); |
27 | | - expect(result.ok).toBe(false); |
28 | | - if (result.ok) { |
29 | | - throw new Error("expected resolution to fail"); |
30 | | - } |
31 | | - expect(result.error.message).toBe(expectedMessage); |
32 | | -} |
33 | | - |
34 | | -function expectResolutionOk(params: ResolveParams, expectedTarget: string) { |
35 | | - const result = resolveWhatsAppOutboundTarget(params); |
36 | | - expect(result).toEqual({ ok: true, to: expectedTarget }); |
37 | | -} |
38 | | - |
39 | | -function mockNormalizedDirectMessage(...values: Array<string | null>) { |
40 | | - const normalizeMock = vi.mocked(normalize.normalizeWhatsAppTarget); |
41 | | - for (const value of values) { |
42 | | - normalizeMock.mockReturnValueOnce(value); |
43 | | - } |
44 | | - vi.mocked(normalize.isWhatsAppGroupJid).mockReturnValueOnce(false); |
45 | | -} |
46 | | - |
47 | | -function expectAllowedForTarget(params: { |
48 | | - allowFrom: ResolveParams["allowFrom"]; |
49 | | - mode: ResolveParams["mode"]; |
50 | | - to?: string; |
51 | | -}) { |
52 | | - const to = params.to ?? PRIMARY_TARGET; |
53 | | - expectResolutionOk( |
54 | | - { |
55 | | - to, |
56 | | - allowFrom: params.allowFrom, |
57 | | - mode: params.mode, |
58 | | - }, |
59 | | - to, |
60 | | - ); |
61 | | -} |
62 | | - |
63 | | -function expectDeniedForTarget(params: { |
64 | | - allowFrom: ResolveParams["allowFrom"]; |
65 | | - mode: ResolveParams["mode"]; |
66 | | - to?: string; |
67 | | -}) { |
68 | | - expectResolutionError({ |
69 | | - to: params.to ?? PRIMARY_TARGET, |
70 | | - allowFrom: params.allowFrom, |
71 | | - mode: params.mode, |
72 | | - }); |
73 | | -} |
74 | | - |
75 | 9 | describe("resolveWhatsAppOutboundTarget", () => { |
76 | | - beforeEach(async () => { |
77 | | - vi.resetModules(); |
78 | | - vi.resetAllMocks(); |
79 | | - ({ resolveWhatsAppOutboundTarget } = await import("./resolve-outbound-target.js")); |
80 | | - }); |
81 | | - |
82 | | - describe("empty/missing to parameter", () => { |
83 | | - it.each([ |
84 | | - ["null", null], |
85 | | - ["undefined", undefined], |
86 | | - ["empty string", ""], |
87 | | - ["whitespace only", " "], |
88 | | - ])("returns error when to is %s", (_label, to) => { |
89 | | - expectResolutionError({ to, allowFrom: undefined, mode: undefined }); |
90 | | - }); |
91 | | - }); |
92 | | - |
93 | | - describe("normalization failures", () => { |
94 | | - it("returns error when normalizeWhatsAppTarget returns null/undefined", () => { |
95 | | - vi.mocked(normalize.normalizeWhatsAppTarget).mockReturnValueOnce(null); |
96 | | - expectResolutionError({ |
97 | | - to: "+1234567890", |
98 | | - allowFrom: undefined, |
99 | | - mode: undefined, |
100 | | - }); |
| 10 | + it.each([null, undefined, "", " ", "invalid"])("rejects missing or invalid target %j", (to) => { |
| 11 | + expect(resolveWhatsAppOutboundTarget({ to, allowFrom: undefined, mode: undefined })).toEqual({ |
| 12 | + ok: false, |
| 13 | + error: expect.objectContaining({ |
| 14 | + message: "Delivering to WhatsApp requires target <E.164|group JID|newsletter JID>", |
| 15 | + }), |
101 | 16 | }); |
102 | 17 | }); |
103 | 18 |
|
104 | | - describe("group JID handling", () => { |
105 | | - it("returns success for valid group JID regardless of mode", () => { |
106 | | - vi.mocked(normalize.normalizeWhatsAppTarget).mockReturnValueOnce("[email protected]"); |
107 | | - vi.mocked(normalize.isWhatsAppGroupJid).mockReturnValueOnce(true); |
108 | | - |
109 | | - expectResolutionOk( |
110 | | - { |
111 | | - |
112 | | - allowFrom: undefined, |
113 | | - mode: "implicit", |
114 | | - }, |
115 | | - |
116 | | - ); |
117 | | - }); |
118 | | - |
119 | | - it("returns success for group JID in heartbeat mode", () => { |
120 | | - vi.mocked(normalize.normalizeWhatsAppTarget).mockReturnValueOnce("[email protected]"); |
121 | | - vi.mocked(normalize.isWhatsAppGroupJid).mockReturnValueOnce(true); |
122 | | - |
123 | | - expectResolutionOk( |
124 | | - { |
125 | | - |
126 | | - allowFrom: undefined, |
127 | | - mode: "heartbeat", |
128 | | - }, |
129 | | - |
130 | | - ); |
| 19 | + it.each([ |
| 20 | + ["[email protected]", "implicit"], |
| 21 | + ["[email protected]", "heartbeat"], |
| 22 | + ["120363123456789@newsletter", "implicit"], |
| 23 | + ])("does not apply DM allowFrom to %s in %s mode", (to, mode) => { |
| 24 | + expect(resolveWhatsAppOutboundTarget({ to, allowFrom: [SECONDARY_TARGET], mode })).toEqual({ |
| 25 | + ok: true, |
| 26 | + to, |
131 | 27 | }); |
132 | 28 | }); |
133 | 29 |
|
134 | | - describe("newsletter JID handling", () => { |
135 | | - it("returns success for valid newsletter JID without applying DM allowFrom", () => { |
136 | | - vi.mocked(normalize.normalizeWhatsAppTarget).mockReturnValueOnce( |
137 | | - "120363123456789@newsletter", |
138 | | - ); |
139 | | - vi.mocked(normalize.isWhatsAppGroupJid).mockReturnValueOnce(false); |
140 | | - vi.mocked(normalize.isWhatsAppNewsletterJid).mockReturnValueOnce(true); |
141 | | - |
142 | | - expectResolutionOk( |
143 | | - { |
144 | | - to: "120363123456789@newsletter", |
145 | | - allowFrom: [SECONDARY_TARGET], |
146 | | - mode: "implicit", |
147 | | - }, |
148 | | - "120363123456789@newsletter", |
149 | | - ); |
150 | | - expect(vi.mocked(normalize.normalizeWhatsAppTarget)).toHaveBeenCalledOnce(); |
151 | | - expect(vi.mocked(normalize.normalizeWhatsAppTarget)).toHaveBeenCalledWith( |
152 | | - "120363123456789@newsletter", |
153 | | - ); |
| 30 | + it.each<[string, ResolveParams["allowFrom"], ResolveParams["mode"]]>([ |
| 31 | + ["wildcard", ["*"], "implicit"], |
| 32 | + ["empty allowFrom", [], "implicit"], |
| 33 | + ["matching target", [PRIMARY_TARGET], "implicit"], |
| 34 | + ["numeric target", [11234567890, SECONDARY_TARGET], "implicit"], |
| 35 | + ["invalid entry beside matching target", ["invalid", PRIMARY_TARGET], "implicit"], |
| 36 | + ["whitespace in allowFrom", [` ${PRIMARY_TARGET} `], undefined], |
| 37 | + ["no policy in null mode", undefined, null], |
| 38 | + ["no policy in unspecified mode", undefined, undefined], |
| 39 | + ["matching target in heartbeat mode", [PRIMARY_TARGET], "heartbeat"], |
| 40 | + ["matching target in custom mode", [PRIMARY_TARGET], "broadcast"], |
| 41 | + ])("allows %s", (_label, allowFrom, mode) => { |
| 42 | + expect(resolveWhatsAppOutboundTarget({ to: PRIMARY_TARGET, allowFrom, mode })).toEqual({ |
| 43 | + ok: true, |
| 44 | + to: PRIMARY_TARGET, |
154 | 45 | }); |
155 | 46 | }); |
156 | 47 |
|
157 | | - describe("implicit/heartbeat mode with allowList", () => { |
158 | | - it("allows message when wildcard is present", () => { |
159 | | - mockNormalizedDirectMessage(PRIMARY_TARGET, PRIMARY_TARGET); |
160 | | - expectAllowedForTarget({ allowFrom: ["*"], mode: "implicit" }); |
161 | | - }); |
162 | | - |
163 | | - it("allows message when allowList is empty", () => { |
164 | | - mockNormalizedDirectMessage(PRIMARY_TARGET, PRIMARY_TARGET); |
165 | | - expectAllowedForTarget({ allowFrom: [], mode: "implicit" }); |
166 | | - }); |
167 | | - |
168 | | - it("allows message when target is in allowList", () => { |
169 | | - mockNormalizedDirectMessage(PRIMARY_TARGET, PRIMARY_TARGET); |
170 | | - expectAllowedForTarget({ allowFrom: [PRIMARY_TARGET], mode: "implicit" }); |
171 | | - }); |
172 | | - |
173 | | - it("denies message when target is not in allowList", () => { |
174 | | - mockNormalizedDirectMessage(PRIMARY_TARGET, SECONDARY_TARGET); |
175 | | - expectResolutionErrorMessage( |
176 | | - { |
177 | | - to: PRIMARY_TARGET, |
| 48 | + it.each(["implicit", "heartbeat", "broadcast"])( |
| 49 | + "denies an unlisted target in %s mode with its normalized address", |
| 50 | + (mode) => { |
| 51 | + expect( |
| 52 | + resolveWhatsAppOutboundTarget({ |
| 53 | + to: " +1 (123) 456-7890 ", |
178 | 54 | allowFrom: [SECONDARY_TARGET], |
179 | | - mode: "implicit", |
180 | | - }, |
181 | | - `Target "${PRIMARY_TARGET}" is not listed in the configured WhatsApp allowFrom policy.`, |
182 | | - ); |
183 | | - }); |
184 | | - |
185 | | - it("uses the normalized target in the allowFrom error message", () => { |
186 | | - vi.mocked(normalize.normalizeWhatsAppTarget) |
187 | | - .mockReturnValueOnce(PRIMARY_TARGET) |
188 | | - .mockReturnValueOnce(SECONDARY_TARGET); |
189 | | - vi.mocked(normalize.isWhatsAppGroupJid).mockReturnValueOnce(false); |
190 | | - |
191 | | - expectResolutionErrorMessage( |
192 | | - { |
193 | | - to: "(123) 456-7890", |
194 | | - allowFrom: [SECONDARY_TARGET], |
195 | | - mode: "implicit", |
196 | | - }, |
197 | | - `Target "${PRIMARY_TARGET}" is not listed in the configured WhatsApp allowFrom policy.`, |
198 | | - ); |
199 | | - }); |
200 | | - |
201 | | - it("handles mixed numeric and string allowList entries", () => { |
202 | | - vi.mocked(normalize.normalizeWhatsAppTarget) |
203 | | - .mockReturnValueOnce("+11234567890") |
204 | | - .mockReturnValueOnce("+11234567890") |
205 | | - .mockReturnValueOnce("+11234567890"); |
206 | | - vi.mocked(normalize.isWhatsAppGroupJid).mockReturnValueOnce(false); |
207 | | - |
208 | | - expectAllowedForTarget({ |
209 | | - allowFrom: [1234567890, PRIMARY_TARGET], |
210 | | - mode: "implicit", |
211 | | - }); |
212 | | - }); |
213 | | - |
214 | | - it("filters out invalid normalized entries from allowList", () => { |
215 | | - vi.mocked(normalize.normalizeWhatsAppTarget) |
216 | | - .mockReturnValueOnce("+11234567890") |
217 | | - .mockReturnValueOnce(null) |
218 | | - .mockReturnValueOnce("+11234567890"); |
219 | | - vi.mocked(normalize.isWhatsAppGroupJid).mockReturnValueOnce(false); |
220 | | - |
221 | | - expectAllowedForTarget({ |
222 | | - allowFrom: ["invalid", PRIMARY_TARGET], |
223 | | - mode: "implicit", |
| 55 | + mode, |
| 56 | + }), |
| 57 | + ).toEqual({ |
| 58 | + ok: false, |
| 59 | + error: expect.objectContaining({ |
| 60 | + message: `Target "${PRIMARY_TARGET}" is not listed in the configured WhatsApp allowFrom policy.`, |
| 61 | + }), |
224 | 62 | }); |
225 | | - }); |
226 | | - }); |
227 | | - |
228 | | - describe("heartbeat mode", () => { |
229 | | - it("allows message when target is in allowList in heartbeat mode", () => { |
230 | | - mockNormalizedDirectMessage(PRIMARY_TARGET, PRIMARY_TARGET); |
231 | | - expectAllowedForTarget({ allowFrom: [PRIMARY_TARGET], mode: "heartbeat" }); |
232 | | - }); |
233 | | - |
234 | | - it("denies message when target is not in allowList in heartbeat mode", () => { |
235 | | - mockNormalizedDirectMessage(PRIMARY_TARGET, SECONDARY_TARGET); |
236 | | - expectDeniedForTarget({ allowFrom: [SECONDARY_TARGET], mode: "heartbeat" }); |
237 | | - }); |
238 | | - }); |
239 | | - |
240 | | - describe("explicit/custom modes", () => { |
241 | | - it("allows message in null mode when allowList is not set", () => { |
242 | | - mockNormalizedDirectMessage(PRIMARY_TARGET); |
243 | | - expectAllowedForTarget({ allowFrom: undefined, mode: null }); |
244 | | - }); |
245 | | - |
246 | | - it("allows message in undefined mode when allowList is not set", () => { |
247 | | - mockNormalizedDirectMessage(PRIMARY_TARGET); |
248 | | - expectAllowedForTarget({ allowFrom: undefined, mode: undefined }); |
249 | | - }); |
250 | | - |
251 | | - it("enforces allowList in custom mode string", () => { |
252 | | - mockNormalizedDirectMessage(SECONDARY_TARGET, PRIMARY_TARGET); |
253 | | - expectDeniedForTarget({ allowFrom: [SECONDARY_TARGET], mode: "broadcast" }); |
254 | | - }); |
255 | | - |
256 | | - it("allows message in custom mode string when target is in allowList", () => { |
257 | | - mockNormalizedDirectMessage(PRIMARY_TARGET, PRIMARY_TARGET); |
258 | | - expectAllowedForTarget({ allowFrom: [PRIMARY_TARGET], mode: "broadcast" }); |
259 | | - }); |
260 | | - }); |
261 | | - |
262 | | - describe("whitespace handling", () => { |
263 | | - it("trims whitespace from to parameter", () => { |
264 | | - mockNormalizedDirectMessage(PRIMARY_TARGET); |
265 | | - |
266 | | - expectResolutionOk( |
267 | | - { |
268 | | - to: ` ${PRIMARY_TARGET} `, |
269 | | - allowFrom: undefined, |
270 | | - mode: undefined, |
271 | | - }, |
272 | | - PRIMARY_TARGET, |
273 | | - ); |
274 | | - expect(vi.mocked(normalize.normalizeWhatsAppTarget)).toHaveBeenCalledWith(PRIMARY_TARGET); |
275 | | - }); |
276 | | - |
277 | | - it("trims whitespace from allowList entries", () => { |
278 | | - mockNormalizedDirectMessage(PRIMARY_TARGET, PRIMARY_TARGET); |
| 63 | + }, |
| 64 | + ); |
279 | 65 |
|
| 66 | + it("trims the resolved target", () => { |
| 67 | + expect( |
280 | 68 | resolveWhatsAppOutboundTarget({ |
281 | | - to: PRIMARY_TARGET, |
282 | | - allowFrom: [` ${PRIMARY_TARGET} `], |
| 69 | + to: ` ${PRIMARY_TARGET} `, |
| 70 | + allowFrom: undefined, |
283 | 71 | mode: undefined, |
284 | | - }); |
285 | | - |
286 | | - expect(vi.mocked(normalize.normalizeWhatsAppTarget)).toHaveBeenCalledWith(PRIMARY_TARGET); |
287 | | - }); |
| 72 | + }), |
| 73 | + ).toEqual({ ok: true, to: PRIMARY_TARGET }); |
288 | 74 | }); |
289 | 75 | }); |
0 commit comments