mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:18:37 +00:00
refactor(googlechat): dedupe outbound media runtime fixture setup
This commit is contained in:
@@ -12,26 +12,51 @@ vi.mock("./api.js", () => ({
|
|||||||
import { googlechatPlugin } from "./channel.js";
|
import { googlechatPlugin } from "./channel.js";
|
||||||
import { setGoogleChatRuntime } from "./runtime.js";
|
import { setGoogleChatRuntime } from "./runtime.js";
|
||||||
|
|
||||||
|
function createGoogleChatCfg(): OpenClawConfig {
|
||||||
|
return {
|
||||||
|
channels: {
|
||||||
|
googlechat: {
|
||||||
|
enabled: true,
|
||||||
|
serviceAccount: {
|
||||||
|
type: "service_account",
|
||||||
|
client_email: "bot@example.com",
|
||||||
|
private_key: "test-key",
|
||||||
|
token_uri: "https://oauth2.googleapis.com/token",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupRuntimeMediaMocks(params: { loadFileName: string; loadBytes: string }) {
|
||||||
|
const loadWebMedia = vi.fn(async () => ({
|
||||||
|
buffer: Buffer.from(params.loadBytes),
|
||||||
|
fileName: params.loadFileName,
|
||||||
|
contentType: "image/png",
|
||||||
|
}));
|
||||||
|
const fetchRemoteMedia = vi.fn(async () => ({
|
||||||
|
buffer: Buffer.from("remote-bytes"),
|
||||||
|
fileName: "remote.png",
|
||||||
|
contentType: "image/png",
|
||||||
|
}));
|
||||||
|
|
||||||
|
setGoogleChatRuntime({
|
||||||
|
media: { loadWebMedia },
|
||||||
|
channel: {
|
||||||
|
media: { fetchRemoteMedia },
|
||||||
|
text: { chunkMarkdownText: (text: string) => [text] },
|
||||||
|
},
|
||||||
|
} as unknown as PluginRuntime);
|
||||||
|
|
||||||
|
return { loadWebMedia, fetchRemoteMedia };
|
||||||
|
}
|
||||||
|
|
||||||
describe("googlechatPlugin outbound sendMedia", () => {
|
describe("googlechatPlugin outbound sendMedia", () => {
|
||||||
it("loads local media with mediaLocalRoots via runtime media loader", async () => {
|
it("loads local media with mediaLocalRoots via runtime media loader", async () => {
|
||||||
const loadWebMedia = vi.fn(async () => ({
|
const { loadWebMedia, fetchRemoteMedia } = setupRuntimeMediaMocks({
|
||||||
buffer: Buffer.from("image-bytes"),
|
loadFileName: "image.png",
|
||||||
fileName: "image.png",
|
loadBytes: "image-bytes",
|
||||||
contentType: "image/png",
|
});
|
||||||
}));
|
|
||||||
const fetchRemoteMedia = vi.fn(async () => ({
|
|
||||||
buffer: Buffer.from("remote-bytes"),
|
|
||||||
fileName: "remote.png",
|
|
||||||
contentType: "image/png",
|
|
||||||
}));
|
|
||||||
|
|
||||||
setGoogleChatRuntime({
|
|
||||||
media: { loadWebMedia },
|
|
||||||
channel: {
|
|
||||||
media: { fetchRemoteMedia },
|
|
||||||
text: { chunkMarkdownText: (text: string) => [text] },
|
|
||||||
},
|
|
||||||
} as unknown as PluginRuntime);
|
|
||||||
|
|
||||||
uploadGoogleChatAttachmentMock.mockResolvedValue({
|
uploadGoogleChatAttachmentMock.mockResolvedValue({
|
||||||
attachmentUploadToken: "token-1",
|
attachmentUploadToken: "token-1",
|
||||||
@@ -40,19 +65,7 @@ describe("googlechatPlugin outbound sendMedia", () => {
|
|||||||
messageName: "spaces/AAA/messages/msg-1",
|
messageName: "spaces/AAA/messages/msg-1",
|
||||||
});
|
});
|
||||||
|
|
||||||
const cfg: OpenClawConfig = {
|
const cfg = createGoogleChatCfg();
|
||||||
channels: {
|
|
||||||
googlechat: {
|
|
||||||
enabled: true,
|
|
||||||
serviceAccount: {
|
|
||||||
type: "service_account",
|
|
||||||
client_email: "bot@example.com",
|
|
||||||
private_key: "test-key",
|
|
||||||
token_uri: "https://oauth2.googleapis.com/token",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = await googlechatPlugin.outbound?.sendMedia?.({
|
const result = await googlechatPlugin.outbound?.sendMedia?.({
|
||||||
cfg,
|
cfg,
|
||||||
@@ -91,24 +104,10 @@ describe("googlechatPlugin outbound sendMedia", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("keeps remote URL media fetch on fetchRemoteMedia with maxBytes cap", async () => {
|
it("keeps remote URL media fetch on fetchRemoteMedia with maxBytes cap", async () => {
|
||||||
const loadWebMedia = vi.fn(async () => ({
|
const { loadWebMedia, fetchRemoteMedia } = setupRuntimeMediaMocks({
|
||||||
buffer: Buffer.from("should-not-be-used"),
|
loadFileName: "unused.png",
|
||||||
fileName: "unused.png",
|
loadBytes: "should-not-be-used",
|
||||||
contentType: "image/png",
|
});
|
||||||
}));
|
|
||||||
const fetchRemoteMedia = vi.fn(async () => ({
|
|
||||||
buffer: Buffer.from("remote-bytes"),
|
|
||||||
fileName: "remote.png",
|
|
||||||
contentType: "image/png",
|
|
||||||
}));
|
|
||||||
|
|
||||||
setGoogleChatRuntime({
|
|
||||||
media: { loadWebMedia },
|
|
||||||
channel: {
|
|
||||||
media: { fetchRemoteMedia },
|
|
||||||
text: { chunkMarkdownText: (text: string) => [text] },
|
|
||||||
},
|
|
||||||
} as unknown as PluginRuntime);
|
|
||||||
|
|
||||||
uploadGoogleChatAttachmentMock.mockResolvedValue({
|
uploadGoogleChatAttachmentMock.mockResolvedValue({
|
||||||
attachmentUploadToken: "token-2",
|
attachmentUploadToken: "token-2",
|
||||||
@@ -117,19 +116,7 @@ describe("googlechatPlugin outbound sendMedia", () => {
|
|||||||
messageName: "spaces/AAA/messages/msg-2",
|
messageName: "spaces/AAA/messages/msg-2",
|
||||||
});
|
});
|
||||||
|
|
||||||
const cfg: OpenClawConfig = {
|
const cfg = createGoogleChatCfg();
|
||||||
channels: {
|
|
||||||
googlechat: {
|
|
||||||
enabled: true,
|
|
||||||
serviceAccount: {
|
|
||||||
type: "service_account",
|
|
||||||
client_email: "bot@example.com",
|
|
||||||
private_key: "test-key",
|
|
||||||
token_uri: "https://oauth2.googleapis.com/token",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = await googlechatPlugin.outbound?.sendMedia?.({
|
const result = await googlechatPlugin.outbound?.sendMedia?.({
|
||||||
cfg,
|
cfg,
|
||||||
|
|||||||
Reference in New Issue
Block a user