forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto-reply.retry.test.ts
More file actions
89 lines (79 loc) · 2.25 KB
/
auto-reply.retry.test.ts
File metadata and controls
89 lines (79 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { describe, expect, it, vi } from "vitest";
vi.mock("../src/web/media.js", () => ({
loadWebMedia: vi.fn(async () => ({
buffer: Buffer.from("img"),
contentType: "image/jpeg",
kind: "image",
fileName: "img.jpg",
})),
}));
import type { WebInboundMessage } from "../src/web/inbound.js";
import { defaultRuntime } from "../src/runtime.js";
import { deliverWebReply } from "../src/web/auto-reply.js";
const noopLogger = {
info: vi.fn(),
warn: vi.fn(),
};
function makeMsg(): WebInboundMessage {
const reply = vi.fn<
Parameters<WebInboundMessage["reply"]>,
ReturnType<WebInboundMessage["reply"]>
>();
const sendMedia = vi.fn<
Parameters<WebInboundMessage["sendMedia"]>,
ReturnType<WebInboundMessage["sendMedia"]>
>();
const sendComposing = vi.fn<
Parameters<WebInboundMessage["sendComposing"]>,
ReturnType<WebInboundMessage["sendComposing"]>
>();
return {
from: "+10000000000",
conversationId: "+10000000000",
to: "+20000000000",
id: "abc",
body: "hello",
chatType: "direct",
chatId: "chat-1",
sendComposing,
reply,
sendMedia,
};
}
describe("deliverWebReply retry", () => {
it("retries text send on transient failure", async () => {
const msg = makeMsg();
msg.reply.mockRejectedValueOnce(new Error("connection closed"));
msg.reply.mockResolvedValueOnce(undefined);
await expect(
deliverWebReply({
replyResult: { text: "hi" },
msg,
maxMediaBytes: 5_000_000,
replyLogger: noopLogger,
runtime: defaultRuntime,
skipLog: true,
}),
).resolves.toBeUndefined();
expect(msg.reply).toHaveBeenCalledTimes(2);
});
it("retries media send on transient failure", async () => {
const msg = makeMsg();
msg.sendMedia.mockRejectedValueOnce(new Error("socket reset"));
msg.sendMedia.mockResolvedValueOnce(undefined);
await expect(
deliverWebReply({
replyResult: {
text: "caption",
mediaUrl: "http://example.com/img.jpg",
},
msg,
maxMediaBytes: 5_000_000,
replyLogger: noopLogger,
runtime: defaultRuntime,
skipLog: true,
}),
).resolves.toBeUndefined();
expect(msg.sendMedia).toHaveBeenCalledTimes(2);
});
});