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

@@ -249,52 +249,28 @@ describe("browser control server", () => {
await stopBrowserControlServer(); await stopBrowserControlServer();
}); });
it("covers primary control routes, validation, and profile compatibility", async () => { it("keeps maxChars unset when snapshot explicitly passes zero", async () => {
const { startBrowserControlServerFromConfig } = await import("./server.js"); const { startBrowserControlServerFromConfig } = await import("./server.js");
const started = await startBrowserControlServerFromConfig(); const started = await startBrowserControlServerFromConfig();
expect(started?.port).toBe(testPort); expect(started?.port).toBe(testPort);
const base = `http://127.0.0.1:${testPort}`; const base = `http://127.0.0.1:${testPort}`;
const statusBeforeStart = (await realFetch(`${base}/`).then((r) => r.json())) as {
running: boolean;
pid: number | null;
};
expect(statusBeforeStart.running).toBe(false);
expect(statusBeforeStart.pid).toBe(null);
expect(statusBeforeStart.profile).toBe("openclaw");
const startedPayload = (await realFetch(`${base}/start`, { method: "POST" }).then((r) => const startedPayload = (await realFetch(`${base}/start`, { method: "POST" }).then((r) =>
r.json(), r.json(),
)) as { ok: boolean; profile?: string }; )) as { ok: boolean; profile?: string };
expect(startedPayload.ok).toBe(true); expect(startedPayload.ok).toBe(true);
expect(startedPayload.profile).toBe("openclaw"); expect(startedPayload.profile).toBe("openclaw");
const statusAfterStart = (await realFetch(`${base}/`).then((r) => r.json())) as {
running: boolean;
pid: number | null;
chosenBrowser: string | null;
};
expect(statusAfterStart.running).toBe(true);
expect(statusAfterStart.pid).toBe(123);
expect(statusAfterStart.chosenBrowser).toBe("chrome");
expect(launchCalls.length).toBeGreaterThan(0);
const snapAi = (await realFetch(`${base}/snapshot?format=ai&maxChars=0`).then((r) => const snapAi = (await realFetch(`${base}/snapshot?format=ai&maxChars=0`).then((r) =>
r.json(), r.json(),
)) as { ok: boolean; format?: string }; )) as { ok: boolean; format?: string };
expect(snapAi.ok).toBe(true); expect(snapAi.ok).toBe(true);
expect(snapAi.format).toBe("ai"); expect(snapAi.format).toBe("ai");
expect(launchCalls.length).toBeGreaterThan(0);
const [call] = pwMocks.snapshotAiViaPlaywright.mock.calls.at(-1) ?? []; const [call] = pwMocks.snapshotAiViaPlaywright.mock.calls.at(-1) ?? [];
expect(call).toEqual({ expect(call).toEqual({
cdpUrl: cdpBaseUrl, cdpUrl: cdpBaseUrl,
targetId: "abcd1234", targetId: "abcd1234",
}); });
const stopped = (await realFetch(`${base}/stop`, { method: "POST" }).then((r) => r.json())) as {
ok: boolean;
profile?: string;
};
expect(stopped.ok).toBe(true);
expect(stopped.profile).toBe("openclaw");
}); });
}); });

View File

