refactor(outbound): centralize telegram account defaults
This commit is contained in:
69
src/telegram/accounts.test.ts
Normal file
69
src/telegram/accounts.test.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import { resolveTelegramAccount } from "./accounts.js";
|
||||
|
||||
describe("resolveTelegramAccount", () => {
|
||||
it("falls back to the first configured account when accountId is omitted", () => {
|
||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
||||
try {
|
||||
const cfg: ClawdbotConfig = {
|
||||
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");
|
||||
} finally {
|
||||
if (prevTelegramToken === undefined) {
|
||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
||||
} else {
|
||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it("prefers TELEGRAM_BOT_TOKEN when accountId is omitted", () => {
|
||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||
process.env.TELEGRAM_BOT_TOKEN = "tok-env";
|
||||
try {
|
||||
const cfg: ClawdbotConfig = {
|
||||
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");
|
||||
} 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", () => {
|
||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||
process.env.TELEGRAM_BOT_TOKEN = "";
|
||||
try {
|
||||
const cfg: ClawdbotConfig = {
|
||||
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("");
|
||||
} finally {
|
||||
if (prevTelegramToken === undefined) {
|
||||
delete process.env.TELEGRAM_BOT_TOKEN;
|
||||
} else {
|
||||
process.env.TELEGRAM_BOT_TOKEN = prevTelegramToken;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -56,20 +56,37 @@ export function resolveTelegramAccount(params: {
|
||||
cfg: ClawdbotConfig;
|
||||
accountId?: string | null;
|
||||
}): ResolvedTelegramAccount {
|
||||
const accountId = normalizeAccountId(params.accountId);
|
||||
const hasExplicitAccountId = Boolean(params.accountId?.trim());
|
||||
const baseEnabled = params.cfg.telegram?.enabled !== false;
|
||||
const merged = mergeTelegramAccountConfig(params.cfg, accountId);
|
||||
const accountEnabled = merged.enabled !== false;
|
||||
const enabled = baseEnabled && accountEnabled;
|
||||
const tokenResolution = resolveTelegramToken(params.cfg, { accountId });
|
||||
return {
|
||||
accountId,
|
||||
enabled,
|
||||
name: merged.name?.trim() || undefined,
|
||||
token: tokenResolution.token,
|
||||
tokenSource: tokenResolution.source,
|
||||
config: merged,
|
||||
|
||||
const resolve = (accountId: string) => {
|
||||
const merged = mergeTelegramAccountConfig(params.cfg, accountId);
|
||||
const accountEnabled = merged.enabled !== false;
|
||||
const enabled = baseEnabled && accountEnabled;
|
||||
const tokenResolution = resolveTelegramToken(params.cfg, { accountId });
|
||||
return {
|
||||
accountId,
|
||||
enabled,
|
||||
name: merged.name?.trim() || undefined,
|
||||
token: tokenResolution.token,
|
||||
tokenSource: tokenResolution.source,
|
||||
config: merged,
|
||||
} satisfies ResolvedTelegramAccount;
|
||||
};
|
||||
|
||||
const normalized = normalizeAccountId(params.accountId);
|
||||
const primary = resolve(normalized);
|
||||
if (hasExplicitAccountId) return primary;
|
||||
if (primary.tokenSource !== "none") return primary;
|
||||
|
||||
// If accountId is omitted, prefer a configured account token over failing on
|
||||
// the implicit "default" account. This keeps env-based setups working (env
|
||||
// still wins) while making config-only tokens work for things like heartbeats.
|
||||
const fallbackId = resolveDefaultTelegramAccountId(params.cfg);
|
||||
if (fallbackId === primary.accountId) return primary;
|
||||
const fallback = resolve(fallbackId);
|
||||
if (fallback.tokenSource === "none") return primary;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
export function listEnabledTelegramAccounts(
|
||||
|
||||
Reference in New Issue
Block a user