Files
openclaw/extensions/imessage/src/send.test.ts
2026-05-10 18:20:12 +01:00

112 lines
3.2 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.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("<media:image>");
expect(result.receipt.primaryPlatformMessageId).toBe("12345");
expect(result.receipt.platformMessageIds).toEqual(["12345"]);
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("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([]);
});
});