|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + createBlockReplyContentKey, |
| 4 | + createBlockReplyPayloadKey, |
| 5 | + createBlockReplyPipeline, |
| 6 | +} from "./block-reply-pipeline.js"; |
| 7 | + |
| 8 | +describe("createBlockReplyPayloadKey", () => { |
| 9 | + it("produces different keys for payloads differing only by replyToId", () => { |
| 10 | + const a = createBlockReplyPayloadKey({ text: "hello world", replyToId: "post-1" }); |
| 11 | + const b = createBlockReplyPayloadKey({ text: "hello world", replyToId: "post-2" }); |
| 12 | + const c = createBlockReplyPayloadKey({ text: "hello world" }); |
| 13 | + expect(a).not.toBe(b); |
| 14 | + expect(a).not.toBe(c); |
| 15 | + }); |
| 16 | + |
| 17 | + it("produces different keys for payloads with different text", () => { |
| 18 | + const a = createBlockReplyPayloadKey({ text: "hello" }); |
| 19 | + const b = createBlockReplyPayloadKey({ text: "world" }); |
| 20 | + expect(a).not.toBe(b); |
| 21 | + }); |
| 22 | + |
| 23 | + it("produces different keys for payloads with different media", () => { |
| 24 | + const a = createBlockReplyPayloadKey({ text: "hello", mediaUrl: "file:///a.png" }); |
| 25 | + const b = createBlockReplyPayloadKey({ text: "hello", mediaUrl: "file:///b.png" }); |
| 26 | + expect(a).not.toBe(b); |
| 27 | + }); |
| 28 | + |
| 29 | + it("trims whitespace from text for key comparison", () => { |
| 30 | + const a = createBlockReplyPayloadKey({ text: " hello " }); |
| 31 | + const b = createBlockReplyPayloadKey({ text: "hello" }); |
| 32 | + expect(a).toBe(b); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe("createBlockReplyContentKey", () => { |
| 37 | + it("produces the same key for payloads differing only by replyToId", () => { |
| 38 | + const a = createBlockReplyContentKey({ text: "hello world", replyToId: "post-1" }); |
| 39 | + const b = createBlockReplyContentKey({ text: "hello world", replyToId: "post-2" }); |
| 40 | + const c = createBlockReplyContentKey({ text: "hello world" }); |
| 41 | + expect(a).toBe(b); |
| 42 | + expect(a).toBe(c); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe("createBlockReplyPipeline dedup with threading", () => { |
| 47 | + it("keeps separate deliveries for same text with different replyToId", async () => { |
| 48 | + const sent: Array<{ text?: string; replyToId?: string }> = []; |
| 49 | + const pipeline = createBlockReplyPipeline({ |
| 50 | + onBlockReply: async (payload) => { |
| 51 | + sent.push({ text: payload.text, replyToId: payload.replyToId }); |
| 52 | + }, |
| 53 | + timeoutMs: 5000, |
| 54 | + }); |
| 55 | + |
| 56 | + pipeline.enqueue({ text: "response text", replyToId: "thread-root-1" }); |
| 57 | + pipeline.enqueue({ text: "response text", replyToId: undefined }); |
| 58 | + await pipeline.flush(); |
| 59 | + |
| 60 | + expect(sent).toEqual([ |
| 61 | + { text: "response text", replyToId: "thread-root-1" }, |
| 62 | + { text: "response text", replyToId: undefined }, |
| 63 | + ]); |
| 64 | + }); |
| 65 | + |
| 66 | + it("hasSentPayload matches regardless of replyToId", async () => { |
| 67 | + const pipeline = createBlockReplyPipeline({ |
| 68 | + onBlockReply: async () => {}, |
| 69 | + timeoutMs: 5000, |
| 70 | + }); |
| 71 | + |
| 72 | + pipeline.enqueue({ text: "response text", replyToId: "thread-root-1" }); |
| 73 | + await pipeline.flush(); |
| 74 | + |
| 75 | + // Final payload with no replyToId should be recognized as already sent |
| 76 | + expect(pipeline.hasSentPayload({ text: "response text" })).toBe(true); |
| 77 | + expect(pipeline.hasSentPayload({ text: "response text", replyToId: "other-id" })).toBe(true); |
| 78 | + }); |
| 79 | +}); |
0 commit comments