Files
openclaw/src/telegram/accounts.test.ts
2026-02-21 19:13:47 +00:00

67 lines
2.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../config/config.js";
import { withEnv } from "../test-utils/env.js";
import { resolveTelegramAccount } from "./accounts.js";
describe("resolveTelegramAccount", () => {
it("falls back to the first configured account when accountId is omitted", () => {
withEnv({ TELEGRAM_BOT_TOKEN: "" }, () => {
const cfg: OpenClawConfig = {
channels: {
telegram: { accounts: { work: { botToken: "tok-work" } } },
},
};
const account = resolveTelegramAccount({ cfg });
expect(account.accountId).toBe("work");
expect(account.token).toBe("tok-work");
expect(account.tokenSource).toBe("config");
});
});
it("uses TELEGRAM_BOT_TOKEN when default account config is missing", () => {
withEnv({ TELEGRAM_BOT_TOKEN: "tok-env" }, () => {
const cfg: OpenClawConfig = {
channels: {
telegram: { accounts: { work: { botToken: "tok-work" } } },
},
};
const account = resolveTelegramAccount({ cfg });
expect(account.accountId).toBe("default");
expect(account.token).toBe("tok-env");
expect(account.tokenSource).toBe("env");
});
});
it("prefers default config token over TELEGRAM_BOT_TOKEN", () => {
withEnv({ TELEGRAM_BOT_TOKEN: "tok-env" }, () => {
const cfg: OpenClawConfig = {
channels: {
telegram: { botToken: "tok-config" },
},
};
const account = resolveTelegramAccount({ cfg });
expect(account.accountId).toBe("default");
expect(account.token).toBe("tok-config");
expect(account.tokenSource).toBe("config");
});
});
it("does not fall back when accountId is explicitly provided", () => {
withEnv({ TELEGRAM_BOT_TOKEN: "" }, () => {
const cfg: OpenClawConfig = {
channels: {
telegram: { accounts: { work: { botToken: "tok-work" } } },
},
};
const account = resolveTelegramAccount({ cfg, accountId: "default" });
expect(account.accountId).toBe("default");
expect(account.tokenSource).toBe("none");
expect(account.token).toBe("");
});
});
});