mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-26 20:35:39 -06:00
dff0f41e4a
* fix(imessage): reject ambiguous outbound targets * fix(imessage): preserve tel URI identities * fix(imessage): explain target qualification * test(imessage): keep target proof plugin-local * docs(imessage): explain qualified contact targets
280 lines
8.9 KiB
TypeScript
280 lines
8.9 KiB
TypeScript
// Imessage tests cover targets plugin behavior.
|
|
import { installChannelDmPolicyContractSuite } from "openclaw/plugin-sdk/channel-test-helpers";
|
|
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
|
|
import { describe, expect, it } from "vitest";
|
|
import {
|
|
resolveIMessageGroupRequireMention,
|
|
resolveIMessageGroupToolPolicy,
|
|
} from "./group-policy.js";
|
|
import { imessageDmPolicy } from "./setup-core.js";
|
|
import { parseIMessageAllowFromEntries } from "./setup-surface.js";
|
|
import {
|
|
formatIMessageChatTarget,
|
|
inferIMessageTargetChatType,
|
|
isAllowedIMessageReplyContextSender,
|
|
isAllowedIMessageSender,
|
|
looksLikeIMessageExplicitTargetId,
|
|
normalizeIMessageHandle,
|
|
parseIMessageTarget,
|
|
} from "./targets.js";
|
|
|
|
describe("imessage targets", () => {
|
|
it("parses chat_id targets", () => {
|
|
const target = parseIMessageTarget("chat_id:123");
|
|
expect(target).toEqual({ kind: "chat_id", chatId: 123 });
|
|
});
|
|
|
|
it("parses chat targets", () => {
|
|
const target = parseIMessageTarget("chat:456");
|
|
expect(target).toEqual({ kind: "chat_id", chatId: 456 });
|
|
});
|
|
|
|
it("parses sms handles with service", () => {
|
|
const target = parseIMessageTarget("sms:+1555");
|
|
expect(target).toEqual({
|
|
kind: "handle",
|
|
to: "+1555",
|
|
service: "sms",
|
|
serviceExplicit: true,
|
|
});
|
|
});
|
|
|
|
it("normalizes handles", () => {
|
|
expect(normalizeIMessageHandle("Name@Example.com")).toBe("name@example.com");
|
|
expect(normalizeIMessageHandle(" +1 (555) 222-3333 ")).toBe("+15552223333");
|
|
});
|
|
|
|
it("normalizes chat_id prefixes case-insensitively", () => {
|
|
expect(normalizeIMessageHandle("CHAT_ID:123")).toBe("chat_id:123");
|
|
expect(normalizeIMessageHandle("Chat_Id:456")).toBe("chat_id:456");
|
|
expect(normalizeIMessageHandle("chatid:789")).toBe("chat_id:789");
|
|
expect(normalizeIMessageHandle("CHAT:42")).toBe("chat_id:42");
|
|
});
|
|
|
|
it("normalizes chat_guid prefixes case-insensitively", () => {
|
|
expect(normalizeIMessageHandle("CHAT_GUID:abc-def")).toBe("chat_guid:abc-def");
|
|
expect(normalizeIMessageHandle("ChatGuid:XYZ")).toBe("chat_guid:XYZ");
|
|
expect(normalizeIMessageHandle("GUID:test-guid")).toBe("chat_guid:test-guid");
|
|
});
|
|
|
|
it("normalizes chat_identifier prefixes case-insensitively", () => {
|
|
expect(normalizeIMessageHandle("CHAT_IDENTIFIER:iMessage;-;chat123")).toBe(
|
|
"chat_identifier:iMessage;-;chat123",
|
|
);
|
|
expect(normalizeIMessageHandle("ChatIdentifier:test")).toBe("chat_identifier:test");
|
|
expect(normalizeIMessageHandle("CHATIDENT:foo")).toBe("chat_identifier:foo");
|
|
});
|
|
|
|
it("does not check allowFrom against conversation targets", () => {
|
|
const ok = isAllowedIMessageSender({
|
|
allowFrom: ["chat_id:9"],
|
|
sender: "+1555",
|
|
chatId: 9,
|
|
});
|
|
expect(ok).toBe(false);
|
|
|
|
expect(
|
|
isAllowedIMessageSender({
|
|
allowFrom: ["imessage:chat_id:9"],
|
|
sender: "+1555",
|
|
chatId: 9,
|
|
}),
|
|
).toBe(false);
|
|
|
|
expect(
|
|
isAllowedIMessageSender({
|
|
allowFrom: ["chat_guid:team-thread"],
|
|
sender: "+1555",
|
|
chatGuid: "team-thread",
|
|
}),
|
|
).toBe(false);
|
|
|
|
expect(
|
|
isAllowedIMessageSender({
|
|
allowFrom: ["chat_identifier:team"],
|
|
sender: "+1555",
|
|
chatIdentifier: "team",
|
|
}),
|
|
).toBe(false);
|
|
|
|
expect(
|
|
isAllowedIMessageSender({
|
|
allowFrom: ["chat_id:9"],
|
|
sender: "+1555",
|
|
chatId: 9,
|
|
allowConversationTargets: true,
|
|
}),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("checks allowFrom against handle", () => {
|
|
const ok = isAllowedIMessageSender({
|
|
allowFrom: ["user@example.com"],
|
|
sender: "User@Example.com",
|
|
});
|
|
expect(ok).toBe(true);
|
|
});
|
|
|
|
it("checks reply context allowFrom against conversation targets", () => {
|
|
expect(
|
|
isAllowedIMessageReplyContextSender({
|
|
allowFrom: ["chat_id:9"],
|
|
sender: "+1555",
|
|
chatId: 9,
|
|
}),
|
|
).toBe(true);
|
|
|
|
expect(
|
|
isAllowedIMessageReplyContextSender({
|
|
allowFrom: ["imessage:chat_guid:team-thread"],
|
|
sender: "+1555",
|
|
chatGuid: "team-thread",
|
|
}),
|
|
).toBe(true);
|
|
|
|
expect(
|
|
isAllowedIMessageReplyContextSender({
|
|
allowFrom: ["chat_identifier:team"],
|
|
sender: "+1555",
|
|
chatIdentifier: "team",
|
|
}),
|
|
).toBe(true);
|
|
});
|
|
|
|
it("denies when allowFrom is empty", () => {
|
|
const ok = isAllowedIMessageSender({
|
|
allowFrom: [],
|
|
sender: "+1555",
|
|
});
|
|
expect(ok).toBe(false);
|
|
});
|
|
|
|
it("formats chat targets", () => {
|
|
expect(formatIMessageChatTarget(42)).toBe("chat_id:42");
|
|
expect(formatIMessageChatTarget(undefined)).toBe("");
|
|
});
|
|
|
|
it("only treats explicit chat targets as immediate ids", () => {
|
|
expect(looksLikeIMessageExplicitTargetId("chat_id:42")).toBe(true);
|
|
expect(looksLikeIMessageExplicitTargetId("sms:+15552223333")).toBe(true);
|
|
expect(looksLikeIMessageExplicitTargetId("+15552223333")).toBe(false);
|
|
expect(looksLikeIMessageExplicitTargetId("user@example.com")).toBe(false);
|
|
expect(looksLikeIMessageExplicitTargetId("7d5297154d5f436d83dbbdf03fcc8fdd")).toBe(true);
|
|
});
|
|
|
|
it("infers direct and group chat types from normalized targets", () => {
|
|
expect(inferIMessageTargetChatType("+15552223333")).toBe("direct");
|
|
expect(inferIMessageTargetChatType("chat_id:42")).toBe("group");
|
|
});
|
|
|
|
it("treats bare 32-char hex strings as chat identifiers, not phone numbers", () => {
|
|
const hex = "7d5297154d5f436d83dbbdf03fcc8fdd";
|
|
expect(normalizeIMessageHandle(hex)).toBe(`chat_identifier:${hex}`);
|
|
expect(normalizeIMessageHandle(hex.toUpperCase())).toBe(`chat_identifier:${hex}`);
|
|
expect(parseIMessageTarget(hex)).toEqual({
|
|
kind: "chat_identifier",
|
|
chatIdentifier: hex,
|
|
});
|
|
expect(parseIMessageTarget(`imessage:${hex.toUpperCase()}`)).toEqual({
|
|
kind: "chat_identifier",
|
|
chatIdentifier: hex,
|
|
});
|
|
expect(inferIMessageTargetChatType(hex)).toBe("group");
|
|
});
|
|
|
|
it.each(["7d5297154d5f436d83dbbdf03fcc8fd", "7d5297154d5f436d83dbbdf03fcc8fdg"])(
|
|
"keeps non-hex or wrong-length value %s on the handle path",
|
|
(value) => {
|
|
expect(normalizeIMessageHandle(value)).toBe(value);
|
|
expect(parseIMessageTarget(value)).toEqual({ kind: "handle", to: value, service: "auto" });
|
|
},
|
|
);
|
|
|
|
it("normalizes tel URIs without treating arbitrary prefixed identifiers as phone numbers", () => {
|
|
expect(normalizeIMessageHandle("tel:+1 (555) 222-3333")).toBe("+15552223333");
|
|
expect(normalizeIMessageHandle("tel:C0AG22RN7L3")).toBe("tel:C0AG22RN7L3");
|
|
});
|
|
|
|
it("accepts the all-digit edge of the 32-hex identifier contract", () => {
|
|
const identifier = "1".repeat(32);
|
|
expect(parseIMessageTarget(identifier)).toEqual({
|
|
kind: "chat_identifier",
|
|
chatIdentifier: identifier,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("imessage group policy", () => {
|
|
it("uses generic channel group policy helpers", () => {
|
|
const cfg = {
|
|
channels: {
|
|
imessage: {
|
|
groups: {
|
|
"chat:family": {
|
|
requireMention: false,
|
|
tools: { deny: ["exec"] },
|
|
},
|
|
"*": {
|
|
requireMention: true,
|
|
tools: { allow: ["message.send"] },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
} as OpenClawConfig;
|
|
|
|
expect(resolveIMessageGroupRequireMention({ cfg, groupId: "chat:family" })).toBe(false);
|
|
expect(resolveIMessageGroupRequireMention({ cfg, groupId: "chat:other" })).toBe(true);
|
|
expect(resolveIMessageGroupToolPolicy({ cfg, groupId: "chat:family" })).toEqual({
|
|
deny: ["exec"],
|
|
});
|
|
expect(resolveIMessageGroupToolPolicy({ cfg, groupId: "chat:other" })).toEqual({
|
|
allow: ["message.send"],
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("parseIMessageAllowFromEntries", () => {
|
|
it("parses handles", () => {
|
|
expect(parseIMessageAllowFromEntries("+15555550123, user@example.com")).toEqual({
|
|
entries: ["+15555550123", "user@example.com"],
|
|
});
|
|
});
|
|
|
|
it("returns validation errors for chat target entries", () => {
|
|
expect(parseIMessageAllowFromEntries("chat_id:123")).toEqual({
|
|
entries: [],
|
|
error: "iMessage allowFrom entries must be sender handles: chat_id:123",
|
|
});
|
|
|
|
expect(parseIMessageAllowFromEntries("imessage:chat_id:123")).toEqual({
|
|
entries: [],
|
|
error: "iMessage allowFrom entries must be sender handles: imessage:chat_id:123",
|
|
});
|
|
});
|
|
|
|
it("returns validation errors for chat_identifier entries", () => {
|
|
expect(parseIMessageAllowFromEntries("chat_identifier:")).toEqual({
|
|
entries: [],
|
|
error: "iMessage allowFrom entries must be sender handles: chat_identifier:",
|
|
});
|
|
});
|
|
|
|
installChannelDmPolicyContractSuite({
|
|
dmPolicy: imessageDmPolicy,
|
|
cases: [
|
|
{
|
|
name: "iMessage named accounts",
|
|
channel: "imessage",
|
|
accountId: "work",
|
|
accountConfig: { cliPath: "imsg" },
|
|
inheritedAllowFrom: ["+15555550123"],
|
|
defaultAccount: {
|
|
rootAllowFrom: ["+15555550123"],
|
|
accountAllowFrom: ["chat_id:123"],
|
|
},
|
|
},
|
|
],
|
|
});
|
|
});
|