|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { setReplyPayloadMetadata } from "../reply-payload.js"; |
| 3 | +import { createBlockReplyPipeline } from "./block-reply-pipeline.js"; |
| 4 | + |
| 5 | +function blockFor(text: string, assistantMessageIndex: number) { |
| 6 | +return setReplyPayloadMetadata({ text }, { assistantMessageIndex }); |
| 7 | +} |
| 8 | + |
| 9 | +describe("block reply pipeline multi-assistant-message suppression", () => { |
| 10 | +it("recognizes each fully-streamed message across a multi-message turn", async () => { |
| 11 | +const sent: string[] = []; |
| 12 | +const pipeline = createBlockReplyPipeline({ |
| 13 | +onBlockReply: async (payload) => { |
| 14 | +if (payload.text) { |
| 15 | +sent.push(payload.text); |
| 16 | +} |
| 17 | +}, |
| 18 | +timeoutMs: 5000, |
| 19 | +}); |
| 20 | + |
| 21 | +pipeline.enqueue(blockFor("Alpha one.", 0)); |
| 22 | +pipeline.enqueue(blockFor("Alpha two.", 0)); |
| 23 | +pipeline.enqueue(blockFor("Beta one.", 1)); |
| 24 | +pipeline.enqueue(blockFor("Beta two.", 1)); |
| 25 | +await pipeline.flush({ force: true }); |
| 26 | + |
| 27 | +expect(sent).toEqual(["Alpha one.", "Alpha two.", "Beta one.", "Beta two."]); |
| 28 | +expect(pipeline.hasSentPayload({ text: "Alpha one. Alpha two." })).toBe(true); |
| 29 | +expect(pipeline.hasSentPayload({ text: "Beta one. Beta two." })).toBe(true); |
| 30 | +}); |
| 31 | + |
| 32 | +it("does not treat one message as covering another message's text", async () => { |
| 33 | +const pipeline = createBlockReplyPipeline({ |
| 34 | +onBlockReply: async () => {}, |
| 35 | +timeoutMs: 5000, |
| 36 | +}); |
| 37 | + |
| 38 | +pipeline.enqueue(blockFor("Alpha one.", 0)); |
| 39 | +pipeline.enqueue(blockFor("Alpha two.", 0)); |
| 40 | +pipeline.enqueue(blockFor("Beta one.", 1)); |
| 41 | +pipeline.enqueue(blockFor("Beta two.", 1)); |
| 42 | +await pipeline.flush({ force: true }); |
| 43 | + |
| 44 | +expect(pipeline.hasSentPayload({ text: "Alpha one. Alpha two. Beta one. Beta two." })).toBe( |
| 45 | +false, |
| 46 | +); |
| 47 | +}); |
| 48 | + |
| 49 | +it("suppresses a single message split into multiple blocks", async () => { |
| 50 | +const pipeline = createBlockReplyPipeline({ |
| 51 | +onBlockReply: async () => {}, |
| 52 | +timeoutMs: 5000, |
| 53 | +}); |
| 54 | + |
| 55 | +pipeline.enqueue(blockFor("Gamma one.", 0)); |
| 56 | +pipeline.enqueue(blockFor("Gamma two.", 0)); |
| 57 | +await pipeline.flush({ force: true }); |
| 58 | + |
| 59 | +expect(pipeline.hasSentPayload({ text: "Gamma one. Gamma two." })).toBe(true); |
| 60 | +}); |
| 61 | +}); |