test: reduce telegram media harness imports

This commit is contained in:
Peter Steinberger
2026-04-03 18:32:07 +01:00
parent fb9be1fcb6
commit 14c863dc4a
11 changed files with 94 additions and 96 deletions

View File

@@ -0,0 +1,5 @@
export {
resolveAgentDir,
resolveDefaultAgentId,
resolveDefaultModelForAgent,
} from "openclaw/plugin-sdk/agent-runtime";

View File

@@ -1,6 +1,4 @@
import type { Message, ReactionTypeEmoji } from "@grammyjs/types"; import type { Message, ReactionTypeEmoji } from "@grammyjs/types";
import { resolveAgentDir, resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
import { resolveDefaultModelForAgent } from "openclaw/plugin-sdk/agent-runtime";
import { resolveChannelConfigWrites } from "openclaw/plugin-sdk/channel-config-helpers"; import { resolveChannelConfigWrites } from "openclaw/plugin-sdk/channel-config-helpers";
import { shouldDebounceTextInbound } from "openclaw/plugin-sdk/channel-inbound"; import { shouldDebounceTextInbound } from "openclaw/plugin-sdk/channel-inbound";
import { import {
@@ -44,6 +42,11 @@ import {
type NormalizedAllowFrom, type NormalizedAllowFrom,
} from "./bot-access.js"; } from "./bot-access.js";
import { defaultTelegramBotDeps } from "./bot-deps.js"; import { defaultTelegramBotDeps } from "./bot-deps.js";
import {
resolveAgentDir,
resolveDefaultAgentId,
resolveDefaultModelForAgent,
} from "./bot-handlers.agent.runtime.js";
import { import {
hasInboundMedia, hasInboundMedia,
hasReplyTargetMedia, hasReplyTargetMedia,

View File

@@ -0,0 +1,7 @@
export {
findModelInCatalog,
loadModelCatalog,
modelSupportsVision,
resolveAgentDir,
resolveDefaultModelForAgent,
} from "openclaw/plugin-sdk/agent-runtime";

View File

@@ -4,7 +4,7 @@ export {
resolveSessionStoreEntry, resolveSessionStoreEntry,
resolveStorePath, resolveStorePath,
} from "openclaw/plugin-sdk/config-runtime"; } from "openclaw/plugin-sdk/config-runtime";
export { getAgentScopedMediaLocalRoots } from "openclaw/plugin-sdk/media-runtime"; export { getAgentScopedMediaLocalRoots } from "./telegram-media.runtime.js";
export { export {
generateTopicLabel, generateTopicLabel,
resolveAutoTopicLabelConfig, resolveAutoTopicLabelConfig,

View File

@@ -1,11 +1,4 @@
import type { Bot } from "grammy"; import type { Bot } from "grammy";
import { resolveAgentDir } from "openclaw/plugin-sdk/agent-runtime";
import {
findModelInCatalog,
loadModelCatalog,
modelSupportsVision,
} from "openclaw/plugin-sdk/agent-runtime";
import { resolveDefaultModelForAgent } from "openclaw/plugin-sdk/agent-runtime";
import { import {
logAckFailure, logAckFailure,
logTypingFailure, logTypingFailure,
@@ -25,6 +18,13 @@ import { danger, logVerbose } from "openclaw/plugin-sdk/runtime-env";
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env"; import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
import { defaultTelegramBotDeps, type TelegramBotDeps } from "./bot-deps.js"; import { defaultTelegramBotDeps, type TelegramBotDeps } from "./bot-deps.js";
import type { TelegramMessageContext } from "./bot-message-context.js"; import type { TelegramMessageContext } from "./bot-message-context.js";
import {
findModelInCatalog,
loadModelCatalog,
modelSupportsVision,
resolveAgentDir,
resolveDefaultModelForAgent,
} from "./bot-message-dispatch.agent.runtime.js";
import { import {
loadSessionStore, loadSessionStore,
resolveMarkdownTableMode, resolveMarkdownTableMode,

View File

@@ -0,0 +1 @@
export { resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";

View File

@@ -127,6 +127,7 @@ const mediaHarnessReplySpy = vi.hoisted(() =>
return undefined; return undefined;
}), }),
); );
export { mediaHarnessReplySpy };
const mediaHarnessDispatchReplyWithBufferedBlockDispatcher = vi.hoisted(() => const mediaHarnessDispatchReplyWithBufferedBlockDispatcher = vi.hoisted(() =>
vi.fn<DispatchReplyWithBufferedBlockDispatcherFn>(async (params: DispatchReplyHarnessParams) => { vi.fn<DispatchReplyWithBufferedBlockDispatcherFn>(async (params: DispatchReplyHarnessParams) => {
@@ -179,45 +180,29 @@ vi.doMock("./bot.runtime.js", () => ({
...telegramBotRuntimeForTest, ...telegramBotRuntimeForTest,
})); }));
vi.mock("undici", async (importOriginal) => { vi.mock("undici", () => ({
const actual = await importOriginal<typeof import("undici")>(); Agent: vi.fn(function MockAgent(this: { options?: unknown }, options?: unknown) {
return { this.options = options;
...actual, }),
fetch: (...args: Parameters<typeof undiciFetchSpy>) => undiciFetchSpy(...args), EnvHttpProxyAgent: vi.fn(function MockEnvHttpProxyAgent(
}; this: { options?: unknown },
}); options?: unknown,
) {
this.options = options;
}),
ProxyAgent: vi.fn(function MockProxyAgent(this: { options?: unknown }, options?: unknown) {
this.options = options;
}),
fetch: (...args: Parameters<typeof undiciFetchSpy>) => undiciFetchSpy(...args),
setGlobalDispatcher: vi.fn(),
}));
export async function mockMediaRuntimeModuleForTest( vi.mock("./telegram-media.runtime.js", () => ({
importOriginal: () => Promise<typeof import("openclaw/plugin-sdk/media-runtime")>, fetchRemoteMedia: (...args: Parameters<typeof fetchRemoteMediaSpy>) =>
) { fetchRemoteMediaSpy(...args),
const actual = await importOriginal(); getAgentScopedMediaLocalRoots: vi.fn(() => []),
const mockModule = Object.create(null) as Record<string, unknown>; saveMediaBuffer: (...args: Parameters<typeof saveMediaBufferSpy>) => saveMediaBufferSpy(...args),
Object.defineProperties(mockModule, Object.getOwnPropertyDescriptors(actual)); }));
Object.defineProperty(mockModule, "fetchRemoteMedia", {
configurable: true,
enumerable: true,
writable: true,
value: (...args: Parameters<typeof fetchRemoteMediaSpy>) => fetchRemoteMediaSpy(...args),
});
Object.defineProperty(mockModule, "saveMediaBuffer", {
configurable: true,
enumerable: true,
writable: true,
value: (...args: Parameters<typeof saveMediaBufferSpy>) => saveMediaBufferSpy(...args),
});
return mockModule;
}
vi.mock("openclaw/plugin-sdk/media-runtime", mockMediaRuntimeModuleForTest);
vi.doMock("openclaw/plugin-sdk/config-runtime", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/config-runtime")>();
return {
...actual,
loadConfig: telegramBotDepsForTest.loadConfig,
updateLastRoute: vi.fn(async () => undefined),
};
});
vi.doMock("./bot-message-context.session.runtime.js", async (importOriginal) => { vi.doMock("./bot-message-context.session.runtime.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("./bot-message-context.session.runtime.js")>(); const actual = await importOriginal<typeof import("./bot-message-context.session.runtime.js")>();
@@ -228,37 +213,26 @@ vi.doMock("./bot-message-context.session.runtime.js", async (importOriginal) =>
}; };
}); });
vi.doMock("openclaw/plugin-sdk/agent-runtime", async (importOriginal) => { vi.mock("./bot.agent.runtime.js", () => ({
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/agent-runtime")>(); resolveDefaultAgentId: vi.fn(() => "default"),
return { }));
...actual,
findModelInCatalog: vi.fn(() => undefined),
loadModelCatalog: vi.fn(async () => []),
modelSupportsVision: vi.fn(() => false),
resolveDefaultModelForAgent: vi.fn(() => ({
provider: "openai",
model: "gpt-test",
})),
};
});
vi.doMock("openclaw/plugin-sdk/conversation-runtime", async (importOriginal) => { vi.mock("./bot-handlers.agent.runtime.js", () => ({
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/conversation-runtime")>(); resolveAgentDir: vi.fn(() => "/tmp/agent"),
return { resolveDefaultAgentId: vi.fn(() => "default"),
...actual, resolveDefaultModelForAgent: vi.fn(() => ({
readChannelAllowFromStore: telegramBotDepsForTest.readChannelAllowFromStore, provider: "openai",
upsertChannelPairingRequest: vi.fn(async () => ({ model: "gpt-test",
code: "PAIRCODE", })),
created: true, }));
})),
};
});
vi.doMock("openclaw/plugin-sdk/reply-runtime", async (importOriginal) => { vi.mock("./bot-message-dispatch.agent.runtime.js", () => ({
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/reply-runtime")>(); findModelInCatalog: vi.fn(() => undefined),
return { loadModelCatalog: vi.fn(async () => []),
...actual, modelSupportsVision: vi.fn(() => false),
getReplyFromConfig: mediaHarnessReplySpy, resolveAgentDir: vi.fn(() => "/tmp/agent"),
__replySpy: mediaHarnessReplySpy, resolveDefaultModelForAgent: vi.fn(() => ({
}; provider: "openai",
}); model: "gpt-test",
})),
}));

View File

@@ -1,5 +1,5 @@
import * as ssrf from "openclaw/plugin-sdk/ssrf-runtime"; import * as ssrf from "openclaw/plugin-sdk/ssrf-runtime";
import { afterEach, beforeEach, expect, vi, type Mock } from "vitest"; import { afterEach, beforeAll, beforeEach, expect, vi, type Mock } from "vitest";
import * as harness from "./bot.media.e2e-harness.js"; import * as harness from "./bot.media.e2e-harness.js";
type StickerSpy = Mock<(...args: unknown[]) => unknown>; type StickerSpy = Mock<(...args: unknown[]) => unknown>;
@@ -132,13 +132,17 @@ async function loadTelegramBotHarness() {
...opts, ...opts,
telegramDeps: harness.telegramBotDepsForTest, telegramDeps: harness.telegramBotDepsForTest,
}); });
const replyModule = await import("openclaw/plugin-sdk/reply-runtime"); replySpyRef = harness.mediaHarnessReplySpy;
replySpyRef = (replyModule as unknown as { __replySpy: ReturnType<typeof vi.fn> }).__replySpy;
} }
beforeEach(async () => { beforeAll(async () => {
vi.resetModules();
await loadTelegramBotHarness(); await loadTelegramBotHarness();
});
beforeEach(() => {
onSpyRef.mockClear();
replySpyRef.mockClear();
sendChatActionSpyRef.mockClear();
vi.useRealTimers(); vi.useRealTimers();
lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]); lookupMock.mockResolvedValue([{ address: "93.184.216.34", family: 4 }]);
resolvePinnedHostnameSpy = vi resolvePinnedHostnameSpy = vi
@@ -152,12 +156,11 @@ afterEach(() => {
resolvePinnedHostnameSpy = null; resolvePinnedHostnameSpy = null;
}); });
vi.mock("./sticker-cache.js", async (importOriginal) => { vi.mock("./sticker-cache.js", () => ({
const actual = await importOriginal<typeof import("./sticker-cache.js")>(); cacheSticker: (...args: unknown[]) => cacheStickerSpy(...args),
return { getCachedSticker: (...args: unknown[]) => getCachedStickerSpy(...args),
...actual, describeStickerImage: (...args: unknown[]) => describeStickerImageSpy(...args),
cacheSticker: (...args: unknown[]) => cacheStickerSpy(...args), getAllCachedStickers: vi.fn(() => []),
getCachedSticker: (...args: unknown[]) => getCachedStickerSpy(...args), getCacheStats: vi.fn(() => ({ count: 0 })),
describeStickerImage: (...args: unknown[]) => describeStickerImageSpy(...args), searchStickers: vi.fn(() => []),
}; }));
});

