Skip to content

Commit 5da90d0

Browse files
committed
test: tighten discord message queue assertions
1 parent 5579b00 commit 5da90d0

1 file changed

Lines changed: 35 additions & 30 deletions

File tree

extensions/discord/src/monitor/message-handler.queue.test.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ vi.mock("./typing.js", () => ({
2929
}));
3030

3131
type SetStatusFn = (patch: Record<string, unknown>) => void;
32+
33+
function statusPatches(setStatus: { mock: { calls: Array<[Record<string, unknown>]> } }) {
34+
return setStatus.mock.calls.map(([patch]) => patch);
35+
}
36+
37+
function expectStatusPatch(
38+
setStatus: { mock: { calls: Array<[Record<string, unknown>]> } },
39+
expected: Record<string, unknown>,
40+
) {
41+
expect(
42+
statusPatches(setStatus).some((patch) =>
43+
Object.entries(expected).every(([key, value]) => patch[key] === value),
44+
),
45+
).toBe(true);
46+
}
47+
3248
function createDeferred<T = void>() {
3349
let resolve: (value: T | PromiseLike<T>) => void = () => {};
3450
const promise = new Promise<T>((innerResolve) => {
@@ -167,12 +183,10 @@ describe("createDiscordMessageHandler queue behavior", () => {
167183

168184
await flushQueueWork();
169185

170-
expect(earlyTypingMocks.createDiscordRestClient).toHaveBeenCalledWith(
171-
expect.objectContaining({
172-
accountId: "default",
173-
token: "test-token",
174-
}),
175-
);
186+
expect(earlyTypingMocks.createDiscordRestClient).toHaveBeenCalledTimes(1);
187+
const [restClientParams] = earlyTypingMocks.createDiscordRestClient.mock.calls[0] ?? [];
188+
expect((restClientParams as { accountId?: unknown } | undefined)?.accountId).toBe("default");
189+
expect((restClientParams as { token?: unknown } | undefined)?.token).toBe("test-token");
176190
expect(earlyTypingMocks.sendTyping).toHaveBeenCalledWith({
177191
rest: { kind: "discord-rest" },
178192
channelId: "dm-1",
@@ -274,12 +288,7 @@ describe("createDiscordMessageHandler queue behavior", () => {
274288
const setStatus = vi.fn();
275289
createDiscordMessageHandler(createDiscordHandlerParams({ setStatus }));
276290

277-
expect(setStatus).toHaveBeenCalledWith(
278-
expect.objectContaining({
279-
activeRuns: 0,
280-
busy: false,
281-
}),
282-
);
291+
expectStatusPatch(setStatus, { activeRuns: 0, busy: false });
283292
});
284293

285294
it("returns immediately and tracks busy status while queued runs execute", async () => {
@@ -302,12 +311,7 @@ describe("createDiscordMessageHandler queue behavior", () => {
302311

303312
await flushQueueWork();
304313
expect(processDiscordMessageMock).toHaveBeenCalledTimes(1);
305-
expect(setStatus).toHaveBeenCalledWith(
306-
expect.objectContaining({
307-
activeRuns: 1,
308-
busy: true,
309-
}),
310-
);
314+
expectStatusPatch(setStatus, { activeRuns: 1, busy: true });
311315

312316
await expect(handler(createMessageData("m-2") as never, {} as never)).resolves.toBeUndefined();
313317

@@ -325,12 +329,9 @@ describe("createDiscordMessageHandler queue behavior", () => {
325329
await secondRun.promise;
326330

327331
await flushQueueWork();
328-
expect(setStatus).toHaveBeenLastCalledWith(
329-
expect.objectContaining({
330-
activeRuns: 0,
331-
busy: false,
332-
}),
333-
);
332+
const lastStatusPatch = statusPatches(setStatus).at(-1);
333+
expect(lastStatusPatch?.activeRuns).toBe(0);
334+
expect(lastStatusPatch?.busy).toBe(false);
334335
});
335336

336337
it("drops duplicate inbound message deliveries before they reach preflight", async () => {
@@ -363,8 +364,9 @@ describe("createDiscordMessageHandler queue behavior", () => {
363364
await expect(handler(duplicate as never, {} as never)).resolves.toBeUndefined();
364365
await flushQueueWork();
365366
expect(processDiscordMessageMock).toHaveBeenCalledTimes(1);
366-
expect(params.runtime.error).toHaveBeenCalledWith(
367-
expect.stringContaining("discord message run failed: DiscordRetryableInboundError: retry me"),
367+
expect(params.runtime.error).toHaveBeenCalledTimes(1);
368+
expect(String(params.runtime.error.mock.calls[0]?.[0])).toContain(
369+
"discord message run failed: DiscordRetryableInboundError: retry me",
368370
);
369371

370372
await expect(handler(duplicate as never, {} as never)).resolves.toBeUndefined();
@@ -390,8 +392,9 @@ describe("createDiscordMessageHandler queue behavior", () => {
390392
await expect(handler(duplicate as never, {} as never)).resolves.toBeUndefined();
391393
await flushQueueWork();
392394
expect(processDiscordMessageMock).toHaveBeenCalledTimes(1);
393-
expect(params.runtime.error).toHaveBeenCalledWith(
394-
expect.stringContaining("discord message run failed: Error: post-send failure"),
395+
expect(params.runtime.error).toHaveBeenCalledTimes(1);
396+
expect(String(params.runtime.error.mock.calls[0]?.[0])).toContain(
397+
"discord message run failed: Error: post-send failure",
395398
);
396399

397400
await expect(handler(duplicate as never, {} as never)).resolves.toBeUndefined();
@@ -441,7 +444,9 @@ describe("createDiscordMessageHandler queue behavior", () => {
441444

442445
expect(processDiscordMessageMock).toHaveBeenCalledTimes(1);
443446
expect(capturedAbortSignals).toEqual([undefined]);
444-
expect(params.runtime.error).not.toHaveBeenCalledWith(expect.stringContaining("timed out"));
447+
expect(
448+
params.runtime.error.mock.calls.some(([message]) => String(message).includes("timed out")),
449+
).toBe(false);
445450

446451
firstRun.resolve();
447452
await firstRun.promise;
@@ -654,6 +659,6 @@ describe("createDiscordMessageHandler queue behavior", () => {
654659

655660
await flushQueueWork();
656661
expect(processDiscordMessageMock).toHaveBeenCalledTimes(2);
657-
expect(setStatus).toHaveBeenCalledWith(expect.objectContaining({ activeRuns: 0, busy: false }));
662+
expectStatusPatch(setStatus, { activeRuns: 0, busy: false });
658663
});
659664
});

0 commit comments

Comments
 (0)