refactor: unify account list/default scaffolding

This commit is contained in:
Peter Steinberger
2026-03-07 19:44:56 +00:00
parent 2bcd56cfac
commit 7242777d63
17 changed files with 143 additions and 299 deletions

View File

@@ -0,0 +1,78 @@
import { describe, expect, it } from "vitest";
import { listIrcAccountIds, resolveDefaultIrcAccountId } from "./accounts.js";
import type { CoreConfig } from "./types.js";
function asConfig(value: unknown): CoreConfig {
return value as CoreConfig;
}
describe("listIrcAccountIds", () => {
it("returns default when no accounts are configured", () => {
expect(listIrcAccountIds(asConfig({}))).toEqual(["default"]);
});
it("normalizes, deduplicates, and sorts configured account ids", () => {
const cfg = asConfig({
channels: {
irc: {
accounts: {
"Ops Team": {},
"ops-team": {},
Work: {},
},
},
},
});
expect(listIrcAccountIds(cfg)).toEqual(["ops-team", "work"]);
});
});
describe("resolveDefaultIrcAccountId", () => {
it("prefers configured defaultAccount when it matches", () => {
const cfg = asConfig({
channels: {
irc: {
defaultAccount: "Ops Team",
accounts: {
default: {},
"ops-team": {},
},
},
},
});
expect(resolveDefaultIrcAccountId(cfg)).toBe("ops-team");
});
it("falls back to default when configured defaultAccount is missing", () => {
const cfg = asConfig({
channels: {
irc: {
defaultAccount: "missing",
accounts: {
default: {},
work: {},
},
},
},
});
expect(resolveDefaultIrcAccountId(cfg)).toBe("default");
});
it("falls back to first sorted account when default is absent", () => {
const cfg = asConfig({
channels: {
irc: {
accounts: {
zzz: {},
aaa: {},
},
},
},
});
expect(resolveDefaultIrcAccountId(cfg)).toBe("aaa");
});
});

View File

@@ -1,10 +1,9 @@
import { readFileSync } from "node:fs";
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk/account-id";
import {
DEFAULT_ACCOUNT_ID,
normalizeAccountId,
normalizeOptionalAccountId,
} from "openclaw/plugin-sdk/account-id";
import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/irc";
createAccountListHelpers,
normalizeResolvedSecretInputString,
} from "openclaw/plugin-sdk/irc";
import type { CoreConfig, IrcAccountConfig, IrcNickServConfig } from "./types.js";
const TRUTHY_ENV = new Set(["true", "1", "yes", "on"]);
@@ -54,19 +53,9 @@ function parseListEnv(value?: string): string[] | undefined {
return parsed.length > 0 ? parsed : undefined;
}
function listConfiguredAccountIds(cfg: CoreConfig): string[] {
const accounts = cfg.channels?.irc?.accounts;
if (!accounts || typeof accounts !== "object") {
return [];
}
const ids = new Set<string>();
for (const key of Object.keys(accounts)) {
if (key.trim()) {
ids.add(normalizeAccountId(key));
}
}
return [...ids];
}
const { listAccountIds: listIrcAccountIds, resolveDefaultAccountId: resolveDefaultIrcAccountId } =
createAccountListHelpers("irc", { normalizeAccountId });
export { listIrcAccountIds, resolveDefaultIrcAccountId };
function resolveAccountConfig(cfg: CoreConfig, accountId: string): IrcAccountConfig | undefined {
const accounts = cfg.channels?.irc?.accounts;
@@ -165,29 +154,6 @@ function resolveNickServConfig(accountId: string, nickserv?: IrcNickServConfig):
return merged;
}
export function listIrcAccountIds(cfg: CoreConfig): string[] {
const ids = listConfiguredAccountIds(cfg);
if (ids.length === 0) {
return [DEFAULT_ACCOUNT_ID];
}
return ids.toSorted((a, b) => a.localeCompare(b));
}
export function resolveDefaultIrcAccountId(cfg: CoreConfig): string {
const preferred = normalizeOptionalAccountId(cfg.channels?.irc?.defaultAccount);
if (
preferred &&
listIrcAccountIds(cfg).some((accountId) => normalizeAccountId(accountId) === preferred)
) {
return preferred;
}
const ids = listIrcAccountIds(cfg);
if (ids.includes(DEFAULT_ACCOUNT_ID)) {
return DEFAULT_ACCOUNT_ID;
}
return ids[0] ?? DEFAULT_ACCOUNT_ID;
}
export function resolveIrcAccount(params: {
cfg: CoreConfig;
accountId?: string | null;