|
1 | 1 | // Qa Channel tests cover inbound plugin behavior. |
2 | 2 | import { createPluginRuntimeMock } from "openclaw/plugin-sdk/channel-test-helpers"; |
3 | | -import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
4 | 4 | import { setQaChannelRuntime } from "../api.js"; |
| 5 | +import { deleteQaBusMessage, editQaBusMessage, sendQaBusMessage } from "./bus-client.js"; |
5 | 6 | import { handleQaInbound, isHttpMediaUrl } from "./inbound.js"; |
6 | 7 |
|
| 8 | +vi.mock("./bus-client.js", async (importOriginal) => { |
| 9 | + const actual = await importOriginal<typeof import("./bus-client.js")>(); |
| 10 | + return { |
| 11 | + ...actual, |
| 12 | + deleteQaBusMessage: vi.fn(async () => ({ message: {} })), |
| 13 | + editQaBusMessage: vi.fn(async () => ({ message: {} })), |
| 14 | + sendQaBusMessage: vi.fn(async () => ({ message: { id: "preview-1" } })), |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
7 | 18 | type HandleQaInboundParams = Parameters<typeof handleQaInbound>[0]; |
8 | 19 |
|
9 | 20 | function createQaInboundParams( |
@@ -66,6 +77,161 @@ describe("isHttpMediaUrl", () => { |
66 | 77 | }); |
67 | 78 |
|
68 | 79 | describe("handleQaInbound", () => { |
| 80 | + beforeEach(() => { |
| 81 | + vi.clearAllMocks(); |
| 82 | + }); |
| 83 | + |
| 84 | + it("publishes partial replies as one edited preview before final delivery", async () => { |
| 85 | + const runtime = createPluginRuntimeMock(); |
| 86 | + setQaChannelRuntime(runtime); |
| 87 | + |
| 88 | + await handleQaInbound( |
| 89 | + createQaInboundParams({ |
| 90 | + message: { |
| 91 | + conversation: { id: "qa-room", kind: "group" }, |
| 92 | + threadId: "42", |
| 93 | + }, |
| 94 | + }), |
| 95 | + ); |
| 96 | + |
| 97 | + const assembled = firstRunAssembledParams(runtime); |
| 98 | + await assembled.replyOptions?.onPartialReply?.({ text: "preview" }); |
| 99 | + await assembled.replyOptions?.onPartialReply?.({ text: "preview expanded" }); |
| 100 | + await assembled.delivery.deliver({ text: "final answer" }, { kind: "final" }); |
| 101 | + |
| 102 | + expect(sendQaBusMessage).toHaveBeenCalledOnce(); |
| 103 | + expect(sendQaBusMessage).toHaveBeenCalledWith( |
| 104 | + expect.objectContaining({ |
| 105 | + replyToId: "msg-1", |
| 106 | + text: "preview", |
| 107 | + threadId: "42", |
| 108 | + to: "thread:qa-room/42", |
| 109 | + }), |
| 110 | + ); |
| 111 | + expect(editQaBusMessage).toHaveBeenNthCalledWith( |
| 112 | + 1, |
| 113 | + expect.objectContaining({ messageId: "preview-1", text: "preview expanded" }), |
| 114 | + ); |
| 115 | + expect(editQaBusMessage).toHaveBeenNthCalledWith( |
| 116 | + 2, |
| 117 | + expect.objectContaining({ messageId: "preview-1", text: "final answer" }), |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it("keeps block deliveries separate and retains tool calls discovered after a preview", async () => { |
| 122 | + const runtime = createPluginRuntimeMock(); |
| 123 | + setQaChannelRuntime(runtime); |
| 124 | + |
| 125 | + await handleQaInbound(createQaInboundParams()); |
| 126 | + |
| 127 | + const assembled = firstRunAssembledParams(runtime); |
| 128 | + await assembled.replyOptions?.onPartialReply?.({ text: "preview" }); |
| 129 | + await assembled.replyOptions?.onToolStart?.({ |
| 130 | + phase: "start", |
| 131 | + name: "search", |
| 132 | + args: { query: "qa" }, |
| 133 | + }); |
| 134 | + await assembled.delivery.deliver({ text: "tool result" }, { kind: "block" }); |
| 135 | + await assembled.delivery.deliver({ text: "final answer" }, { kind: "final" }); |
| 136 | + |
| 137 | + expect(deleteQaBusMessage).toHaveBeenCalledOnce(); |
| 138 | + expect(sendQaBusMessage).toHaveBeenCalledTimes(3); |
| 139 | + expect(sendQaBusMessage).toHaveBeenNthCalledWith( |
| 140 | + 2, |
| 141 | + expect.objectContaining({ |
| 142 | + text: "tool result", |
| 143 | + toolCalls: [{ name: "search", arguments: { query: "[redacted]" } }], |
| 144 | + }), |
| 145 | + ); |
| 146 | + expect(sendQaBusMessage).toHaveBeenNthCalledWith( |
| 147 | + 3, |
| 148 | + expect.objectContaining({ |
| 149 | + text: "final answer", |
| 150 | + toolCalls: [{ name: "search", arguments: { query: "[redacted]" } }], |
| 151 | + }), |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it("deletes an active preview when reply dispatch fails", async () => { |
| 156 | + const runtime = createPluginRuntimeMock(); |
| 157 | + setQaChannelRuntime(runtime); |
| 158 | + |
| 159 | + await handleQaInbound(createQaInboundParams()); |
| 160 | + |
| 161 | + const assembled = firstRunAssembledParams(runtime); |
| 162 | + await assembled.replyOptions?.onPartialReply?.({ text: "unfinished preview" }); |
| 163 | + assembled.delivery.onError?.(new Error("model failed"), { kind: "final" }); |
| 164 | + |
| 165 | + await vi.waitFor(() => { |
| 166 | + expect(deleteQaBusMessage).toHaveBeenCalledWith( |
| 167 | + expect.objectContaining({ messageId: "preview-1" }), |
| 168 | + ); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("deletes a preview after a queued edit fails", async () => { |
| 173 | + const runtime = createPluginRuntimeMock(); |
| 174 | + setQaChannelRuntime(runtime); |
| 175 | + vi.mocked(editQaBusMessage).mockRejectedValueOnce(new Error("edit failed")); |
| 176 | + |
| 177 | + await handleQaInbound(createQaInboundParams()); |
| 178 | + |
| 179 | + const assembled = firstRunAssembledParams(runtime); |
| 180 | + await assembled.replyOptions?.onPartialReply?.({ text: "first preview" }); |
| 181 | + await expect( |
| 182 | + assembled.replyOptions?.onPartialReply?.({ text: "broken preview" }), |
| 183 | + ).rejects.toThrow("edit failed"); |
| 184 | + assembled.delivery.onError?.(new Error("dispatch failed"), { kind: "final" }); |
| 185 | + |
| 186 | + await vi.waitFor(() => { |
| 187 | + expect(deleteQaBusMessage).toHaveBeenCalledWith( |
| 188 | + expect.objectContaining({ messageId: "preview-1" }), |
| 189 | + ); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + it("escapes control characters in dispatch error logs", async () => { |
| 194 | + const runtime = createPluginRuntimeMock(); |
| 195 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined); |
| 196 | + const c1Control = String.fromCharCode(0x9b); |
| 197 | + const lineSeparator = String.fromCodePoint(0x2028); |
| 198 | + const paragraphSeparator = String.fromCodePoint(0x2029); |
| 199 | + vi.mocked(deleteQaBusMessage).mockRejectedValueOnce( |
| 200 | + new Error(`cleanup\nforged\u001b[31m${c1Control}32m${lineSeparator}next`), |
| 201 | + ); |
| 202 | + setQaChannelRuntime(runtime); |
| 203 | + |
| 204 | + try { |
| 205 | + await handleQaInbound(createQaInboundParams()); |
| 206 | + |
| 207 | + const assembled = firstRunAssembledParams(runtime); |
| 208 | + await assembled.replyOptions?.onPartialReply?.({ text: "unfinished preview" }); |
| 209 | + assembled.delivery.onError?.(new Error(`dispatch\r\nforged${paragraphSeparator}next`), { |
| 210 | + kind: "final", |
| 211 | + }); |
| 212 | + |
| 213 | + await vi.waitFor(() => { |
| 214 | + expect(warn).toHaveBeenCalledTimes(2); |
| 215 | + }); |
| 216 | + assembled.delivery.onError?.(undefined, { kind: "final" }); |
| 217 | + await vi.waitFor(() => { |
| 218 | + expect(warn).toHaveBeenCalledTimes(3); |
| 219 | + }); |
| 220 | + const output = warn.mock.calls.flat().join(" "); |
| 221 | + expect(output).not.toContain("\r"); |
| 222 | + expect(output).not.toContain("\n"); |
| 223 | + expect(output).not.toContain(String.fromCharCode(0x1b)); |
| 224 | + expect(output).not.toContain(c1Control); |
| 225 | + expect(output).not.toContain(lineSeparator); |
| 226 | + expect(output).not.toContain(paragraphSeparator); |
| 227 | + expect(output).toContain("dispatch\\u000d\\u000aforged\\u2029next"); |
| 228 | + expect(output).toContain("cleanup\\u000aforged\\u001b[31m\\u009b32m\\u2028next"); |
| 229 | + expect(output).toContain("[object Undefined]"); |
| 230 | + } finally { |
| 231 | + warn.mockRestore(); |
| 232 | + } |
| 233 | + }); |
| 234 | + |
69 | 235 | it("marks group messages that match configured mention patterns", async () => { |
70 | 236 | const runtime = createPluginRuntimeMock(); |
71 | 237 | vi.mocked(runtime.channel.mentions.buildMentionRegexes).mockReturnValue([/\b@?openclaw\b/i]); |
|
0 commit comments