mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 14:34:32 +00:00
refactor(test): dedupe telegram token env handling in tests
This commit is contained in:
@@ -8,6 +8,7 @@ import { STATE_DIR } from "../../config/paths.js";
|
|||||||
import { setActivePluginRegistry } from "../../plugins/runtime.js";
|
import { setActivePluginRegistry } from "../../plugins/runtime.js";
|
||||||
import { markdownToSignalTextChunks } from "../../signal/format.js";
|
import { markdownToSignalTextChunks } from "../../signal/format.js";
|
||||||
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
|
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
|
||||||
|
import { withEnvAsync } from "../../test-utils/env.js";
|
||||||
import { createIMessageTestPlugin } from "../../test-utils/imessage-test-plugin.js";
|
import { createIMessageTestPlugin } from "../../test-utils/imessage-test-plugin.js";
|
||||||
import { createInternalHookEventPayload } from "../../test-utils/internal-hook-event-payload.js";
|
import { createInternalHookEventPayload } from "../../test-utils/internal-hook-event-payload.js";
|
||||||
|
|
||||||
@@ -101,9 +102,7 @@ describe("deliverOutboundPayloads", () => {
|
|||||||
});
|
});
|
||||||
it("chunks telegram markdown and passes through accountId", async () => {
|
it("chunks telegram markdown and passes through accountId", async () => {
|
||||||
const sendTelegram = vi.fn().mockResolvedValue({ messageId: "m1", chatId: "c1" });
|
const sendTelegram = vi.fn().mockResolvedValue({ messageId: "m1", chatId: "c1" });
|
||||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
await withEnvAsync({ TELEGRAM_BOT_TOKEN: "" }, async () => {
|
||||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
|
||||||
try {
|
|
||||||
const results = await deliverOutboundPayloads({
|
const results = await deliverOutboundPayloads({
|
||||||
cfg: telegramChunkConfig,
|
cfg: telegramChunkConfig,
|
||||||
channel: "telegram",
|
channel: "telegram",
|
||||||
@@ -120,20 +119,12 @@ describe("deliverOutboundPayloads", () => {
|
|||||||
}
|
}
|
||||||
expect(results).toHaveLength(2);
|
expect(results).toHaveLength(2);
|
||||||
expect(results[0]).toMatchObject({ channel: "telegram", chatId: "c1" });
|
expect(results[0]).toMatchObject({ channel: "telegram", chatId: "c1" });
|
||||||
} finally {
|
});
|
||||||
if (prevTelegramToken === undefined) {
|
|
||||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
|
||||||
} else {
|
|
||||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps payload replyToId across all chunked telegram sends", async () => {
|
it("keeps payload replyToId across all chunked telegram sends", async () => {
|
||||||
const sendTelegram = vi.fn().mockResolvedValue({ messageId: "m1", chatId: "c1" });
|
const sendTelegram = vi.fn().mockResolvedValue({ messageId: "m1", chatId: "c1" });
|
||||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
await withEnvAsync({ TELEGRAM_BOT_TOKEN: "" }, async () => {
|
||||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
|
||||||
try {
|
|
||||||
await deliverOutboundPayloads({
|
await deliverOutboundPayloads({
|
||||||
cfg: telegramChunkConfig,
|
cfg: telegramChunkConfig,
|
||||||
channel: "telegram",
|
channel: "telegram",
|
||||||
@@ -146,13 +137,7 @@ describe("deliverOutboundPayloads", () => {
|
|||||||
for (const call of sendTelegram.mock.calls) {
|
for (const call of sendTelegram.mock.calls) {
|
||||||
expect(call[2]).toEqual(expect.objectContaining({ replyToMessageId: 777 }));
|
expect(call[2]).toEqual(expect.objectContaining({ replyToMessageId: 777 }));
|
||||||
}
|
}
|
||||||
} finally {
|
});
|
||||||
if (prevTelegramToken === undefined) {
|
|
||||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
|
||||||
} else {
|
|
||||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("passes explicit accountId to sendTelegram", async () => {
|
it("passes explicit accountId to sendTelegram", async () => {
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import type { OpenClawConfig } from "../config/config.js";
|
import type { OpenClawConfig } from "../config/config.js";
|
||||||
|
import { withEnv } from "../test-utils/env.js";
|
||||||
import { resolveTelegramAccount } from "./accounts.js";
|
import { resolveTelegramAccount } from "./accounts.js";
|
||||||
|
|
||||||
describe("resolveTelegramAccount", () => {
|
describe("resolveTelegramAccount", () => {
|
||||||
it("falls back to the first configured account when accountId is omitted", () => {
|
it("falls back to the first configured account when accountId is omitted", () => {
|
||||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
withEnv({ TELEGRAM_BOT_TOKEN: "" }, () => {
|
||||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
|
||||||
try {
|
|
||||||
const cfg: OpenClawConfig = {
|
const cfg: OpenClawConfig = {
|
||||||
channels: {
|
channels: {
|
||||||
telegram: { accounts: { work: { botToken: "tok-work" } } },
|
telegram: { accounts: { work: { botToken: "tok-work" } } },
|
||||||
@@ -17,19 +16,11 @@ describe("resolveTelegramAccount", () => {
|
|||||||
expect(account.accountId).toBe("work");
|
expect(account.accountId).toBe("work");
|
||||||
expect(account.token).toBe("tok-work");
|
expect(account.token).toBe("tok-work");
|
||||||
expect(account.tokenSource).toBe("config");
|
expect(account.tokenSource).toBe("config");
|
||||||
} finally {
|
});
|
||||||
if (prevTelegramToken === undefined) {
|
|
||||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
|
||||||
} else {
|
|
||||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses TELEGRAM_BOT_TOKEN when default account config is missing", () => {
|
it("uses TELEGRAM_BOT_TOKEN when default account config is missing", () => {
|
||||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
withEnv({ TELEGRAM_BOT_TOKEN: "tok-env" }, () => {
|
||||||
process.env.TELEGRAM_BOT_TOKEN = "tok-env";
|
|
||||||
try {
|
|
||||||
const cfg: OpenClawConfig = {
|
const cfg: OpenClawConfig = {
|
||||||
channels: {
|
channels: {
|
||||||
telegram: { accounts: { work: { botToken: "tok-work" } } },
|
telegram: { accounts: { work: { botToken: "tok-work" } } },
|
||||||
@@ -40,19 +31,11 @@ describe("resolveTelegramAccount", () => {
|
|||||||
expect(account.accountId).toBe("default");
|
expect(account.accountId).toBe("default");
|
||||||
expect(account.token).toBe("tok-env");
|
expect(account.token).toBe("tok-env");
|
||||||
expect(account.tokenSource).toBe("env");
|
expect(account.tokenSource).toBe("env");
|
||||||
} finally {
|
});
|
||||||
if (prevTelegramToken === undefined) {
|
|
||||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
|
||||||
} else {
|
|
||||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("prefers default config token over TELEGRAM_BOT_TOKEN", () => {
|
it("prefers default config token over TELEGRAM_BOT_TOKEN", () => {
|
||||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
withEnv({ TELEGRAM_BOT_TOKEN: "tok-env" }, () => {
|
||||||
process.env.TELEGRAM_BOT_TOKEN = "tok-env";
|
|
||||||
try {
|
|
||||||
const cfg: OpenClawConfig = {
|
const cfg: OpenClawConfig = {
|
||||||
channels: {
|
channels: {
|
||||||
telegram: { botToken: "tok-config" },
|
telegram: { botToken: "tok-config" },
|
||||||
@@ -63,19 +46,11 @@ describe("resolveTelegramAccount", () => {
|
|||||||
expect(account.accountId).toBe("default");
|
expect(account.accountId).toBe("default");
|
||||||
expect(account.token).toBe("tok-config");
|
expect(account.token).toBe("tok-config");
|
||||||
expect(account.tokenSource).toBe("config");
|
expect(account.tokenSource).toBe("config");
|
||||||
} finally {
|
});
|
||||||
if (prevTelegramToken === undefined) {
|
|
||||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
|
||||||
} else {
|
|
||||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not fall back when accountId is explicitly provided", () => {
|
it("does not fall back when accountId is explicitly provided", () => {
|
||||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
withEnv({ TELEGRAM_BOT_TOKEN: "" }, () => {
|
||||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
|
||||||
try {
|
|
||||||
const cfg: OpenClawConfig = {
|
const cfg: OpenClawConfig = {
|
||||||
channels: {
|
channels: {
|
||||||
telegram: { accounts: { work: { botToken: "tok-work" } } },
|
telegram: { accounts: { work: { botToken: "tok-work" } } },
|
||||||
@@ -86,12 +61,6 @@ describe("resolveTelegramAccount", () => {
|
|||||||
expect(account.accountId).toBe("default");
|
expect(account.accountId).toBe("default");
|
||||||
expect(account.tokenSource).toBe("none");
|
expect(account.tokenSource).toBe("none");
|
||||||
expect(account.token).toBe("");
|
expect(account.token).toBe("");
|
||||||
} finally {
|
});
|
||||||
if (prevTelegramToken === undefined) {
|
|
||||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
|
||||||
} else {
|
|
||||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user