perf(test): trim redundant suites and tighten wait loops

This commit is contained in:
Peter Steinberger
2026-02-14 02:00:43 +00:00
parent 9769b96fb1
commit 0b8227fa92
5 changed files with 65 additions and 241 deletions

View File

@@ -107,7 +107,7 @@ describe("web inbound media saves with extension", () => {
await fs.rm(HOME, { recursive: true, force: true });
});
it("stores inbound image with jpeg extension", async () => {
it("stores image extension, extracts caption mentions, and keeps document filename", async () => {
const onMessage = vi.fn();
const listener = await monitorWebInbox({ verbose: false, onMessage });
const { createWaSocket } = await import("./session.js");
@@ -117,7 +117,7 @@ describe("web inbound media saves with extension", () => {
}>
)();
const upsert = {
realSock.ev.emit("messages.upsert", {
type: "notify",
messages: [
{
@@ -126,31 +126,17 @@ describe("web inbound media saves with extension", () => {
messageTimestamp: 1_700_000_001,
},
],
};
});
realSock.ev.emit("messages.upsert", upsert);
const msg = await waitForMessage(onMessage);
const mediaPath = msg.mediaPath;
const first = await waitForMessage(onMessage);
const mediaPath = first.mediaPath;
expect(mediaPath).toBeDefined();
expect(path.extname(mediaPath as string)).toBe(".jpg");
const stat = await fs.stat(mediaPath as string);
expect(stat.size).toBeGreaterThan(0);
await listener.close();
});
it("extracts mentions from media captions", async () => {
const onMessage = vi.fn();
const listener = await monitorWebInbox({ verbose: false, onMessage });
const { createWaSocket } = await import("./session.js");
const realSock = await (
createWaSocket as unknown as () => Promise<{
ev: import("node:events").EventEmitter;
}>
)();
const upsert = {
onMessage.mockClear();
realSock.ev.emit("messages.upsert", {
type: "notify",
messages: [
{
@@ -171,13 +157,30 @@ describe("web inbound media saves with extension", () => {
messageTimestamp: 1_700_000_002,
},
],
};
});
realSock.ev.emit("messages.upsert", upsert);
const second = await waitForMessage(onMessage);
expect(second.chatType).toBe("group");
expect(second.mentionedJids).toEqual(["999@s.whatsapp.net"]);
const msg = await waitForMessage(onMessage);
expect(msg.chatType).toBe("group");
expect(msg.mentionedJids).toEqual(["999@s.whatsapp.net"]);
onMessage.mockClear();
const fileName = "invoice.pdf";
realSock.ev.emit("messages.upsert", {
type: "notify",
messages: [
{
key: { id: "doc1", fromMe: false, remoteJid: "333@s.whatsapp.net" },
message: { documentMessage: { mimetype: "application/pdf", fileName } },
messageTimestamp: 1_700_000_004,
},
],
});
const third = await waitForMessage(onMessage);
expect(third.mediaFileName).toBe(fileName);
expect(saveMediaBufferSpy).toHaveBeenCalled();
const lastCall = saveMediaBufferSpy.mock.calls.at(-1);
expect(lastCall?.[4]).toBe(fileName);
await listener.close();
});
@@ -216,37 +219,4 @@ describe("web inbound media saves with extension", () => {
await listener.close();
});
it("passes document filenames to saveMediaBuffer", async () => {
const onMessage = vi.fn();
const listener = await monitorWebInbox({ verbose: false, onMessage });
const { createWaSocket } = await import("./session.js");
const realSock = await (
createWaSocket as unknown as () => Promise<{
ev: import("node:events").EventEmitter;
}>
)();
const fileName = "invoice.pdf";
const upsert = {
type: "notify",
messages: [
{
key: { id: "doc1", fromMe: false, remoteJid: "333@s.whatsapp.net" },
message: { documentMessage: { mimetype: "application/pdf", fileName } },
messageTimestamp: 1_700_000_004,
},
],
};
realSock.ev.emit("messages.upsert", upsert);
const msg = await waitForMessage(onMessage);
expect(msg.mediaFileName).toBe(fileName);
expect(saveMediaBufferSpy).toHaveBeenCalled();
const lastCall = saveMediaBufferSpy.mock.calls.at(-1);
expect(lastCall?.[4]).toBe(fileName);
await listener.close();
});
});

View File

@@ -40,8 +40,8 @@ beforeAll(async () => {
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-"));
largeJpegBuffer = await sharp({
create: {
width: 1200,
height: 1200,
width: 900,
height: 900,
channels: 3,
background: "#ff0000",
},
@@ -138,24 +138,6 @@ describe("web media loading", () => {
expect(result.contentType).toBe("image/jpeg");
});
it("adds extension to URL fileName when missing", async () => {
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce({
ok: true,
body: true,
arrayBuffer: async () => Buffer.from("%PDF-1.4").buffer,
headers: { get: () => "application/pdf" },
status: 200,
} as Response);
const result = await loadWebMedia("https://example.com/download", 1024 * 1024);
expect(result.kind).toBe("document");
expect(result.contentType).toBe("application/pdf");
expect(result.fileName).toBe("download.pdf");
fetchMock.mockRestore();
});
it("includes URL + status in fetch errors", async () => {
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce({
ok: false,
@@ -217,50 +199,6 @@ describe("web media loading", () => {
fetchMock.mockRestore();
});
it("preserves GIF animation by skipping JPEG optimization", async () => {
// Create a minimal valid GIF (1x1 pixel)
// GIF89a header + minimal image data
const gifBuffer = Buffer.from([
0x47,
0x49,
0x46,
0x38,
0x39,
0x61, // GIF89a
0x01,
0x00,
0x01,
0x00, // 1x1 dimensions
0x00,
0x00,
0x00, // no global color table
0x2c,
0x00,
0x00,
0x00,
0x00, // image descriptor
0x01,
0x00,
0x01,
0x00,
0x00, // 1x1 image
0x02,
0x01,
0x44,
0x00,
0x3b, // minimal LZW data + trailer
]);
const file = await writeTempFile(gifBuffer, ".gif");
const result = await loadWebMedia(file, 1024 * 1024);
expect(result.kind).toBe("image");
expect(result.contentType).toBe("image/gif");
// GIF should NOT be converted to JPEG
expect(result.buffer.slice(0, 3).toString()).toBe("GIF");
});
it("preserves GIF from URL without JPEG conversion", async () => {
const gifBytes = new Uint8Array([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,