fix: align channel config schemas and env precedence
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { ZodTypeAny } from "zod";
|
||||
|
||||
import type { ChannelConfigSchema } from "./types.js";
|
||||
import type { ChannelConfigSchema } from "./types.plugin.js";
|
||||
|
||||
export function buildChannelConfigSchema(schema: ZodTypeAny): ChannelConfigSchema {
|
||||
return {
|
||||
|
||||
@@ -27,7 +27,7 @@ describe("resolveTelegramAccount", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("prefers TELEGRAM_BOT_TOKEN when accountId is omitted", () => {
|
||||
it("uses TELEGRAM_BOT_TOKEN when default account config is missing", () => {
|
||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||
process.env.TELEGRAM_BOT_TOKEN = "tok-env";
|
||||
try {
|
||||
@@ -50,6 +50,29 @@ describe("resolveTelegramAccount", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("prefers default config token over TELEGRAM_BOT_TOKEN", () => {
|
||||
const prevTelegramToken = process.env.TELEGRAM_BOT_TOKEN;
|
||||
process.env.TELEGRAM_BOT_TOKEN = "tok-env";
|
||||
try {
|
||||
const cfg: ClawdbotConfig = {
|
||||
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");
|
||||
} 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 = "";
|
||||
|
||||
@@ -74,8 +74,8 @@ export function resolveTelegramAccount(params: {
|
||||
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.
|
||||
// the implicit "default" account. This keeps env-based setups working 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);
|
||||
|
||||
Reference in New Issue
Block a user