From bb7ba83550be2fdfb7a841d544924d820824d4fe Mon Sep 17 00:00:00 2001 From: Gustavo Madeira Santana Date: Fri, 17 Apr 2026 11:25:38 -0400 Subject: [PATCH] Twitch: align normalized token lookup --- extensions/twitch/src/config.test.ts | 28 ++++++++++++++++++++++++++++ extensions/twitch/src/token.test.ts | 21 +++++++++++++++++++++ extensions/twitch/src/token.ts | 12 +++++++----- 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/extensions/twitch/src/config.test.ts b/extensions/twitch/src/config.test.ts index db072c723a6..97bb6989f50 100644 --- a/extensions/twitch/src/config.test.ts +++ b/extensions/twitch/src/config.test.ts @@ -202,4 +202,32 @@ describe("resolveTwitchAccountContext", () => { expect(context.accountId).toBe("secondary"); expect(context.account?.username).toBe("second-bot"); }); + + it("keeps account and token lookup aligned after account id normalization", () => { + const context = resolveTwitchAccountContext( + { + channels: { + twitch: { + accounts: { + Secondary: { + username: "second-bot", + accessToken: "oauth:second-token", + clientId: "second-client", + channel: "#second", + }, + }, + }, + }, + } as Parameters[0], + "secondary", + ); + + expect(context.accountId).toBe("secondary"); + expect(context.account?.username).toBe("second-bot"); + expect(context.tokenResolution).toEqual({ + token: "oauth:second-token", + source: "config", + }); + expect(context.configured).toBe(true); + }); }); diff --git a/extensions/twitch/src/token.test.ts b/extensions/twitch/src/token.test.ts index ac9c96f5221..1e33dc6bdea 100644 --- a/extensions/twitch/src/token.test.ts +++ b/extensions/twitch/src/token.test.ts @@ -65,6 +65,27 @@ describe("token", () => { expect(result.source).toBe("config"); }); + it("should resolve token from normalized account id", () => { + const result = resolveTwitchToken( + { + channels: { + twitch: { + accounts: { + Secondary: { + username: "secondary", + accessToken: "oauth:secondary-token", + }, + }, + }, + }, + } as unknown as OpenClawConfig, + { accountId: "secondary" }, + ); + + expect(result.token).toBe("oauth:secondary-token"); + expect(result.source).toBe("config"); + }); + it("should prioritize config token over env var (simplified config)", () => { process.env.OPENCLAW_TWITCH_ACCESS_TOKEN = "oauth:env-token"; diff --git a/extensions/twitch/src/token.ts b/extensions/twitch/src/token.ts index 40ba98e6da0..13ef6ec20c2 100644 --- a/extensions/twitch/src/token.ts +++ b/extensions/twitch/src/token.ts @@ -9,8 +9,12 @@ * 2. Environment variable: OPENCLAW_TWITCH_ACCESS_TOKEN (default account only) */ +import { + DEFAULT_ACCOUNT_ID, + normalizeAccountId, + resolveNormalizedAccountEntry, +} from "openclaw/plugin-sdk/account-resolution"; import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime"; -import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk/routing"; export type TwitchTokenSource = "env" | "config" | "none"; @@ -56,10 +60,8 @@ export function resolveTwitchToken( // Get merged account config (handles both simplified and multi-account patterns) const twitchCfg = cfg?.channels?.twitch; - const accountCfg = - accountId === DEFAULT_ACCOUNT_ID - ? (twitchCfg?.accounts?.[DEFAULT_ACCOUNT_ID] as Record | undefined) - : (twitchCfg?.accounts?.[accountId] as Record | undefined); + const accounts = twitchCfg?.accounts as Record> | undefined; + const accountCfg = resolveNormalizedAccountEntry(accounts, accountId, normalizeAccountId); // For default account, also check base-level config let token: string | undefined;