|
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"; |
@@ -101,6 +101,23 @@ function requireFirstSendMessageCall(): [unknown, unknown, unknown] { |
101 | 101 | return call as [unknown, unknown, unknown]; |
102 | 102 | } |
103 | 103 |
|
| 104 | +type CapturedReplyDelivery = { |
| 105 | + preparePayload: (payload: OutboundReplyPayload) => OutboundReplyPayload; |
| 106 | + deliver: (payload: OutboundReplyPayload) => Promise<void>; |
| 107 | +}; |
| 108 | + |
| 109 | +function requireFirstReplyDelivery(mock: ReturnType<typeof vi.fn>): CapturedReplyDelivery { |
| 110 | + const assembledRequest = requireFirstMockArg(mock, "Nextcloud Talk assembled request") as { |
| 111 | + delivery?: Partial<CapturedReplyDelivery>; |
| 112 | + }; |
| 113 | + const preparePayload = assembledRequest.delivery?.preparePayload; |
| 114 | + const deliver = assembledRequest.delivery?.deliver; |
| 115 | + if (!preparePayload || !deliver) { |
| 116 | + throw new Error("expected Nextcloud Talk reply delivery hooks"); |
| 117 | + } |
| 118 | + return { preparePayload, deliver }; |
| 119 | +} |
| 120 | + |
104 | 121 | function createAccount( |
105 | 122 | overrides?: Partial<ResolvedNextcloudTalkAccount>, |
106 | 123 | ): ResolvedNextcloudTalkAccount { |
@@ -307,4 +324,98 @@ describe("nextcloud-talk inbound behavior", () => { |
307 | 324 | ) as { replyPipeline?: unknown }; |
308 | 325 | expect(assembledRequest.replyPipeline).toEqual({}); |
309 | 326 | }); |
| 327 | + |
| 328 | + it("sanitizes inbound replies before local delivery while preserving reply metadata", async () => { |
| 329 | + const coreRuntime = createPluginRuntimeMock(); |
| 330 | + setNextcloudTalkRuntime(coreRuntime as unknown as PluginRuntime); |
| 331 | + createChannelPairingControllerMock.mockReturnValue({ |
| 332 | + readStoreForDmPolicy: vi.fn(async () => []), |
| 333 | + issueChallenge: vi.fn(), |
| 334 | + }); |
| 335 | + sendMessageNextcloudTalkMock.mockResolvedValue(undefined); |
| 336 | + |
| 337 | + const config = { channels: { "nextcloud-talk": {} } } as CoreConfig; |
| 338 | + await handleNextcloudTalkInbound({ |
| 339 | + message: createMessage(), |
| 340 | + account: createAccount({ |
| 341 | + config: { |
| 342 | + dmPolicy: "allowlist", |
| 343 | + allowFrom: ["user-1"], |
| 344 | + groupPolicy: "allowlist", |
| 345 | + groupAllowFrom: [], |
| 346 | + }, |
| 347 | + }), |
| 348 | + config, |
| 349 | + runtime: createRuntimeEnv(), |
| 350 | + }); |
| 351 | + |
| 352 | + const delivery = requireFirstReplyDelivery( |
| 353 | + coreRuntime.channel.inbound.dispatchReply as ReturnType<typeof vi.fn>, |
| 354 | + ); |
| 355 | + const cases = [ |
| 356 | + { |
| 357 | + text: "Done.\n⚠️ 🛠️ `search repos (agent)` failed", |
| 358 | + expected: "Done.", |
| 359 | + }, |
| 360 | + { |
| 361 | + text: '<tool_call>{"name":"exec"}</tool_call>Meeting notes sent.', |
| 362 | + expected: "Meeting notes sent.", |
| 363 | + }, |
| 364 | + { |
| 365 | + text: [ |
| 366 | + "Checking now.", |
| 367 | + "<function_response>", |
| 368 | + 'Searching for: "agenda"', |
| 369 | + "</function_response>", |
| 370 | + "Meeting notes sent.", |
| 371 | + ].join("\n"), |
| 372 | + expected: "Checking now.\n\nMeeting notes sent.", |
| 373 | + }, |
| 374 | + { |
| 375 | + text: "The agenda has 3 open action items.", |
| 376 | + expected: "The agenda has 3 open action items.", |
| 377 | + }, |
| 378 | + ]; |
| 379 | + for (const testCase of cases) { |
| 380 | + expect(delivery.preparePayload({ text: testCase.text })).toEqual({ |
| 381 | + text: testCase.expected, |
| 382 | + }); |
| 383 | + } |
| 384 | + |
| 385 | + const mediaOnlyPayload = { mediaUrl: "https://example.com/a.png" }; |
| 386 | + expect(delivery.preparePayload(mediaOnlyPayload)).toBe(mediaOnlyPayload); |
| 387 | + |
| 388 | + const preparedPayload = delivery.preparePayload({ |
| 389 | + text: "Done.\n⚠️ 🛠️ `search repos (agent)` failed", |
| 390 | + mediaUrls: ["https://example.com/a.png"], |
| 391 | + replyToId: "reply-1", |
| 392 | + }); |
| 393 | + await delivery.deliver(preparedPayload); |
| 394 | + await delivery.deliver( |
| 395 | + delivery.preparePayload({ |
| 396 | + text: '<tool_call>{"name":"exec"}</tool_call>Meeting notes sent.', |
| 397 | + }), |
| 398 | + ); |
| 399 | + |
| 400 | + expect(sendMessageNextcloudTalkMock).toHaveBeenCalledTimes(2); |
| 401 | + expect(requireFirstSendMessageCall()).toEqual([ |
| 402 | + "room-1", |
| 403 | + "Done.\n\nAttachment: https://example.com/a.png", |
| 404 | + { |
| 405 | + cfg: config, |
| 406 | + accountId: "default", |
| 407 | + replyTo: "reply-1", |
| 408 | + }, |
| 409 | + ]); |
| 410 | + expect(sendMessageNextcloudTalkMock).toHaveBeenNthCalledWith( |
| 411 | + 2, |
| 412 | + "room-1", |
| 413 | + "Meeting notes sent.", |
| 414 | + { |
| 415 | + cfg: config, |
| 416 | + accountId: "default", |
| 417 | + replyTo: undefined, |
| 418 | + }, |
| 419 | + ); |
| 420 | + }); |
310 | 421 | }); |
0 commit comments