@@ -235,7 +235,7 @@ describe("Cron issue regressions", () => {
}); });
await cron.start(); await cron.start();
const runAt = Date.now() + 5; const runAt = Date.now() + 1;
const job = await cron.add({ const job = await cron.add({
name: "timer-overlap", name: "timer-overlap",
enabled: true, enabled: true,
@@ -246,8 +246,8 @@ describe("Cron issue regressions", () => {
delivery: { mode: "none" }, delivery: { mode: "none" },
}); });
for (let i = 0; i < 30 && runIsolatedAgentJob.mock.calls.length === 0; i++) { for (let i = 0; i < 20 && runIsolatedAgentJob.mock.calls.length === 0; i++) {
await delay(5); await delay(1);
} }
expect(runIsolatedAgentJob).toHaveBeenCalledTimes(1); expect(runIsolatedAgentJob).toHaveBeenCalledTimes(1);
@@ -256,12 +256,12 @@ describe("Cron issue regressions", () => {
expect(runIsolatedAgentJob).toHaveBeenCalledTimes(1); expect(runIsolatedAgentJob).toHaveBeenCalledTimes(1);
resolveRun?.({ status: "ok", summary: "done" }); resolveRun?.({ status: "ok", summary: "done" });
for (let i = 0; i < 30; i++) { for (let i = 0; i < 20; i++) {
const jobs = await cron.list({ includeDisabled: true }); const jobs = await cron.list({ includeDisabled: true });
if (jobs.some((j) => j.id === job.id && j.state.lastStatus === "ok")) { if (jobs.some((j) => j.id === job.id && j.state.lastStatus === "ok")) {
break; break;
} }
await delay(5); await delay(1);
} }
cron.stop(); cron.stop();

View File

@@ -3,109 +3,49 @@ import { markdownTheme } from "./theme.js";
describe("markdownTheme", () => { describe("markdownTheme", () => {
describe("highlightCode", () => { describe("highlightCode", () => {
it("should return an array of lines for JavaScript code", () => { it("returns highlighted lines for common language inputs", () => {
const code = `const x = 42;`; const code = `const x = 42;`;
const result = markdownTheme.highlightCode!(code, "javascript"); const js = markdownTheme.highlightCode!(code, "javascript");
const ts = markdownTheme.highlightCode!(
expect(result).toBeInstanceOf(Array); `function greet(name: string) {
expect(result).toHaveLength(1);
// Result should contain the original code (possibly with ANSI codes)
expect(result[0]).toContain("const");
expect(result[0]).toContain("42");
});
it("should return correct line count for multi-line code", () => {
const code = `function greet(name: string) {
return "Hello, " + name; return "Hello, " + name;
}`; }`,
const result = markdownTheme.highlightCode!(code, "typescript"); "typescript",
);
expect(result).toHaveLength(3); expect(js).toBeInstanceOf(Array);
expect(result[0]).toContain("function"); expect(js).toHaveLength(1);
expect(result[1]).toContain("return"); expect(js[0]).toContain("const");
expect(result[2]).toContain("}"); expect(js[0]).toContain("42");
expect(ts).toHaveLength(3);
expect(ts[0]).toContain("function");
expect(ts[1]).toContain("return");
expect(ts[2]).toContain("}");
}); });
it("should handle Python code", () => { it("handles unknown and missing language without throwing", () => {
const code = `def hello():
print("world")`;
const result = markdownTheme.highlightCode!(code, "python");
expect(result).toHaveLength(2);
expect(result[0]).toContain("def");
expect(result[1]).toContain("print");
});
it("should handle unknown languages gracefully", () => {
const code = `const x = 42;`;
const result = markdownTheme.highlightCode!(code, "not-a-real-language");
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(1);
// Should still return the code content
expect(result[0]).toContain("const");
});
it("should handle code without language specifier", () => {
const code = `echo "hello"`; const code = `echo "hello"`;
const result = markdownTheme.highlightCode!(code, undefined); const unknown = markdownTheme.highlightCode!(code, "not-a-real-language");
const missing = markdownTheme.highlightCode!(code, undefined);
expect(result).toBeInstanceOf(Array); expect(unknown).toBeInstanceOf(Array);
expect(result).toHaveLength(1); expect(missing).toBeInstanceOf(Array);
expect(result[0]).toContain("echo"); expect(unknown).toHaveLength(1);
expect(missing).toHaveLength(1);
expect(unknown[0]).toContain("echo");
expect(missing[0]).toContain("echo");
}); });
it("should handle empty code", () => { it("preserves code content and handles empty input", () => {
const result = markdownTheme.highlightCode!("", "javascript");
expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(1);
expect(result[0]).toBe("");
});
it("should handle bash/shell code", () => {
const code = `#!/bin/bash
echo "Hello"
for i in {1..5}; do
echo $i
done`;
const result = markdownTheme.highlightCode!(code, "bash");
expect(result).toHaveLength(5);
expect(result[0]).toContain("#!/bin/bash");
expect(result[1]).toContain("echo");
});
it("should handle JSON", () => {
const code = `{"name": "test", "count": 42, "active": true}`;
const result = markdownTheme.highlightCode!(code, "json");
expect(result).toHaveLength(1);
expect(result[0]).toContain("name");
expect(result[0]).toContain("42");
});
it("should handle code with special characters", () => {
const code = `const regex = /\\d+/g;
const str = "Hello\\nWorld";`;
const result = markdownTheme.highlightCode!(code, "javascript");
expect(result).toHaveLength(2);
// Should not throw and should return valid output
expect(result[0].length).toBeGreaterThan(0);
expect(result[1].length).toBeGreaterThan(0);
});
it("should preserve code content through highlighting", () => {
const code = `const message = "Hello, World!"; const code = `const message = "Hello, World!";
console.log(message);`; console.log(message);`;
const result = markdownTheme.highlightCode!(code, "javascript"); const result = markdownTheme.highlightCode!(code, "javascript");
const empty = markdownTheme.highlightCode!("", "javascript");
// Strip ANSI codes to verify content is preserved
const stripAnsi = (str: string) => const stripAnsi = (str: string) =>
str.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), ""); str.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), "");
expect(stripAnsi(result[0])).toBe(`const message = "Hello, World!";`); expect(stripAnsi(result[0])).toBe(`const message = "Hello, World!";`);
expect(stripAnsi(result[1])).toBe("console.log(message);"); expect(stripAnsi(result[1])).toBe("console.log(message);");
expect(empty).toEqual([""]);
}); });
}); });
}); });

