Files
openclaw/extensions/telegram/src/threading-tool-context.test.ts
T
Peter Steinberger 725fe96bd7 fix(telegram): avoid credential reads in reply policy
Resolve reply mode through Telegram’s config-only account projection so threading policy never reads token files or SecretRefs.
2026-08-08 06:53:37 -07:00

103 lines
3.0 KiB
TypeScript

// Telegram tests cover threading tool context plugin behavior.
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
import { describe, expect, it, vi } from "vitest";
import { telegramPlugin } from "./channel.js";
import { buildTelegramThreadingToolContext } from "./threading-tool-context.js";
const tryReadSecretFileSyncMock = vi.hoisted(() =>
vi.fn(() => {
throw new Error("reply mode must not read Telegram credentials");
}),
);
vi.mock("openclaw/plugin-sdk/secret-file-runtime", async (importOriginal) => ({
...(await importOriginal<typeof import("openclaw/plugin-sdk/secret-file-runtime")>()),
tryReadSecretFileSync: tryReadSecretFileSyncMock,
}));
describe("telegramPlugin reply threading", () => {
it.each([
{
name: "uses an account override",
telegram: {
accounts: {
sut: {
tokenFile: "/tmp/openclaw-telegram-reply-mode-must-not-read",
replyToMode: "first" as const,
},
},
},
expected: "first",
},
{
name: "inherits the top-level mode",
telegram: {
replyToMode: "all" as const,
accounts: {
sut: {
botToken: { source: "file", provider: "telegram_token", id: "value" },
},
},
},
expected: "all",
},
{
name: "allows an account to disable replies",
telegram: {
replyToMode: "all" as const,
accounts: { sut: { replyToMode: "off" as const } },
},
expected: "off",
},
])("$name", ({ telegram, expected }) => {
tryReadSecretFileSyncMock.mockClear();
const resolveReplyToMode = telegramPlugin.threading?.resolveReplyToMode;
if (!resolveReplyToMode) {
throw new Error("Telegram reply mode resolver is unavailable");
}
const cfg = { channels: { telegram } } as unknown as OpenClawConfig;
expect(resolveReplyToMode({ cfg, accountId: "sut" })).toBe(expected);
expect(tryReadSecretFileSyncMock).not.toHaveBeenCalled();
});
});
describe("buildTelegramThreadingToolContext", () => {
it("keeps topic thread state in plugin-owned tool context", () => {
const hasRepliedRef = { value: false };
expect(
buildTelegramThreadingToolContext({
cfg: {} as OpenClawConfig,
accountId: "default",
context: {
To: "telegram:-1001:topic:77",
MessageThreadId: 77,
CurrentMessageId: "msg-1",
},
hasRepliedRef,
}),
).toEqual({
currentChannelId: "telegram:-1001:topic:77",
currentThreadTs: "77",
hasRepliedRef,
});
});
it("parses topic thread state from target grammar when MessageThreadId is absent", () => {
expect(
buildTelegramThreadingToolContext({
cfg: {} as OpenClawConfig,
accountId: "default",
context: {
To: "telegram:-1001:topic:77",
CurrentMessageId: "msg-1",
},
}),
).toEqual({
currentChannelId: "telegram:-1001:topic:77",
currentThreadTs: "77",
hasRepliedRef: undefined,
});
});
});