fix: honor zalo default runtime account

This commit is contained in:
Tak Hoffman 2026-04-03 13:19:39 -05:00
parent 5e365a8ec4
commit 5eb3341db1
No known key found for this signature in database
4 changed files with 41 additions and 2 deletions

View File

@ -45,4 +45,26 @@ describe("resolveZaloAccount", () => {
expect(resolved.enabled).toBe(true);
expect(resolved.config.webhookUrl).toBe("https://top.example.com");
});
it("uses configured defaultAccount when accountId is omitted", () => {
const resolved = resolveZaloAccount({
cfg: {
channels: {
zalo: {
defaultAccount: "work",
accounts: {
work: {
name: "Work",
botToken: "work-token",
},
},
},
},
},
});
expect(resolved.accountId).toBe("work");
expect(resolved.name).toBe("Work");
expect(resolved.token).toBe("work-token");
});
});

View File

@ -29,7 +29,9 @@ export function resolveZaloAccount(params: {
accountId?: string | null;
allowUnresolvedSecretRef?: boolean;
}): ResolvedZaloAccount {
const accountId = normalizeAccountId(params.accountId);
const accountId = normalizeAccountId(
params.accountId ?? (params.cfg.channels?.zalo as ZaloConfig | undefined)?.defaultAccount,
);
const baseEnabled = (params.cfg.channels?.zalo as ZaloConfig | undefined)?.enabled !== false;
const merged = mergeZaloAccountConfig(params.cfg, accountId);
const accountEnabled = merged.enabled !== false;

View File

@ -32,6 +32,21 @@ describe("resolveZaloToken", () => {
expect(res.source).toBe("config");
});
it("uses configured defaultAccount token when accountId is omitted", () => {
const cfg = {
defaultAccount: "work",
botToken: "top-level-token",
accounts: {
work: {
botToken: "work-token",
},
},
} as ZaloConfig;
const res = resolveZaloToken(cfg);
expect(res.token).toBe("work-token");
expect(res.source).toBe("config");
});
it("does not inherit top-level token when account token is explicitly blank", () => {
const cfg = {
botToken: "top-level-token",

View File

@ -18,7 +18,7 @@ export function resolveZaloToken(
accountId?: string | null,
options?: { allowUnresolvedSecretRef?: boolean },
): ZaloTokenResolution {
const resolvedAccountId = accountId ?? DEFAULT_ACCOUNT_ID;
const resolvedAccountId = normalizeAccountId(accountId ?? config?.defaultAccount);
const isDefaultAccount = resolvedAccountId === DEFAULT_ACCOUNT_ID;
const baseConfig = config;
const accountConfig = resolveAccountEntry(