test: reduce telegram broad partial mocks

This commit is contained in:
Peter Steinberger
2026-04-03 12:00:07 +01:00
parent 225431665a
commit 4e22e75697
11 changed files with 68 additions and 94 deletions

View File

@@ -6,13 +6,11 @@ const fetchWithTimeoutMock = vi.hoisted(() => vi.fn());
const resolveTelegramFetchMock = vi.hoisted(() => vi.fn(() => fetchWithTimeoutMock));
const resolveTelegramApiBaseMock = vi.hoisted(() => vi.fn(() => "https://api.telegram.org"));
vi.mock("openclaw/plugin-sdk/text-runtime", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/text-runtime")>();
return {
...actual,
fetchWithTimeout: fetchWithTimeoutMock,
};
});
vi.mock("openclaw/plugin-sdk/text-runtime", () => ({
fetchWithTimeout: fetchWithTimeoutMock,
isRecord: (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null,
}));
function mockGetChatMemberStatus(status: string) {
fetchWithTimeoutMock.mockResolvedValueOnce(

View File

@@ -10,7 +10,6 @@ import {
import {
buildCommandsMessagePaginated,
buildCommandsPaginationKeyboard,
formatModelsAvailableHeader,
resolveStoredModelOverride,
} from "openclaw/plugin-sdk/command-auth";
import { writeConfigFile } from "openclaw/plugin-sdk/config-runtime";
@@ -32,6 +31,7 @@ import {
resolvePluginConversationBindingApproval,
} from "openclaw/plugin-sdk/conversation-runtime";
import { parseExecApprovalCommandText } from "openclaw/plugin-sdk/infra-runtime";
import { formatModelsAvailableHeader } from "openclaw/plugin-sdk/models-provider-runtime";
import { dispatchPluginInteractiveHandler } from "openclaw/plugin-sdk/plugin-runtime";
import { resolveAgentRoute } from "openclaw/plugin-sdk/routing";
import { resolveThreadSessionKeys } from "openclaw/plugin-sdk/routing";

View File

@@ -1,4 +1,3 @@
import type { SkillCommandSpec } from "openclaw/plugin-sdk/command-auth";
import type { RuntimeEnv } from "openclaw/plugin-sdk/runtime-env";
import { expect, vi } from "vitest";
import type { OpenClawConfig } from "../runtime-api.js";
@@ -23,9 +22,7 @@ type CreateCommandBotResult = {
};
const skillCommandMocks = vi.hoisted(() => ({
listSkillCommandsForAgents: vi.fn<
(params: { cfg: OpenClawConfig; agentIds?: string[] }) => SkillCommandSpec[]
>(() => []),
listSkillCommandsForAgents: vi.fn<TelegramBotDeps["listSkillCommandsForAgents"]>(() => []),
}));
const deliveryMocks = vi.hoisted(() => ({
@@ -39,14 +36,6 @@ export const deliverReplies = deliveryMocks.deliverReplies;
export const editMessageTelegram = deliveryMocks.editMessageTelegram;
export const emitTelegramMessageSentHooks = deliveryMocks.emitTelegramMessageSentHooks;
vi.mock("openclaw/plugin-sdk/command-auth", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/command-auth")>();
return {
...actual,
listSkillCommandsForAgents,
};
});
vi.mock("./bot/delivery.js", () => ({
deliverReplies,
emitTelegramMessageSentHooks,

View File

@@ -249,22 +249,6 @@ function createModelsProviderDataFromConfig(cfg: OpenClawConfig): {
return { byProvider, providers, resolvedDefault, modelNames: new Map<string, string>() };
}
vi.doMock("openclaw/plugin-sdk/command-auth", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/command-auth")>();
return {
...actual,
listSkillCommandsForAgents: skillCommandListHoisted.listSkillCommandsForAgents,
buildModelsProviderData,
};
});
vi.doMock("openclaw/plugin-sdk/command-auth.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/command-auth")>();
return {
...actual,
listSkillCommandsForAgents: skillCommandListHoisted.listSkillCommandsForAgents,
buildModelsProviderData,
};
});
vi.doMock("openclaw/plugin-sdk/reply-runtime", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/reply-runtime")>();
return {
@@ -296,14 +280,6 @@ const execApprovalHoisted = vi.hoisted(() => ({
}));
export const resolveExecApprovalSpy = execApprovalHoisted.resolveExecApprovalSpy;
vi.doMock("openclaw/plugin-sdk/infra-runtime", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/infra-runtime")>();
return {
...actual,
enqueueSystemEvent: systemEventsHoisted.enqueueSystemEventSpy,
};
});
const sentMessageCacheHoisted = vi.hoisted(() => ({
wasSentByBot: vi.fn(() => false),
}));

View File

@@ -9,15 +9,14 @@ import {
resolveChannelGroupPolicy,
resolveChannelGroupRequireMention,
} from "openclaw/plugin-sdk/config-runtime";
import { loadSessionStore, resolveStorePath } from "openclaw/plugin-sdk/config-runtime";
import {
resolveThreadBindingIdleTimeoutMsForChannel,
resolveThreadBindingMaxAgeMsForChannel,
resolveThreadBindingSpawnPolicy,
} from "openclaw/plugin-sdk/conversation-runtime";
import { formatUncaughtError } from "openclaw/plugin-sdk/error-runtime";
import { DEFAULT_GROUP_HISTORY_LIMIT, type HistoryEntry } from "openclaw/plugin-sdk/reply-history";
import { resolveTextChunkLimit } from "openclaw/plugin-sdk/reply-chunking";
import { DEFAULT_GROUP_HISTORY_LIMIT, type HistoryEntry } from "openclaw/plugin-sdk/reply-history";
import { danger, logVerbose, shouldLogVerbose } from "openclaw/plugin-sdk/runtime-env";
import { getChildLogger } from "openclaw/plugin-sdk/runtime-env";
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
@@ -437,9 +436,13 @@ export function createTelegramBot(opts: TelegramBotOptions) {
const sessionKey =
params.sessionKey ??
`agent:${agentId}:telegram:group:${buildTelegramGroupPeerId(params.chatId, params.messageThreadId)}`;
const storePath = resolveStorePath(cfg.session?.store, { agentId });
const storePath = telegramDeps.resolveStorePath(cfg.session?.store, { agentId });
try {
const store = (telegramDeps.loadSessionStore ?? loadSessionStore)(storePath);
const loadSessionStore = telegramDeps.loadSessionStore;
if (!loadSessionStore) {
return undefined;
}
const store = loadSessionStore(storePath);
const entry = store[sessionKey];
if (entry?.groupActivation === "always") {
return false;

View File

@@ -1,13 +1,11 @@
import type { TelegramNetworkConfig } from "openclaw/plugin-sdk/config-runtime";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("openclaw/plugin-sdk/runtime-env", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/runtime-env")>();
return {
...actual,
isWSL2Sync: vi.fn(() => false),
};
});
vi.mock("openclaw/plugin-sdk/runtime-env", () => ({
isTruthyEnvValue: (value: string | undefined) =>
typeof value === "string" && /^(1|true|yes|on)$/i.test(value.trim()),
isWSL2Sync: vi.fn(() => false),
}));
let isWSL2Sync: typeof import("openclaw/plugin-sdk/runtime-env").isWSL2Sync;
let resetTelegramNetworkConfigStateForTests: typeof import("./network-config.js").resetTelegramNetworkConfigStateForTests;

View File

@@ -22,14 +22,11 @@ vi.mock("./api-logging.js", () => ({
withTelegramApiErrorLogging: async ({ fn }: { fn: () => Promise<unknown> }) => await fn(),
}));
vi.mock("openclaw/plugin-sdk/runtime-env", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/runtime-env")>();
return {
...actual,
computeBackoff: computeBackoffMock,
sleepWithAbort: sleepWithAbortMock,
};
});
vi.mock("openclaw/plugin-sdk/runtime-env", () => ({
computeBackoff: computeBackoffMock,
formatDurationPrecise: vi.fn((ms: number) => `${ms}ms`),
sleepWithAbort: sleepWithAbortMock,
}));
let TelegramPollingSession: typeof import("./polling-session.js").TelegramPollingSession;

View File

@@ -0,0 +1,10 @@
export { loadConfig, resolveMarkdownTableMode } from "openclaw/plugin-sdk/config-runtime";
export type { PollInput, MediaKind } from "openclaw/plugin-sdk/media-runtime";
export {
buildOutboundMediaLoadOptions,
getImageMetadata,
isGifMedia,
kindFromMime,
normalizePollInput,
} from "openclaw/plugin-sdk/media-runtime";
export { loadWebMedia } from "openclaw/plugin-sdk/web-media";

View File

@@ -1,3 +1,10 @@
import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/config-runtime";
import {
buildOutboundMediaLoadOptions,
isGifMedia,
kindFromMime,
normalizePollInput,
} from "openclaw/plugin-sdk/media-runtime";
import type { MockFn } from "openclaw/plugin-sdk/testing";
import { beforeEach, vi } from "vitest";
@@ -89,14 +96,6 @@ vi.mock("openclaw/plugin-sdk/web-media", () => ({
loadWebMedia,
}));
vi.mock("openclaw/plugin-sdk/media-runtime", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/media-runtime")>();
return {
...actual,
getImageMetadata: vi.fn(async () => ({ ...imageMetadata })),
};
});
vi.mock("grammy", () => ({
API_CONSTANTS: {
DEFAULT_UPDATE_TYPES: ["message"],
@@ -136,14 +135,17 @@ vi.mock("undici", () => ({
setGlobalDispatcher: undiciSetGlobalDispatcher,
}));
vi.mock("openclaw/plugin-sdk/config-runtime", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/config-runtime")>();
return {
...actual,
loadConfig,
resolveStorePath,
};
});
vi.mock("./send.runtime.js", () => ({
buildOutboundMediaLoadOptions,
getImageMetadata: vi.fn(async () => ({ ...imageMetadata })),
isGifMedia,
kindFromMime,
loadConfig,
loadWebMedia,
normalizePollInput,
resolveMarkdownTableMode,
resolveStorePath,
}));
vi.mock("./target-writeback.js", () => ({
maybePersistResolvedTelegramTarget,

View File

@@ -6,22 +6,14 @@ import type {
} from "@grammyjs/types";
import { type ApiClientOptions, Bot, HttpError } from "grammy";
import * as grammy from "grammy";
import { loadConfig } from "openclaw/plugin-sdk/config-runtime";
import { resolveMarkdownTableMode } from "openclaw/plugin-sdk/config-runtime";
import { isDiagnosticFlagEnabled } from "openclaw/plugin-sdk/diagnostic-runtime";
import { formatUncaughtError } from "openclaw/plugin-sdk/error-runtime";
import { recordChannelActivity } from "openclaw/plugin-sdk/infra-runtime";
import type { MediaKind } from "openclaw/plugin-sdk/media-runtime";
import { buildOutboundMediaLoadOptions } from "openclaw/plugin-sdk/media-runtime";
import { getImageMetadata } from "openclaw/plugin-sdk/media-runtime";
import { isGifMedia, kindFromMime } from "openclaw/plugin-sdk/media-runtime";
import { normalizePollInput, type PollInput } from "openclaw/plugin-sdk/media-runtime";
import { createTelegramRetryRunner, type RetryConfig } from "openclaw/plugin-sdk/retry-runtime";
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
import { formatErrorMessage } from "openclaw/plugin-sdk/ssrf-runtime";
import { redactSensitiveText } from "openclaw/plugin-sdk/text-runtime";
import { loadWebMedia } from "openclaw/plugin-sdk/web-media";
import { type ResolvedTelegramAccount, resolveTelegramAccount } from "./accounts.js";
import { withTelegramApiErrorLogging } from "./api-logging.js";
import { buildTelegramThreadParams, buildTypingThreadParams } from "./bot/helpers.js";
@@ -37,6 +29,18 @@ import {
} from "./network-errors.js";
import { normalizeTelegramReplyToMessageId } from "./outbound-params.js";
import { makeProxyFetch } from "./proxy.js";
import {
buildOutboundMediaLoadOptions,
getImageMetadata,
isGifMedia,
kindFromMime,
loadConfig,
loadWebMedia,
normalizePollInput,
type PollInput,
type MediaKind,
resolveMarkdownTableMode,
} from "./send.runtime.js";
import { recordSentMessage } from "./sent-message-cache.js";
import { maybePersistResolvedTelegramTarget } from "./target-writeback.js";
import {

View File

@@ -5,13 +5,10 @@ const mocks = vi.hoisted(() => ({
}));
// Mock the runtime-exported backoff sleep that the handler actually imports.
vi.mock("openclaw/plugin-sdk/runtime-env", async (importOriginal) => {
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/runtime-env")>();
return {
...actual,
sleepWithAbort: mocks.sleepWithAbort,
};
});
vi.mock("openclaw/plugin-sdk/runtime-env", () => ({
computeBackoff: vi.fn((_policy, attempt: number) => attempt * 1000),
sleepWithAbort: mocks.sleepWithAbort,
}));
let createTelegramSendChatActionHandler: typeof import("./sendchataction-401-backoff.js").createTelegramSendChatActionHandler;