diff --git a/extensions/feishu/src/tool-account.test.ts b/extensions/feishu/src/tool-account.test.ts new file mode 100644 index 00000000000..208215e6e79 --- /dev/null +++ b/extensions/feishu/src/tool-account.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, it } from "vitest"; +import { resolveFeishuToolAccount } from "./tool-account.js"; + +describe("resolveFeishuToolAccount", () => { + const cfg = { + channels: { + feishu: { + enabled: true, + defaultAccount: "ops", + appId: "base-app-id", + appSecret: "base-app-secret", // pragma: allowlist secret + accounts: { + ops: { + enabled: true, + appId: "ops-app-id", + appSecret: "ops-app-secret", // pragma: allowlist secret + }, + work: { + enabled: true, + appId: "work-app-id", + appSecret: "work-app-secret", // pragma: allowlist secret + }, + }, + }, + }, + }; + + it("prefers the active contextual account over configured defaultAccount", () => { + const resolved = resolveFeishuToolAccount({ + api: { config: cfg }, + defaultAccountId: "work", + }); + + expect(resolved.accountId).toBe("work"); + }); + + it("falls back to configured defaultAccount when there is no contextual account", () => { + const resolved = resolveFeishuToolAccount({ + api: { config: cfg }, + }); + + expect(resolved.accountId).toBe("ops"); + }); +}); diff --git a/extensions/feishu/src/tool-account.ts b/extensions/feishu/src/tool-account.ts index 305b9fb4f99..a6b13e5c1a0 100644 --- a/extensions/feishu/src/tool-account.ts +++ b/extensions/feishu/src/tool-account.ts @@ -35,25 +35,23 @@ function resolveImplicitToolAccountId(params: { return explicitAccountId; } + const contextualAccountId = normalizeOptionalAccountId(params.defaultAccountId); + if (contextualAccountId && listFeishuAccountIds(params.api.config).includes(contextualAccountId)) { + const contextualAccount = resolveFeishuAccount({ + cfg: params.api.config, + accountId: contextualAccountId, + }); + if (contextualAccount.enabled) { + return contextualAccountId; + } + } + const configuredDefaultAccountId = readConfiguredDefaultAccountId(params.api.config); if (configuredDefaultAccountId) { return configuredDefaultAccountId; } - const contextualAccountId = normalizeOptionalAccountId(params.defaultAccountId); - if (!contextualAccountId) { - return undefined; - } - - if (!listFeishuAccountIds(params.api.config).includes(contextualAccountId)) { - return undefined; - } - - const contextualAccount = resolveFeishuAccount({ - cfg: params.api.config, - accountId: contextualAccountId, - }); - return contextualAccount.enabled ? contextualAccountId : undefined; + return undefined; } export function resolveFeishuToolAccount(params: {