Skip to content

Commit 725dc05

Browse files
committed
Googlechat: preserve remote fetch path and add coverage
1 parent 621ed20 commit 725dc05

2 files changed

Lines changed: 86 additions & 4 deletions

File tree

extensions/googlechat/src/channel.outbound.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,80 @@ describe("googlechatPlugin outbound sendMedia", () => {
8989
chatId: "spaces/AAA",
9090
});
9191
});
92+
93+
it("keeps remote URL media fetch on fetchRemoteMedia with maxBytes cap", async () => {
94+
const loadWebMedia = vi.fn(async () => ({
95+
buffer: Buffer.from("should-not-be-used"),
96+
fileName: "unused.png",
97+
contentType: "image/png",
98+
}));
99+
const fetchRemoteMedia = vi.fn(async () => ({
100+
buffer: Buffer.from("remote-bytes"),
101+
fileName: "remote.png",
102+
contentType: "image/png",
103+
}));
104+
105+
setGoogleChatRuntime({
106+
media: { loadWebMedia },
107+
channel: {
108+
media: { fetchRemoteMedia },
109+
text: { chunkMarkdownText: (text: string) => [text] },
110+
},
111+
} as unknown as PluginRuntime);
112+
113+
uploadGoogleChatAttachmentMock.mockResolvedValue({
114+
attachmentUploadToken: "token-2",
115+
});
116+
sendGoogleChatMessageMock.mockResolvedValue({
117+
messageName: "spaces/AAA/messages/msg-2",
118+
});
119+
120+
const cfg: OpenClawConfig = {
121+
channels: {
122+
googlechat: {
123+
enabled: true,
124+
serviceAccount: {
125+
type: "service_account",
126+
client_email: "bot@example.com",
127+
private_key: "test-key",
128+
token_uri: "https://oauth2.googleapis.com/token",
129+
},
130+
},
131+
},
132+
};
133+
134+
const result = await googlechatPlugin.outbound?.sendMedia?.({
135+
cfg,
136+
to: "spaces/AAA",
137+
text: "caption",
138+
mediaUrl: "https://example.com/image.png",
139+
accountId: "default",
140+
});
141+
142+
expect(fetchRemoteMedia).toHaveBeenCalledWith(
143+
expect.objectContaining({
144+
url: "https://example.com/image.png",
145+
maxBytes: 20 * 1024 * 1024,
146+
}),
147+
);
148+
expect(loadWebMedia).not.toHaveBeenCalled();
149+
expect(uploadGoogleChatAttachmentMock).toHaveBeenCalledWith(
150+
expect.objectContaining({
151+
space: "spaces/AAA",
152+
filename: "remote.png",
153+
contentType: "image/png",
154+
}),
155+
);
156+
expect(sendGoogleChatMessageMock).toHaveBeenCalledWith(
157+
expect.objectContaining({
158+
space: "spaces/AAA",
159+
text: "caption",
160+
}),
161+
);
162+
expect(result).toEqual({
163+
channel: "googlechat",
164+
messageId: "spaces/AAA/messages/msg-2",
165+
chatId: "spaces/AAA",
166+
});
167+
});
92168
});

extensions/googlechat/src/channel.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,16 @@ export const googlechatPlugin: ChannelPlugin<ResolvedGoogleChatAccount> = {
452452
(cfg.channels?.["googlechat"] as { mediaMaxMb?: number } | undefined)?.mediaMaxMb,
453453
accountId,
454454
});
455-
const loaded = await runtime.media.loadWebMedia(mediaUrl, {
456-
maxBytes: maxBytes ?? (account.config.mediaMaxMb ?? 20) * 1024 * 1024,
457-
localRoots: mediaLocalRoots?.length ? mediaLocalRoots : undefined,
458-
});
455+
const effectiveMaxBytes = maxBytes ?? (account.config.mediaMaxMb ?? 20) * 1024 * 1024;
456+
const loaded = /^https?:\/\//i.test(mediaUrl)
457+
? await runtime.channel.media.fetchRemoteMedia({
458+
url: mediaUrl,
459+
maxBytes: effectiveMaxBytes,
460+
})
461+
: await runtime.media.loadWebMedia(mediaUrl, {
462+
maxBytes: effectiveMaxBytes,
463+
localRoots: mediaLocalRoots?.length ? mediaLocalRoots : undefined,
464+
});
459465
const upload = await uploadGoogleChatAttachment({
460466
account,
461467
space,

0 commit comments

Comments
 (0)