import { describe, expect, it } from "vitest"; import { normalizeWebhookMessage, normalizeWebhookReaction } from "./monitor-normalize.js"; describe("normalizeWebhookMessage", () => { it("falls back to DM chatGuid handle when sender handle is missing", () => { const result = normalizeWebhookMessage({ type: "new-message", data: { guid: "msg-1", text: "hello", isGroup: false, isFromMe: false, handle: null, chatGuid: "iMessage;-;+15551234567", }, }); expect(result).not.toBeNull(); expect(result?.senderId).toBe("+15551234567"); expect(result?.chatGuid).toBe("iMessage;-;+15551234567"); }); it("does not infer sender from group chatGuid when sender handle is missing", () => { const result = normalizeWebhookMessage({ type: "new-message", data: { guid: "msg-1", text: "hello group", isGroup: true, isFromMe: false, handle: null, chatGuid: "iMessage;+;chat123456", }, }); expect(result).toBeNull(); }); it("accepts array-wrapped payload data", () => { const result = normalizeWebhookMessage({ type: "new-message", data: [ { guid: "msg-1", text: "hello", handle: { address: "+15551234567" }, isGroup: false, isFromMe: false, }, ], }); expect(result).not.toBeNull(); expect(result?.senderId).toBe("+15551234567"); }); }); describe("normalizeWebhookReaction", () => { it("falls back to DM chatGuid handle when reaction sender handle is missing", () => { const result = normalizeWebhookReaction({ type: "updated-message", data: { guid: "msg-2", associatedMessageGuid: "p:0/msg-1", associatedMessageType: 2000, isGroup: false, isFromMe: false, handle: null, chatGuid: "iMessage;-;+15551234567", }, }); expect(result).not.toBeNull(); expect(result?.senderId).toBe("+15551234567"); expect(result?.messageId).toBe("p:0/msg-1"); expect(result?.action).toBe("added"); }); });