perf(test): dedupe slow discord monitor cases

This commit is contained in:
Peter Steinberger
2026-02-18 04:03:59 +00:00
parent ac0db68235
commit b099171db5
3 changed files with 86 additions and 117 deletions

View File

@@ -1,4 +1,4 @@
import type { Message } from "@buape/carbon";
import { ChannelType, type Client, type Message } from "@buape/carbon";
import { beforeEach, describe, expect, it, vi } from "vitest";
const fetchRemoteMedia = vi.fn();
@@ -16,8 +16,13 @@ vi.mock("../../globals.js", () => ({
logVerbose: () => {},
}));
const { resolveDiscordMessageChannelId, resolveForwardedMediaList } =
await import("./message-utils.js");
const {
__resetDiscordChannelInfoCacheForTest,
resolveDiscordChannelInfo,
resolveDiscordMessageChannelId,
resolveDiscordMessageText,
resolveForwardedMediaList,
} = await import("./message-utils.js");
function asMessage(payload: Record<string, unknown>): Message {
return payload as unknown as Message;
@@ -122,3 +127,72 @@ describe("resolveForwardedMediaList", () => {
expect(fetchRemoteMedia).not.toHaveBeenCalled();
});
});
describe("resolveDiscordMessageText", () => {
it("includes forwarded message snapshots in body text", () => {
const text = resolveDiscordMessageText(
asMessage({
content: "",
rawData: {
message_snapshots: [
{
message: {
content: "forwarded hello",
embeds: [],
attachments: [],
author: {
id: "u2",
username: "Bob",
discriminator: "0",
},
},
},
],
},
}),
{ includeForwarded: true },
);
expect(text).toContain("[Forwarded message from @Bob]");
expect(text).toContain("forwarded hello");
});
});
describe("resolveDiscordChannelInfo", () => {
beforeEach(() => {
__resetDiscordChannelInfoCacheForTest();
});
it("caches channel lookups between calls", async () => {
const fetchChannel = vi.fn().mockResolvedValue({
type: ChannelType.DM,
name: "dm",
});
const client = { fetchChannel } as unknown as Client;
const first = await resolveDiscordChannelInfo(client, "cache-channel-1");
const second = await resolveDiscordChannelInfo(client, "cache-channel-1");
expect(first).toEqual({
type: ChannelType.DM,
name: "dm",
topic: undefined,
parentId: undefined,
ownerId: undefined,
});
expect(second).toEqual(first);
expect(fetchChannel).toHaveBeenCalledTimes(1);
});
it("negative-caches missing channels", async () => {
const fetchChannel = vi.fn().mockResolvedValue(null);
const client = { fetchChannel } as unknown as Client;
const first = await resolveDiscordChannelInfo(client, "missing-channel");
const second = await resolveDiscordChannelInfo(client, "missing-channel");
expect(first).toBeNull();
expect(second).toBeNull();
expect(fetchChannel).toHaveBeenCalledTimes(1);
});
});