mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-22 11:34:02 +00:00
Keep media-only iMessage sends from delivering visible <media:image> text while preserving a non-visible echo key for self-echo dedupe. Thanks @homer-byte.
143 lines
4.1 KiB
TypeScript
143 lines
4.1 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import type { IMessageRpcClient } from "./client.js";
|
|
import { sendMessageIMessage } from "./send.js";
|
|
|
|
const IMESSAGE_TEST_CFG = {
|
|
channels: {
|
|
imessage: {
|
|
accounts: {
|
|
default: {},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
function createClient(result: Record<string, unknown>): IMessageRpcClient {
|
|
return {
|
|
request: vi.fn(async () => result),
|
|
stop: vi.fn(async () => {}),
|
|
} as unknown as IMessageRpcClient;
|
|
}
|
|
|
|
describe("sendMessageIMessage receipts", () => {
|
|
it("attaches a text receipt for native send ids", async () => {
|
|
const client = createClient({ guid: "p:0/imsg-1" });
|
|
|
|
const result = await sendMessageIMessage("chat_id:42", "hello", {
|
|
config: IMESSAGE_TEST_CFG,
|
|
client,
|
|
replyToId: "reply-1",
|
|
});
|
|
|
|
expect(result.messageId).toBe("p:0/imsg-1");
|
|
expect(result.sentText).toBe("hello");
|
|
expect(result.echoText).toBe("hello");
|
|
expect(result.receipt.primaryPlatformMessageId).toBe("p:0/imsg-1");
|
|
expect(result.receipt.platformMessageIds).toEqual(["p:0/imsg-1"]);
|
|
expect(result.receipt.replyToId).toBe("reply-1");
|
|
expect(result.receipt.raw).toEqual([
|
|
{
|
|
channel: "imessage",
|
|
messageId: "p:0/imsg-1",
|
|
chatId: "42",
|
|
meta: { targetKind: "chat_id" },
|
|
},
|
|
]);
|
|
expect(result.receipt.parts).toEqual([
|
|
{
|
|
index: 0,
|
|
platformMessageId: "p:0/imsg-1",
|
|
kind: "text",
|
|
replyToId: "reply-1",
|
|
raw: {
|
|
channel: "imessage",
|
|
messageId: "p:0/imsg-1",
|
|
chatId: "42",
|
|
meta: { targetKind: "chat_id" },
|
|
},
|
|
},
|
|
]);
|
|
expect(result.receipt.sentAt).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("attaches a media receipt after attachment resolution", async () => {
|
|
const client = createClient({ message_id: 12345 });
|
|
|
|
const result = await sendMessageIMessage("chat_guid:chat-1", "", {
|
|
config: IMESSAGE_TEST_CFG,
|
|
client,
|
|
mediaUrl: "/tmp/image.png",
|
|
resolveAttachmentImpl: async () => ({ path: "/tmp/image.png", contentType: "image/png" }),
|
|
});
|
|
|
|
expect(result.messageId).toBe("12345");
|
|
expect(result.sentText).toBe("");
|
|
expect(result.echoText).toBe("<media:image>");
|
|
expect(result.receipt.primaryPlatformMessageId).toBe("12345");
|
|
expect(result.receipt.platformMessageIds).toEqual(["12345"]);
|
|
expect(client.request).toHaveBeenCalledWith(
|
|
"send",
|
|
expect.objectContaining({
|
|
chat_guid: "chat-1",
|
|
file: "/tmp/image.png",
|
|
text: "",
|
|
}),
|
|
expect.any(Object),
|
|
);
|
|
expect(result.receipt.raw).toEqual([
|
|
{
|
|
channel: "imessage",
|
|
messageId: "12345",
|
|
conversationId: "chat-1",
|
|
meta: { targetKind: "chat_guid" },
|
|
},
|
|
]);
|
|
expect(result.receipt.parts).toEqual([
|
|
{
|
|
index: 0,
|
|
platformMessageId: "12345",
|
|
kind: "media",
|
|
raw: {
|
|
channel: "imessage",
|
|
messageId: "12345",
|
|
conversationId: "chat-1",
|
|
meta: { targetKind: "chat_guid" },
|
|
},
|
|
},
|
|
]);
|
|
expect(result.receipt.sentAt).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("preserves literal media placeholder text when no attachment is sent", async () => {
|
|
const client = createClient({ guid: "p:0/imsg-text" });
|
|
|
|
const result = await sendMessageIMessage("chat_id:42", "literal <media:image> text", {
|
|
config: IMESSAGE_TEST_CFG,
|
|
client,
|
|
});
|
|
|
|
expect(result.sentText).toBe("literal <media:image> text");
|
|
expect(result.echoText).toBe("literal <media:image> text");
|
|
expect(client.request).toHaveBeenCalledWith(
|
|
"send",
|
|
expect.objectContaining({
|
|
chat_id: 42,
|
|
text: "literal <media:image> text",
|
|
}),
|
|
expect.any(Object),
|
|
);
|
|
});
|
|
|
|
it("does not treat compatibility ok responses as visible platform ids", async () => {
|
|
const client = createClient({ ok: "true" });
|
|
|
|
const result = await sendMessageIMessage("+15551234567", "hello", {
|
|
config: IMESSAGE_TEST_CFG,
|
|
client,
|
|
});
|
|
|
|
expect(result.messageId).toBe("ok");
|
|
expect(result.receipt.platformMessageIds).toStrictEqual([]);
|
|
});
|
|
});
|