From c22edbb8eeb3668f60b0b23cd8e8e11b4340f6d6 Mon Sep 17 00:00:00 2001 From: Shakker Date: Mon, 30 Mar 2026 17:08:26 +0100 Subject: [PATCH] test: align ci regression stubs with production behavior --- src/auto-reply/reply/commands.test.ts | 70 ++++++++++++++++--- .../media-understanding-misc.test.ts | 3 +- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/auto-reply/reply/commands.test.ts b/src/auto-reply/reply/commands.test.ts index c3b61cbecb8..9a7bc5144e0 100644 --- a/src/auto-reply/reply/commands.test.ts +++ b/src/auto-reply/reply/commands.test.ts @@ -7,6 +7,7 @@ import type { OpenClawConfig } from "../../config/config.js"; import { updateSessionStore, type SessionEntry } from "../../config/sessions.js"; import { formatAllowFromLowercase } from "../../plugin-sdk/allow-from.js"; import { buildDmGroupAccountAllowlistAdapter } from "../../plugin-sdk/allowlist-config-edit.js"; +import { resolveApprovalApprovers } from "../../plugin-sdk/approval-approvers.js"; import { createApproverRestrictedNativeApprovalAdapter } from "../../plugin-sdk/approval-runtime.js"; import { createScopedChannelConfigAdapter } from "../../plugin-sdk/channel-config-helpers.js"; import { setActivePluginRegistry } from "../../plugins/runtime.js"; @@ -216,15 +217,36 @@ function resolveTelegramTestAccount( } function normalizeTelegramAllowFromEntries(values: Array): string[] { - return formatAllowFromLowercase({ allowFrom: values }); + return formatAllowFromLowercase({ allowFrom: values, stripPrefixRe: /^(telegram|tg):/i }); +} + +function stripTelegramInternalPrefixes(value: string): string { + let trimmed = value.trim(); + let strippedTelegramPrefix = false; + while (true) { + const next = (() => { + if (/^(telegram|tg):/i.test(trimmed)) { + strippedTelegramPrefix = true; + return trimmed.replace(/^(telegram|tg):/i, "").trim(); + } + if (strippedTelegramPrefix && /^group:/i.test(trimmed)) { + return trimmed.replace(/^group:/i, "").trim(); + } + return trimmed; + })(); + if (next === trimmed) { + return trimmed; + } + trimmed = next; + } } function normalizeTelegramDirectApproverId(value: string | number): string | undefined { - const normalized = String(value).trim(); + const normalized = stripTelegramInternalPrefixes(String(value)); if (!normalized || normalized.startsWith("-")) { return undefined; } - return normalized.replace(/^(?:tg|telegram):/i, ""); + return normalized; } function getTelegramExecApprovalApprovers(params: { @@ -232,12 +254,11 @@ function getTelegramExecApprovalApprovers(params: { accountId?: string | null; }): string[] { const account = resolveTelegramTestAccount(params.cfg, params.accountId); - const explicit = account.execApprovals?.approvers; - const allowFrom = account.allowFrom; - const source = Array.isArray(explicit) ? explicit : Array.isArray(allowFrom) ? allowFrom : []; - return source - .map((entry) => normalizeTelegramDirectApproverId(entry)) - .filter((entry): entry is string => Boolean(entry)); + return resolveApprovalApprovers({ + explicit: account.execApprovals?.approvers, + allowFrom: account.allowFrom, + normalizeApprover: normalizeTelegramDirectApproverId, + }); } function isTelegramExecApprovalTargetRecipient(params: { @@ -272,7 +293,7 @@ function isTelegramExecApprovalAuthorizedSender(params: { accountId?: string | null; senderId?: string | null; }): boolean { - const senderId = params.senderId?.trim(); + const senderId = params.senderId ? normalizeTelegramDirectApproverId(params.senderId) : undefined; if (!senderId) { return false; } @@ -355,6 +376,35 @@ const telegramCommandTestPlugin: ChannelPlugin = { }), }; +describe("telegram command test plugin helpers", () => { + it("normalizes telegram allowFrom entries like the production adapter", () => { + expect(normalizeTelegramAllowFromEntries([" TG:123 ", "telegram:456", "@Alice"])).toEqual([ + "123", + "456", + "@alice", + ]); + }); + + it("falls back to allowFrom when explicit exec approvers are empty", () => { + expect( + getTelegramExecApprovalApprovers({ + cfg: { + channels: { + telegram: { + allowFrom: ["tg:123"], + execApprovals: { enabled: true, approvers: [] }, + }, + }, + } as OpenClawConfig, + }), + ).toEqual(["123"]); + }); + + it("rejects prefixed telegram group ids as direct approvers", () => { + expect(normalizeTelegramDirectApproverId("tg:-100123456")).toBeUndefined(); + }); +}); + function setMinimalChannelPluginRegistryForTests(): void { setActivePluginRegistry( createTestRegistry([ diff --git a/src/media-understanding/media-understanding-misc.test.ts b/src/media-understanding/media-understanding-misc.test.ts index 5dc06b17420..0c1d3297180 100644 --- a/src/media-understanding/media-understanding-misc.test.ts +++ b/src/media-understanding/media-understanding-misc.test.ts @@ -135,8 +135,7 @@ describe("media understanding attachments SSRF", () => { openSpy.mockImplementation(async (filePath, flags) => { const handle = await originalOpen(filePath, flags); - const candidatePath = - (await fs.realpath(String(filePath)).catch(() => String(filePath))) ?? String(filePath); + const candidatePath = await fs.realpath(String(filePath)).catch(() => String(filePath)); if (candidatePath !== canonicalAttachmentPath) { return handle; }