Twitch: normalize setup account ids

This commit is contained in:
Gustavo Madeira Santana
2026-04-17 03:43:31 -04:00
parent 93cb0dd17b
commit 4453e698e5
2 changed files with 30 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ import {
promptRefreshTokenSetup,
promptToken,
promptUsername,
setTwitchAccount,
twitchSetupPlugin,
twitchSetupWizard,
} from "./setup-surface.js";
@@ -315,6 +316,29 @@ describe("setup surface helpers", () => {
});
describe("setup wizard account routing", () => {
it("normalizes account ids before using them as config keys", () => {
const cfg = setTwitchAccount(
{} as Parameters<typeof setTwitchAccount>[0],
{
username: "normalized-bot",
accessToken: "oauth:normalized",
clientId: "normalized-client",
channel: "#normalized",
},
"__proto__",
);
expect(cfg.channels?.twitch?.accounts?.default?.username).toBe("normalized-bot");
expect(Object.prototype).not.toHaveProperty("username");
expect(
twitchSetupWizard.status?.resolveStatusLines?.({
cfg: {},
accountId: "Alerts\r\n\u001b[31m",
configured: false,
} as never),
).toEqual(["Twitch (alerts-31m): needs username, token, and clientId"]);
});
it("reports account-scoped DM policy config keys", () => {
expect(
twitchSetupWizard.dmPolicy?.resolveConfigKeys?.(

View File

@@ -10,6 +10,7 @@ import {
type ChannelSetupWizard,
type OpenClawConfig,
type WizardPrompter,
normalizeAccountId,
} from "openclaw/plugin-sdk/setup";
import {
DEFAULT_ACCOUNT_ID,
@@ -26,11 +27,11 @@ const channel = "twitch" as const;
function resolveSetupAccountId(cfg: OpenClawConfig, requestedAccountId?: string): string {
const requested = requestedAccountId?.trim();
if (requested) {
return requested;
return normalizeAccountId(requested);
}
const preferred = cfg.channels?.twitch?.defaultAccount?.trim();
return preferred || resolveDefaultTwitchAccountId(cfg);
return preferred ? normalizeAccountId(preferred) : resolveDefaultTwitchAccountId(cfg);
}
export function setTwitchAccount(
@@ -38,7 +39,8 @@ export function setTwitchAccount(
account: Partial<TwitchAccountConfig>,
accountId: string = resolveSetupAccountId(cfg),
): OpenClawConfig {
const existing = getAccountConfig(cfg, accountId);
const resolvedAccountId = normalizeAccountId(accountId);
const existing = getAccountConfig(cfg, resolvedAccountId);
const merged: TwitchAccountConfig = {
username: account.username ?? existing?.username ?? "",
accessToken: account.accessToken ?? existing?.accessToken ?? "",
@@ -67,7 +69,7 @@ export function setTwitchAccount(
...((
(cfg.channels as Record<string, unknown>)?.twitch as Record<string, unknown> | undefined
)?.accounts as Record<string, unknown> | undefined),
[accountId]: merged,
[resolvedAccountId]: merged,
},
},
},