test: guard discord message process mock calls

This commit is contained in:
Peter Steinberger
2026-05-12 02:49:14 +01:00
parent 2bf827ae35
commit e1cd9275dc

View File

@@ -452,6 +452,24 @@ function requireRecord(value: unknown, label: string): Record<string, unknown> {
return value as Record<string, unknown>;
}
type MockWithCalls = { mock: { calls: unknown[][] } };
function firstMockCall(mock: MockWithCalls, label: string): unknown[] {
const call = mock.mock.calls.at(0);
if (!call) {
throw new Error(`missing ${label} call`);
}
return call;
}
function firstMockArg(mock: MockWithCalls, label: string) {
return firstMockCall(mock, label)[0];
}
function firstDispatchParams(): DispatchInboundParams {
return firstMockArg(dispatchInboundMessage, "dispatchInboundMessage") as DispatchInboundParams;
}
function expectRecordFields(record: Record<string, unknown>, fields: Record<string, unknown>) {
for (const [key, value] of Object.entries(fields)) {
expect(record[key]).toEqual(value);
@@ -560,10 +578,7 @@ function createMockDraftStreamForTest() {
}
function expectPreviewEditContent(content: string) {
const call = editMessageDiscord.mock.calls[0] as unknown[] | undefined;
if (!call) {
throw new Error("missing preview edit call");
}
const call = firstMockCall(editMessageDiscord, "preview edit");
expect(call[0]).toBe("c1");
expect(call[1]).toBe("preview-1");
expect(call[2]).toEqual({ content });
@@ -655,11 +670,14 @@ describe("processDiscordMessage ack reactions", () => {
expect(sendMocks.reactMessageDiscord).toHaveBeenCalled();
const feedbackOptions = requireRecord(
sendMocks.reactMessageDiscord.mock.calls[0]?.[3],
requireReactionCall(sendMocks.reactMessageDiscord, 0)[3],
"feedback reaction options",
);
expect(feedbackOptions.rest).toBe(feedbackRest);
const deliveryParams = requireRecord(deliverDiscordReply.mock.calls[0]?.[0], "delivery params");
const deliveryParams = requireRecord(
firstMockArg(deliverDiscordReply, "deliverDiscordReply"),
"delivery params",
);
expect(deliveryParams.rest).toBe(deliveryRest);
expect(feedbackRest).not.toBe(deliveryRest);
});
@@ -753,12 +771,10 @@ describe("processDiscordMessage ack reactions", () => {
await runProcessDiscordMessage(ctx);
await vi.runAllTimersAsync();
const resolveCall = discordTargetMocks.resolveDiscordTargetChannelId.mock.calls[0] as
| unknown[]
| undefined;
if (!resolveCall) {
throw new Error("missing Discord target resolve call");
}
const resolveCall = firstMockCall(
discordTargetMocks.resolveDiscordTargetChannelId,
"resolveDiscordTargetChannelId",
);
expect(resolveCall[0]).toBe("user:u1");
expect(requireRecord(resolveCall[1], "Discord target resolve options").accountId).toBe(
"default",
@@ -1553,9 +1569,7 @@ describe("processDiscordMessage draft streaming", () => {
await runProcessDiscordMessage(ctx);
expect(draftStream.update).toHaveBeenCalledWith("Hello");
expect(dispatchInboundMessage.mock.calls[0]?.[0]?.replyOptions?.disableBlockStreaming).toBe(
true,
);
expect(firstDispatchParams().replyOptions?.disableBlockStreaming).toBe(true);
});
it("keeps progress label visible when Discord tool progress lines are disabled", async () => {
@@ -1586,10 +1600,8 @@ describe("processDiscordMessage draft streaming", () => {
expect(draftStream.update).toHaveBeenCalledWith("Shelling");
expect(draftStream.flush).toHaveBeenCalledTimes(1);
expect(
requireRecord(
dispatchInboundMessage.mock.calls[0]?.[0]?.replyOptions,
"dispatch reply options",
).suppressDefaultToolProgressMessages,
requireRecord(firstDispatchParams().replyOptions, "dispatch reply options")
.suppressDefaultToolProgressMessages,
).toBe(true);
});
@@ -1879,9 +1891,7 @@ describe("processDiscordMessage draft streaming", () => {
await runProcessDiscordMessage(ctx);
expect(
dispatchInboundMessage.mock.calls[0]?.[0]?.replyOptions?.suppressDefaultToolProgressMessages,
).toBe(true);
expect(firstDispatchParams().replyOptions?.suppressDefaultToolProgressMessages).toBe(true);
});
it("strips reply tags from preview partials", async () => {