|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createDeferred } from "../../../test/helpers/promise.js"; |
| 3 | +import { createChatSendLateFollowupDisposition } from "./chat-send-late-followup.js"; |
| 4 | + |
| 5 | +describe("chat.send late queued follow-up disposition", () => { |
| 6 | + it.each([ |
| 7 | + ["discord", "discord", "non-webchat-origin"], |
| 8 | + ["webchat", "discord", "origin-mismatch"], |
| 9 | + ])("records an explicit drop for %s -> %s", async (originatingChannel, batchChannel, reason) => { |
| 10 | + const info = vi.fn(); |
| 11 | + const deliver = vi.fn(async () => ({ kind: "delivered" as const })); |
| 12 | + const disposition = createChatSendLateFollowupDisposition({ |
| 13 | + runId: "original-run", |
| 14 | + originatingChannel, |
| 15 | + logGateway: { info } as never, |
| 16 | + deliver, |
| 17 | + }); |
| 18 | + disposition.recordQueued(); |
| 19 | + await disposition.deliver({ |
| 20 | + kind: "queued-followup", |
| 21 | + runId: "followup-run", |
| 22 | + originatingChannel: batchChannel, |
| 23 | + payloads: [{ text: "must not leak" }], |
| 24 | + }); |
| 25 | + expect(deliver).not.toHaveBeenCalled(); |
| 26 | + expect(info).toHaveBeenCalledWith( |
| 27 | + "webchat late reply disposition", |
| 28 | + expect.objectContaining({ outcome: "late-and-dropped", reason }), |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + it("claims delivery before awaiting and records a concurrent duplicate", async () => { |
| 33 | + const info = vi.fn(); |
| 34 | + const delivery = createDeferred<{ kind: "delivered" }>(); |
| 35 | + const deliver = vi.fn(() => delivery.promise); |
| 36 | + const disposition = createChatSendLateFollowupDisposition({ |
| 37 | + runId: "original-run", |
| 38 | + originatingChannel: "webchat", |
| 39 | + logGateway: { info } as never, |
| 40 | + deliver, |
| 41 | + }); |
| 42 | + disposition.recordQueued(); |
| 43 | + const first = disposition.deliver({ |
| 44 | + kind: "queued-followup", |
| 45 | + runId: "first-followup", |
| 46 | + originatingChannel: "webchat", |
| 47 | + payloads: [{ text: "first" }], |
| 48 | + }); |
| 49 | + await disposition.deliver({ |
| 50 | + kind: "queued-followup", |
| 51 | + runId: "duplicate-followup", |
| 52 | + originatingChannel: "webchat", |
| 53 | + payloads: [{ text: "duplicate" }], |
| 54 | + }); |
| 55 | + expect(deliver).toHaveBeenCalledOnce(); |
| 56 | + expect(info).toHaveBeenCalledWith( |
| 57 | + "webchat late reply disposition", |
| 58 | + expect.objectContaining({ reason: "delivery-in-flight" }), |
| 59 | + ); |
| 60 | + delivery.resolve({ kind: "delivered" }); |
| 61 | + await first; |
| 62 | + }); |
| 63 | + |
| 64 | + it("settles a failed delivery and rejects every subsequent attempt", async () => { |
| 65 | + const info = vi.fn(); |
| 66 | + const deliver = vi.fn(async () => { |
| 67 | + throw new Error("broadcast failed"); |
| 68 | + }); |
| 69 | + const disposition = createChatSendLateFollowupDisposition({ |
| 70 | + runId: "original-run", |
| 71 | + originatingChannel: "webchat", |
| 72 | + logGateway: { info } as never, |
| 73 | + deliver, |
| 74 | + }); |
| 75 | + disposition.recordQueued(); |
| 76 | + const batch = { |
| 77 | + kind: "queued-followup" as const, |
| 78 | + runId: "failed-followup", |
| 79 | + originatingChannel: "webchat", |
| 80 | + payloads: [{ text: "reply" }], |
| 81 | + }; |
| 82 | + await expect(disposition.deliver(batch)).rejects.toThrow("broadcast failed"); |
| 83 | + await disposition.deliver({ ...batch, runId: "retry" }); |
| 84 | + expect(info).toHaveBeenCalledWith( |
| 85 | + "webchat late reply disposition", |
| 86 | + expect.objectContaining({ reason: "delivery-failed" }), |
| 87 | + ); |
| 88 | + expect(info).toHaveBeenCalledWith( |
| 89 | + "webchat late reply disposition", |
| 90 | + expect.objectContaining({ reason: "already-settled" }), |
| 91 | + ); |
| 92 | + expect(deliver).toHaveBeenCalledOnce(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments