|
| 1 | +import type { ChannelMessageActionContext } from "openclaw/plugin-sdk/matrix"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { CoreConfig } from "./types.js"; |
| 4 | + |
| 5 | +const mocks = vi.hoisted(() => ({ |
| 6 | + handleMatrixAction: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("./tool-actions.js", () => ({ |
| 10 | + handleMatrixAction: mocks.handleMatrixAction, |
| 11 | +})); |
| 12 | + |
| 13 | +const { matrixMessageActions } = await import("./actions.js"); |
| 14 | + |
| 15 | +function createContext( |
| 16 | + overrides: Partial<ChannelMessageActionContext>, |
| 17 | +): ChannelMessageActionContext { |
| 18 | + return { |
| 19 | + channel: "matrix", |
| 20 | + action: "send", |
| 21 | + cfg: { |
| 22 | + channels: { |
| 23 | + matrix: { |
| 24 | + enabled: true, |
| 25 | + homeserver: "https://matrix.example.org", |
| 26 | + userId: "@bot:example.org", |
| 27 | + accessToken: "token", |
| 28 | + }, |
| 29 | + }, |
| 30 | + } as CoreConfig, |
| 31 | + params: {}, |
| 32 | + ...overrides, |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +describe("matrixMessageActions account propagation", () => { |
| 37 | + beforeEach(() => { |
| 38 | + mocks.handleMatrixAction.mockReset().mockResolvedValue({ |
| 39 | + ok: true, |
| 40 | + output: "", |
| 41 | + details: { ok: true }, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it("forwards accountId for send actions", async () => { |
| 46 | + await matrixMessageActions.handleAction?.( |
| 47 | + createContext({ |
| 48 | + action: "send", |
| 49 | + accountId: "ops", |
| 50 | + params: { |
| 51 | + to: "room:!room:example", |
| 52 | + message: "hello", |
| 53 | + }, |
| 54 | + }), |
| 55 | + ); |
| 56 | + |
| 57 | + expect(mocks.handleMatrixAction).toHaveBeenCalledWith( |
| 58 | + expect.objectContaining({ |
| 59 | + action: "sendMessage", |
| 60 | + accountId: "ops", |
| 61 | + }), |
| 62 | + expect.any(Object), |
| 63 | + { mediaLocalRoots: undefined }, |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it("forwards accountId for permissions actions", async () => { |
| 68 | + await matrixMessageActions.handleAction?.( |
| 69 | + createContext({ |
| 70 | + action: "permissions", |
| 71 | + accountId: "ops", |
| 72 | + params: { |
| 73 | + operation: "verification-list", |
| 74 | + }, |
| 75 | + }), |
| 76 | + ); |
| 77 | + |
| 78 | + expect(mocks.handleMatrixAction).toHaveBeenCalledWith( |
| 79 | + expect.objectContaining({ |
| 80 | + action: "verificationList", |
| 81 | + accountId: "ops", |
| 82 | + }), |
| 83 | + expect.any(Object), |
| 84 | + { mediaLocalRoots: undefined }, |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("forwards accountId for self-profile updates", async () => { |
| 89 | + await matrixMessageActions.handleAction?.( |
| 90 | + createContext({ |
| 91 | + action: "set-profile", |
| 92 | + accountId: "ops", |
| 93 | + params: { |
| 94 | + displayName: "Ops Bot", |
| 95 | + avatarUrl: "mxc://example/avatar", |
| 96 | + }, |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + expect(mocks.handleMatrixAction).toHaveBeenCalledWith( |
| 101 | + expect.objectContaining({ |
| 102 | + action: "setProfile", |
| 103 | + accountId: "ops", |
| 104 | + displayName: "Ops Bot", |
| 105 | + avatarUrl: "mxc://example/avatar", |
| 106 | + }), |
| 107 | + expect.any(Object), |
| 108 | + { mediaLocalRoots: undefined }, |
| 109 | + ); |
| 110 | + }); |
| 111 | + |
| 112 | + it("forwards local avatar paths for self-profile updates", async () => { |
| 113 | + await matrixMessageActions.handleAction?.( |
| 114 | + createContext({ |
| 115 | + action: "set-profile", |
| 116 | + accountId: "ops", |
| 117 | + params: { |
| 118 | + path: "/tmp/avatar.jpg", |
| 119 | + }, |
| 120 | + }), |
| 121 | + ); |
| 122 | + |
| 123 | + expect(mocks.handleMatrixAction).toHaveBeenCalledWith( |
| 124 | + expect.objectContaining({ |
| 125 | + action: "setProfile", |
| 126 | + accountId: "ops", |
| 127 | + avatarPath: "/tmp/avatar.jpg", |
| 128 | + }), |
| 129 | + expect.any(Object), |
| 130 | + { mediaLocalRoots: undefined }, |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it("forwards mediaLocalRoots for media sends", async () => { |
| 135 | + await matrixMessageActions.handleAction?.( |
| 136 | + createContext({ |
| 137 | + action: "send", |
| 138 | + accountId: "ops", |
| 139 | + mediaLocalRoots: ["/tmp/openclaw-matrix-test"], |
| 140 | + params: { |
| 141 | + to: "room:!room:example", |
| 142 | + message: "hello", |
| 143 | + media: "file:///tmp/photo.png", |
| 144 | + }, |
| 145 | + }), |
| 146 | + ); |
| 147 | + |
| 148 | + expect(mocks.handleMatrixAction).toHaveBeenCalledWith( |
| 149 | + expect.objectContaining({ |
| 150 | + action: "sendMessage", |
| 151 | + accountId: "ops", |
| 152 | + mediaUrl: "file:///tmp/photo.png", |
| 153 | + }), |
| 154 | + expect.any(Object), |
| 155 | + { mediaLocalRoots: ["/tmp/openclaw-matrix-test"] }, |
| 156 | + ); |
| 157 | + }); |
| 158 | + |
| 159 | + it("allows media-only sends without requiring a message body", async () => { |
| 160 | + await matrixMessageActions.handleAction?.( |
| 161 | + createContext({ |
| 162 | + action: "send", |
| 163 | + accountId: "ops", |
| 164 | + params: { |
| 165 | + to: "room:!room:example", |
| 166 | + media: "file:///tmp/photo.png", |
| 167 | + }, |
| 168 | + }), |
| 169 | + ); |
| 170 | + |
| 171 | + expect(mocks.handleMatrixAction).toHaveBeenCalledWith( |
| 172 | + expect.objectContaining({ |
| 173 | + action: "sendMessage", |
| 174 | + accountId: "ops", |
| 175 | + content: undefined, |
| 176 | + mediaUrl: "file:///tmp/photo.png", |
| 177 | + }), |
| 178 | + expect.any(Object), |
| 179 | + { mediaLocalRoots: undefined }, |
| 180 | + ); |
| 181 | + }); |
| 182 | +}); |
0 commit comments