Skip to content

Commit 7c8f08a

Browse files
committed
fix(ci): split discord thread-creation tests under the max-lines cap
#117354 added 246 lines of thread initial-message chunking coverage, taking send.creates-thread.test.ts to 1082 counted lines against the 1000-line cap and leaving main check-lint red. Move the 9 chunking tests to a sibling file. The suite remains 51 tests total: 51 before; 42 + 9 after.
1 parent 2dffeae commit 7c8f08a

3 files changed

Lines changed: 320 additions & 273 deletions

File tree

Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
import { ChannelType, Routes } from "discord-api-types/v10";
2+
import { createRequireRecord } from "openclaw/plugin-sdk/test-fixtures";
3+
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
4+
import { hasDiscordMessageCreateAmbiguity } from "./retry.js";
5+
import {
6+
makeDiscordRest,
7+
requestBody,
8+
requestPath,
9+
type MockCallSource,
10+
} from "./send.test-harness.js";
11+
12+
let createThreadDiscord: typeof import("./send.js").createThreadDiscord;
13+
let DiscordThreadInitialMessageError: typeof import("./send.js").DiscordThreadInitialMessageError;
14+
15+
const DISCORD_TEST_CFG = {
16+
channels: {
17+
discord: {
18+
accounts: {
19+
default: {},
20+
},
21+
},
22+
},
23+
};
24+
25+
function discordClientOpts(rest: ReturnType<typeof makeDiscordRest>["rest"]) {
26+
return { cfg: DISCORD_TEST_CFG, rest, token: "t" };
27+
}
28+
29+
const requireRecord = createRequireRecord("object", "expected-label");
30+
31+
beforeAll(async () => {
32+
({ createThreadDiscord, DiscordThreadInitialMessageError } = await import("./send.js"));
33+
});
34+
35+
beforeEach(() => {
36+
vi.clearAllMocks();
37+
});
38+
39+
describe("sendMessageDiscord", () => {
40+
it("keeps forum starter messages within Discord's content limit", async () => {
41+
const { rest, getMock, postMock } = makeDiscordRest();
42+
getMock.mockResolvedValue({ type: ChannelType.GuildForum });
43+
postMock.mockResolvedValue({ id: "t1" });
44+
const content = "a".repeat(2001);
45+
46+
await createThreadDiscord("chan1", { name: "thread", content }, discordClientOpts(rest));
47+
48+
expect(postMock).toHaveBeenCalledTimes(2);
49+
expect(requestBody(postMock as unknown as MockCallSource, 0)).toEqual({
50+
name: "thread",
51+
message: { content: "a".repeat(2000) },
52+
});
53+
expect(requestPath(postMock as unknown as MockCallSource, 1)).toBe(
54+
Routes.channelMessages("t1"),
55+
);
56+
expect(requestBody(postMock as unknown as MockCallSource, 1)).toMatchObject({
57+
content: "a",
58+
enforce_nonce: true,
59+
});
60+
});
61+
62+
it("keeps sub-limit multi-line forum content in one starter message", async () => {
63+
const { rest, getMock, postMock } = makeDiscordRest();
64+
getMock.mockResolvedValue({ type: ChannelType.GuildForum });
65+
postMock.mockResolvedValue({ id: "t1" });
66+
const content = Array.from({ length: 18 }, (_, index) => `line ${index + 1}`).join("\n");
67+
68+
await createThreadDiscord("chan1", { name: "thread", content }, discordClientOpts(rest));
69+
70+
expect(postMock).toHaveBeenCalledTimes(1);
71+
expect(requestBody(postMock as unknown as MockCallSource)).toEqual({
72+
name: "thread",
73+
message: { content },
74+
});
75+
});
76+
77+
it("reports a delivered forum starter when a continuation chunk fails", async () => {
78+
const { rest, getMock, postMock } = makeDiscordRest();
79+
getMock.mockResolvedValue({ type: ChannelType.GuildForum });
80+
postMock
81+
.mockResolvedValueOnce({ id: "t1", message: { id: "starter1", channel_id: "t1" } })
82+
.mockRejectedValueOnce(Object.assign(new Error("missing access"), { status: 403 }));
83+
84+
let thrown: unknown;
85+
try {
86+
await createThreadDiscord(
87+
"chan1",
88+
{ name: "thread", content: "a".repeat(2001) },
89+
discordClientOpts(rest),
90+
);
91+
} catch (error) {
92+
thrown = error;
93+
}
94+
95+
expect(thrown).toBeInstanceOf(DiscordThreadInitialMessageError);
96+
expect(requireRecord(thrown, "thread initial message error").initialMessageDelivery).toEqual({
97+
starterMessageDelivered: true,
98+
deliveredChunkCount: 1,
99+
deliveredMessageIds: ["starter1"],
100+
failedChunkDelivery: "not_delivered",
101+
failedChunkIndex: 1,
102+
totalChunkCount: 2,
103+
});
104+
});
105+
106+
it("reports an exhausted ambiguous forum continuation as unknown delivery", async () => {
107+
const { rest, getMock, postMock } = makeDiscordRest();
108+
getMock.mockResolvedValue({ type: ChannelType.GuildForum });
109+
const ambiguous = Object.assign(new Error("response lost"), { status: 502 });
110+
postMock
111+
.mockResolvedValueOnce({ id: "t1", message: { id: "starter1", channel_id: "t1" } })
112+
.mockRejectedValue(ambiguous);
113+
114+
let thrown: unknown;
115+
try {
116+
await createThreadDiscord(
117+
"chan1",
118+
{ name: "thread", content: "a".repeat(2001) },
119+
{
120+
...discordClientOpts(rest),
121+
retry: { attempts: 2, minDelayMs: 0, maxDelayMs: 0, jitter: 0 },
122+
},
123+
);
124+
} catch (error) {
125+
thrown = error;
126+
}
127+
128+
expect(postMock).toHaveBeenCalledTimes(3);
129+
expect(thrown).toBeInstanceOf(DiscordThreadInitialMessageError);
130+
expect(hasDiscordMessageCreateAmbiguity(thrown)).toBe(true);
131+
expect(requireRecord(thrown, "thread initial message error").message).toContain(
132+
"delivery of the remaining initial content could not be confirmed",
133+
);
134+
expect(requireRecord(thrown, "thread initial message error").initialMessageDelivery).toEqual({
135+
starterMessageDelivered: true,
136+
deliveredChunkCount: 1,
137+
deliveredMessageIds: ["starter1"],
138+
failedChunkDelivery: "unknown",
139+
failedChunkIndex: 1,
140+
totalChunkCount: 2,
141+
});
142+
});
143+
144+
it("chunks long initial messages for non-forum threads", async () => {
145+
const { rest, getMock, postMock } = makeDiscordRest();
146+
getMock.mockResolvedValue({ type: ChannelType.GuildText });
147+
postMock.mockResolvedValue({ id: "t1" });
148+
const content = "a".repeat(2001);
149+
150+
await createThreadDiscord("chan1", { name: "thread", content }, discordClientOpts(rest));
151+
152+
expect(postMock).toHaveBeenCalledTimes(3);
153+
expect(requestPath(postMock as unknown as MockCallSource, 1)).toBe(
154+
Routes.channelMessages("t1"),
155+
);
156+
expect(requestBody(postMock as unknown as MockCallSource, 1)).toMatchObject({
157+
content: "a".repeat(2000),
158+
enforce_nonce: true,
159+
});
160+
expect(requestPath(postMock as unknown as MockCallSource, 2)).toBe(
161+
Routes.channelMessages("t1"),
162+
);
163+
expect(requestBody(postMock as unknown as MockCallSource, 2)).toMatchObject({
164+
content: "a",
165+
enforce_nonce: true,
166+
});
167+
});
168+
169+
it("keeps sub-limit multi-line non-forum content in one initial message", async () => {
170+
const { rest, getMock, postMock } = makeDiscordRest();
171+
getMock.mockResolvedValue({ type: ChannelType.GuildText });
172+
postMock.mockResolvedValue({ id: "t1", channel_id: "t1" });
173+
const content = Array.from({ length: 18 }, (_, index) => `line ${index + 1}`).join("\n");
174+
175+
await createThreadDiscord("chan1", { name: "thread", content }, discordClientOpts(rest));
176+
177+
expect(postMock).toHaveBeenCalledTimes(2);
178+
expect(requestBody(postMock as unknown as MockCallSource, 1)).toMatchObject({ content });
179+
});
180+
181+
it("reports delivered non-forum chunks when a later chunk fails", async () => {
182+
const { rest, getMock, postMock } = makeDiscordRest();
183+
getMock.mockResolvedValue({ type: ChannelType.GuildText });
184+
postMock
185+
.mockResolvedValueOnce({ id: "t1", name: "thread", type: ChannelType.PublicThread })
186+
.mockResolvedValueOnce({ id: "msg1", channel_id: "t1" })
187+
.mockRejectedValueOnce(Object.assign(new Error("missing access"), { status: 403 }));
188+
189+
let thrown: unknown;
190+
try {
191+
await createThreadDiscord(
192+
"chan1",
193+
{ name: "thread", content: "a".repeat(4001) },
194+
discordClientOpts(rest),
195+
);
196+
} catch (error) {
197+
thrown = error;
198+
}
199+
200+
expect(thrown).toBeInstanceOf(DiscordThreadInitialMessageError);
201+
expect(requireRecord(thrown, "thread initial message error").initialMessageDelivery).toEqual({
202+
starterMessageDelivered: false,
203+
deliveredChunkCount: 1,
204+
deliveredMessageIds: ["msg1"],
205+
failedChunkDelivery: "not_delivered",
206+
failedChunkIndex: 1,
207+
totalChunkCount: 3,
208+
});
209+
});
210+
211+
it("reports an exhausted ambiguous non-forum chunk as unknown delivery", async () => {
212+
const { rest, getMock, postMock } = makeDiscordRest();
213+
getMock.mockResolvedValue({ type: ChannelType.GuildText });
214+
const ambiguous = Object.assign(new Error("response lost"), { status: 502 });
215+
postMock
216+
.mockResolvedValueOnce({ id: "t1", name: "thread", type: ChannelType.PublicThread })
217+
.mockResolvedValueOnce({ id: "msg1", channel_id: "t1" })
218+
.mockRejectedValue(ambiguous);
219+
220+
let thrown: unknown;
221+
try {
222+
await createThreadDiscord(
223+
"chan1",
224+
{ name: "thread", content: "a".repeat(4001) },
225+
{
226+
...discordClientOpts(rest),
227+
retry: { attempts: 2, minDelayMs: 0, maxDelayMs: 0, jitter: 0 },
228+
},
229+
);
230+
} catch (error) {
231+
thrown = error;
232+
}
233+
234+
expect(postMock).toHaveBeenCalledTimes(4);
235+
expect(thrown).toBeInstanceOf(DiscordThreadInitialMessageError);
236+
expect(hasDiscordMessageCreateAmbiguity(thrown)).toBe(true);
237+
expect(requireRecord(thrown, "thread initial message error").message).toContain(
238+
"delivery of the remaining initial content could not be confirmed",
239+
);
240+
expect(requireRecord(thrown, "thread initial message error").initialMessageDelivery).toEqual({
241+
starterMessageDelivered: false,
242+
deliveredChunkCount: 1,
243+
deliveredMessageIds: ["msg1"],
244+
failedChunkDelivery: "unknown",
245+
failedChunkIndex: 1,
246+
totalChunkCount: 3,
247+
});
248+
});
249+
250+
it("retries continuation sends with a stable nonce per chunk", async () => {
251+
const { rest, getMock, postMock } = makeDiscordRest();
252+
getMock.mockResolvedValue({ type: ChannelType.GuildText });
253+
postMock
254+
.mockResolvedValueOnce({ id: "t1", name: "thread", type: ChannelType.PublicThread })
255+
.mockRejectedValueOnce(Object.assign(new Error("bad gateway"), { status: 502 }))
256+
.mockResolvedValueOnce({ id: "msg1", channel_id: "t1" })
257+
.mockResolvedValueOnce({ id: "msg2", channel_id: "t1" });
258+
259+
await createThreadDiscord(
260+
"chan1",
261+
{ name: "thread", content: "a".repeat(2001) },
262+
{
263+
...discordClientOpts(rest),
264+
retry: { attempts: 2, minDelayMs: 0, maxDelayMs: 0, jitter: 0 },
265+
},
266+
);
267+
268+
expect(postMock).toHaveBeenCalledTimes(4);
269+
const firstAttempt = requestBody(postMock as unknown as MockCallSource, 1);
270+
const retryAttempt = requestBody(postMock as unknown as MockCallSource, 2);
271+
const nextChunk = requestBody(postMock as unknown as MockCallSource, 3);
272+
expect(firstAttempt.enforce_nonce).toBe(true);
273+
expect(retryAttempt.nonce).toBe(firstAttempt.nonce);
274+
expect(nextChunk.enforce_nonce).toBe(true);
275+
expect(nextChunk.nonce).not.toBe(firstAttempt.nonce);
276+
});
277+
});

0 commit comments

Comments
 (0)