|
| 1 | +import type { MatrixClient } from "@vector-im/matrix-bot-sdk"; |
| 2 | +import type { PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +const sendMessageMatrixMock = vi.hoisted(() => vi.fn().mockResolvedValue({ messageId: "mx-1" })); |
| 6 | + |
| 7 | +vi.mock("../send.js", () => ({ |
| 8 | + sendMessageMatrix: (...args: unknown[]) => sendMessageMatrixMock(...args), |
| 9 | +})); |
| 10 | + |
| 11 | +import { setMatrixRuntime } from "../../runtime.js"; |
| 12 | +import { deliverMatrixReplies } from "./replies.js"; |
| 13 | + |
| 14 | +describe("deliverMatrixReplies", () => { |
| 15 | + const loadConfigMock = vi.fn(() => ({})); |
| 16 | + const resolveMarkdownTableModeMock = vi.fn(() => "code"); |
| 17 | + const convertMarkdownTablesMock = vi.fn((text: string) => text); |
| 18 | + const resolveChunkModeMock = vi.fn(() => "length"); |
| 19 | + const chunkMarkdownTextWithModeMock = vi.fn((text: string) => [text]); |
| 20 | + |
| 21 | + const runtimeStub = { |
| 22 | + config: { |
| 23 | + loadConfig: (...args: unknown[]) => loadConfigMock(...args), |
| 24 | + }, |
| 25 | + channel: { |
| 26 | + text: { |
| 27 | + resolveMarkdownTableMode: (...args: unknown[]) => resolveMarkdownTableModeMock(...args), |
| 28 | + convertMarkdownTables: (...args: unknown[]) => convertMarkdownTablesMock(...args), |
| 29 | + resolveChunkMode: (...args: unknown[]) => resolveChunkModeMock(...args), |
| 30 | + chunkMarkdownTextWithMode: (...args: unknown[]) => chunkMarkdownTextWithModeMock(...args), |
| 31 | + }, |
| 32 | + }, |
| 33 | + logging: { |
| 34 | + shouldLogVerbose: () => false, |
| 35 | + }, |
| 36 | + } as unknown as PluginRuntime; |
| 37 | + |
| 38 | + const runtimeEnv: RuntimeEnv = { |
| 39 | + log: vi.fn(), |
| 40 | + error: vi.fn(), |
| 41 | + } as unknown as RuntimeEnv; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks(); |
| 45 | + setMatrixRuntime(runtimeStub); |
| 46 | + chunkMarkdownTextWithModeMock.mockImplementation((text: string) => [text]); |
| 47 | + }); |
| 48 | + |
| 49 | + it("keeps replyToId on first reply only when replyToMode=first", async () => { |
| 50 | + chunkMarkdownTextWithModeMock.mockImplementation((text: string) => text.split("|")); |
| 51 | + |
| 52 | + await deliverMatrixReplies({ |
| 53 | + replies: [ |
| 54 | + { text: "first-a|first-b", replyToId: "reply-1" }, |
| 55 | + { text: "second", replyToId: "reply-2" }, |
| 56 | + ], |
| 57 | + roomId: "room:1", |
| 58 | + client: {} as MatrixClient, |
| 59 | + runtime: runtimeEnv, |
| 60 | + textLimit: 4000, |
| 61 | + replyToMode: "first", |
| 62 | + }); |
| 63 | + |
| 64 | + expect(sendMessageMatrixMock).toHaveBeenCalledTimes(3); |
| 65 | + expect(sendMessageMatrixMock.mock.calls[0]?.[2]).toEqual( |
| 66 | + expect.objectContaining({ replyToId: "reply-1", threadId: undefined }), |
| 67 | + ); |
| 68 | + expect(sendMessageMatrixMock.mock.calls[1]?.[2]).toEqual( |
| 69 | + expect.objectContaining({ replyToId: "reply-1", threadId: undefined }), |
| 70 | + ); |
| 71 | + expect(sendMessageMatrixMock.mock.calls[2]?.[2]).toEqual( |
| 72 | + expect.objectContaining({ replyToId: undefined, threadId: undefined }), |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("keeps replyToId on every reply when replyToMode=all", async () => { |
| 77 | + await deliverMatrixReplies({ |
| 78 | + replies: [ |
| 79 | + { |
| 80 | + text: "caption", |
| 81 | + mediaUrls: ["https://example.com/a.jpg", "https://example.com/b.jpg"], |
| 82 | + replyToId: "reply-media", |
| 83 | + audioAsVoice: true, |
| 84 | + }, |
| 85 | + { text: "plain", replyToId: "reply-text" }, |
| 86 | + ], |
| 87 | + roomId: "room:2", |
| 88 | + client: {} as MatrixClient, |
| 89 | + runtime: runtimeEnv, |
| 90 | + textLimit: 4000, |
| 91 | + replyToMode: "all", |
| 92 | + }); |
| 93 | + |
| 94 | + expect(sendMessageMatrixMock).toHaveBeenCalledTimes(3); |
| 95 | + expect(sendMessageMatrixMock.mock.calls[0]).toEqual([ |
| 96 | + "room:2", |
| 97 | + "caption", |
| 98 | + expect.objectContaining({ mediaUrl: "https://example.com/a.jpg", replyToId: "reply-media" }), |
| 99 | + ]); |
| 100 | + expect(sendMessageMatrixMock.mock.calls[1]).toEqual([ |
| 101 | + "room:2", |
| 102 | + "", |
| 103 | + expect.objectContaining({ mediaUrl: "https://example.com/b.jpg", replyToId: "reply-media" }), |
| 104 | + ]); |
| 105 | + expect(sendMessageMatrixMock.mock.calls[2]?.[2]).toEqual( |
| 106 | + expect.objectContaining({ replyToId: "reply-text" }), |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it("suppresses replyToId when threadId is set", async () => { |
| 111 | + chunkMarkdownTextWithModeMock.mockImplementation((text: string) => text.split("|")); |
| 112 | + |
| 113 | + await deliverMatrixReplies({ |
| 114 | + replies: [{ text: "hello|thread", replyToId: "reply-thread" }], |
| 115 | + roomId: "room:3", |
| 116 | + client: {} as MatrixClient, |
| 117 | + runtime: runtimeEnv, |
| 118 | + textLimit: 4000, |
| 119 | + replyToMode: "all", |
| 120 | + threadId: "thread-77", |
| 121 | + }); |
| 122 | + |
| 123 | + expect(sendMessageMatrixMock).toHaveBeenCalledTimes(2); |
| 124 | + expect(sendMessageMatrixMock.mock.calls[0]?.[2]).toEqual( |
| 125 | + expect.objectContaining({ replyToId: undefined, threadId: "thread-77" }), |
| 126 | + ); |
| 127 | + expect(sendMessageMatrixMock.mock.calls[1]?.[2]).toEqual( |
| 128 | + expect.objectContaining({ replyToId: undefined, threadId: "thread-77" }), |
| 129 | + ); |
| 130 | + }); |
| 131 | +}); |
0 commit comments