mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 06:41:22 +00:00
refactor: align channel chatType
This commit is contained in:
@@ -1,46 +1,27 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveMediaUnderstandingScope } from "./scope.js";
|
||||
|
||||
describe("resolveMediaUnderstandingScope", () => {
|
||||
it("defaults to allow when scope is undefined", () => {
|
||||
expect(resolveMediaUnderstandingScope({})).toBe("allow");
|
||||
import { normalizeMediaUnderstandingChatType, resolveMediaUnderstandingScope } from "./scope.js";
|
||||
|
||||
describe("media understanding scope", () => {
|
||||
it("normalizes channel/room", () => {
|
||||
expect(normalizeMediaUnderstandingChatType("channel")).toBe("channel");
|
||||
expect(normalizeMediaUnderstandingChatType("room")).toBe("channel");
|
||||
});
|
||||
|
||||
it("uses first matching rule", () => {
|
||||
const decision = resolveMediaUnderstandingScope({
|
||||
scope: {
|
||||
default: "deny",
|
||||
rules: [
|
||||
{ action: "allow", match: { channel: "whatsapp" } },
|
||||
{ action: "deny", match: { channel: "whatsapp", chatType: "direct" } },
|
||||
],
|
||||
},
|
||||
channel: "whatsapp",
|
||||
chatType: "direct",
|
||||
sessionKey: "whatsapp:direct:123",
|
||||
});
|
||||
expect(decision).toBe("allow");
|
||||
it("treats room match as channel", () => {
|
||||
const scope = {
|
||||
rules: [{ action: "deny", match: { chatType: "room" } }],
|
||||
} as const;
|
||||
|
||||
expect(resolveMediaUnderstandingScope({ scope, chatType: "channel" })).toBe("deny");
|
||||
});
|
||||
|
||||
it("matches keyPrefix when provided", () => {
|
||||
const decision = resolveMediaUnderstandingScope({
|
||||
scope: {
|
||||
default: "deny",
|
||||
rules: [{ action: "allow", match: { keyPrefix: "agent:main:" } }],
|
||||
},
|
||||
sessionKey: "agent:main:whatsapp:group:123",
|
||||
});
|
||||
expect(decision).toBe("allow");
|
||||
});
|
||||
it("matches channel chatType explicitly", () => {
|
||||
const scope = {
|
||||
rules: [{ action: "deny", match: { chatType: "channel" } }],
|
||||
} as const;
|
||||
|
||||
it("matches keyPrefix case-insensitively", () => {
|
||||
const decision = resolveMediaUnderstandingScope({
|
||||
scope: {
|
||||
default: "deny",
|
||||
rules: [{ action: "allow", match: { keyPrefix: "agent:main:" } }],
|
||||
},
|
||||
sessionKey: "AGENT:MAIN:WHATSAPP:GROUP:123",
|
||||
});
|
||||
expect(decision).toBe("allow");
|
||||
expect(resolveMediaUnderstandingScope({ scope, chatType: "channel" })).toBe("deny");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ export function normalizeMediaUnderstandingChatType(raw?: string | null): string
|
||||
if (!value) return undefined;
|
||||
if (value === "dm" || value === "direct_message" || value === "private") return "direct";
|
||||
if (value === "groups") return "group";
|
||||
if (value === "channel") return "room";
|
||||
if (value === "room") return "channel";
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ export function resolveMediaUnderstandingScope(params: {
|
||||
if (!scope) return "allow";
|
||||
|
||||
const channel = normalizeMatch(params.channel);
|
||||
const chatType = normalizeMatch(params.chatType);
|
||||
const chatType = normalizeMediaUnderstandingChatType(params.chatType) ?? normalizeMatch(params.chatType);
|
||||
const sessionKey = normalizeMatch(params.sessionKey) ?? "";
|
||||
|
||||
for (const rule of scope.rules ?? []) {
|
||||
@@ -41,7 +41,8 @@ export function resolveMediaUnderstandingScope(params: {
|
||||
const action = normalizeDecision(rule.action) ?? "allow";
|
||||
const match = rule.match ?? {};
|
||||
const matchChannel = normalizeMatch(match.channel);
|
||||
const matchChatType = normalizeMatch(match.chatType);
|
||||
const matchChatType =
|
||||
normalizeMediaUnderstandingChatType(match.chatType) ?? normalizeMatch(match.chatType);
|
||||
const matchPrefix = normalizeMatch(match.keyPrefix);
|
||||
|
||||
if (matchChannel && matchChannel !== channel) continue;
|
||||
|
||||
Reference in New Issue
Block a user