mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 15:34:31 +00:00
fix(channels): add optional defaultAccount routing
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { withEnv } from "../test-utils/env.js";
|
||||
import { listTelegramAccountIds, resolveTelegramAccount } from "./accounts.js";
|
||||
import {
|
||||
listTelegramAccountIds,
|
||||
resolveDefaultTelegramAccountId,
|
||||
resolveTelegramAccount,
|
||||
} from "./accounts.js";
|
||||
|
||||
const { warnMock } = vi.hoisted(() => ({
|
||||
warnMock: vi.fn(),
|
||||
@@ -100,6 +104,47 @@ describe("resolveTelegramAccount", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveDefaultTelegramAccountId", () => {
|
||||
it("prefers channels.telegram.defaultAccount when it matches a configured account", () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
channels: {
|
||||
telegram: {
|
||||
defaultAccount: "work",
|
||||
accounts: { default: { botToken: "tok-default" }, work: { botToken: "tok-work" } },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
expect(resolveDefaultTelegramAccountId(cfg)).toBe("work");
|
||||
});
|
||||
|
||||
it("normalizes channels.telegram.defaultAccount before lookup", () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
channels: {
|
||||
telegram: {
|
||||
defaultAccount: "Router D",
|
||||
accounts: { "router-d": { botToken: "tok-work" } },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
expect(resolveDefaultTelegramAccountId(cfg)).toBe("router-d");
|
||||
});
|
||||
|
||||
it("falls back when channels.telegram.defaultAccount is not configured", () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
channels: {
|
||||
telegram: {
|
||||
defaultAccount: "missing",
|
||||
accounts: { default: { botToken: "tok-default" }, work: { botToken: "tok-work" } },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
expect(resolveDefaultTelegramAccountId(cfg)).toBe("default");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveTelegramAccount allowFrom precedence", () => {
|
||||
it("prefers accounts.default allowlists over top-level for default account", () => {
|
||||
const resolved = resolveTelegramAccount({
|
||||
|
||||
@@ -6,7 +6,11 @@ import { isTruthyEnvValue } from "../infra/env.js";
|
||||
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||
import { resolveAccountEntry } from "../routing/account-lookup.js";
|
||||
import { listBoundAccountIds, resolveDefaultAgentBoundAccountId } from "../routing/bindings.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js";
|
||||
import {
|
||||
DEFAULT_ACCOUNT_ID,
|
||||
normalizeAccountId,
|
||||
normalizeOptionalAccountId,
|
||||
} from "../routing/session-key.js";
|
||||
import { resolveTelegramToken } from "./token.js";
|
||||
|
||||
const log = createSubsystemLogger("telegram/accounts");
|
||||
@@ -68,6 +72,13 @@ export function resolveDefaultTelegramAccountId(cfg: OpenClawConfig): string {
|
||||
if (boundDefault) {
|
||||
return boundDefault;
|
||||
}
|
||||
const preferred = normalizeOptionalAccountId(cfg.channels?.telegram?.defaultAccount);
|
||||
if (
|
||||
preferred &&
|
||||
listTelegramAccountIds(cfg).some((accountId) => normalizeAccountId(accountId) === preferred)
|
||||
) {
|
||||
return preferred;
|
||||
}
|
||||
const ids = listTelegramAccountIds(cfg);
|
||||
if (ids.includes(DEFAULT_ACCOUNT_ID)) {
|
||||
return DEFAULT_ACCOUNT_ID;
|
||||
@@ -86,9 +97,13 @@ function resolveAccountConfig(
|
||||
function mergeTelegramAccountConfig(cfg: OpenClawConfig, accountId: string): TelegramAccountConfig {
|
||||
const {
|
||||
accounts: _ignored,
|
||||
defaultAccount: _ignoredDefaultAccount,
|
||||
groups: channelGroups,
|
||||
...base
|
||||
} = (cfg.channels?.telegram ?? {}) as TelegramAccountConfig & { accounts?: unknown };
|
||||
} = (cfg.channels?.telegram ?? {}) as TelegramAccountConfig & {
|
||||
accounts?: unknown;
|
||||
defaultAccount?: unknown;
|
||||
};
|
||||
const account = resolveAccountConfig(cfg, accountId) ?? {};
|
||||
|
||||
// In multi-account setups, channel-level `groups` must NOT be inherited by
|
||||
|
||||
Reference in New Issue
Block a user