refactor: share config adapter allowFrom and defaultTo helpers

This commit is contained in:
Peter Steinberger
2026-03-07 23:02:59 +00:00
parent feac26c3b7
commit 556aa8a702
17 changed files with 100 additions and 57 deletions

View File

@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { mapAllowFromEntries, resolveOptionalConfigString } from "./channel-config-helpers.js";
describe("mapAllowFromEntries", () => {
it("coerces allowFrom entries to strings", () => {
expect(mapAllowFromEntries(["user", 42, null])).toEqual(["user", "42", "null"]);
});
it("returns empty list for missing input", () => {
expect(mapAllowFromEntries(undefined)).toEqual([]);
});
});
describe("resolveOptionalConfigString", () => {
it("trims and returns string values", () => {
expect(resolveOptionalConfigString(" room:123 ")).toBe("room:123");
});
it("coerces numeric values", () => {
expect(resolveOptionalConfigString(123)).toBe("123");
});
it("returns undefined for empty values", () => {
expect(resolveOptionalConfigString(" ")).toBeUndefined();
expect(resolveOptionalConfigString(undefined)).toBeUndefined();
});
});

View File

@@ -4,10 +4,26 @@ import { resolveIMessageAccount } from "../imessage/accounts.js";
import { normalizeAccountId } from "../routing/session-key.js";
import { resolveWhatsAppAccount } from "../web/accounts.js";
export function mapAllowFromEntries(
allowFrom: Array<string | number> | null | undefined,
): string[] {
return (allowFrom ?? []).map((entry) => String(entry));
}
export function formatTrimmedAllowFromEntries(allowFrom: Array<string | number>): string[] {
return allowFrom.map((entry) => String(entry).trim()).filter(Boolean);
}
export function resolveOptionalConfigString(
value: string | number | null | undefined,
): string | undefined {
if (value == null) {
return undefined;
}
const normalized = String(value).trim();
return normalized || undefined;
}
export function resolveWhatsAppConfigAllowFrom(params: {
cfg: OpenClawConfig;
accountId?: string | null;
@@ -33,12 +49,12 @@ export function resolveIMessageConfigAllowFrom(params: {
cfg: OpenClawConfig;
accountId?: string | null;
}): string[] {
return (resolveIMessageAccount(params).config.allowFrom ?? []).map((entry) => String(entry));
return mapAllowFromEntries(resolveIMessageAccount(params).config.allowFrom);
}
export function resolveIMessageConfigDefaultTo(params: {
cfg: OpenClawConfig;
accountId?: string | null;
}): string | undefined {
return resolveIMessageAccount(params).config.defaultTo?.trim() || undefined;
return resolveOptionalConfigString(resolveIMessageAccount(params).config.defaultTo);
}

View File

@@ -378,6 +378,8 @@ export { SILENT_REPLY_TOKEN, isSilentReplyText } from "../auto-reply/tokens.js";
export { formatInboundFromLabel } from "../auto-reply/envelope.js";
export {
formatTrimmedAllowFromEntries,
mapAllowFromEntries,
resolveOptionalConfigString,
formatWhatsAppConfigAllowFromEntries,
resolveIMessageConfigAllowFrom,
resolveIMessageConfigDefaultTo,