View File

@@ -107,7 +107,7 @@ describe("web inbound media saves with extension", () => {
await fs.rm(HOME, { recursive: true, force: true }); 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 onMessage = vi.fn();
const listener = await monitorWebInbox({ verbose: false, onMessage }); const listener = await monitorWebInbox({ verbose: false, onMessage });
const { createWaSocket } = await import("./session.js"); 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", type: "notify",
messages: [ messages: [
{ {
@@ -126,31 +126,17 @@ describe("web inbound media saves with extension", () => {
messageTimestamp: 1_700_000_001, messageTimestamp: 1_700_000_001,
}, },
], ],
}; });
realSock.ev.emit("messages.upsert", upsert); const first = await waitForMessage(onMessage);
const mediaPath = first.mediaPath;
const msg = await waitForMessage(onMessage);
const mediaPath = msg.mediaPath;
expect(mediaPath).toBeDefined(); expect(mediaPath).toBeDefined();
expect(path.extname(mediaPath as string)).toBe(".jpg"); expect(path.extname(mediaPath as string)).toBe(".jpg");
const stat = await fs.stat(mediaPath as string); const stat = await fs.stat(mediaPath as string);
expect(stat.size).toBeGreaterThan(0); expect(stat.size).toBeGreaterThan(0);
await listener.close(); onMessage.mockClear();
}); realSock.ev.emit("messages.upsert", {
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 = {
type: "notify", type: "notify",
messages: [ messages: [
{ {
@@ -171,13 +157,30 @@ describe("web inbound media saves with extension", () => {
messageTimestamp: 1_700_000_002, 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); onMessage.mockClear();
expect(msg.chatType).toBe("group"); const fileName = "invoice.pdf";
expect(msg.mentionedJids).toEqual(["999@s.whatsapp.net"]); 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(); await listener.close();
}); });
@@ -216,37 +219,4 @@ describe("web inbound media saves with extension", () => {
await listener.close(); 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-")); fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-media-test-"));
largeJpegBuffer = await sharp({ largeJpegBuffer = await sharp({
create: { create: {
width: 1200, width: 900,
height: 1200, height: 900,
channels: 3, channels: 3,
background: "#ff0000", background: "#ff0000",
}, },
@@ -138,24 +138,6 @@ describe("web media loading", () => {
expect(result.contentType).toBe("image/jpeg"); 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 () => { it("includes URL + status in fetch errors", async () => {
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce({ const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValueOnce({
ok: false, ok: false,
@@ -217,50 +199,6 @@ describe("web media loading", () => {
fetchMock.mockRestore(); 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 () => { it("preserves GIF from URL without JPEG conversion", async () => {
const gifBytes = new Uint8Array([ const gifBytes = new Uint8Array([
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x2c, 0x00,