Skip to content

Commit 2904bbf

Browse files
authored
test(imessage): exercise chat lookup through runtime boundary (#121205)
1 parent 5262874 commit 2904bbf

3 files changed

Lines changed: 111 additions & 117 deletions

File tree

‎extensions/imessage/src/actions-chat-guid.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,6 @@ function chatListCacheSet(
7272
}
7373
}
7474

75-
export function normalizeDirectChatIdentifierForTest(raw: string): string {
76-
return normalizeDirectChatIdentifier(raw);
77-
}
78-
79-
export function findChatGuidForTest(
80-
chats: readonly Record<string, unknown>[],
81-
target: Extract<IMessageTarget, { kind: "chat_id" | "chat_identifier" }>,
82-
): string | null {
83-
return findChatGuid(chats, target);
84-
}
85-
8675
function findChatGuid(
8776
chats: readonly Record<string, unknown>[],
8877
target: Extract<IMessageTarget, { kind: "chat_id" | "chat_identifier" }>,

‎extensions/imessage/src/actions.runtime.test.ts‎

Lines changed: 110 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { access, readFile } from "node:fs/promises";
33
import { basename, dirname } from "node:path";
44
import { afterEach, describe, expect, it, vi } from "vitest";
5+
import { normalizeDirectChatIdentifier } from "./chat-context.js";
56

67
const createIMessageRpcClientMock = vi.hoisted(() => vi.fn());
78
const runIMessageCliJsonCommandMock = vi.hoisted(() => vi.fn());
@@ -19,8 +20,7 @@ vi.mock("./remote-file.js", () => ({
1920
withIMessageRemoteFile: withIMessageRemoteFileMock,
2021
}));
2122

22-
const { imessageActionsRuntime, findChatGuidForTest, normalizeDirectChatIdentifierForTest } =
23-
await import("./actions.runtime.js");
23+
const { imessageActionsRuntime } = await import("./actions.runtime.js");
2424

2525
afterEach(() => {
2626
vi.restoreAllMocks();
@@ -672,111 +672,121 @@ describe("findChatGuid cross-format identifier resolution", () => {
672672
},
673673
];
674674

675-
it("matches a synthesized iMessage;-;<phone> target against the chats.list <phone> identifier", () => {
676-
const result = findChatGuidForTest(chatsList, {
677-
kind: "chat_identifier",
678-
chatIdentifier: "iMessage;-;+12069106512",
679-
});
680-
expect(result).toBe("any;-;+12069106512");
681-
});
682-
683-
it("matches a synthesized SMS;-;<phone> target the same way", () => {
684-
const result = findChatGuidForTest(chatsList, {
685-
kind: "chat_identifier",
686-
chatIdentifier: "SMS;-;+12069106512",
687-
});
688-
expect(result).toBe("any;-;+12069106512");
689-
});
690-
691-
it("matches a bare <phone> identifier exactly", () => {
692-
const result = findChatGuidForTest(chatsList, {
693-
kind: "chat_identifier",
694-
chatIdentifier: "+12069106512",
695-
});
696-
expect(result).toBe("any;-;+12069106512");
697-
});
675+
type ChatGuidCase = {
676+
name: string;
677+
cliPath: string;
678+
chats: Array<Record<string, unknown>>;
679+
target:
680+
| { kind: "chat_id"; chatId: number }
681+
| { kind: "chat_identifier"; chatIdentifier: string };
682+
expected: string | null;
683+
};
698684

699-
it("matches an any;-;<phone> guid form against the chats.list guid column", () => {
700-
const result = findChatGuidForTest(chatsList, {
701-
kind: "chat_identifier",
702-
chatIdentifier: "any;-;+12069106512",
703-
});
704-
expect(result).toBe("any;-;+12069106512");
705-
});
706-
707-
it("matches a group chat by exact guid", () => {
708-
const result = findChatGuidForTest(chatsList, {
709-
kind: "chat_identifier",
710-
chatIdentifier: "iMessage;+;chat0000",
711-
});
712-
expect(result).toBe("iMessage;+;chat0000");
713-
});
714-
715-
it("matches a group chat by chat_id", () => {
716-
const result = findChatGuidForTest(chatsList, { kind: "chat_id", chatId: 7 });
717-
expect(result).toBe("iMessage;+;chat0000");
718-
});
685+
it.each([
686+
{
687+
name: "matches a synthesized iMessage;-;<phone> target against the chats.list <phone> identifier",
688+
cliPath: "imsg-cross-format-imessage",
689+
chats: chatsList,
690+
target: { kind: "chat_identifier", chatIdentifier: "iMessage;-;+12069106512" },
691+
expected: "any;-;+12069106512",
692+
},
693+
{
694+
name: "matches a synthesized SMS;-;<phone> target the same way",
695+
cliPath: "imsg-cross-format-sms",
696+
chats: chatsList,
697+
target: { kind: "chat_identifier", chatIdentifier: "SMS;-;+12069106512" },
698+
expected: "any;-;+12069106512",
699+
},
700+
{
701+
name: "matches a bare <phone> identifier exactly",
702+
cliPath: "imsg-cross-format-bare",
703+
chats: chatsList,
704+
target: { kind: "chat_identifier", chatIdentifier: "+12069106512" },
705+
expected: "any;-;+12069106512",
706+
},
707+
{
708+
name: "matches an any;-;<phone> guid form against the chats.list guid column",
709+
cliPath: "imsg-cross-format-any",
710+
chats: chatsList,
711+
target: { kind: "chat_identifier", chatIdentifier: "any;-;+12069106512" },
712+
expected: "any;-;+12069106512",
713+
},
714+
{
715+
name: "matches a group chat by exact guid",
716+
cliPath: "imsg-cross-format-group-guid",
717+
chats: chatsList,
718+
target: { kind: "chat_identifier", chatIdentifier: "iMessage;+;chat0000" },
719+
expected: "iMessage;+;chat0000",
720+
},
721+
{
722+
name: "matches a group chat by chat_id",
723+
cliPath: "imsg-cross-format-chat-id",
724+
chats: chatsList,
725+
target: { kind: "chat_id", chatId: 7 },
726+
expected: "iMessage;+;chat0000",
727+
},
728+
{
729+
name: "does not coerce non-decimal chat ids from chats.list",
730+
cliPath: "imsg-cross-format-nondecimal-id",
731+
chats: [{ id: "0x7", identifier: "wrong", guid: "iMessage;+;wrong" }],
732+
target: { kind: "chat_id", chatId: 7 },
733+
expected: null,
734+
},
735+
{
736+
name: "returns null for a phone number that does not exist in chats.list",
737+
cliPath: "imsg-cross-format-missing-phone",
738+
chats: chatsList,
739+
target: { kind: "chat_identifier", chatIdentifier: "iMessage;-;+19999999999" },
740+
expected: null,
741+
},
742+
{
743+
name: "does not cross-match different phone numbers via the prefix-stripping path",
744+
cliPath: "imsg-cross-format-different-phone",
745+
chats: chatsList,
746+
target: { kind: "chat_identifier", chatIdentifier: "iMessage;-;+18001234567" },
747+
expected: null,
748+
},
749+
{
750+
name: "does not match a DM target against a group's chat_identifier",
751+
cliPath: "imsg-cross-format-group-mismatch",
752+
chats: chatsList,
753+
target: { kind: "chat_identifier", chatIdentifier: "iMessage;+;chat-not-here" },
754+
expected: null,
755+
},
756+
] satisfies ChatGuidCase[])("$name", async ({ cliPath, chats, target, expected }) => {
757+
const client = mockRpcChatList(chats);
719758

720-
it("does not coerce non-decimal chat ids from chats.list", () => {
721-
const result = findChatGuidForTest(
722-
[
723-
{
724-
id: "0x7",
725-
identifier: "wrong",
726-
guid: "iMessage;+;wrong",
727-
},
728-
],
729-
{ kind: "chat_id", chatId: 7 },
759+
await expect(
760+
imessageActionsRuntime.resolveChatGuidForTarget({
761+
target,
762+
options: { cliPath },
763+
conversationReadOrigin: "delegated",
764+
}),
765+
).resolves.toBe(expected);
766+
expect(client.request).toHaveBeenCalledWith(
767+
"chats.list",
768+
{ limit: 1000 },
769+
{ timeoutMs: undefined },
730770
);
731-
expect(result).toBeNull();
732-
});
733-
734-
it("returns null for a phone number that does not exist in chats.list", () => {
735-
const result = findChatGuidForTest(chatsList, {
736-
kind: "chat_identifier",
737-
chatIdentifier: "iMessage;-;+19999999999",
738-
});
739-
expect(result).toBeNull();
740-
});
741-
742-
it("does not cross-match different phone numbers via the prefix-stripping path", () => {
743-
const result = findChatGuidForTest(chatsList, {
744-
kind: "chat_identifier",
745-
chatIdentifier: "iMessage;-;+18001234567",
746-
});
747-
expect(result).toBeNull();
748-
});
749-
750-
it("does not match a DM target against a group's chat_identifier", () => {
751-
const result = findChatGuidForTest(chatsList, {
752-
kind: "chat_identifier",
753-
chatIdentifier: "iMessage;+;chat-not-here",
754-
});
755-
expect(result).toBeNull();
771+
expect(client.stop).toHaveBeenCalledOnce();
756772
});
757773
});
758774

759775
describe("normalizeDirectChatIdentifier", () => {
760-
it("strips the iMessage;-; prefix", () => {
761-
expect(normalizeDirectChatIdentifierForTest("iMessage;-;+12069106512")).toBe("+12069106512");
762-
});
763-
it("strips the SMS;-; prefix", () => {
764-
expect(normalizeDirectChatIdentifierForTest("SMS;-;+12069106512")).toBe("+12069106512");
765-
});
766-
it("strips the any;-; prefix", () => {
767-
expect(normalizeDirectChatIdentifierForTest("any;-;+12069106512")).toBe("+12069106512");
768-
});
769-
it("matches case-insensitively", () => {
770-
expect(normalizeDirectChatIdentifierForTest("IMESSAGE;-;+12069106512")).toBe("+12069106512");
771-
});
772-
it("leaves group identifiers (iMessage;+;chat...) unchanged", () => {
773-
expect(normalizeDirectChatIdentifierForTest("iMessage;+;chat0000")).toBe("iMessage;+;chat0000");
774-
expect(normalizeDirectChatIdentifierForTest("iMessage;+;[email protected]")).toBe(
776+
it.each([
777+
["strips the iMessage;-; prefix", "iMessage;-;+12069106512", "+12069106512"],
778+
["strips the SMS;-; prefix", "SMS;-;+12069106512", "+12069106512"],
779+
["strips the any;-; prefix", "any;-;+12069106512", "+12069106512"],
780+
["matches case-insensitively", "IMESSAGE;-;+12069106512", "+12069106512"],
781+
["leaves group identifiers unchanged", "iMessage;+;chat0000", "iMessage;+;chat0000"],
782+
[
783+
"leaves group email identifiers unchanged",
775784
"iMessage;+;[email protected]",
776-
);
777-
});
778-
it("leaves bare values unchanged", () => {
779-
expect(normalizeDirectChatIdentifierForTest("+12069106512")).toBe("+12069106512");
780-
expect(normalizeDirectChatIdentifierForTest("[email protected]")).toBe("[email protected]");
785+
"iMessage;+;[email protected]",
786+
],
787+
["leaves bare phone values unchanged", "+12069106512", "+12069106512"],
788+
["leaves bare email values unchanged", "[email protected]", "[email protected]"],
789+
])("%s", (_name, input, expected) => {
790+
expect(normalizeDirectChatIdentifier(input)).toBe(expected);
781791
});
782792
});

‎extensions/imessage/src/actions.runtime.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ import { basename, parse, win32 } from "node:path";
33
import { sanitizeUntrustedFileName } from "openclaw/plugin-sdk/security-runtime";
44
import { resolvePreferredOpenClawTmpDir, withTempWorkspace } from "openclaw/plugin-sdk/temp-path";
55
import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
6-
import {
7-
findChatGuidForTest,
8-
normalizeDirectChatIdentifierForTest,
9-
resolveIMessageActionChatGuid,
10-
} from "./actions-chat-guid.js";
6+
import { resolveIMessageActionChatGuid } from "./actions-chat-guid.js";
117
import {
128
type IMessageActionTransportOptions,
139
requestIMessageActionRpc,
@@ -21,7 +17,6 @@ import {
2117
} from "./monitor-reply-cache.js";
2218
import { sanitizeIMessageFinalOutboundText } from "./monitor/sanitize-outbound.js";
2319
import { withIMessageRemoteFile } from "./remote-file.js";
24-
export { findChatGuidForTest, normalizeDirectChatIdentifierForTest };
2520

2621
type IMessageBridgeActionOptions = IMessageActionTransportOptions & {
2722
chatGuid: string;

0 commit comments

Comments
 (0)