|
| 1 | +// Proves follow-up batch ownership through the real channel route and durable-send boundary. |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { ChannelPlugin } from "../../channels/plugins/types.public.js"; |
| 4 | +import type { OpenClawConfig } from "../../config/config.js"; |
| 5 | +import { setActivePluginRegistry } from "../../plugins/runtime.js"; |
| 6 | +import { |
| 7 | + createChannelTestPluginBase, |
| 8 | + createTestRegistry, |
| 9 | +} from "../../test-utils/channel-plugins.js"; |
| 10 | +import { setReplyPayloadMetadata } from "../reply-payload.js"; |
| 11 | +import type { ReplyPayload } from "../types.js"; |
| 12 | +import { deliverFollowupDecision } from "./followup-delivery.js"; |
| 13 | +import type { AdmittedFollowupTurn } from "./followup-turn-admission.js"; |
| 14 | + |
| 15 | +const channelState = vi.hoisted(() => ({ |
| 16 | + outcomes: [] as Array<"delivered" | "failed">, |
| 17 | + deliver: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("../../agents/runtime-plan/build.js", () => ({ |
| 21 | + buildAgentRuntimeDeliveryPlan: () => ({ |
| 22 | + isSilentPayload: () => false, |
| 23 | + resolveFollowupRoute: () => undefined, |
| 24 | + }), |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock("../../infra/outbound/deliver.js", () => ({ |
| 28 | + deliverOutboundPayloadsInternal: (...args: unknown[]) => channelState.deliver(...args), |
| 29 | +})); |
| 30 | + |
| 31 | +function createChannelPlugin(id: ChannelPlugin["id"]): ChannelPlugin { |
| 32 | + return createChannelTestPluginBase({ |
| 33 | + id, |
| 34 | + label: String(id), |
| 35 | + config: { listAccountIds: () => [], resolveAccount: () => ({}) }, |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +function createTurn(params: { |
| 40 | + messageProvider: string; |
| 41 | + originatingChannel: string; |
| 42 | +}): AdmittedFollowupTurn { |
| 43 | + return { |
| 44 | + runId: "run-1", |
| 45 | + queued: { |
| 46 | + prompt: "queued", |
| 47 | + enqueuedAt: 1, |
| 48 | + originatingChannel: params.originatingChannel, |
| 49 | + originatingTo: "channel:C1", |
| 50 | + run: { |
| 51 | + agentId: "agent", |
| 52 | + agentDir: "/tmp/agent", |
| 53 | + sessionId: "session", |
| 54 | + sessionKey: "main", |
| 55 | + sessionFile: "/tmp/session.jsonl", |
| 56 | + workspaceDir: "/tmp", |
| 57 | + config: {}, |
| 58 | + provider: "anthropic", |
| 59 | + model: "claude", |
| 60 | + messageProvider: params.messageProvider, |
| 61 | + timeoutMs: 1_000, |
| 62 | + blockReplyBreak: "message_end", |
| 63 | + }, |
| 64 | + }, |
| 65 | + operation: {} as AdmittedFollowupTurn["operation"], |
| 66 | + config: {} as OpenClawConfig, |
| 67 | + session: { |
| 68 | + kind: "session", |
| 69 | + key: "main", |
| 70 | + current: () => undefined, |
| 71 | + publish: () => undefined, |
| 72 | + adopt: () => undefined, |
| 73 | + }, |
| 74 | + sendPolicy: "allow", |
| 75 | + preflightCompactionApplied: false, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +function createDefaults(onBlockReply: (payload: ReplyPayload) => Promise<void>) { |
| 80 | + return { |
| 81 | + defaultModel: "claude", |
| 82 | + typingMode: "never" as const, |
| 83 | + typing: { |
| 84 | + onReplyStart: vi.fn(async () => {}), |
| 85 | + startTypingLoop: vi.fn(async () => {}), |
| 86 | + startTypingOnText: vi.fn(async () => {}), |
| 87 | + refreshTypingTtl: vi.fn(), |
| 88 | + isActive: vi.fn(() => false), |
| 89 | + markRunComplete: vi.fn(), |
| 90 | + markDispatchIdle: vi.fn(), |
| 91 | + cleanup: vi.fn(), |
| 92 | + }, |
| 93 | + opts: { onBlockReply }, |
| 94 | + }; |
| 95 | +} |
| 96 | + |
| 97 | +async function deliverBatch(params: { |
| 98 | + messageProvider: string; |
| 99 | + originatingChannel: string; |
| 100 | + outcomes: Array<"delivered" | "failed">; |
| 101 | + payloads: ReplyPayload[]; |
| 102 | +}) { |
| 103 | + channelState.outcomes = [...params.outcomes]; |
| 104 | + const onBlockReply = vi.fn(async (_payload: ReplyPayload) => {}); |
| 105 | + await deliverFollowupDecision({ |
| 106 | + decision: { kind: "deliver", payloads: params.payloads }, |
| 107 | + turn: createTurn(params), |
| 108 | + defaults: createDefaults(onBlockReply), |
| 109 | + runId: "run-1", |
| 110 | + runFollowup: vi.fn(async () => {}), |
| 111 | + }); |
| 112 | + return onBlockReply; |
| 113 | +} |
| 114 | + |
| 115 | +beforeEach(() => { |
| 116 | + setActivePluginRegistry( |
| 117 | + createTestRegistry( |
| 118 | + (["discord", "imessage", "slack"] as const).map((id) => ({ |
| 119 | + pluginId: id, |
| 120 | + plugin: createChannelPlugin(id), |
| 121 | + source: "test", |
| 122 | + })), |
| 123 | + ), |
| 124 | + ); |
| 125 | + channelState.outcomes = []; |
| 126 | + channelState.deliver.mockReset(); |
| 127 | + channelState.deliver.mockImplementation(async (params: { channel: string }) => { |
| 128 | + const outcome = channelState.outcomes.shift(); |
| 129 | + if (outcome === "delivered") { |
| 130 | + return [{ channel: params.channel, messageId: `message-${channelState.outcomes.length}` }]; |
| 131 | + } |
| 132 | + throw new Error("simulated channel delivery failure"); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +afterEach(() => { |
| 137 | + setActivePluginRegistry(createTestRegistry()); |
| 138 | +}); |
| 139 | + |
| 140 | +describe("follow-up delivery channel boundary", () => { |
| 141 | + it("emits one safe cross-channel error when a terminal payload fails after status delivery", async () => { |
| 142 | + const onBlockReply = await deliverBatch({ |
| 143 | + messageProvider: "discord", |
| 144 | + originatingChannel: "slack", |
| 145 | + outcomes: ["delivered", "failed"], |
| 146 | + payloads: [ |
| 147 | + { text: "status delivered", isStatusNotice: true }, |
| 148 | + setReplyPayloadMetadata( |
| 149 | + { text: "private terminal reply" }, |
| 150 | + { assistantTranscriptOwned: true }, |
| 151 | + ), |
| 152 | + ], |
| 153 | + }); |
| 154 | + |
| 155 | + expect(onBlockReply).toHaveBeenCalledOnce(); |
| 156 | + expect(onBlockReply.mock.calls[0]?.[0]).toMatchObject({ isError: true }); |
| 157 | + expect(onBlockReply.mock.calls[0]?.[0]?.text).toContain("could not deliver"); |
| 158 | + expect(onBlockReply.mock.calls[0]?.[0]?.text).not.toContain("private terminal reply"); |
| 159 | + }); |
| 160 | + |
| 161 | + it("keeps a delivered terminal valid when a later TTS supplement fails", async () => { |
| 162 | + const onBlockReply = await deliverBatch({ |
| 163 | + messageProvider: "discord", |
| 164 | + originatingChannel: "slack", |
| 165 | + outcomes: ["delivered", "failed"], |
| 166 | + payloads: [ |
| 167 | + { text: "terminal reply" }, |
| 168 | + { |
| 169 | + mediaUrl: "file:///tmp/terminal.mp3", |
| 170 | + ttsSupplement: { spokenText: "terminal reply", visibleTextAlreadyDelivered: true }, |
| 171 | + }, |
| 172 | + ], |
| 173 | + }); |
| 174 | + |
| 175 | + expect(onBlockReply).not.toHaveBeenCalled(); |
| 176 | + }); |
| 177 | + |
| 178 | + it("emits one safe error when every supplemental origin delivery fails", async () => { |
| 179 | + const onBlockReply = await deliverBatch({ |
| 180 | + messageProvider: "discord", |
| 181 | + originatingChannel: "slack", |
| 182 | + outcomes: ["failed", "failed"], |
| 183 | + payloads: [ |
| 184 | + { text: "status one", isStatusNotice: true }, |
| 185 | + { text: "status two", isFallbackNotice: true }, |
| 186 | + ], |
| 187 | + }); |
| 188 | + |
| 189 | + expect(onBlockReply).toHaveBeenCalledOnce(); |
| 190 | + expect(onBlockReply.mock.calls[0]?.[0]?.text).toContain("could not deliver"); |
| 191 | + }); |
| 192 | + |
| 193 | + it("returns only the failed payload through same-channel recovery", async () => { |
| 194 | + const failedFinal = { text: "same-channel final" }; |
| 195 | + const onBlockReply = await deliverBatch({ |
| 196 | + messageProvider: "slack", |
| 197 | + originatingChannel: "slack", |
| 198 | + outcomes: ["delivered", "failed"], |
| 199 | + payloads: [{ text: "status", isStatusNotice: true }, failedFinal], |
| 200 | + }); |
| 201 | + |
| 202 | + expect(onBlockReply).toHaveBeenCalledOnce(); |
| 203 | + expect(onBlockReply).toHaveBeenCalledWith(failedFinal); |
| 204 | + }); |
| 205 | + |
| 206 | + it("keeps alias-equivalent built-in channels on same-channel recovery", async () => { |
| 207 | + const failedFinal = { text: "alias same-channel final" }; |
| 208 | + const onBlockReply = await deliverBatch({ |
| 209 | + messageProvider: "imessage", |
| 210 | + originatingChannel: "imsg", |
| 211 | + outcomes: ["failed"], |
| 212 | + payloads: [failedFinal], |
| 213 | + }); |
| 214 | + |
| 215 | + expect(onBlockReply).toHaveBeenCalledOnce(); |
| 216 | + expect(onBlockReply).toHaveBeenCalledWith(failedFinal); |
| 217 | + }); |
| 218 | +}); |
0 commit comments