View File

@@ -1,4 +1,3 @@
import { resolveDefaultAgentId } from "openclaw/plugin-sdk/agent-runtime";
import { import {
isNativeCommandsExplicitlyDisabled, isNativeCommandsExplicitlyDisabled,
resolveNativeCommandsEnabled, resolveNativeCommandsEnabled,
@@ -32,6 +31,7 @@ import {
resolveTelegramUpdateId, resolveTelegramUpdateId,
type TelegramUpdateKeyContext, type TelegramUpdateKeyContext,
} from "./bot-updates.js"; } from "./bot-updates.js";
import { resolveDefaultAgentId } from "./bot.agent.runtime.js";
import { apiThrottler, Bot, sequentialize, type ApiClientOptions } from "./bot.runtime.js"; import { apiThrottler, Bot, sequentialize, type ApiClientOptions } from "./bot.runtime.js";
import { buildTelegramGroupPeerId, resolveTelegramStreamMode } from "./bot/helpers.js"; import { buildTelegramGroupPeerId, resolveTelegramStreamMode } from "./bot/helpers.js";
import { resolveTelegramTransport, type TelegramTransport } from "./fetch.js"; import { resolveTelegramTransport, type TelegramTransport } from "./fetch.js";

View File

@@ -1,7 +1,7 @@
import { fetchRemoteMedia, saveMediaBuffer } from "openclaw/plugin-sdk/media-runtime";
import { logVerbose, retryAsync, warn } from "openclaw/plugin-sdk/runtime-env"; import { logVerbose, retryAsync, warn } from "openclaw/plugin-sdk/runtime-env";
import { formatErrorMessage } from "openclaw/plugin-sdk/ssrf-runtime"; import { formatErrorMessage } from "openclaw/plugin-sdk/ssrf-runtime";
import { resolveTelegramApiBase, shouldRetryTelegramTransportFallback } from "../fetch.js"; import { resolveTelegramApiBase, shouldRetryTelegramTransportFallback } from "../fetch.js";
import { fetchRemoteMedia, saveMediaBuffer } from "../telegram-media.runtime.js";
export { export {
fetchRemoteMedia, fetchRemoteMedia,

View File

@@ -0,0 +1,5 @@
export {
fetchRemoteMedia,
getAgentScopedMediaLocalRoots,
saveMediaBuffer,
} from "openclaw/plugin-sdk/media-runtime";