mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 11:41:24 +00:00
refactor(channels): share allowlist resolution summary
This commit is contained in:
25
src/channels/allowlists/resolve-utils.test.ts
Normal file
25
src/channels/allowlists/resolve-utils.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildAllowlistResolutionSummary } from "./resolve-utils.js";
|
||||
|
||||
describe("buildAllowlistResolutionSummary", () => {
|
||||
it("returns mapping, additions, and unresolved (including missing ids)", () => {
|
||||
const resolvedUsers = [
|
||||
{ input: "a", resolved: true, id: "1" },
|
||||
{ input: "b", resolved: false },
|
||||
{ input: "c", resolved: true },
|
||||
];
|
||||
const result = buildAllowlistResolutionSummary(resolvedUsers);
|
||||
expect(result.mapping).toEqual(["a→1"]);
|
||||
expect(result.additions).toEqual(["1"]);
|
||||
expect(result.unresolved).toEqual(["b", "c"]);
|
||||
});
|
||||
|
||||
it("supports custom resolved formatting", () => {
|
||||
const resolvedUsers = [{ input: "a", resolved: true, id: "1", note: "x" }];
|
||||
const result = buildAllowlistResolutionSummary(resolvedUsers, {
|
||||
formatResolved: (entry) =>
|
||||
`${entry.input}→${entry.id}${(entry as { note?: string }).note ? " (note)" : ""}`,
|
||||
});
|
||||
expect(result.mapping).toEqual(["a→1 (note)"]);
|
||||
});
|
||||
});
|
||||
@@ -35,17 +35,25 @@ export function mergeAllowlist(params: {
|
||||
|
||||
export function buildAllowlistResolutionSummary<T extends AllowlistUserResolutionLike>(
|
||||
resolvedUsers: T[],
|
||||
opts?: { formatResolved?: (entry: T) => string },
|
||||
): {
|
||||
resolvedMap: Map<string, T>;
|
||||
mapping: string[];
|
||||
unresolved: string[];
|
||||
additions: string[];
|
||||
} {
|
||||
const resolvedMap = new Map(resolvedUsers.map((entry) => [entry.input, entry]));
|
||||
const mapping = resolvedUsers
|
||||
.filter((entry) => entry.resolved && entry.id)
|
||||
.map((entry) => `${entry.input}→${entry.id}`);
|
||||
const unresolved = resolvedUsers.filter((entry) => !entry.resolved).map((entry) => entry.input);
|
||||
return { resolvedMap, mapping, unresolved };
|
||||
const resolvedOk = (entry: T) => Boolean(entry.resolved && entry.id);
|
||||
const formatResolved = opts?.formatResolved ?? ((entry: T) => `${entry.input}→${entry.id}`);
|
||||
const mapping = resolvedUsers.filter(resolvedOk).map(formatResolved);
|
||||
const additions = resolvedUsers
|
||||
.filter(resolvedOk)
|
||||
.map((entry) => entry.id)
|
||||
.filter((entry): entry is string => Boolean(entry));
|
||||
const unresolved = resolvedUsers
|
||||
.filter((entry) => !resolvedOk(entry))
|
||||
.map((entry) => entry.input);
|
||||
return { resolvedMap, mapping, unresolved, additions };
|
||||
}
|
||||
|
||||
export function resolveAllowlistIdAdditions<T extends AllowlistUserResolutionLike>(params: {
|
||||
|
||||
Reference in New Issue
Block a user