|
1 | 1 | // Nextcloud Talk tests cover inbound.behavior plugin behavior. |
2 | 2 | import { createPluginRuntimeMock } from "openclaw/plugin-sdk/channel-test-helpers"; |
3 | 3 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
4 | | -import type { PluginRuntime, RuntimeEnv } from "../runtime-api.js"; |
| 4 | +import type { OutboundReplyPayload, PluginRuntime, RuntimeEnv } from "../runtime-api.js"; |
5 | 5 | import type { ResolvedNextcloudTalkAccount } from "./accounts.js"; |
6 | 6 | import { handleNextcloudTalkInbound } from "./inbound.js"; |
7 | 7 | import { setNextcloudTalkRuntime } from "./runtime.js"; |
@@ -307,4 +307,73 @@ describe("nextcloud-talk inbound behavior", () => { |
307 | 307 | ) as { replyPipeline?: unknown }; |
308 | 308 | expect(assembledRequest.replyPipeline).toEqual({}); |
309 | 309 | }); |
| 310 | + |
| 311 | + it("sanitizes inbound replies before local delivery while preserving transport fields", async () => { |
| 312 | + const coreRuntime = createPluginRuntimeMock(); |
| 313 | + setNextcloudTalkRuntime(coreRuntime as unknown as PluginRuntime); |
| 314 | + createChannelPairingControllerMock.mockReturnValue({ |
| 315 | + readStoreForDmPolicy: vi.fn(async () => []), |
| 316 | + issueChallenge: vi.fn(), |
| 317 | + }); |
| 318 | + sendMessageNextcloudTalkMock.mockResolvedValue(undefined); |
| 319 | + |
| 320 | + const config = { channels: { "nextcloud-talk": {} } } as CoreConfig; |
| 321 | + await handleNextcloudTalkInbound({ |
| 322 | + message: createMessage(), |
| 323 | + account: createAccount({ |
| 324 | + config: { |
| 325 | + dmPolicy: "allowlist", |
| 326 | + allowFrom: ["user-1"], |
| 327 | + groupPolicy: "allowlist", |
| 328 | + groupAllowFrom: [], |
| 329 | + }, |
| 330 | + }), |
| 331 | + config, |
| 332 | + runtime: createRuntimeEnv(), |
| 333 | + }); |
| 334 | + |
| 335 | + const assembledRequest = requireFirstMockArg( |
| 336 | + coreRuntime.channel.inbound.dispatchReply as ReturnType<typeof vi.fn>, |
| 337 | + "Nextcloud Talk assembled request", |
| 338 | + ) as { |
| 339 | + delivery?: { |
| 340 | + preparePayload?: (payload: OutboundReplyPayload) => OutboundReplyPayload; |
| 341 | + deliver?: (payload: OutboundReplyPayload) => Promise<{ visibleReplySent: boolean }>; |
| 342 | + }; |
| 343 | + }; |
| 344 | + const preparePayload = assembledRequest.delivery?.preparePayload; |
| 345 | + const deliver = assembledRequest.delivery?.deliver; |
| 346 | + if (!preparePayload || !deliver) { |
| 347 | + throw new Error("expected Nextcloud Talk reply delivery hooks"); |
| 348 | + } |
| 349 | + |
| 350 | + const mediaOnlyPayload = { mediaUrl: "https://example.com/a.png" }; |
| 351 | + expect(preparePayload(mediaOnlyPayload)).toBe(mediaOnlyPayload); |
| 352 | + |
| 353 | + const preparedPayload = preparePayload({ |
| 354 | + text: "Done.\n⚠️ 🛠️ `search repos (agent)` failed", |
| 355 | + mediaUrls: ["https://example.com/a.png"], |
| 356 | + replyToId: "reply-1", |
| 357 | + }); |
| 358 | + expect(preparedPayload).toEqual({ |
| 359 | + text: "Done.", |
| 360 | + mediaUrls: ["https://example.com/a.png"], |
| 361 | + replyToId: "reply-1", |
| 362 | + }); |
| 363 | + await expect(deliver(preparedPayload)).resolves.toEqual({ visibleReplySent: true }); |
| 364 | + await expect( |
| 365 | + deliver(preparePayload({ text: "⚠️ 🛠️ `search repos (agent)` failed" })), |
| 366 | + ).resolves.toEqual({ visibleReplySent: false }); |
| 367 | + |
| 368 | + expect(sendMessageNextcloudTalkMock).toHaveBeenCalledTimes(1); |
| 369 | + expect(requireFirstSendMessageCall()).toEqual([ |
| 370 | + "room-1", |
| 371 | + "Done.\n\nAttachment: https://example.com/a.png", |
| 372 | + { |
| 373 | + cfg: config, |
| 374 | + accountId: "default", |
| 375 | + replyTo: "reply-1", |
| 376 | + }, |
| 377 | + ]); |
| 378 | + }); |
310 | 379 | }); |
0 commit comments