mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 08:27:39 +00:00
refactor: share allowFrom formatter scaffolding
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isAllowedParsedChatSender, isNormalizedSenderAllowed } from "./allow-from.js";
|
||||
import {
|
||||
formatAllowFromLowercase,
|
||||
formatNormalizedAllowFromEntries,
|
||||
isAllowedParsedChatSender,
|
||||
isNormalizedSenderAllowed,
|
||||
} from "./allow-from.js";
|
||||
|
||||
function parseAllowTarget(
|
||||
entry: string,
|
||||
@@ -102,3 +107,34 @@ describe("isNormalizedSenderAllowed", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatAllowFromLowercase", () => {
|
||||
it("trims, strips prefixes, and lowercases entries", () => {
|
||||
expect(
|
||||
formatAllowFromLowercase({
|
||||
allowFrom: [" Telegram:UserA ", "tg:UserB", " "],
|
||||
stripPrefixRe: /^(telegram|tg):/i,
|
||||
}),
|
||||
).toEqual(["usera", "userb"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatNormalizedAllowFromEntries", () => {
|
||||
it("applies custom normalization after trimming", () => {
|
||||
expect(
|
||||
formatNormalizedAllowFromEntries({
|
||||
allowFrom: [" @Alice ", "", " @Bob "],
|
||||
normalizeEntry: (entry) => entry.replace(/^@/, "").toLowerCase(),
|
||||
}),
|
||||
).toEqual(["alice", "bob"]);
|
||||
});
|
||||
|
||||
it("filters empty normalized entries", () => {
|
||||
expect(
|
||||
formatNormalizedAllowFromEntries({
|
||||
allowFrom: ["@", "valid"],
|
||||
normalizeEntry: (entry) => entry.replace(/^@$/, ""),
|
||||
}),
|
||||
).toEqual(["valid"]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,6 +9,17 @@ export function formatAllowFromLowercase(params: {
|
||||
.map((entry) => entry.toLowerCase());
|
||||
}
|
||||
|
||||
export function formatNormalizedAllowFromEntries(params: {
|
||||
allowFrom: Array<string | number>;
|
||||
normalizeEntry: (entry: string) => string | undefined | null;
|
||||
}): string[] {
|
||||
return params.allowFrom
|
||||
.map((entry) => String(entry).trim())
|
||||
.filter(Boolean)
|
||||
.map((entry) => params.normalizeEntry(entry))
|
||||
.filter((entry): entry is string => Boolean(entry));
|
||||
}
|
||||
|
||||
export function isNormalizedSenderAllowed(params: {
|
||||
senderId: string | number;
|
||||
allowFrom: Array<string | number>;
|
||||
|
||||
@@ -274,6 +274,7 @@ export {
|
||||
} from "../routing/session-key.js";
|
||||
export {
|
||||
formatAllowFromLowercase,
|
||||
formatNormalizedAllowFromEntries,
|
||||
isAllowedParsedChatSender,
|
||||
isNormalizedSenderAllowed,
|
||||
} from "./allow-from.js";
|
||||
|
||||
Reference in New Issue
Block a user